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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPartService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesProdOrgExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesConfigService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesInputDefectRecordService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesPartInspectionInputModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesPartInspectionViewModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesPartModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.*;
import cn.estsh.i3plus.pojo.mes.bean.nc.MesPartInspection;
import cn.estsh.i3plus.pojo.mes.bean.nc.MesPartInspectionDetail;
import cn.estsh.i3plus.pojo.mes.bean.rework.MesReworkTask;
import cn.estsh.i3plus.pojo.mes.bean.rework.MesReworkTaskDetail;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import cn.estsh.i3plus.pojo.mes.repository.*;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.ApiOperation;
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.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description :
 * @Reference :
 * @Author :
 * @CreateDate : 2024-05-09 10:54
 * @Modify:
 **/

@Service
@Slf4j
public class MesInputDefectRecordService implements IMesInputDefectRecordService {

    @Autowired
    private MesProduceSnRepository produceSnRepository;

    @Autowired
    private IMesProdOrgExtService prodOrgExtService;

    @Autowired
    private MesPartRepository partRepository;

    @Autowired
    private MesPartTypePictureRepository partTypePictureRepository;

    @Autowired
    private MesLocationConfigRepository locationConfigRepository;
    @Autowired
    private MesDefectWarnConfigRepository defectWarnConfigRepository;
    @Autowired
    private MesPartTypeDefectRepository partTypeDefectRepository;
    @Autowired
    private MesMoveRepository moveRepository;

    @Autowired
    private MesPartInspectionRepository partInspectionRepository;
    @Autowired
    private MesPartInspectionDetailRepository partInspectionDetailRepository;

    @Autowired
    private MesReworkTaskRepository reworkTaskRepository;

    @Autowired
    private IMesConfigService configService;

    @Autowired
    private ISyncFuncService syncFuncService;

    @Autowired
    private MesDefectTypeRepository defectTypeRepository;

    @Autowired
    private MesReworkTaskDetailRepository mesReworkTaskDetailRDao;

    @Autowired
    private IMesProduceSnExtService produceSnExtService;

    @Autowired
    private MesProductVersionRepository productVersionRepository;

    @Autowired
    private MesProduceSnLogRepository produceSnLogRepository;

    @Autowired
    private MesWorkOrderRepository workOrderRepository;
    @Autowired
    private IMesWorkOrderExtService workOrderExtService;

    @Autowired
    private IMesPartService mesPartService;

    @Autowired
    private MesWorkCenterPartRelationRepository workCenterPartRelationRepository;

    @Autowired
    private MesProductionRecordRepository productionRecordRepository;

    @Autowired
    private IMesMoveRuleRepository mesMoveRuleRepository;

    @Autowired
    private MesWorkOrderPartRepository workOrderPartRepository;

    @Autowired
    private MesPartProdGroupRepository mesPartProdGroupRao;

    @Override
    public MesPartInspectionViewModel queryPartInspection(MesPartInspectionInputModel model) {

        //检查物料
        MesPart part;
        //获取NC-零件检测-单据
        MesPartInspection partInspection = null;
        Integer sourceType;
        if (!StringUtils.isEmpty(model.getSn()) && StringUtils.isEmpty(model.getLotNo())) {
            // 数据校验
            ValidatorBean.checkNotNull(model.getFrontBack(), "正反面不能为空");
            //检查条码
            MesProduceSn produceSn = checkProduceSn(model.getSn(), model.getOrganizeCode());

            //获取NC-零件检测-单据
            partInspection = getPartInspection(produceSn, model, model.getOrganizeCode());

            //检查物料
            part = checkPart(produceSn, model.getOrganizeCode());

            sourceType = MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue();

        } else {
            // 数据校验
            ValidatorBean.checkNotNull(model.getPartNo(), "物料不能为空");
            ValidatorBean.checkNotNull(model.getLotNo(), "批次不能为空");
            ValidatorBean.checkNotNull(model.getFrontBack(), "正反面不能为空");
            ValidatorBean.checkNotNull(model.getQty(), "数量不能为空");

            //检查物料
            part = checkPartByPartNo(model.getPartNo(), model.getOrganizeCode());

            //获取NC-零件检测-单据
            if(!Objects.isNull(model.getId())){
                partInspection = getPartInspectionByPartAndLot(model, model.getOrganizeCode());
            }
            if (!StringUtils.isEmpty(model.getSn())) {
                sourceType = MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.IMPORT.getValue();
            } else {
                sourceType = MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.LOT.getValue();
            }
        }

        //检查零件类型与图片对应关系
        MesPartTypePicture partTypePicture = checkPartTypePicture(part, model.getOrganizeCode());

        //构造返回信息
        MesPartInspectionViewModel result = getModel(partInspection, part, partTypePicture, model, sourceType);
        result.setSourceType(sourceType);
        result.setTransferFlg(model.isTransferFlg());

        return result;
    }

    /**
     * 查询零件类型缺陷类型对应关系
     *
     * @param partNo
     * @param defectLocation
     * @param flg
     * @param frontBack
     * @param org
     * @return
     */
    @Override
    public List<MesPartTypeDefect> queryPartTypeDefect(String partNo, String defectLocation, boolean flg, Integer frontBack, String org) {

        //检查物料
        MesPart part = checkPartByPartNo(partNo, org);

        //查询零件类型缺陷类型对应关系
        List<MesPartTypeDefect> partTypeDefectList = checkPartTypeDefect(part, org);

        //flg==true 则是标黄的位置，需要查询位置下具体需要标黄的缺陷类型
        if (!StringUtils.isEmpty(flg) && flg) {

            List<String> partTypeDefectCodeList = partTypeDefectList.stream().map(k -> k.getDefectTypeCode()).collect(Collectors.toList());

            //查询预警数量
            List<MesDefectWarnConfig> alarmConfigList = queryDefectAlarmConfig(part, frontBack, defectLocation, partTypeDefectCodeList, org);

            //根据不良类型 合计预警数量
            Map<String, MesDefectWarnConfig> alarmConfigMap = alarmConfigList.stream().collect(Collectors.toMap(k -> k.getDefectTypeCode(), k -> k));

            //位置是否标黄Map,key：不良类型，value：标黄标识
            Map<String, Boolean> map = new HashMap<>();
            alarmConfigMap.forEach((k, v) -> {
                //若有这个不良类型则标黄
                if (!map.containsKey(k)) {
                    map.put(k, true);
                }
            });

            //找到标黄的缺陷类型 标识置为true
            partTypeDefectList.forEach(k -> {
                if (map.containsKey(k.getDefectTypeCode())) {
                    k.setFlg(true);
                }
            });
        }

        return partTypeDefectList;
    }

