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.BizDaywork;
import cn.ezhizao.project.business.domain.BizOutsourcedOrderDetail;
import cn.ezhizao.project.business.domain.BizReturnReceipt;
import cn.ezhizao.project.business.service.IBizOutsourcedOrderDetailService;
import cn.ezhizao.project.business.service.IBizReturnReceiptService;
import cn.ezhizao.project.system.domain.SysDept;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.ezhizao.project.business.mapper.BizReturnReceiptDetailMapper;
import cn.ezhizao.project.business.domain.BizReturnReceiptDetail;
import cn.ezhizao.project.business.service.IBizReturnReceiptDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 外协单从Service业务层处理
 *
 * @author ezhizao
 * @date 2024-05-08
 */
@Service
public class BizReturnReceiptDetailServiceImpl  extends ServiceImpl<BizReturnReceiptDetailMapper, BizReturnReceiptDetail> implements IBizReturnReceiptDetailService
{
    @Resource
    private BizReturnReceiptDetailMapper bizReturnReceiptDetailMapper;
    @Resource
    private IBizReturnReceiptService bizReturnReceiptService;
    @Resource
    private IBizOutsourcedOrderDetailService bizOutsourcedOrderDetailService;

    /**
     * 查询外协单从列表
     *
     * @param bizReturnReceiptDetail 外协单从
     * @return 外协单从
     */
    @Override
    public List<BizReturnReceiptDetail> getList(BizReturnReceiptDetail bizReturnReceiptDetail)
    {
        return bizReturnReceiptDetailMapper.getList(bizReturnReceiptDetail);
    }

    @Override
    @Transactional
    public void removeOrEditDetails(List<BizOutsourcedOrderDetail> newDetails, List<BizOutsourcedOrderDetail> oldDetails) {
        //如果工序名称不同，则判断修改工序，则将收回单明细删除
        List<Long> needRemoveOutsourcedDetailIds = new ArrayList<>();
        //如果修改发出单明细的箱号，如果已经有收回单，则收回单不删除，只修改
        List<BizReturnReceiptDetail> editReturnReceiptDetails = new ArrayList<>();
        List<Long> newOutsourcedDetailIds = newDetails.stream().map(BizOutsourcedOrderDetail::getId).collect(Collectors.toList());
        oldDetails.forEach(oldDetail -> {
            if(!newOutsourcedDetailIds.contains(oldDetail.getId())) {
                needRemoveOutsourcedDetailIds.add(oldDetail.getId());
            }
        });
        List<Long> oldOutsourcedDetailIds = oldDetails.stream().map(BizOutsourcedOrderDetail::getId).collect(Collectors.toList());
        for(int i = 0;i<oldDetails.size();i++) {
            for (int j = 0;j<newDetails.size();j++) {
                if(oldDetails.get(i).getId().equals(newDetails.get(j).getId())){
                    if(!oldDetails.get(i).getProcessNames().equals(newDetails.get(j).getProcessNames())) {
                        needRemoveOutsourcedDetailIds.add(oldDetails.get(i).getId());
                    }else{
                        if(!oldDetails.get(i).getNewCarrierName().equals(newDetails.get(j).getNewCarrierName())) {
                            BizReturnReceiptDetail bizReturnReceiptDetail = new BizReturnReceiptDetail();
                            bizReturnReceiptDetail.setOutsourceDetailId(newDetails.get(j).getId());
                            bizReturnReceiptDetail.setNewCarrierCount(newDetails.get(j).getNewCarrierCount());
                            bizReturnReceiptDetail.setNewCarrier(newDetails.get(j).getNewCarrierName());
                            bizReturnReceiptDetail.setLotId(newDetails.get(j).getLotId());
                            editReturnReceiptDetails.add(bizReturnReceiptDetail);
                        }
                    }
                }
            }
        }
        //删除收回单明细
        if(!needRemoveOutsourcedDetailIds.isEmpty()) {
            List<BizReturnReceiptDetail> returnReceiptDetails = query().in("outsource_detail_id", needRemoveOutsourcedDetailIds).list();
            if(!returnReceiptDetails.isEmpty()) {
                //主表id，判断如果主表没有明细了，则主表删除
                List<Long> returnReceiptIds = returnReceiptDetails.stream().map(BizReturnReceiptDetail::getMasterId).distinct().collect(Collectors.toList());
                //删除明细
                removeBatchByIds(returnReceiptDetails);
                List<BizReturnReceiptDetail> returnReceiptDetailList = query().in("master_id", returnReceiptIds).list();
                List<Long> needRemoveReturnReceiptIds = new ArrayList<>();
                returnReceiptIds.forEach(item ->{
                    List<BizReturnReceiptDetail> detailList = returnReceiptDetailList.stream().filter(item1 -> item1.getMasterId().equals(item)).collect(Collectors.toList());
                    if(detailList.isEmpty()) {
                        needRemoveReturnReceiptIds.add(item);
                    }
                });
                if(!needRemoveReturnReceiptIds.isEmpty()) {
                    bizReturnReceiptService.removeBatchByIds(needRemoveReturnReceiptIds);
                }
            }
        }
        //编辑收回单明细
        if(!editReturnReceiptDetails.isEmpty()){
            List<Long> outsourceDetailIds = editReturnReceiptDetails.stream().map(BizReturnReceiptDetail::getOutsourceDetailId).collect(Collectors.toList());
            List<BizReturnReceiptDetail> returnDetail = query().in("outsource_detail_id", outsourceDetailIds).list();
            if(!returnDetail.isEmpty()) {
            returnDetail.forEach(item ->{
                editReturnReceiptDetails.forEach(item1 ->{
                    if(item.getOutsourceDetailId().equals(item1.getOutsourceDetailId())) {
                        item.setNewCarrierCount(item1.getNewCarrierCount());
                        item.setNewCarrier(item1.getNewCarrier());
                    }
                });
            });
            }
            updateBatchById(returnDetail);
        }
    }

