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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IQualityDecisionService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IWorkCenterExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IEpSxRepairWorkService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnRepairService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.RepairRequestModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpBadCellDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpDefectModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.ISyncFuncService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EpSxRepairWorkService implements IEpSxRepairWorkService {

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private MesBadCellDetailRepository badCellDetailRepository;

    @Autowired
    private MesBadCellRepository badCellRepository;

    @Autowired
    private MesRepairRecordExtRepository repairRecordExtRepository;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private IWorkCenterExtService workCenterExtService;

    @Autowired
    private IQualityDecisionService qualityDecisionService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private ISyncFuncService syncFuncService;

    @Autowired
    private MesRepairTaskRepository repairTaskRepository;

    @Override
    public void insertMesBadCellDetail(EpBadCellDetailModel mesBadCellDetail) {
        String workOrderNo = mesBadCellDetail.getOrderNo();

        if (!StringUtils.isEmpty(mesBadCellDetail.getSerialNumber())) {
            MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(mesBadCellDetail.getOrganizeCode(), mesBadCellDetail.getSerialNumber());
            if (null == produceSnExt) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("产品条码[%s]不存在", mesBadCellDetail.getSerialNumber())
                        .build();
            }
            if (!StringUtils.isEmpty(produceSnExt.getLastWorkOrderNo()) && !produceSnExt.getLastWorkOrderNo().equals(produceSnExt.getWorkOrderNo())) {
                MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySn(mesBadCellDetail.getOrganizeCode(), mesBadCellDetail.getSerialNumber());
                if (null == produceSnRepair) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("产品条码[%s]与返工工单[%s]的关联关系不存在", mesBadCellDetail.getSerialNumber(), produceSnExt.getLastWorkOrderNo())
                            .build();
                }
                workOrderNo = produceSnRepair.getWorkOrderNo();
            } else {
                workOrderNo = produceSnExt.getWorkOrderNo();
            }
            mesBadCellDetail.setWorkCenterCode(produceSnExt.getWorkCenterCode());
        } else {
            if (StringUtils.isEmpty(workOrderNo)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("入参缺少生产工单号")
                        .build();
            }
        }

        MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(mesBadCellDetail.getOrganizeCode(), mesBadCellDetail.getWorkCenterCode());
        if (null == workCenterExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线代码[%s]无效", mesBadCellDetail.getWorkCenterCode())
                    .build();
        }
        if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线[%s]未启动班次", mesBadCellDetail.getWorkCenterCode())
                    .build();
        }

        List<EpDefectModel> mesDefectList = mesBadCellDetail.getMesDefectList();
        if (!CollectionUtils.isEmpty(mesDefectList)) {
            MesBadCell badCell = badCellRepository.getByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                            MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE, MesPcnExtConstWords.WORK_CELL_CODE},
                    new Object[]{mesBadCellDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                            workOrderNo, workCenterExt.getWorkTime(), workCenterExt.getShiftCode(), mesBadCellDetail.getWorkCellCode()});

            if (null == badCell) {
                if (!StringUtils.isEmpty(mesBadCellDetail.getSerialNumber())) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("根据产品条码[%s]基于生产日期[%s]班次[%s]上未在工位[%s]执行质量判定作业",
                                    mesBadCellDetail.getSerialNumber(), workCenterExt.getWorkTime(), workCenterExt.getShiftCode(), mesBadCellDetail.getWorkCellCode())
                            .build();
                } else {
                    MesProcessCell processCell = qualityDecisionService.getMesProcessCell(workCenterExt.getWorkCenterCode(), mesBadCellDetail.getWorkCellCode(), mesBadCellDetail.getOrganizeCode());
                    if (null == processCell) {
                        throw ImppExceptionBuilder.newInstance()
                                .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                                .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                                .setErrorDetail("生产线代码[%s]工位代码[%s]未关联工序", workCenterExt.getWorkCenterCode(), mesBadCellDetail.getWorkCellCode())
                                .build();
                    }

                    badCell = new MesBadCell();
                    badCell.setOrganizeCode(mesBadCellDetail.getOrganizeCode());
                    badCell.setPartNo(mesBadCellDetail.getPartNo());
                    badCell.setPartNameRdd(mesBadCellDetail.getPartNameRdd());
                    badCell.setWorkCenterCode(workCenterExt.getWorkCenterCode());
                    badCell.setWorkCellCode(mesBadCellDetail.getWorkCellCode());
                    badCell.setProcessCode(processCell.getProcessCode());
                    badCell.setProcessNameRdd(processCell.getProcessName());
                    badCell.setQty(new Double(mesDefectList.size()));
                    badCell.setWorkOrderNo(workOrderNo);
                    badCell.setWorkTime(workCenterExt.getWorkTime());
                    badCell.setShiftCode(workCenterExt.getShiftCode());
                    ConvertBean.serviceModelInitialize(badCell, mesBadCellDetail.getModifyUser());
                    badCellRepository.insert(badCell);
                }
            } else {
                badCell.setQty(MathOperation.add(badCell.getQty(), new Double(mesDefectList.size())));
                ConvertBean.serviceModelUpdate(badCell, mesBadCellDetail.getModifyUser());
                badCellRepository.save(badCell);
            }

            for (EpDefectModel mesDefect : mesDefectList) {
                if (null == mesDefect) {
                    continue;
                }
                MesBadCellDetail badCellDetail = new MesBadCellDetail();
                badCellDetail.setWorkCenterCode(mesBadCellDetail.getWorkCenterCode());
                badCellDetail.setWorkOrderNo(workOrderNo);
                badCellDetail.setWorkTime(workCenterExt.getWorkTime());
                badCellDetail.setShiftCode(workCenterExt.getShiftCode());
                badCellDetail.setWorkCellCode(mesBadCellDetail.getWorkCellCode());
                badCellDetail.setPartNo(badCell.getPartNo());
                badCellDetail.setPartNameRdd(badCell.getPartNameRdd());
                badCellDetail.setDefectType(mesDefect.getDefectType());
                badCellDetail.setDefectTypeName(mesDefect.getDefectTypeName());
                badCellDetail.setDefectCode(mesDefect.getDefectCode());
                badCellDetail.setDefectName(mesDefect.getDefectName());
                badCellDetail.setRepairStatus(MesPcnExtEnumUtil.MES_REPAIR_STATUS.NO_REPAIR.getValue());
                badCellDetail.setSerialNumber(mesBadCellDetail.getSerialNumber());
                badCellDetail.setOrganizeCode(mesBadCellDetail.getOrganizeCode());
                //增加成品物料号、成品物料名称、发生岗位
                badCellDetail.setItemPartName(mesDefect.getItemPartName());
                badCellDetail.setItemPartNo(mesDefect.getItemPartNo());
                badCellDetail.setHappenStation(mesDefect.getHappenStation());
                ConvertBean.serviceModelInitialize(badCellDetail, mesBadCellDetail.getModifyUser());
                badCellDetail.setDefectActionTime(badCellDetail.getCreateDatetime());
                badCellDetailRepository.insert(badCellDetail);
            }
        }
    }

    @Override
    public void insertMesRepairRecord(MesRepairRecordExt repairRecord) {
        MesRepairRecordExt repairRecordExt = new MesRepairRecordExt();
        BeanUtils.copyProperties(repairRecord, repairRecordExt, MesPcnExtConstWords.ID);
        ConvertBean.serviceModelInitialize(repairRecordExt, repairRecord.getModifyUser());
        new ConvertBeanExt(repairRecordExt).convertBean(repairRecordExt);
        repairRecordExtRepository.insert(repairRecordExt);
        badCellDetailRepository.updateByProperties(new String[]{MesPcnExtConstWords.ID}, new Object[]{repairRecord.getDefectRecordId()},
                new String[]{MesPcnExtConstWords.REPAIR_STATUS, MesPcnExtConstWords.MODIFY_USER, MesPcnExtConstWords.MODIFY_DATE_TIME},
                new Object[]{MesPcnExtEnumUtil.MES_REPAIR_STATUS.REPAIRED.getValue(), repairRecord.getModifyUser(), TimeTool.getNowTime(true)});
    }

    @Override
    public void updateMesRepairRecord(MesRepairRecordExt repairRecord) {
        MesRepairRecordExt repairRecordDb = repairRecordExtRepository.getById(repairRecord.getId());
        if (repairRecordDb == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("ID[%s]数据不存在", repairRecord.getId())
                    .build();
        }
        repairRecordDb.setDcCode(repairRecord.getDcCode());
        repairRecordDb.setDcName(repairRecord.getDcName());
        repairRecordDb.setRepairCode(repairRecord.getRepairCode());
        repairRecordDb.setRepairName(repairRecord.getRepairName());
        repairRecordDb.setRepairDatetime(repairRecord.getRepairDatetime());
        repairRecordDb.setRepairJudge(repairRecord.getRepairJudge());
        repairRecordDb.setMemo(repairRecord.getMemo());
        repairRecordDb.setBadPartNo(repairRecord.getBadPartNo());
        repairRecordDb.setBadPartName(repairRecord.getMemo());
        repairRecordDb.setMemo(repairRecord.getMemo());
        ConvertBean.serviceModelUpdate(repairRecordDb, repairRecord.getModifyUser());
        new ConvertBeanExt(repairRecordDb).convertBean(repairRecordDb);
        repairRecordExtRepository.save(repairRecordDb);
    }

    @Override
    public void insertMesRepairDefectRecord(RepairRequestModel requestModel) {
        List<MesBadCellDetail> badCellDetailList = requestModel.getMesDefectRecordList();
        List<MesRepairRecordExt> repairRecordList = requestModel.getMesRepairRecordList();
        Map<String, Long> defectCodeMap = new HashMap<>();

        String workOrderNo = repairRecordList.get(0).getWorkOrderNo();
        if (!StringUtils.isEmpty(badCellDetailList.get(0).getSerialNumber())) {
            MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(requestModel.getOrganizeCode(), badCellDetailList.get(0).getSerialNumber());
            if (null == produceSnExt) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("产品条码[%s]不存在", badCellDetailList.get(0).getSerialNumber())
                        .build();
            }
            if (!StringUtils.isEmpty(produceSnExt.getLastWorkOrderNo()) && !produceSnExt.getLastWorkOrderNo().equals(produceSnExt.getWorkOrderNo())) {
                MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySn(requestModel.getOrganizeCode(), produceSnExt.getSerialNumber());
                if (null == produceSnRepair) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("产品条码[%s]与返工工单[%s]的关联关系不存在", produceSnExt.getSerialNumber(), produceSnExt.getLastWorkOrderNo())
                            .build();
                }
                workOrderNo = produceSnRepair.getWorkOrderNo();
            } else {
                workOrderNo = produceSnExt.getWorkOrderNo();
            }
        } else {
            if (StringUtils.isEmpty(workOrderNo)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("入参缺少生产工单号")
                        .build();
            }
        }

        MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(requestModel.getOrganizeCode(), badCellDetailList.get(0).getWorkCenterCode());
        if (null == workCenterExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线代码[%s]无效",  badCellDetailList.get(0).getWorkCenterCode())
                    .build();
        }
        if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线[%s]未启动班次",  badCellDetailList.get(0).getWorkCenterCode())
                    .build();
        }

        List<MesBadCellDetail> badCellDetails = badCellDetailList.stream().filter(mesBadCellDetail -> mesBadCellDetail != null).collect(Collectors.toList());

        badCellDetails.stream().forEach(badCellDetail->{if(StringUtils.isEmpty(badCellDetail.getItemPartNo()) || StringUtils.isEmpty(badCellDetail.getHappenStation()) || StringUtils.isEmpty(badCellDetail.getItemPartName())){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("原材料物料编码或原材料物料名称或发生岗位为空")
                    .build();
        }});

        MesBadCell badCell = badCellRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE, MesPcnExtConstWords.WORK_CELL_CODE},
                new Object[]{requestModel.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        workOrderNo, workCenterExt.getWorkTime(), workCenterExt.getShiftCode(), requestModel.getWorkCellCode()});
        if (null == badCell) {
            if (!StringUtils.isEmpty(badCellDetailList.get(0).getSerialNumber())) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("根据产品条码[%s]基于生产日期[%s]班次[%s]上未在工位[%s]执行质量判定作业",
                                badCellDetailList.get(0).getSerialNumber(), workCenterExt.getWorkTime(), workCenterExt.getShiftCode(), requestModel.getWorkCellCode())
                        .build();
            } else {
                MesProcessCell processCell = qualityDecisionService.getMesProcessCell(workCenterExt.getWorkCenterCode(), requestModel.getWorkCellCode(), requestModel.getOrganizeCode());
                if (null == processCell) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("生产线代码[%s]工位代码[%s]未关联工序", workCenterExt.getWorkCenterCode(), requestModel.getWorkCellCode())
                            .build();
                }

                badCell = new MesBadCell();
                badCell.setOrganizeCode(requestModel.getOrganizeCode());
                badCell.setPartNo(repairRecordList.get(0).getPartNo());
                badCell.setPartNameRdd(repairRecordList.get(0).getPartName());
                badCell.setWorkCenterCode(workCenterExt.getWorkCenterCode());
                badCell.setWorkCellCode(requestModel.getWorkCellCode());
                badCell.setProcessCode(processCell.getProcessCode());
                badCell.setProcessNameRdd(processCell.getProcessName());
                badCell.setQty(new Double(badCellDetailList.size()));
                badCell.setWorkOrderNo(workOrderNo);
                badCell.setWorkTime(workCenterExt.getWorkTime());
                badCell.setShiftCode(workCenterExt.getShiftCode());
                ConvertBean.serviceModelInitialize(badCell, requestModel.getModifyUser());
                badCellRepository.insert(badCell);
            }
        } else {
            badCell.setQty(MathOperation.add(badCell.getQty(), badCellDetailList.size()));
            ConvertBean.serviceModelUpdate(badCell, requestModel.getModifyUser());
            badCellRepository.save(badCell);
        }

        for (MesBadCellDetail detail : badCellDetailList) {
            if (null == detail) {
                continue;
            }
            MesBadCellDetail badCellDetail = new MesBadCellDetail();
            badCellDetail.setWorkCenterCode(detail.getWorkCenterCode());
            badCellDetail.setWorkOrderNo(workOrderNo);
            badCellDetail.setWorkTime(workCenterExt.getWorkTime());
            badCellDetail.setShiftCode(workCenterExt.getShiftCode());
            badCellDetail.setWorkCellCode(requestModel.getWorkCellCode());
            badCellDetail.setPartNo(badCell.getPartNo());
            badCellDetail.setPartNameRdd(badCell.getPartNameRdd());
            badCellDetail.setDefectType(detail.getDefectType());
            badCellDetail.setDefectTypeName(detail.getDefectTypeName());
            badCellDetail.setDefectCode(detail.getDefectCode());
            badCellDetail.setDefectName(detail.getDefectName());
            badCellDetail.setRepairStatus(MesPcnExtEnumUtil.MES_REPAIR_STATUS.REPAIRED.getValue());
            badCellDetail.setSerialNumber(detail.getSerialNumber());
            badCellDetail.setOrganizeCode(requestModel.getOrganizeCode());

            //增加 原材料物料编码、原材料物料名称、发生岗位
            badCellDetail.setItemPartNo(detail.getItemPartNo());
            badCellDetail.setItemPartName(detail.getItemPartName());
            badCellDetail.setHappenStation(detail.getHappenStation());

            ConvertBean.serviceModelInitialize(badCellDetail, requestModel.getModifyUser());
            badCellDetail.setDefectActionTime(badCellDetail.getCreateDatetime());
            defectCodeMap.put(detail.getDefectCode(), badCellDetailRepository.insert(badCellDetail).getId());
        }

        for (MesRepairRecordExt detail : repairRecordList) {
            if (null == detail) {
                continue;
            }
            MesRepairRecordExt repairRecordExt = new MesRepairRecordExt();
            BeanUtils.copyProperties(detail, repairRecordExt, MesPcnExtConstWords.ID);
            repairRecordExt.setDefectRecordId(defectCodeMap.get(detail.getDefectCode()));
            repairRecordExt.setWorkTime(workCenterExt.getWorkTime());
            repairRecordExt.setShiftCode(workCenterExt.getShiftCode());
            repairRecordExt.setWorkOrderNo(workOrderNo);
            ConvertBean.serviceModelInitialize(repairRecordExt, requestModel.getModifyUser());
            new ConvertBeanExt(repairRecordExt).convertBean(repairRecordExt);
            repairRecordExtRepository.insert(repairRecordExt);
        }
    }

    @Override
    public void updateRepairComplete(String serialNumber, String defectType, String organizeCode, String modifyUser) {
        String workOrderNo;
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(organizeCode, serialNumber);
        if (null == produceSnExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("产品条码[%s]无效", serialNumber)
                    .build();
        }
        if (MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue() == produceSnExt.getQcStatus()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("产品条码[%s]质量状态为合格,不能执行维修完成操作", serialNumber)
                    .build();
        }
        if (!StringUtils.isEmpty(produceSnExt.getLastWorkOrderNo()) && !produceSnExt.getLastWorkOrderNo().equals(produceSnExt.getWorkOrderNo())) {
            MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySn(organizeCode, serialNumber);
            if (null == produceSnRepair) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("产品条码[%s]与返工工单[%s]的关联关系不存在", serialNumber, produceSnExt.getLastWorkOrderNo())
                        .build();
            }
            workOrderNo = produceSnRepair.getWorkOrderNo();
        } else {
            workOrderNo = produceSnExt.getWorkOrderNo();
        }

        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]关联的工单号[%s]无效", serialNumber, workOrderNo)
                    .build();
        }

        List<MesBadCellDetail> badCellDetailList = badCellDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.SERIAL_NUMBER},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, serialNumber});
        badCellDetailList = CollectionUtils.isEmpty(badCellDetailList) ? null :
                badCellDetailList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getRepairStatus()) && MesPcnExtEnumUtil.MES_REPAIR_STATUS.NO_REPAIR.getValue() == o.getRepairStatus())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(badCellDetailList)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产工单[%s]产品条码[%s]存在未维修的工位不良数据", workOrderNo, serialNumber)
                    .build();
        }

        MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(organizeCode, produceSnExt.getWorkCenterCode());
        if (null == workCenterExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线代码[%s]无效",  produceSnExt.getWorkCenterCode())
                    .build();
        }
        if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线[%s]未启动班次",  produceSnExt.getWorkCenterCode())
                    .build();
        }

        produceSnExt.setQcStatus(MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue());
        ConvertBean.serviceModelUpdate(produceSnExt, modifyUser);
        produceSnExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnExt).convertBean(produceSnExt);
        produceSnExtRepository.save(produceSnExt);

        workOrderExt.setRepairQty(StringUtils.isEmpty(workOrderExt.getRepairQty()) ? new Double(1) : MathOperation.add(workOrderExt.getRepairQty(), new Double(1)));
        ConvertBean.serviceModelUpdate(workOrderExt, modifyUser);
        new ConvertBeanExt(workOrderExt).convertBean(workOrderExt);
        workOrderExtRepository.save(workOrderExt);

        MesRepairTask repairTask = new MesRepairTask();
        List<String> orderNoList = getOrderNoByRuleCode(1, MesPcnExtConstWords.REPAIR_NO_RULE, new GenSerialNoModel(), modifyUser, organizeCode);
        repairTask.setRepairNo(orderNoList.get(0));
        repairTask.setWorkOrderNo(workOrderNo);
        repairTask.setSerialNumber(serialNumber);
        repairTask.setRepairQty(new Double(1));
        repairTask.setWorkCenterCode(produceSnExt.getWorkCenterCode());
        repairTask.setPartNo(produceSnExt.getPartNo());
        repairTask.setPartNameRdd(produceSnExt.getPartNameRdd());
        repairTask.setShiftCode(workCenterExt.getShiftCode());
        repairTask.setWorkTime(workCenterExt.getWorkTime());
        repairTask.setDefectType(defectType);
        ConvertBean.serviceModelInitialize(repairTask, modifyUser);
        repairTask.setRepairDate(repairTask.getCreateDatetime());
        repairTask.setRepairUser(modifyUser);
        repairTaskRepository.insert(repairTask);
    }

    /**
     * 生成单据编号
     *
     * @param number
     * @param ruleCode
     * @param genSerialNoModel
     * @param userName
     * @param org
     * @return
     */
    public List<String> getOrderNoByRuleCode(int number, String ruleCode, GenSerialNoModel genSerialNoModel, String userName, String org) {
        genSerialNoModel.setRuleCode(ruleCode);
        BaseResultBean<String> serialNumberBean = syncFuncService.syncSerialNo(genSerialNoModel, userName, org, Integer.valueOf(number));
        if (serialNumberBean != null && serialNumberBean.isSuccess()) {
            return serialNumberBean.getResultList();
        } else {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION.getCode())
                    .setErrorDetail(serialNumberBean == null ? "单据编号未生成成功" : serialNumberBean.getErrorMsg())
                    .build();
        }
    }

    @Override
    public void updateRepairBatchComplete(String workOrderNo, String defectType, Double defectQty, String organizeCode, String modifyUser) {
        if (StringUtils.isEmpty(defectQty) || MathOperation.compareTo(defectQty, new Double(1)) < 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("新增不良数量必须大于0")
                    .build();
        }

        List<MesBadCellDetail> badCellDetailList = badCellDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo});
        badCellDetailList = CollectionUtils.isEmpty(badCellDetailList) ? null :
                badCellDetailList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getRepairStatus()) && MesPcnExtEnumUtil.MES_REPAIR_STATUS.NO_REPAIR.getValue() == o.getRepairStatus())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(badCellDetailList)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产工单[%s]存在未维修的工位不良数据", workOrderNo)
                    .build();
        }

        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();
        }

        MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(organizeCode, workOrderExt.getWorkCenterCode());
        if (null == workCenterExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线代码[%s]无效",  workOrderExt.getWorkCenterCode())
                    .build();
        }
        if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("生产线[%s]未启动班次",  workOrderExt.getWorkCenterCode())
                    .build();
        }

        workOrderExt.setRepairQty(StringUtils.isEmpty(workOrderExt.getRepairQty()) ? defectQty : MathOperation.add(workOrderExt.getRepairQty(), defectQty));
        ConvertBean.serviceModelUpdate(workOrderExt, modifyUser);
        new ConvertBeanExt(workOrderExt).convertBean(workOrderExt);
        workOrderExtRepository.save(workOrderExt);

        //新增维修作业单 记录
        MesRepairTask repairTask = new MesRepairTask();
        //设置单据编号 调用平台生成条码的方法
        List<String> orderNoList = getOrderNoByRuleCode(1, MesPcnExtConstWords.REPAIR_NO_RULE, new GenSerialNoModel(), modifyUser, organizeCode);
        repairTask.setRepairNo(orderNoList.get(0));
        repairTask.setWorkOrderNo(workOrderNo);
        repairTask.setRepairQty(defectQty);
        repairTask.setWorkCenterCode(workOrderExt.getWorkCenterCode());
        repairTask.setPartNo(workOrderExt.getPartNo());
        repairTask.setPartNameRdd(workOrderExt.getPartNameRdd());
        repairTask.setShiftCode(workCenterExt.getShiftCode());
        repairTask.setWorkTime(workCenterExt.getWorkTime());
        repairTask.setDefectType(defectType);
        ConvertBean.serviceModelInitialize(repairTask, modifyUser);
        repairTask.setRepairUser(modifyUser);
        repairTask.setRepairDate(repairTask.getCreateDatetime());
        repairTaskRepository.insert(repairTask);
    }

}
