package cn.ezhizao.project.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.ezhizao.project.business.domain.BizOutsourcedOrder;
import cn.ezhizao.project.business.domain.BizOutsourcedOrderDetail;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.ezhizao.project.business.mapper.BizDayworkItemExamineMapper;
import cn.ezhizao.project.business.domain.BizDayworkItemExamine;
import cn.ezhizao.project.business.service.IBizDayworkItemExamineService;

/**
 * 分选包装审核申请Service业务层处理
 *
 * @author ezhizao
 * @date 2024-07-29
 */
@Service
public class BizDayworkItemExamineServiceImpl  extends ServiceImpl<BizDayworkItemExamineMapper, BizDayworkItemExamine> implements IBizDayworkItemExamineService
{
    @Resource
    private BizDayworkItemExamineMapper bizDayworkItemExamineMapper;

    /**
     * 查询分选包装审核申请列表
     *
     * @param bizDayworkItemExamine 分选包装审核申请
     * @return 分选包装审核申请
     */
    @Override
    public List<BizDayworkItemExamine> getList(BizDayworkItemExamine bizDayworkItemExamine)
    {
        return bizDayworkItemExamineMapper.getList(bizDayworkItemExamine);
    }

    @Override
    public List<BizDayworkItemExamine> getAllowAddedList(List<Long> dayworkItemIds) {
        return bizDayworkItemExamineMapper.getAllowAddedList(dayworkItemIds);
    }

    @Override
    public BizDayworkItemExamine getDetailById(Long id) {
        return bizDayworkItemExamineMapper.getDetailById(id);
    }

    @Override
    public void updateOrSave(List<BizOutsourcedOrderDetail> outsourcedOrderDetails, BizOutsourcedOrder bizOutsourcedOrder) {
        List<Long> dayworkIds = outsourcedOrderDetails.stream().filter(v->v.getIsPackage().equals(1)).map(BizOutsourcedOrderDetail::getDayworkId).collect(Collectors.toList());
        if(!dayworkIds.isEmpty()) {
            List<BizDayworkItemExamine> dayworkItemExamineList = bizDayworkItemExamineMapper.getListByDayworkIds(dayworkIds);
            List<BizDayworkItemExamine> needAddList = new ArrayList<>();
            List<BizDayworkItemExamine> needDeleteList = new ArrayList<>();
            if (!dayworkItemExamineList.isEmpty()) {
                //如果审核工段是内部外协的工段则不新增
                dayworkIds.forEach(id -> {
                    BizOutsourcedOrderDetail outsourcedOrderDetail = outsourcedOrderDetails.stream().filter(v1 -> v1.getDayworkId().equals(id)).findFirst().orElse(new BizOutsourcedOrderDetail());
                    List<BizDayworkItemExamine> collect = dayworkItemExamineList.stream().filter(v1 -> v1.getDayworkId().equals(outsourcedOrderDetail.getDayworkId())).collect(Collectors.toList());
                    //当前批次已经提交了审核
                    if (!collect.isEmpty()) {
                        //提交的工段是否是内部外协的工段
                        if (!collect.get(0).getDeptId().equals(bizOutsourcedOrder.getOutsourceDeptId())) {
                            needDeleteList.add(collect.get(0));
                            //新增审核申请
                            BizDayworkItemExamine examine = new BizDayworkItemExamine();
                            examine.setDayworkId(outsourcedOrderDetail.getDayworkId());
                            examine.setDayworkItemId(outsourcedOrderDetail.getDayworkItemId());
                            examine.setDeptId(bizOutsourcedOrder.getOutsourceDeptId());
                            examine.setProductionPlanDetailId(outsourcedOrderDetail.getProductionPlanDetailId());
                            examine.setProductDescription(outsourcedOrderDetail.getProductDescription());
                            examine.setLotId(outsourcedOrderDetail.getLotId());
                            examine.setLotCode(outsourcedOrderDetail.getLotCode());
                            examine.setProductId(outsourcedOrderDetail.getProductId());
                            examine.setTechnologicalProcessId(outsourcedOrderDetail.getTechnologicalProcessId());
                            examine.setTechnologicalProcessDetailId(outsourcedOrderDetail.getTechnologicalProcessDetailId());
                            examine.setProcessId(outsourcedOrderDetail.getProcessId());
                            examine.setProcessAlias(outsourcedOrderDetail.getProcessAlias());
                            examine.setStatus(0);
                            needAddList.add(examine);
                        }
                    } else {
                        //新增审核申请
                        BizDayworkItemExamine examine = new BizDayworkItemExamine();
                        examine.setDayworkId(outsourcedOrderDetail.getDayworkId());
                        examine.setDayworkItemId(outsourcedOrderDetail.getDayworkItemId());
                        examine.setDeptId(bizOutsourcedOrder.getOutsourceDeptId());
                        examine.setProductionPlanDetailId(outsourcedOrderDetail.getProductionPlanDetailId());
                        examine.setProductDescription(outsourcedOrderDetail.getProductDescription());
                        examine.setLotId(outsourcedOrderDetail.getLotId());
                        examine.setLotCode(outsourcedOrderDetail.getLotCode());
                        examine.setProductId(outsourcedOrderDetail.getProductId());
                        examine.setTechnologicalProcessId(outsourcedOrderDetail.getTechnologicalProcessId());
                        examine.setTechnologicalProcessDetailId(outsourcedOrderDetail.getTechnologicalProcessDetailId());
                        examine.setProcessId(outsourcedOrderDetail.getProcessId());
                        examine.setProcessAlias(outsourcedOrderDetail.getProcessAlias());
                        examine.setStatus(0);
                        needAddList.add(examine);
                    }

                });
            } else {
                outsourcedOrderDetails.forEach(v -> {
                    //新增审核申请
                    BizDayworkItemExamine examine = new BizDayworkItemExamine();
                    examine.setDayworkId(v.getDayworkId());
                    examine.setDayworkItemId(v.getDayworkItemId());
                    examine.setDeptId(bizOutsourcedOrder.getOutsourceDeptId());
                    examine.setProductionPlanDetailId(v.getProductionPlanDetailId());
                    examine.setProductDescription(v.getProductDescription());
                    examine.setLotId(v.getLotId());
                    examine.setLotCode(v.getLotCode());
                    examine.setProductId(v.getProductId());
                    examine.setTechnologicalProcessId(v.getTechnologicalProcessId());
                    examine.setTechnologicalProcessDetailId(v.getTechnologicalProcessDetailId());
                    examine.setProcessId(v.getProcessId());
                    examine.setProcessAlias(v.getProcessAlias());
                    examine.setStatus(0);
                    needAddList.add(examine);
                });

            }
            removeBatchByIds(needDeleteList);
            saveBatch(needAddList);
        }
    }


    /**
     * 物理删除
     * @param bizDayworkItemExamine
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizDayworkItemExamine bizDayworkItemExamine){ return bizDayworkItemExamineMapper.physicalDelete(bizDayworkItemExamine); };

}
