package com.zehong.system.service.impl.track;

import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.system.domain.track.TProductInspectDetail;
import com.zehong.system.domain.vo.HouseVo;
import com.zehong.system.mapper.track.TProductInspectDetailMapper;
import com.zehong.system.service.track.ITProductInspectDetailService;
import com.zehong.system.service.track.ITProductStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生产执行中间Service业务层处理
 * 
 * @author zehong
 * @date 2024-08-26
 */
@Service
public class TProductInspectDetailServiceImpl implements ITProductInspectDetailService 
{
    @Autowired
    private TProductInspectDetailMapper tProductInspectDetailMapper;

    @Autowired
    private ITProductStoreService itProductStoreService;

    /**
     * 查询生产执行中间
     * 
     * @param id 生产执行中间ID
     * @return 生产执行中间
     */
    @Override
    public TProductInspectDetail selectTProductInspectDetailById(Long id)
    {
        return tProductInspectDetailMapper.selectTProductInspectDetailById(id);
    }

    /**
     * 查询生产执行中间列表
     * 
     * @param tProductInspectDetail 生产执行中间
     * @return 生产执行中间
     */
    @Override
    public List<TProductInspectDetail> selectTProductInspectDetailList(TProductInspectDetail tProductInspectDetail)
    {
        return tProductInspectDetailMapper.selectTProductInspectDetailList(tProductInspectDetail);
    }

    /**
     * 新增生产执行中间
     * 
     * @param tProductInspectDetail 生产执行中间
     * @return 结果
     */
    @Override
    public int insertTProductInspectDetail(TProductInspectDetail tProductInspectDetail)
    {
        tProductInspectDetail.setCreateTime(DateUtils.getNowDate());
        tProductInspectDetail.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
        return tProductInspectDetailMapper.insertTProductInspectDetail(tProductInspectDetail);
    }

    /**
     * 修改生产执行中间
     * 
     * @param tProductInspectDetail 生产执行中间
     * @return 结果
     */
    @Override
    public int updateTProductInspectDetail(TProductInspectDetail tProductInspectDetail)
    {
        return tProductInspectDetailMapper.updateTProductInspectDetail(tProductInspectDetail);
    }

    /**
     * 批量删除生产执行中间
     * 
     * @param ids 需要删除的生产执行中间ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTProductInspectDetailByIds(Long[] ids)
    {
        List<HouseVo> storeInfo = new ArrayList<>();
        for(Long id : ids){
            TProductInspectDetail detail = tProductInspectDetailMapper.selectTProductInspectDetailById(id);
            if(2 == detail.getType()){
                HouseVo house = new HouseVo();
                house.setId(detail.getTypeId());
                house.setNum(detail.getNumber());
                storeInfo.add(house);
            }
        }
        if(!CollectionUtils.isEmpty(storeInfo)) itProductStoreService.computeStock(storeInfo,false,"add");
        return tProductInspectDetailMapper.deleteTProductInspectDetailByIds(ids);
    }

    /**
     * 删除生产执行中间信息
     * 
     * @param id 生产执行中间ID
     * @return 结果
     */
    @Override
    public int deleteTProductInspectDetailById(Long id)
    {
        return tProductInspectDetailMapper.deleteTProductInspectDetailById(id);
    }

    /**
     * 批量新增详情
     * @param productInspectDetailList
     * @return
     */
    public int batchAddDetailInfo(List<TProductInspectDetail> productInspectDetailList){
        validateDetailInfo(productInspectDetailList);
        for(TProductInspectDetail detail : productInspectDetailList){
            detail.setCreateTime(new Date());
            detail.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        synchronized (this){
            return subtractStoreStock(productInspectDetailList);
        }
    }

    /**
     * 详情校验
     * @param productInspectDetailList
     */
    private void validateDetailInfo(List<TProductInspectDetail> productInspectDetailList){
        //校验图库和模版是否存在
        List<TProductInspectDetail> detailList = productInspectDetailList.stream().filter(item -> (1 == item.getType() || 3 == item.getType())).collect(Collectors.toList());
        for(TProductInspectDetail detail :detailList){
            TProductInspectDetail tProductInspectDetail = new TProductInspectDetail();
            tProductInspectDetail.setTypeId(detail.getTypeId());
            tProductInspectDetail.setType(detail.getType());
            List<TProductInspectDetail> detailInfo = tProductInspectDetailMapper.selectTProductInspectDetailList(tProductInspectDetail);
            if(!CollectionUtils.isEmpty(detailInfo)){
                throw new CustomException((1 == detailInfo.get(0).getType() ? "图纸" : "装配模版") + detailInfo.get(0).getTypeName() + "已存在");
            }
        }
    }

    /**
     * 扣出库存
     * @param productInspectDetailList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int subtractStoreStock(List<TProductInspectDetail> productInspectDetailList){
        List<HouseVo> storeList = productInspectDetailList.stream().filter(item -> 2 == item.getType())
                .map(item ->{
                    HouseVo house = new HouseVo();
                    house.setId(item.getTypeId());
                    house.setNum(item.getNumber());
                    return house;
                })
                .collect(Collectors.toList());
        itProductStoreService.computeStock(storeList,false,"subtract");
        return tProductInspectDetailMapper.batchAddDetailInfo(productInspectDetailList);
    }
}