    @Override
    public void removeByMasterIds(List<Long> ids) {
        //删除收回明细表，如果收回单主表下的明细为空，则同时删除收回单主表
        List<Long> outsourcedDetailIds = bizOutsourcedOrderDetailService.query().in("master_id", ids).list().stream().map(BizOutsourcedOrderDetail::getId).collect(Collectors.toList());
        List<BizReturnReceiptDetail> outsourceDetailList = query().in("outsource_detail_id", outsourcedDetailIds).list();
        List<Long> needRemoveIds = new ArrayList<>();
        if(!outsourceDetailList.isEmpty()) {
            List<Long> masterIds = outsourceDetailList.stream().map(BizReturnReceiptDetail::getMasterId).collect(Collectors.toList());
            removeBatchByIds(outsourceDetailList);
            List<BizReturnReceiptDetail> detail = query().in("master_id", masterIds).list();
            masterIds.forEach(item ->{
               if(detail.stream().filter(label ->label.getMasterId().equals(item)).collect(Collectors.toList()).size() == 0) {
                   needRemoveIds.add(item);
               }
            });
            bizReturnReceiptService.removeBatchByIds(needRemoveIds);
        }
    }

    @Override
    public List<SysDept> getTurnoverAbDeptList(BizDaywork bizDaywork) {
        return bizReturnReceiptDetailMapper.getTurnoverAbDeptList(bizDaywork);
    }

    @Override
    public List<SysDept> getTurnoverDeptList(BizDaywork bizDaywork) {
        return bizReturnReceiptDetailMapper.getTurnoverDeptList(bizDaywork);
    }

    /**
     * 物理删除
     * @param bizReturnReceiptDetail
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizReturnReceiptDetail bizReturnReceiptDetail){ return bizReturnReceiptDetailMapper.physicalDelete(bizReturnReceiptDetail); };

}