    /**
     * 完成质检-保存不良记录
     *
     * @param model
     */
    @Override
    public void savePartInspection(MesPartInspectionInputModel model) {

        MesProduceSn produceSn = getProduceSn(model.getSn(), model.getOrganizeCode());

        if (null != produceSn && StringUtils.isEmpty(model.getPartNo())) model.setPartNo(produceSn.getPartNo());
        //武汉 会输入客户条码
        boolean isWorkOrderQcStatus=false;
        if (!StringUtil.isEmpty(produceSn)){
            model.setCustSn(produceSn.getCustSn());
            model.setSn(produceSn.getProductSn());
            model.setWorkCenterCode(produceSn.getWorkCenterCode());
            //排序产线 更新工单的 qcStatus
            MesWorkCenter centerDb = prodOrgExtService.getWorkCenterDb(model.getOrganizeCode(), produceSn.getWorkCenterCode());
            if (!StringUtil.isEmpty(centerDb)){
                isWorkOrderQcStatus= centerDb.getCenterType() == MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue();
            }
        }

        if(!StringUtils.isEmpty(model.getPartInspection())){
            model.getPartInspection().setQmsSync(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        }
        if(!Objects.isNull(model.getPart())){
            model.setPart(mesPartService.getMesPartSapByPartNo(model.getPart().getPartNo(), model.getOrganizeCode()));
        }

        // 是否头零件模式
        boolean headModel = true;
        if (!StringUtil.isEmpty(model.getSn())) {
            // 判断是头零件还是散件   查询加工记录
            DdlPackBean productionPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(model.getSn(), "productSn", productionPackBean);
            DdlPreparedPack.getNumNOEqualPack(MesExtEnumUtil.REPORT_STATUS.REPORT_STATUS_30.getValue(), "reportStatus", productionPackBean);
            DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.MES_REPORT_TYPE.CUSTOMER_SUPPLY_MOVE.getValue(), "reportType", productionPackBean);
            MesProductionRecord record = productionRecordRepository.getByProperty(productionPackBean);
            if (record != null) {
                headModel = false;
            }
        }
        // 如果是散件模式，需要查询出对应明细
        List<MesPartModel> mesPartModels = new ArrayList<>();
        if (!headModel) {
            DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(model.getSn(), "workOrderNo", partPackBean);
            List<MesWorkOrderPart> workOrderParts = workOrderPartRepository.findByHqlWhere(partPackBean);
            if (!CollectionUtils.isEmpty(workOrderParts)) {
                DdlPackBean partProdBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(produceSn.getWorkCenterCode(), "workCenterCode", partProdBean);
                List<MesPartProdGroup> mesPartProdGroupList = mesPartProdGroupRao.findByHqlTopWhere(partProdBean, 1);
                for (MesWorkOrderPart workOrderPart : workOrderParts) {
                    MesPart partInfo = mesPartService.getMesPartByPartNo(workOrderPart.getPartNo(), model.getOrganizeCode());
                    String esd = partInfo.getEsd();

                    // 查询移库规则
                    DdlPackBean movePackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(esd, "srcType", movePackBean);
                    DdlPreparedPack.getStringEqualPack(mesPartProdGroupList.get(0).getPartProdGroupCode(), "code", movePackBean);
                    List<MesMoveRule> moveRules = mesMoveRuleRepository.findByHqlWhere(movePackBean);
                    if (!CollectionUtils.isEmpty(moveRules)) {
                        MesPartModel mesPartModel = new MesPartModel();
                        mesPartModel.setPartNo(workOrderPart.getPartNo());
                        mesPartModel.setLocateNo(moveRules.get(0).getErpDestLocateNo());
                        mesPartModels.add(mesPartModel);
                    }
                }
            }
        }


        //零件检测详情为空，则代表本次校验为合格。
        if (CollectionUtils.isEmpty(model.getPartInspectionDetailList())) {
            MesPartInspection partInspection = null;
            //返工单返修完成，需可疑品移正常库

            //判断首次合格还是 多次检验最终为合格品
            //若返修单返修完成，则会生成一个新的检验单并合格
            //若检验单为空，则生成新的单据并合格
            if (StringUtils.isEmpty(model.getPartInspection())) {

                partInspection = createPartInspection(model, MesExtEnumUtil.PART_INSPECTION_STATUS.QUALIFIED.getValue(), model.getOrganizeCode());
                if (model.getOptType() != null && model.getOptType() == 2) {
                    partInspection.setRefundFlag(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                }
                partInspection = partInspectionRepository.save(partInspection);

                if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {
                    //已创建返修单
                    DdlPackBean reworkPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(model.getSn(), "sn", reworkPackBean);
                    DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.REWORK_TASK_STATUS.FINISH.getValue(), "status", reworkPackBean);

                    DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, reworkPackBean);
                    int count = reworkTaskRepository.findByHqlWhereCount(reworkPackBean);
                    if (count > 0) {
                        // 如果是批次还需要移库
                        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                        DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", ddlPackBean);
                        MesProductVersion version = productVersionRepository.getByProperty(ddlPackBean);
                        String srcNo = configService.getCfgValue(model.getOrganizeCode(), "LGORT");
                        if (version != null) {
                            srcNo = version.getReceiveInventoryPoint();
                        }
                        String destLocateNo = getDestLocateNo(model, model.getOrganizeCode());
                        if(!model.isTransferFlg() && !Objects.equals(srcNo, destLocateNo)) {
                            throw new ImppBusiException(String.format("【%s】该条码需要完成质检,再重新录入电子化检验", model.getSn()));
                        }
                    }

                    checkProduceSn(produceSn, model.getSn());
                    produceSn.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue());
                    ConvertBean.serviceModelInitialize(produceSn, model.getUserInfo());
                    produceSnExtService.update(produceSn);
                    //更新工单对应qcStatus
                    if(isWorkOrderQcStatus){
                        MesWorkOrder workOrder = getWorkOrder(model.getOrganizeCode(), produceSn.getWorkOrderNo());
                        if (!StringUtil.isEmpty(workOrder)){
                            workOrder.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue());
                            ConvertBean.serviceModelUpdate(workOrder, model.getUserInfo());
                            workOrderRepository.update(workOrder);
                        }
                    }
                }


            } else {
                //原单据不合格 现合格
                if (model.getPartInspection().getInspectionStatus() == MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue()) {
                    model.getPartInspection().setInspectionStatus(MesExtEnumUtil.PART_INSPECTION_STATUS.QUALIFIED.getValue());
                    ConvertBean.serviceModelUpdate(model.getPartInspection(), model.getUserInfo());

                    if (model.getOptType() != null && model.getOptType() == 2) {
                        model.getPartInspection().setRefundFlag(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                    }
                    partInspectionRepository.save(model.getPartInspection());

                    //查询当前单据明细
                    List<MesPartInspectionDetail> resultDetailList = queryResultDetailList(model, model.getOrganizeCode());

                    List<String> defectTypeNoExitList = new ArrayList<>();
                    for (MesPartInspectionDetail detail : model.getPartInspectionDetailList()) {

                        //根据页面上选择的位置 查询位置，若不存在则提示 位置不存在，请检查数据
                        DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                        DdlPreparedPack.getStringEqualPack(detail.getDefectLocation(), "defectTypeCode", packBean);
                        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.DEFECT_CATEGORY.POSITION.getValue(), "defectType", packBean);
                        MesDefectType defectType = defectTypeRepository.getByProperty(packBean);

                        if (StringUtils.isEmpty(defectType)) {
                            defectTypeNoExitList.add(detail.getDefectLocation());
                            continue;
                        }

                        detail.setDefectTypeId(defectType.getId());
                        detail.setId(null);
                        detail.setOrganizeCode(model.getOrganizeCode());
                        detail.setPid(model.getPartInspection().getId());
                        setSystemSyncStatus(detail);
                        ConvertBean.serviceModelInitialize(detail, model.getUserInfo());

                    }
                    if (!CollectionUtils.isEmpty(defectTypeNoExitList)) {
                        throw new ImppBusiException(String.format("【%s】位置不存在，请检查数据", defectTypeNoExitList));
                    }

                    partInspectionDetailRepository.saveAll(resultDetailList);
                    partInspectionDetailRepository.saveAll(model.getPartInspectionDetailList());

                    if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {
                        checkProduceSn(produceSn, model.getSn());
                        produceSn.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue());
                        ConvertBean.serviceModelInitialize(produceSn, model.getUserInfo());
                        produceSnExtService.update(produceSn);
                        //更新工单对应qcStatus
                        if(isWorkOrderQcStatus){
                            MesWorkOrder workOrder = getWorkOrder(model.getOrganizeCode(), produceSn.getWorkOrderNo());
                            if (!StringUtil.isEmpty(workOrder)){
                                workOrder.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue());
                                ConvertBean.serviceModelUpdate(workOrder, model.getUserInfo());
                                workOrderRepository.update(workOrder);
                            }
                        }
                    }
                    //移库
                    String srcLocate;
                    MesMove mesMove = getMesMove(model, model.getOrganizeCode());
                    if(!Objects.isNull(mesMove)){
                        srcLocate = mesMove.getUmlgo();
                    }else{
                        srcLocate= configService.getCfgValue(model.getOrganizeCode(), "UMLGO");
                    }
                    if (model.getOptType() != 2) {

                        if (!headModel && !CollectionUtils.isEmpty(mesPartModels) && mesPartModels.size() > 0) {
                            for (MesPartModel mesPartModel : mesPartModels) {
                                //移库
                                MesMove move = createMove(model, srcLocate, mesPartModel.getLocateNo(), model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode(),mesPartModel.getPartNo());
                                moveRepository.save(move);
                            }
                        } else {
                            //移库
                            MesMove move = createMove(model, srcLocate, configService.getCfgValue(model.getOrganizeCode(), "LGORT"), model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode());
                            moveRepository.save(move);
                        }

                    }
                }
            }
            if (model.isTransferFlg() && model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {
                //电子化检验
                String  target = configService.getCfgValue(model.getOrganizeCode(), "LGORT");

                DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", ddlPackBean);

                MesProductVersion version = productVersionRepository.getByProperty(ddlPackBean);
                if (version != null) {
                    target = version.getReceiveInventoryPoint();
                }
                log.info("partInspection={}",partInspection);
                if (partInspection != null) {
                    model.setPartInspection(partInspection);
                }
                if (!headModel && !CollectionUtils.isEmpty(mesPartModels) && mesPartModels.size() > 0) {
                    String locateNo = getDestLocateNo(model, model.getOrganizeCode());
                    for (MesPartModel mesPartModel : mesPartModels) {
                        MesMove move = createMove(model,locateNo , mesPartModel.getLocateNo(), model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode(), mesPartModel.getPartNo());
                        moveRepository.save(move);
                    }
                } else {
                    MesMove move = createMove(model, getDestLocateNo(model, model.getOrganizeCode()), target, model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode());
                    moveRepository.save(move);
                }
            }
        } else {
            //零件检测详情不为空，则代表本次校验为不合格。

            //零件检测-单据为空，则是首次不合格
            MesPartInspection partInspection;
            if (StringUtils.isEmpty(model.getPartInspection())) {


                partInspection = createPartInspection(model, MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue(), model.getOrganizeCode());

                if (model.getOptType() != null && model.getOptType() == 2) {
                    partInspection.setRefundFlag(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                }

                model.setPartInspection(partInspectionRepository.save(partInspection));

                List<String> defectTypeNoExitList = new ArrayList<>();
                for (MesPartInspectionDetail detail : model.getPartInspectionDetailList()) {

                    //根据页面上选择的位置 查询位置，若不存在则提示 位置不存在，请检查数据
                    DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(detail.getDefectLocation(), "defectTypeCode", packBean);
                    DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.DEFECT_CATEGORY.POSITION.getValue(), "defectType", packBean);
                    MesDefectType defectType = defectTypeRepository.getByProperty(packBean);

                    if (StringUtils.isEmpty(defectType)) {
                        defectTypeNoExitList.add(detail.getDefectLocation());
                        continue;
                    }

                    detail.setDefectTypeId(defectType.getId());
                    detail.setId(null);
                    detail.setOrganizeCode(model.getOrganizeCode());
                    detail.setPid(partInspection.getId());
                    setSystemSyncStatus(detail);
                    ConvertBean.serviceModelInitialize(detail, model.getUserInfo());

                }
                if (!CollectionUtils.isEmpty(defectTypeNoExitList)) {
                    throw new ImppBusiException(String.format("【%s】位置不存在，请检查数据", defectTypeNoExitList));
                }

                partInspectionDetailRepository.saveAll(model.getPartInspectionDetailList());

                String srcLocateNo = null;
                //若是扫描条码 则判定条码可疑
                if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {
                    //已创建返修单
                    DdlPackBean reworkPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(model.getSn(), "sn", reworkPackBean);
                    DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.REWORK_TASK_STATUS.FINISH.getValue(), "status", reworkPackBean);

                    DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, reworkPackBean);
                    int count = reworkTaskRepository.findByHqlWhereCount(reworkPackBean);
                    if (count > 0) {
                        // 如果是批次还需要移库
                        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                        DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", ddlPackBean);
                        MesProductVersion version = productVersionRepository.getByProperty(ddlPackBean);
                        String srcNo = configService.getCfgValue(model.getOrganizeCode(), "LGORT");
                        if (version != null) {
                            srcNo = version.getReceiveInventoryPoint();
                        }
                        if (!headModel) {
                            srcNo = mesPartModels.get(0).getLocateNo();
                        }
                        String destLocateNo = getDestLocateNo(model, model.getOrganizeCode());
                        if(!Objects.equals(srcNo, destLocateNo)) {
                            throw new ImppBusiException(String.format("【%s】该条码需要完成质检,再重新录入电子化检验", model.getSn()));
                        }
                    }

                    checkProduceSn(produceSn, model.getSn());
                    produceSn.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS.getValue());
                    ConvertBean.serviceModelInitialize(produceSn, model.getUserInfo());
                    produceSnExtService.update(produceSn);
                    //更新工单对应qcStatus
                    if(isWorkOrderQcStatus){
                        MesWorkOrder workOrder = getWorkOrder(model.getOrganizeCode(), produceSn.getWorkOrderNo());
                        if (!StringUtil.isEmpty(workOrder)){
                            workOrder.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS.getValue());
                            ConvertBean.serviceModelUpdate(workOrder, model.getUserInfo());
                            workOrderRepository.update(workOrder);
                        }
                    }
                    //查询条码所在库位
                    // 20241010不找当前所在库位，先找生产版本库位，再找2000
                    // srcLocateNo = getDestLocateNo(model, org);
                }
                if (Objects.isNull(srcLocateNo)) {
                    srcLocateNo = configService.getCfgValue(model.getOrganizeCode(), "LGORT");

                    DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", ddlPackBean);

                    MesProductVersion version = productVersionRepository.getByProperty(ddlPackBean);
                    if (version != null) {
                        srcLocateNo = version.getReceiveInventoryPoint();
                    } else {
                        //当前为初判, 生产版本不存在的情况下, 取生产线信息的材料库位, 如果没有加工记录,生产线使用产线与零件关系表的唯一生产线
                        String workCenterCode;
                        if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {
                            if (null == produceSn || StringUtils.isEmpty(produceSn.getProcessCode())) {
                                List<MesWorkCenterPartRelation> workCenterPartRelationList = getWorkCenterPartRelation(model.getOrganizeCode(), model.getPartNo());
                                if (CollectionUtils.isEmpty(workCenterPartRelationList) || workCenterPartRelationList.size() > 1 || StringUtils.isEmpty(workCenterPartRelationList.get(0).getWorkCenterCode())) {
                                    throw new ImppBusiException(String.format("条码[%s]未查询到有效的来源库位!", model.getSn()));
                                }
                                workCenterCode = workCenterPartRelationList.get(0).getWorkCenterCode();
                            } else {
                                workCenterCode = produceSn.getWorkCenterCode();
                            }
                        } else {
                            workCenterCode = model.getWorkCenterCode();
                        }

                        MesWorkCenter workCenter = prodOrgExtService.getWorkCenterDb(model.getOrganizeCode(), workCenterCode);
                        if (null == workCenter) throw new ImppBusiException(String.format("条码[%s]查询来源库位时获取的生产线代码[%s]信息不存在!", model.getSn(), workCenterCode));
                        if (StringUtils.isEmpty(workCenter.getRawLocate())) throw new ImppBusiException(String.format("条码[%s]查询来源库位时获取的生产线代码[%s]信息未维护[材料库位]!", model.getSn(), workCenterCode));
                        srcLocateNo = workCenter.getRawLocate();
                    }
                }

                if (model.getOptType() != 2) {
                    //移库
                    //移库
                    String dest = configService.getCfgValue(model.getOrganizeCode(), "UMLGO");
                    if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()){
                        String destLocateNo = getDestLocateNo(model, model.getOrganizeCode());
                        if (!StringUtils.isEmpty(destLocateNo) && Objects.equals(dest, destLocateNo)) {
                            return;
                        }
                    }
                    if (!headModel && !CollectionUtils.isEmpty(mesPartModels) && mesPartModels.size() > 0) {
                        for (MesPartModel mesPartModel : mesPartModels) {
                            MesMove move = createMove(model, mesPartModel.getLocateNo(), dest, model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode(), mesPartModel.getPartNo());
                            moveRepository.save(move);
                        }
                    } else{
                        MesMove move = createMove(model, srcLocateNo, dest, model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode());
                        moveRepository.save(move);
                    }

                }

            } else {

                model.getPartInspection().setInspectionStatus(MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue());
                ConvertBean.serviceModelUpdate(model.getPartInspection(), model.getUserInfo());
                if (model.getOptType() != null && model.getOptType() == 2) {
                    model.getPartInspection().setRefundFlag(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                }
                partInspectionRepository.save(model.getPartInspection());
                //查询当前单据明细
                List<MesPartInspectionDetail> resultDetailList = queryResultDetailList(model, model.getOrganizeCode());

                List<String> defectTypeNoExitList = new ArrayList<>();
                for (MesPartInspectionDetail detail : model.getPartInspectionDetailList()) {

                    //根据页面上选择的位置 查询位置，若不存在则提示 位置不存在，请检查数据
                    DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(detail.getDefectLocation(), "defectTypeCode", packBean);
                    DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.DEFECT_CATEGORY.POSITION.getValue(), "defectType", packBean);
                    MesDefectType defectType = defectTypeRepository.getByProperty(packBean);

                    if (StringUtils.isEmpty(defectType)) {
                        defectTypeNoExitList.add(detail.getDefectLocation());
                        continue;
                    }

                    detail.setDefectTypeId(defectType.getId());
                    detail.setId(null);
                    detail.setOrganizeCode(model.getOrganizeCode());
                    detail.setPid(model.getPartInspection().getId());
                    setSystemSyncStatus(detail);
                    ConvertBean.serviceModelInitialize(detail, model.getUserInfo());

                }
                if (!CollectionUtils.isEmpty(defectTypeNoExitList)) {
                    throw new ImppBusiException(String.format("【%s】位置不存在，请检查数据", defectTypeNoExitList));
                }

                //若是扫描条码 则判定条码可疑
                if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {

                    //已创建返修单
                    DdlPackBean reworkPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(model.getSn(), "sn", reworkPackBean);
                    DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.REWORK_TASK_STATUS.FINISH.getValue(), "status", reworkPackBean);

                    DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, reworkPackBean);
                    int count = reworkTaskRepository.findByHqlWhereCount(reworkPackBean);
                    if (count > 0) {
                        // 如果是批次还需要移库
                        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                        DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", ddlPackBean);
                        MesProductVersion version = productVersionRepository.getByProperty(ddlPackBean);
                        String srcNo = configService.getCfgValue(model.getOrganizeCode(), "LGORT");
                        if (version != null) {
                            srcNo = version.getReceiveInventoryPoint();
                        }
                        String destLocateNo = getDestLocateNo(model, model.getOrganizeCode());
                        if (!Objects.equals(srcNo, destLocateNo)) {
                            throw new ImppBusiException(String.format("【%s】该条码需要完成质检,再重新录入电子化检验", model.getSn()));
                        }
                    }

                    checkProduceSn(produceSn, model.getSn());
                    produceSn.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS.getValue());
                    ConvertBean.serviceModelInitialize(produceSn, model.getUserInfo());
                    produceSnExtService.update(produceSn);
                    //更新工单对应qcStatus
                    if(isWorkOrderQcStatus){
                        MesWorkOrder workOrder = getWorkOrder(model.getOrganizeCode(), produceSn.getWorkOrderNo());
                        if (!StringUtil.isEmpty(workOrder)){
                            workOrder.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS.getValue());
                            ConvertBean.serviceModelUpdate(workOrder, model.getUserInfo());
                            workOrderRepository.update(workOrder);
                        }
                    }
                }
                partInspectionDetailRepository.saveAll(resultDetailList);
                partInspectionDetailRepository.saveAll(model.getPartInspectionDetailList());

                //移库
                if (model.getOptType() != 2) {
                    String srcLocateNo = configService.getCfgValue(model.getOrganizeCode(), "LGORT");

                    DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                    DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", ddlPackBean);

                    MesProductVersion version = productVersionRepository.getByProperty(ddlPackBean);
                    if (version != null) {
                        srcLocateNo = version.getReceiveInventoryPoint();
                    }
                    //移库
                    String dest = configService.getCfgValue(model.getOrganizeCode(), "UMLGO");
                    if (model.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()){
                        String destLocateNo = getDestLocateNo(model, model.getOrganizeCode());
                        if (!StringUtils.isEmpty(destLocateNo) && Objects.equals(dest, destLocateNo)) {
                            return;
                        }
                    }
                    if (!headModel && !CollectionUtils.isEmpty(mesPartModels) && mesPartModels.size() > 0) {
                        for (MesPartModel mesPartModel : mesPartModels) {
                            //移库
                            MesMove move = createMove(model, mesPartModel.getLocateNo(), dest, model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode(),mesPartModel.getPartNo());
                            moveRepository.save(move);
                        }
                    } else{
                        MesMove move = createMove(model, srcLocateNo, dest, model.getOrganizeCode(), null == produceSn ? model.getWorkCenterCode() : produceSn.getWorkCenterCode());
                        moveRepository.save(move);
                    }

                }

            }

        }
    }

    private List<MesWorkCenterPartRelation> getWorkCenterPartRelation(String org, String partNo) {
        return workCenterPartRelationRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PART_NO},
                new Object[]{org, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
    }

    public MesWorkOrder getWorkOrder(String organizeCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo)) return null;
        return workOrderRepository.getByProperty(
                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});
    }

    private String getDestLocateNo(MesPartInspectionInputModel model, String org) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(model.getSn(), "productSn", packBean);
        DdlPreparedPack.getOrderBy(null, null, packBean);
        MesMove mesMove = moveRepository.getByProperty(packBean);
        return Objects.isNull(mesMove) ? null : mesMove.getUmlgo();
    }

    private MesMove getMesMove(MesPartInspectionInputModel model, String org) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getNumEqualPack(model.getPartInspection().getId(),"partInspectionId",packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.MOVE_TYPE.RETURN_MOVE.getValue(),"moveType",packBean);
        return moveRepository.getByProperty(packBean);
    }

    /**
     * 获取NC-零件检测-单据
     *
     * @param inputModel
     * @param org
     * @return
     */
    private MesPartInspection getPartInspection(MesProduceSn produceSn,MesPartInspectionInputModel inputModel, String org) {

        Integer refundFlag = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
        if (inputModel.getOptType() == 2) {
            refundFlag = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        }

        //根据条码找最近一条单据
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(produceSn.getProductSn(), "sn", packBean);
        DdlPreparedPack.getNumEqualPack(refundFlag, "refundFlag", packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
        MesPartInspection partInspection = partInspectionRepository.getByProperty(packBean);

        //若单据经过了NC处理，则返回为空
        partInspection = checkPartInspection(partInspection, inputModel, org);

        return partInspection;
    }

    /**
     * 获取NC-零件检测-单据
     *
     * @param inputModel
     * @param org
     * @return
     */
    private MesPartInspection getPartInspectionByPartAndLot(MesPartInspectionInputModel inputModel, String org) {

        List<Integer> list = Stream.of(MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.LOT.getValue(),MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.IMPORT.getValue()).collect(Collectors.toList());
        //根据扫描的条码查询条码是否存在
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getNumEqualPack(inputModel.getId(), "id", packBean);
        DdlPreparedPack.getStringEqualPack(inputModel.getPartNo(), "partNo", packBean);
        DdlPreparedPack.getStringEqualPack(inputModel.getLotNo(), "lotNo", packBean);
        DdlPreparedPack.getNegativeNumEqualPack(inputModel.getQty(), "qty", packBean);
        DdlPreparedPack.getInPackList(list, "sourceType", packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
        MesPartInspection partInspection = partInspectionRepository.getByProperty(packBean);

        checkPartInspection(partInspection, inputModel, org);

        return partInspection;
    }

    /**
     * 查询当前单据明细
     *
     * @param model
     * @param org
     * @return
     */
    private List<MesPartInspectionDetail> queryResultDetailList(MesPartInspectionInputModel model, String org) {

        //根据扫描的条码查询条码是否存在
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getNumEqualPack(model.getPartInspection().getId(), "pid", packBean);
        List<MesPartInspectionDetail> resultDetailList = partInspectionDetailRepository.findByHqlWhere(packBean);
        resultDetailList.forEach(k -> {
            k.setIsDeleted(MesCommonConstant.TRUE_INTEGER);
            setSystemSyncStatus(k);
            ConvertBean.serviceModelUpdate(model.getPartInspection(), model.getUserInfo());
        });

        return resultDetailList;
    }

    /**
     * 校验NC-零件检测-单据
     *
     * @param partInspection
     * @return
     */
    private MesPartInspection checkPartInspection(MesPartInspection partInspection, MesPartInspectionInputModel inputModel, String org) {

        //针对检测单-一次NC处理是一个单据 若单据经过了NC处理，则返回为空

        if (!StringUtils.isEmpty(partInspection)) {

            //可疑,待NC
            if (partInspection.getInspectionStatus() == MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue()
                    && partInspection.getNcStatus() == MesExtEnumUtil.PART_INSPECTION_NC_STATUS.CREATE.getValue()) {

                return partInspection;
            } else if (partInspection.getInspectionStatus() == MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue()
                    && partInspection.getNcStatus() == MesExtEnumUtil.PART_INSPECTION_NC_STATUS.SCRAP.getValue()) {

                //报废
                throw ImppExceptionBuilder.newInstance().setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode())
                        .setErrorDetail(String.format("【%s】条码已报废,请检查数据", partInspection.getSn()))
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .build();

            } else if (partInspection.getInspectionStatus() == MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue()
                    && partInspection.getNcStatus() == MesExtEnumUtil.PART_INSPECTION_NC_STATUS.REWORK.getValue()) {


                //已创建返修单
                DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
                DdlPreparedPack.getNumEqualPack(partInspection.getId(), "partInspectionId", packBean);
                DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
                MesReworkTask reworkTask = reworkTaskRepository.getByProperty(packBean);

                if (!StringUtils.isEmpty(reworkTask) && reworkTask.getStatus() == MesExtEnumUtil.REWORK_TASK_STATUS.CREATE.getValue()) {

                    //已创建返修单
                    throw ImppExceptionBuilder.newInstance().setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode())
                            .setErrorDetail(String.format("【%s】已创建返修单,请检查数据", partInspection.getSn()))
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .build();
                } else if (!StringUtils.isEmpty(reworkTask) && reworkTask.getStatus() == MesExtEnumUtil.REWORK_TASK_STATUS.FINISH.getValue() &&
                        partInspection.getSourceType() == MesExtEnumUtil.PART_INSPECTION_SOURCE_TYPE.SINGLE.getValue()) {
                    //返修单 进行返修
                    inputModel.setTransferFlg(true);
                }
                return partInspection;
            }
        }

        return null;
    }

    /**
     * 检查条码
     *
     * @param serialNumber
     * @param org
     * @return
     */
    private MesProduceSn checkProduceSn(String serialNumber, String org) {
        //根据扫描的条码查询条码是否存在
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(serialNumber, "productSn", packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
        MesProduceSn produceSn = produceSnRepository.getByProperty(packBean);

        //若不存在则提示条码不存在
        if (StringUtils.isEmpty(produceSn)) {
            //再查一下排序查产线
            produceSn = (MesProduceSn) workOrderExtService.getWorkOrderSort(org, serialNumber, false);
            if (StringUtils.isEmpty(produceSn)) {
                throw new ImppBusiException(String.format("【%s】此条码不存在，请检查数据", serialNumber));
            }
        }

        //根据条码状态判断 是不是创建或已下线条码
        //若不是创建或已下线的条码则提示 条码状态 请选择创建或已下线的条码
        if (!MesExtEnumUtil.PRODUCE_SN_STATUS.checkAllowInputDefectData(produceSn.getSnStatus())) {
            throw new ImppBusiException(String.format("【%s】此条码状态为【%s】,请选择状态为【%s】【%s】状态的条码", serialNumber, MesExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(produceSn.getSnStatus()),
                    MesExtEnumUtil.PRODUCE_SN_STATUS.CREATE.getDescription(),MesExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getDescription()));
        }

        return produceSn;
    }

    private MesProduceSn getProduceSn(String serialNumber, String org) {
        if (StringUtils.isEmpty(serialNumber)) return null;
        //根据扫描的条码查询条码是否存在
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(serialNumber, "productSn", packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
        MesProduceSn produceSn =produceSnRepository.getByProperty(packBean);
        if (StringUtil.isEmpty(produceSn)){
            produceSn = (MesProduceSn) workOrderExtService.getWorkOrderSort(org, serialNumber, false);
        }
        return produceSn;
    }

    private MesProduceSn checkProduceSn(MesProduceSn produceSn, String serialNumber) {

        //若不存在则提示条码不存在
        if (StringUtils.isEmpty(produceSn)) {
            throw new ImppBusiException(String.format("【%s】此条码不存在，请检查数据", serialNumber));
        }

        //根据条码状态判断 是不是已下线条码
        //若不是已下线的条码则提示 条码状态 请选择已下线的条码
        if (!MesExtEnumUtil.PRODUCE_SN_STATUS.checkAllowInputDefectData(produceSn.getSnStatus())) {
            throw new ImppBusiException(String.format("【%s】此条码状态为【%s】,请选择状态为【%s】状态的条码", serialNumber, MesExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(produceSn.getSnStatus()),
                    MesExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getDescription()));
        }

        return produceSn;
    }

    /**
     * 创建单据
     *
     * @param model
     * @param inspectionStatus
     * @param org
     * @return
     */
    private MesPartInspection createPartInspection(MesPartInspectionInputModel model, Integer inspectionStatus, String org) {
        MesPartInspection partInspection = new MesPartInspection();
        partInspection.setOrganizeCode(org);
        partInspection.setPartNo(model.getPart().getPartNo());
        partInspection.setPartName(model.getPart().getPartName());
        partInspection.setInspectionDate(TimeTool.getToday());
        partInspection.setInspectionStatus(inspectionStatus);
        partInspection.setNcStatus(MesExtEnumUtil.PART_INSPECTION_NC_STATUS.CREATE.getValue());
        partInspection.setSn(model.getSn());
        partInspection.setCustSn(StringUtil.isEmpty(model.getCustSn())?"":model.getCustSn());
        partInspection.setLotNo(model.getLotNo());
        partInspection.setQty(model.getQty());
        partInspection.setWorkCenterCode(model.getWorkCenterCode());
        partInspection.setShiftCode(model.getShiftCode());
        partInspection.setSourceType(model.getSourceType());
        //42984 初检创建时修改人和修改时间不能赋值
        partInspection.setCreateDatetime(TimeTool.getNowTime(true));
        partInspection.setCreateUser(model.getUserInfo());
        partInspection.setIsValid(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        partInspection.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        return partInspection;
    }

    /**
     * 移库
     *
     * @param model
     * @param source 来源
     * @param target 目标
     * @param org
     * @return
     */
    private MesMove createMove(MesPartInspectionInputModel model, String source, String target, String org, String workCenterCode) {
        if (StringUtils.isEmpty(model.getUserInfo())) model.setUserInfo("系统");
        GenSerialNoModel serialNoModel = new GenSerialNoModel("INPUT_DEFECT_ZRSUM");
        serialNoModel.setPartNo(model.getPart().getPartNo());
        ResultBean rb = syncFuncService.syncSerialNo(serialNoModel, model.getUserInfo(), org, 1);
        String zrsum = "";
        if (null != rb && !CollectionUtils.isEmpty(rb.getResultList())) {
            zrsum = (rb.getResultList().get(0)).toString();
        }

        MesMove move = new MesMove();
        move.setMatnr(model.getPart().getPartNo());
        move.setOrganizeCode(org);
        move.setFactoryCode(org);
        move.setLgort(source);
        move.setUmlgo(target);
        move.setMenge(model.getQty());
        move.setMeins(model.getPart().getUnit());
        move.setZrsum(zrsum);
        move.setPostDate(TimeTool.getToday());
        move.setPostTime(TimeTool.getTimeShortWithColon());
        move.setPartInspectionId(Objects.isNull(model.getPartInspection()) ? 0 : model.getPartInspection().getId());
        move.setMoveType(MesExtEnumUtil.MOVE_TYPE.SUSPICIOUS_MOVE.getValue());
        move.setProductSn(model.getSn());
        move.setWorkCenter(prodOrgExtService.getErpWorkCenterCode(org, workCenterCode));
        ConvertBean.serviceModelInitialize(move, model.getUserInfo());
        return move;
    }

    /**
     * 移库
     *
     * @param model
     * @param source 来源
     * @param target 目标
     * @param org
     * @return
     */
    private MesMove createMove(MesPartInspectionInputModel model, String source, String target, String org, String workCenterCode, String partNo) {
        if (StringUtils.isEmpty(model.getUserInfo())) model.setUserInfo("系统");
        GenSerialNoModel serialNoModel = new GenSerialNoModel("INPUT_DEFECT_ZRSUM");
        serialNoModel.setPartNo(partNo);
        ResultBean rb = syncFuncService.syncSerialNo(serialNoModel, model.getUserInfo(), org, 1);
        String zrsum = "";
        if (null != rb && !CollectionUtils.isEmpty(rb.getResultList())) {
            zrsum = (rb.getResultList().get(0)).toString();
        }

        MesMove move = new MesMove();
        move.setMatnr(partNo);
        move.setOrganizeCode(org);
        move.setFactoryCode(org);
        move.setLgort(source);
        move.setUmlgo(target);
        move.setMenge(model.getQty());
        move.setMeins(model.getPart().getUnit());
        move.setZrsum(zrsum);
        move.setPostDate(TimeTool.getToday());
        move.setPostTime(TimeTool.getTimeShortWithColon());
        move.setPartInspectionId(Objects.isNull(model.getPartInspection()) ? 0 : model.getPartInspection().getId());
        move.setMoveType(MesExtEnumUtil.MOVE_TYPE.SUSPICIOUS_MOVE.getValue());
        move.setProductSn(model.getSn());
        move.setWorkCenter(prodOrgExtService.getErpWorkCenterCode(org, workCenterCode));
        ConvertBean.serviceModelInitialize(move, model.getUserInfo());
        return move;
    }
    private MesPart checkPart(MesProduceSn produceSn, String org) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(produceSn.getPartNo(), "partNo", packBean);
        MesPart part = partRepository.getByProperty(packBean);

        //若不存在则提示物料信息不存在
        if (StringUtils.isEmpty(part)) {
            throw new ImppBusiException(String.format("【%s】此条码对应【%s】物料号不存在，请检查数据", produceSn.getProductSn(), produceSn.getPartNo()));
        }

        //根据条码查询零件类型（partType）
        if (StringUtils.isEmpty(part.getPartTypeCode())) {
            throw new ImppBusiException(String.format("【%s】此条码对应【%s】物料号未维护零件类型，请检查数据", produceSn.getProductSn(), produceSn.getPartNo()));
        }


        return part;
    }

    private MesPart checkPartByPartNo(String partNo, String org) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", packBean);
        MesPart part = partRepository.getByProperty(packBean);

        //若不存在则提示物料信息不存在
        if (StringUtils.isEmpty(part)) {
            throw new ImppBusiException(String.format("【%s】物料号不存在，请检查数据", partNo));
        }

        return part;
    }

    private MesPartTypePicture checkPartTypePicture(MesPart part, String org) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(part.getPartTypeCode(), "partTypeCode", packBean);

        return partTypePictureRepository.getByProperty(packBean);
    }

    private List<MesLocationConfig> checkLocationConfig(String org) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue(), CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{"rowLocation", "columnLocation"}, packBean);
        List<MesLocationConfig> configList = locationConfigRepository.findByHqlWhere(packBean);

        if (CollectionUtils.isEmpty(configList)) {
            throw new ImppBusiException(String.format("未配置位置，请检查数据"));
        }

        return configList;
    }

    private List<MesPartTypeDefect> checkPartTypeDefect(MesPart part, String org) {
        if(StringUtils.isEmpty(part.getPartTypeCode()))  throw new ImppBusiException(String.format("物料【%s】零件类型信息未维护，请检查数据", part.getPartNo()));

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(part.getPartTypeCode(), "partTypeCode", packBean);
        List<MesPartTypeDefect> partTypeDefectList = partTypeDefectRepository.findByHqlWhere(packBean);

        if (CollectionUtils.isEmpty(partTypeDefectList)) {
            throw new ImppBusiException(String.format("未配置【%s】零件类型缺陷类型对应关系，请检查数据", part.getPartTypeCode()));
        }

        return partTypeDefectList;
    }

    private List<MesDefectWarnConfig> queryDefectAlarmConfigByLocationCodeList(MesPart part, Integer frontBack, List<String> locationCodeList, List<String> partTypeDefectCodeList,
                                                                               String org) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(part.getPartNo(), "partNo", packBean);
        DdlPreparedPack.getStringEqualPack(part.getPartTypeCode(), "partTypeCode", packBean);
        DdlPreparedPack.getNumEqualPack(frontBack, "frontBack", packBean);
        DdlPreparedPack.getInPackList(locationCodeList, "defectLocation", packBean);
        DdlPreparedPack.getInPackList(partTypeDefectCodeList, "defectTypeCode", packBean);
        List<MesDefectWarnConfig> alarmConfigList = defectWarnConfigRepository.findByHqlWhere(packBean);

        return alarmConfigList;
    }

    private List<MesDefectWarnConfig> queryDefectAlarmConfig(MesPart part, Integer frontBack, String locationCode, List<String> partTypeDefectCodeList, String org) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(part.getPartNo(), "partNo", packBean);
        DdlPreparedPack.getStringEqualPack(part.getPartTypeCode(), "partTypeCode", packBean);
        DdlPreparedPack.getNumEqualPack(frontBack, "frontBack", packBean);
        DdlPreparedPack.getStringEqualPack(locationCode, "defectLocation", packBean);
        DdlPreparedPack.getInPackList(partTypeDefectCodeList, "defectCode", packBean);
        List<MesDefectWarnConfig> alarmConfigList = defectWarnConfigRepository.findByHqlWhere(packBean);

        return alarmConfigList;
    }

    private MesPartInspectionViewModel getModel(MesPartInspection partInspection, MesPart part, MesPartTypePicture partTypePicture, MesPartInspectionInputModel model, Integer sourceType) {

        Integer frontBack = model.getFrontBack();
        List<MesPartInspectionDetail> detailList = new ArrayList<>();
        if (!StringUtils.isEmpty(partInspection) && partInspection.getInspectionStatus() != MesExtEnumUtil.PART_INSPECTION_STATUS.QUALIFIED.getValue()) {

            detailList = checkPartInspectionDetail(partInspection, model.getOrganizeCode());
            if(!CollectionUtils.isEmpty(detailList)){
                //判断是否返工完成
                checkReworkFinished(model.getOrganizeCode(), detailList);
            }
        }
        //位置
        List<MesLocationConfig> locationConfigList = checkLocationConfig(model.getOrganizeCode());
        List<String> locationCodeList = locationConfigList.stream().map(k -> k.getDefectLocation()).collect(Collectors.toList());

        //查询零件类型缺陷类型对应关系
        List<MesPartTypeDefect> partTypeDefectList = checkPartTypeDefect(part, model.getOrganizeCode());
        List<String> partTypeDefectCodeList = partTypeDefectList.stream().map(k -> k.getDefectTypeCode()).collect(Collectors.toList());

        //查询预警数量
        List<MesDefectWarnConfig> alarmConfigList = queryDefectAlarmConfigByLocationCodeList(part, frontBack, locationCodeList, partTypeDefectCodeList, model.getOrganizeCode());

        //根据不良类型+位置 若有数据，则表示需要标黄
        Map<String, List<MesDefectWarnConfig>> alarmConfigMap = alarmConfigList.stream().collect(Collectors.groupingBy(MesDefectWarnConfig::getDefectLocation));

        //返修完成需要标记绿色
        Map<String, List<MesPartInspectionDetail>> detailMap = detailList.stream().collect(Collectors.groupingBy(t->t.getDefectLocation()+t.getFrontBack()));

        locationConfigList.forEach(k -> {
            k.setFrontBack(frontBack);
            if (alarmConfigMap.containsKey(k.getDefectLocation())) {
                k.setIsflg(true);
                k.setColour(MesExtEnumUtil.COLOR.YELLOW.getValue());
            }

            if(detailMap.containsKey(k.getDefectLocation()+frontBack) &&
                    detailMap.get(k.getDefectLocation()+frontBack).stream().allMatch(MesPartInspectionDetail::getReworkFinished)){
                k.setIsflg(true);
                k.setColour(MesExtEnumUtil.COLOR.GREEN.getValue());
            }
        });

        MesPartInspectionViewModel result = new MesPartInspectionViewModel();
        result.setPart(part);
        if (!StringUtils.isEmpty(partInspection) && partInspection.getInspectionStatus() == MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue()
                && partInspection.getNcStatus() == MesExtEnumUtil.PART_INSPECTION_NC_STATUS.CREATE.getValue()) {
            result.setPartInspectionDetailList(detailList);
            result.setPartInspection(partInspection);
        }
        result.setPartTypePicture(partTypePicture);
        result.setLocationConfigList(locationConfigList);
        result.setMesDefectWarnConfigList(alarmConfigList);
        return result;

    }

    private void checkReworkFinished(String org, List<MesPartInspectionDetail> detailList) {
        List<Long> idList = detailList.stream().map(MesPartInspectionDetail::getId).collect(Collectors.toList());
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getInPackList(idList, "partInspectionDetailId", packBean);
        List<MesReworkTaskDetail> mesReworkTaskDetailList = mesReworkTaskDetailRDao.findByHqlWhere(packBean);
        if (!CollectionUtils.isEmpty(mesReworkTaskDetailList)){
            Map<Long, List<MesReworkTaskDetail>> mesReworkTaskDetailMap = mesReworkTaskDetailList.stream().collect(Collectors.groupingBy(MesReworkTaskDetail::getPartInspectionDetailId));
            detailList.forEach(d->{
                if(mesReworkTaskDetailMap.containsKey(d.getId()) &&
                        mesReworkTaskDetailMap.get(d.getId()).stream().allMatch(t->MesExtEnumUtil.REWORK_TASK_STATUS.FINISH.getValue() == t.getStatus())){
                    d.setReworkFinished(true);
                }
            });
        }
    }

    private List<MesPartInspectionDetail> checkPartInspectionDetail(MesPartInspection partInspection, String org) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getNumEqualPack(partInspection.getId(), MesPcnExtConstWords.PID, packBean);

        return partInspectionDetailRepository.findByHqlWhere(packBean);
    }

    @Override
    public void saveSusPartInspection(MesPartInspectionInputModel model, String org) {

        MesPartInspection partInspection = new MesPartInspection();
        BeanUtils.copyProperties(model, partInspection, MesPcnExtConstWords.ID);
        partInspection.setInspectionStatus(MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue());
        partInspection.setNcStatus(MesExtEnumUtil.PART_INSPECTION_NC_STATUS.CREATE.getValue());
        partInspection.setOrganizeCode(org);

        MesConfig mesConfig = configService.getMesConfigNoError(org, "MES_SUSPICIOUS_DEFECT_TYPE_CODE");
        String defectTypeCodeStr = (null != mesConfig && !StringUtils.isEmpty(mesConfig.getCfgValue())) ? mesConfig.getCfgValue() : "QX130";

        //根据页面上选择的位置 查询位置，若不存在则提示 位置不存在，请检查数据
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(org);
        DdlPreparedPack.getStringEqualPack(defectTypeCodeStr, "defectTypeCode", packBean);
        MesDefectType defectType = defectTypeRepository.getByProperty(packBean);
        if (null == defectType) MesPcnException.throwBusiException(String.format("未配置[%s]对应的可疑品缺陷类型!", defectTypeCodeStr));

        partInspection.setDefectTypeCode(defectType.getDefectTypeCode());
        partInspection.setDefectTypeId(defectType.getId());
        ConvertBean.serviceModelInitialize(partInspection, model.getUserInfo());
        partInspection = partInspectionRepository.insert(partInspection);

        MesPartInspectionDetail detail = new MesPartInspectionDetail();

        detail.setPid(partInspection.getId());
        detail.setFrontBack(10);
        detail.setDefectLocation("A01");
        detail.setOrganizeCode(org);
        detail.setDefectTypeId(defectType.getId());
        detail.setDefectTypeCode(defectType.getDefectTypeCode());
        detail.setDefectTypeName(defectType.getDefectTypeName());
        detail.setId(null);
        detail.setOrganizeCode(org);
        ConvertBean.serviceModelInitialize(detail, model.getUserInfo());
        partInspectionDetailRepository.insert(detail);
        //查询来源/目标库位
        MesConfig sourceCfg = configService.getMesConfigNoError(org, "LGORT");
        MesConfig targetCfg = configService.getMesConfigNoError(org, "UMLGO");
        String source = (null != sourceCfg && !StringUtils.isEmpty(sourceCfg.getCfgValue())) ? sourceCfg.getCfgValue() : null;
        String target = (null != targetCfg && !StringUtils.isEmpty(targetCfg.getCfgValue())) ? targetCfg.getCfgValue() : null;
        if (StringUtils.isEmpty(target)) MesPcnException.throwBusiException(String.format("系统配置缺失目标库位配置[%s]!", "UMLGO"));
        //移库
        String partNo = model.getPartNo();
        // 查询生产版本
        String workCenterCode = model.getWorkCenterCode();
        String productVersion = model.getProductVersion();
        MesProductVersion version = null;
        if (!StringUtils.isEmpty(productVersion)) {
            DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(org);
            DdlPreparedPack.getStringEqualPack(partNo, "partNo", ddlPackBean);
            DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", ddlPackBean);
            DdlPreparedPack.getStringEqualPack(productVersion, "productVersion", ddlPackBean);

            version = productVersionRepository.getByProperty(ddlPackBean);
            if (version != null) {
                source = version.getReceiveInventoryPoint();
            }
        }

        //生产版本不存在的情况下, 取生产线信息的材料库位
        if (null == version) {
            MesWorkCenter workCenter = prodOrgExtService.getWorkCenterDb(org, workCenterCode);
            if (null != workCenter && !StringUtils.isEmpty(workCenter.getRawLocate())) source = workCenter.getRawLocate();
        }


        // 是否头零件模式
        boolean headModel = true;
        if (!StringUtil.isEmpty(model.getSn())) {

            // 判断是头零件还是散件   查询加工记录
            DdlPackBean productionPackBean = DdlPackBean.getDdlPackBean(org);
            DdlPreparedPack.getStringEqualPack(model.getSn(), "productSn", productionPackBean);
            DdlPreparedPack.getNumNOEqualPack(MesExtEnumUtil.REPORT_STATUS.REPORT_STATUS_30.getValue(), "reportStatus", productionPackBean);
            DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.MES_REPORT_TYPE.CUSTOMER_SUPPLY_MOVE.getValue(), "reportType", productionPackBean);
            MesProductionRecord record = productionRecordRepository.getByProperty(productionPackBean);
            if (record != null) {
                headModel = false;
            }
        }
        // 如果是散件模式，需要查询出对应明细
        List<MesPartModel> mesPartModels = new ArrayList<>();
        if (!headModel) {
            MesProduceSn produceSn = getProduceSn(model.getSn(), org);

            DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(org);
            DdlPreparedPack.getStringEqualPack(model.getSn(), "workOrderNo", partPackBean);
            List<MesWorkOrderPart> workOrderParts = workOrderPartRepository.findByHqlWhere(partPackBean);
            if (!CollectionUtils.isEmpty(workOrderParts)) {
                DdlPackBean partProdBean = DdlPackBean.getDdlPackBean(org);
                DdlPreparedPack.getStringEqualPack(produceSn.getWorkCenterCode(), "workCenterCode", partProdBean);
                List<MesPartProdGroup> mesPartProdGroupList = mesPartProdGroupRao.findByHqlTopWhere(partProdBean, 1);
                for (MesWorkOrderPart workOrderPart : workOrderParts) {
                    MesPart partInfo = mesPartService.getMesPartByPartNo(workOrderPart.getPartNo(), org);
                    String esd = partInfo.getEsd();

                    // 查询移库规则
                    DdlPackBean movePackBean = DdlPackBean.getDdlPackBean(org);
                    DdlPreparedPack.getStringEqualPack(esd, "srcType", movePackBean);
                    DdlPreparedPack.getStringEqualPack(mesPartProdGroupList.get(0).getPartProdGroupCode(), "code", movePackBean);
                    List<MesMoveRule> moveRules = mesMoveRuleRepository.findByHqlWhere(movePackBean);
                    if (!CollectionUtils.isEmpty(moveRules)) {
                        MesPartModel mesPartModel = new MesPartModel();
                        mesPartModel.setPartNo(workOrderPart.getPartNo());
                        mesPartModel.setLocateNo(moveRules.get(0).getErpDestLocateNo());
                        mesPartModels.add(mesPartModel);
                    }
                }
            }
        }

        if (!headModel && !CollectionUtils.isEmpty(mesPartModels) && mesPartModels.size() > 0) {
            for (MesPartModel mesPartModel : mesPartModels) {
                //移库
                MesMove move = createMove(model, mesPartModel.getLocateNo(),target, org, model.getWorkCenterCode());
                moveRepository.save(move);
            }
        } else  {
            //移库
            if (StringUtils.isEmpty(source)) MesPcnException.throwBusiException("未找到来源库位!");
            MesMove move = createMove(model, source, target, org, model.getWorkCenterCode());
            moveRepository.save(move);
        }
    }

    @ApiOperation(value = "客退品查询")
    public MesProduceSn customerBack(String organizeCode, String sn, String userName) {
        List<MesProduceSn> produceSnList = produceSnExtService.getProduceSnList(organizeCode, sn);
        if (CollectionUtils.isEmpty(produceSnList)) {
            MesPcnException.throwFlowException("条码不存在");
        }
        MesProduceSn mesProduceSn = produceSnList.get(0);


        return mesProduceSn;
    }

    @ApiOperation(value = "客退品提交")
    public void customerBackCommit(String organizeCode, String sn, String userName) {

        List<MesProduceSn> produceSnList = produceSnExtService.getProduceSnList(organizeCode, sn);
        if (CollectionUtils.isEmpty(produceSnList)) {
            MesPcnException.throwFlowException("条码不存在");
        }
        MesProduceSn produceSn = produceSnList.stream().filter(mesProduceSn -> Objects.equals(mesProduceSn.getSnStatus(), MesExtEnumUtil.PRODUCE_SN_STATUS.SHIPPED.getValue())).findFirst().orElse(null);
        if (produceSn == null) {
            MesPcnException.throwFlowException("条码不存在");
        }

        MesProduceSnLog produceSnLog = new MesProduceSnLog();

        if (Objects.equals(produceSn.getSnStatus(), MesExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue())) {
            MesPcnException.throwFlowException("条码状态不为已发运");
        }
        produceSn.setSnStatus(MesExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue());
        ConvertBean.serviceModelUpdate(produceSn, userName);
        produceSnExtService.update(produceSn);

        produceSnLog.setProductSn(produceSn.getProductSn());
        produceSnLog.setCustSn(produceSn.getCustSn());
        produceSnLog.setOriProductSn(null);
        produceSnLog.setBeforeSnStatus(MesExtEnumUtil.PRODUCE_SN_STATUS.SHIPPED.getValue());
        produceSnLog.setAfterSnStatus(MesExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue());
        ConvertBean.serviceModelInitialize(produceSnLog, userName);
        produceSnLogRepository.save(produceSnLog);
    }

    private void setSystemSyncStatus(MesPartInspectionDetail detail) {
        detail.setSystemSyncStatus(CommonEnumUtil.FALSE);
        detail.setSystemSyncDatetime(MesPcnExtConstWords.EMPTY);
    }
}
