package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.ep;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ep.IEpQmsOqcManageService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpProduceSnExtDao;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpProduceSnRepairDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.SxEpOqcManageModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class EpQmsOqcManageByManualService implements IEpQmsOqcManageService {

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private IEpProduceSnExtDao produceSnExtDao;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IEpProduceSnRepairDao produceSnRepairDao;

    @Override
    public SxEpOqcManageModel queryEpQmsOqcManageData(Map<String, Object> params) {
        String organizeCode = (String) params.get(MesPcnExtConstWords.ORGANIZE_CODE);
        String sn = (String) params.get(MesPcnExtConstWords.SN);
        String workOrderNo = (String) params.get(MesPcnExtConstWords.WORK_ORDER_NO);
        String workTime = (String) params.get(MesPcnExtConstWords.WORK_TIME);
        String shiftCode = (String) params.get(MesPcnExtConstWords.SHIFT_CODE);
        return queryEpQmsOqcManageSn(organizeCode, sn, workOrderNo, workTime, shiftCode);
    }

    private SxEpOqcManageModel queryEpQmsOqcManageSn(String organizeCode, String sn, String workOrderNo, String workTime, String shiftCode) {
        log.info("EP-QMS获取出货检验手动检验条码数据,参数organizeCode[{}]sn[{}]workOrderNo[{}]workTime[{}]shiftCode[{}]", organizeCode, sn, workOrderNo, workTime, shiftCode);

        MesWorkOrderExt workOrderExt = getWorkOrderExt(organizeCode, workOrderNo);

        //已关联的条码信息集合
        List<String> snList = StringUtils.isEmpty(sn) ? new ArrayList<>() : JSONObject.parseArray(sn, String.class);

        SxEpOqcManageModel result = packNewOqcManageModel(workOrderExt, workTime, shiftCode);

        Object snDbList;
        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()) {
            snDbList = getStandardSnList(organizeCode, workOrderNo, workTime, shiftCode);
        } else {
            snDbList = getRepairSnList(organizeCode, workOrderNo, workTime, shiftCode);
        }

        return packProdMapList(result, snDbList, snList);

    }

    private Object getStandardSnList(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtByOrderNo(organizeCode, workOrderNo);
        if (null != produceSnExt) {
            List<EpProduceSnExtModel> produceSnExtList = produceSnExtDao.querySnListByOrderNoAndWorkTime4Oqc(organizeCode, workOrderNo, workTime, shiftCode);
            produceSnExtList = CollectionUtils.isEmpty(produceSnExtList) ? null :
                    produceSnExtList.stream().filter(o -> (null != o && o.getWorkOrderNo().equals(o.getLastWorkOrderNo()))).collect(Collectors.toList());
            return produceSnExtList;
        } else {
            List<MesPackageExt> packageExtList = packageExtService.getPackageListByWorkOrderNo(organizeCode, workOrderNo);
            packageExtList = CollectionUtils.isEmpty(packageExtList) ? null :
                    packageExtList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getShiftCode()) &&
                            MesPcnEnumUtil.PACKAGE_IS_SEALED.SEALED.getValue() == o.getIsSealed() &&
                            o.getWorkTime().equals(workTime) && o.getShiftCode().equals(shiftCode))).collect(Collectors.toList());
            return packageExtList;
        }
    }

    private Object getRepairSnList(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        List<EpProduceSnExtModel> produceSnExtList = produceSnRepairDao.querySnRepairListByOrderNoAndWorkTime4Oqc(organizeCode, workOrderNo, workTime, shiftCode);
        return produceSnExtList;
    }

    private SxEpOqcManageModel packProdMapList(SxEpOqcManageModel result, Object obj, List<String> snList) {

        if (null == obj) return result;

        List<Map<String, String>> prodMapList = new ArrayList<>();

        List<?> snDbList = (List<?>) obj;

        if (snDbList.get(0) instanceof EpProduceSnExtModel) {
            for (EpProduceSnExtModel produceSn : (List<EpProduceSnExtModel>) snDbList) {
                if (null == produceSn || (snList.contains(produceSn.getSerialNumber()))) {
                    continue;
                }
                Map<String, String> prodMap = new HashMap<>();
                prodMap.put(MesPcnExtConstWords.PACKAGENO, produceSn.getPackageNo());
                prodMap.put(MesPcnExtConstWords.SERIAL_NUMBER, produceSn.getSerialNumber());
                prodMapList.add(prodMap);
            }
        } else {
            for (MesPackageExt packageExt : (List<MesPackageExt>) snDbList) {
                if (null == packageExt || (snList.contains(packageExt.getPackageNo()))) {
                    continue;
                }
                Map<String, String> prodMap = new HashMap<>();
                prodMap.put(MesPcnExtConstWords.PACKAGENO, packageExt.getPackageNo());
                prodMap.put(MesPcnExtConstWords.QTY, packageExt.getQty().toString());
                prodMapList.add(prodMap);
            }
        }

        result.setProdMapList(prodMapList);

        return result;
    }

    private MesWorkOrderExt getWorkOrderExt(String organizeCode, String workOrderNo) {
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(organizeCode, workOrderNo);
        if (null == workOrderExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产工单[%s]不存在,请重新选择生产工单", workOrderNo)
                    .build();
        }
        return workOrderExt;
    }

    private SxEpOqcManageModel packNewOqcManageModel(MesWorkOrderExt workOrderExt, String workTime, String shiftCode) {
        SxEpOqcManageModel result = new SxEpOqcManageModel();
        result.setWorkOrderNo(workOrderExt.getOrderNo());
        result.setWorkOrderType(workOrderExt.getWorkOrderType());
        result.setWorkTime(workTime);
        result.setShiftCode(shiftCode);
        result.setPartNo(workOrderExt.getPartNo());
        result.setPartNameRdd(workOrderExt.getPartNameRdd());
        result.setQty(workOrderExt.getQty());
        result.setWorkCenterCode(workOrderExt.getWorkCenterCode());
        return result;
    }

}
