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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IBomExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IConsistencyAffirmRecordService;
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.busi.IMissingPartService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnRepairService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep.EpSnLineOffModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkCenterExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderInfoModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProductDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProductDataReviewModel;
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.actor.shipping.dispatch.IFsmCommonService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.mes.pcn.util.PojoAttrUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesBom;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCheck;
import cn.estsh.i3plus.pojo.mes.bean.MesProductData;
import cn.estsh.i3plus.pojo.mes.model.*;
import cn.estsh.i3plus.pojo.mes.repository.MesPartCheckRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesProductDataRepository;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description : 工步业务-EP打包下线工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("epSnLineOffExecStepService")
public class EpSnLineOffExecStepService extends BaseStepService {

    @Autowired
    private EpSnLineOffModuleService snLineOffModuleService;

    @Autowired
    private IMissingPartService missingPartService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private MesErrorLineOffRepository errorLineOffRepository;

    @Autowired
    private MesErrorLineOffDetailRepository errorLineOffDetailRepository;

    @Autowired
    private IBomExtService bomExtService;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Autowired
    private IWorkCenterExtService workCenterExtService;

    @Autowired
    private IConsistencyAffirmRecordService consistencyAffirmRecordService;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private MesPackageExtRepository packageExtRepository;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private MesProduceSnRepairRepository produceSnRepairRepository;

    @Autowired
    private MesProductDataRepository productDataRepository;

    @Autowired
    private MesPartCheckRepository partCheckRepository;

    @Autowired
    private IFsmCommonService fsmCommonService;

    @Autowired
    private MesProductDataReviewRecordRepository productDataReviewRecordRepository;

    @Autowired
    private EpCollectDbProductDataStepService collectDbProductDataStepService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("EpSnLineOffExecStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工厂代码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少生产工单号");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少产品条码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        WorkCenterExtModel centerModel = (WorkCenterExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.WORK_CENTER);
        if (null == centerModel || (StringUtils.isEmpty(centerModel.getShiftCode()) || StringUtils.isEmpty(centerModel.getWorkTime()))) {
            MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
            if (null == workCenterExt) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线代码[%s]无效"), reqBean.getWorkCenterCode());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
            if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线代码[%s]未启动班次"), reqBean.getWorkCenterCode());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
            centerModel = new WorkCenterExtModel();
            BeanUtils.copyProperties(workCenterExt, centerModel, MesPcnExtConstWords.ID);
            reqBean.getDataMap().put(MesPcnExtConstWords.WORK_CENTER, centerModel);
        }

        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null == produceSnExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]无效"), reqBean.getSerialNumber());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null == workOrderExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产工单号[%s]无效"), reqBean.getWorkOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        String offLineTime = TimeTool.getNowTime(true);

        Boolean isDataCollectCenter = reqBean.getDataMap().containsKey(MesPcnExtConstWords.CENTER_IS_DATA_COLLECT) ? (Boolean) reqBean.getDataMap().get(MesPcnExtConstWords.CENTER_IS_DATA_COLLECT) : false;
        Boolean isRfidCellFlag = reqBean.getDataMap().containsKey(MesPcnExtConstWords.IS_RFID_CELL) ? (Boolean) reqBean.getDataMap().get(MesPcnExtConstWords.IS_RFID_CELL) : false;
        String snColonRfidNo = isRfidCellFlag ? (String) snLineOffModuleService.doHandleSnColonRfidNo(reqBean, 1, null) : null;

        //获取采集数据
        List<EpProductDataReviewModel> productDataReviewModelList = null;
        if (isDataCollectCenter) {
            Integer reviewResult;
            try {
                productDataReviewModelList = doHandleProductData(reqBean, resultBean, snColonRfidNo, workOrderExt);
                if (CollectionUtils.isEmpty(productDataReviewModelList)) {
                    err = "采集数据复核没有结果,请检查配置";
                    reviewResult = MesPcnExtEnumUtil.EP_PD_REVIEW_RESULT.ERROR.getValue();
                    reqBean.getDataMap().put(MesPcnExtConstWords.INFO, err);
                    reqBean.getDataMap().put(MesPcnExtConstWords.ERROR, true);
                } else {
                    Optional<EpProductDataReviewModel> existOp = productDataReviewModelList.stream()
                            .filter(o -> (null != o && CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() == o.getReviewFlag())).findFirst();
                    if (null != existOp && existOp.isPresent()) {
                        err = "采集数据复核结果:不合格";
                        reviewResult = MesPcnExtEnumUtil.EP_PD_REVIEW_RESULT.NG.getValue();
                        reqBean.getDataMap().put(MesPcnExtConstWords.INFO, err);
                        reqBean.getDataMap().put(MesPcnExtConstWords.ERROR, true);
                    } else {
                        err = "采集数据复核结果:合格";
                        reviewResult = MesPcnExtEnumUtil.EP_PD_REVIEW_RESULT.OK.getValue();
                        reqBean.getDataMap().put(MesPcnExtConstWords.INFO, err);
                    }
                }
            } catch (Exception e) {
                log.info("数据采集生产线[{}]工位[{}]:采集数据复核出现异常:{}", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), e.getMessage());
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "采集数据复核出现异常:%s"), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);;
                reqBean.getDataMap().put(MesPcnExtConstWords.INFO, "采集数据复核出现异常,请检查配置");
                reviewResult = MesPcnExtEnumUtil.EP_PD_REVIEW_RESULT.ERROR.getValue();
                reqBean.getDataMap().put(MesPcnExtConstWords.ERROR, true);
            }

            insertProductDataReviewRecord(reqBean, workOrderExt, snColonRfidNo, reviewResult, err, productDataReviewModelList);
        } else {
            reqBean.getDataMap().remove(MesPcnExtConstWords.ERROR);
            reqBean.getDataMap().remove(MesPcnExtConstWords.INFO);
        }

        List<MesMissingPart> missingPartList = missingPartService.getMissingPartByWorkOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());

        refresh(reqBean, resultBean, productDataReviewModelList, missingPartList);

        if (!CollectionUtils.isEmpty(missingPartList)) {
            List<String> itemPartNameList = missingPartList.stream().filter(o -> null != o).map(MesMissingPart::getItemPartName).collect(Collectors.toList());
            log.info("生产线[{}]工位[{}]产品条码[{}]存在缺件BOM:{},异常下线", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getSerialNumber(), itemPartNameList.toString());
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]存在缺件BOM,异常下线"), reqBean.getSerialNumber());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);

            List<MesBom> bomList = bomExtService.getBomListByPartNo(reqBean.getOrganizeCode(), workOrderExt.getPartNo());
            Map<String, List<MesBom>> itemMap = CollectionUtils.isEmpty(bomList) ? null :
                    bomList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesBom::getItemPartNo));

            MesErrorLineOff errorLineOff = new MesErrorLineOff();
            errorLineOff.setSerialNumber(reqBean.getSerialNumber());
            errorLineOff.setWorkOrderNo(reqBean.getWorkOrderNo());
            errorLineOff.setPartNo(workOrderExt.getPartNo());
            errorLineOff.setPartName(workOrderExt.getPartNameRdd());
            errorLineOff.setWorkCenterCode(reqBean.getWorkCenterCode());
            errorLineOff.setWorkCellCode(reqBean.getWorkCellCode());
            errorLineOff.setOutWcTime(offLineTime);
            errorLineOff.setFreStatus(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
            errorLineOff.setConfirmStatus(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
            errorLineOff.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            errorLineOff.setOrganizeCode(reqBean.getOrganizeCode());
            ConvertBean.serviceModelInitialize(errorLineOff, reqBean.getUserInfo());
            errorLineOff = errorLineOffRepository.insert(errorLineOff);

            for (MesMissingPart missingPart : missingPartList) {
                if (null == missingPart) {
                    continue;
                }
                MesErrorLineOffDetail errorLineOffDetail = new MesErrorLineOffDetail();
                errorLineOffDetail.setPid(errorLineOff.getId());
                errorLineOffDetail.setSerialNumber(reqBean.getSerialNumber());
                errorLineOffDetail.setWorkOrderNo(reqBean.getWorkOrderNo());
                errorLineOffDetail.setItemPartNo(missingPart.getItemPartNo());
                errorLineOffDetail.setItemPartName(missingPart.getItemPartName());
                errorLineOffDetail.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
                errorLineOffDetail.setQty((!CollectionUtils.isEmpty(itemMap) && itemMap.containsKey(missingPart.getItemPartNo())) ?
                        itemMap.get(missingPart.getItemPartNo()).get(0).getQty() : new Double(0));
                errorLineOffDetail.setOrganizeCode(reqBean.getOrganizeCode());
                ConvertBean.serviceModelInitialize(errorLineOffDetail, reqBean.getUserInfo());
                errorLineOffDetailRepository.insert(errorLineOffDetail);
            }

            workOrderExt.setMissingQty(MathOperation.add(workOrderExt.getMissingQty(), new Double(1)));
            produceSnExt.setSnStatus(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.ERR_OFFLINE.getValue());
        } else {
            produceSnExt.setSnStatus(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue());
        }

        workOrderExt.setCompleteQty(MathOperation.add(workOrderExt.getCompleteQty(), new Double(1)));
        ConvertBean.serviceModelUpdate(workOrderExt, reqBean.getUserInfo());
        workOrderExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(workOrderExt).convertBean(workOrderExt);
        workOrderExtRepository.save(workOrderExt);

        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()) {
            produceSnExt.setOutWorkCenterTime(offLineTime);
            produceSnExt.setShiftCode(centerModel.getShiftCode());
            produceSnExt.setWorkTime(centerModel.getWorkTime());
            produceSnExt.setLotNo(centerModel.getWorkTime());

            MesPackageExt packageExt = packageExtService.getPackageByPackNo(reqBean.getOrganizeCode(), reqBean.getPackageNo());
            if (null != packageExt && (StringUtils.isEmpty(packageExt.getLotNo()) || !packageExt.getLotNo().equals(produceSnExt.getLotNo()))) {
                packageExt.setLotNo(produceSnExt.getLotNo());
                packageExt.setWorkTime(produceSnExt.getWorkTime());
                packageExt.setShiftCode(produceSnExt.getShiftCode());
                ConvertBean.serviceModelUpdate(packageExt, reqBean.getUserInfo());
                packageExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
                new ConvertBeanExt(packageExt).convertBean(packageExt);
                packageExtRepository.save(packageExt);
            }
        } else {
            MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySnAndOrderNo(reqBean.getOrganizeCode(), produceSnExt.getSerialNumber(), reqBean.getWorkOrderNo());
            if (null == produceSnRepair) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]未关联返工工单[%s]"), produceSnExt.getSerialNumber(), reqBean.getWorkOrderNo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
            produceSnRepair.setOutWorkCenterTime(offLineTime);
            produceSnRepair.setShiftCode(centerModel.getShiftCode());
            produceSnRepair.setWorkTime(centerModel.getWorkTime());
            produceSnRepair.setLotNo(centerModel.getWorkTime());
            produceSnRepair.setWorkCellCode(reqBean.getWorkCellCode());
            ConvertBean.serviceModelUpdate(produceSnRepair, reqBean.getUserInfo());
            produceSnRepair.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            produceSnRepairRepository.save(produceSnRepair);
        }
        produceSnExtService.packSnExtProcess(produceSnExt, reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());
        ConvertBean.serviceModelUpdate(produceSnExt, reqBean.getUserInfo());
        produceSnExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnExt).convertBean(produceSnExt);
        produceSnExtRepository.save(produceSnExt);

        try {
            WorkOrderInfoModel workOrderInfoModel = new WorkOrderInfoModel();
            workOrderInfoModel.setWorkOrderNo(reqBean.getWorkOrderNo());
            workOrderInfoModel.setWorkCenterCode(reqBean.getWorkCenterCode());
            workOrderInfoModel.setPartNo(reqBean.getPartNo());
            workOrderInfoModel.setCompleteQty(workOrderExt.getCompleteQty());
            workOrderInfoModel.setQty(workOrderExt.getQty());
            workOrderInfoModel.setOrganizeCode(reqBean.getOrganizeCode());
            workOrderInfoModel.setCreateUser(reqBean.getUserInfo());
            consistencyAffirmRecordService.insertConsistencyAffirmRecord(workOrderInfoModel);
        } catch (Exception e) {
            log.error("产品条码[{}]产生一致性确认记录执行失败", reqBean.getSerialNumber());
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产生一致性确认记录执行失败");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
        }

        EpProduceSnExtModel snModel = new EpProduceSnExtModel();
        BeanUtils.copyProperties(produceSnExt, snModel, MesPcnExtConstWords.ID);
        snModel.setWorkCenterCode(reqBean.getWorkCenterCode());
        snModel.setWorkCellCode(reqBean.getWorkCellCode());
        reqBean.getDataMap().put(MesPcnExtConstWords.PRODUCE_SN, snModel);

        WorkOrderExtModel orderModel = new WorkOrderExtModel();
        BeanUtils.copyProperties(workOrderExt, orderModel, MesPcnExtConstWords.ID);
        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_ORDER, orderModel);

        return StepResult.getSuccessComplete();
    }

    private void refresh(StationRequestBean reqBean, StationResultBean resultBean, List<EpProductDataReviewModel> productDataReviewModelList, List<MesMissingPart> missingPartList) {
        if (!CollectionUtils.isEmpty(productDataReviewModelList) || !CollectionUtils.isEmpty(missingPartList)) {
            resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
            if (!CollectionUtils.isEmpty(productDataReviewModelList) && !CollectionUtils.isEmpty(missingPartList)) {
                List<StationResultBean> stationResultBeans = new ArrayList<>();

                StationResultBean stationResultBean1 = new StationResultBean();
                List<AttrBean> attrBeanList1 = this.getAttrBeanListObjectCodeCommon(reqBean, productDataReviewModelList);
                stationResultBean1.setDataAttrList(attrBeanList1);
                stationResultBean1.setResultList(productDataReviewModelList);
                stationResultBean1.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.TABLE.getValue());
                stationResultBeans.add(stationResultBean1);

                StationResultBean stationResultBean2 = new StationResultBean();
                List<AttrBean> attrBeanList2 = this.getAttrBeanListMissingPartCommon(reqBean);
                stationResultBean2.setDataAttrList(attrBeanList2);
                stationResultBean2.setResultList(missingPartList);
                stationResultBean2.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.TABLE.getValue());
                stationResultBeans.add(stationResultBean2);

                resultBean.setStationResultBeans(stationResultBeans);
            } else {
                if (!CollectionUtils.isEmpty(productDataReviewModelList)) {
                    List<AttrBean> attrBeanList = this.getAttrBeanListObjectCodeCommon(reqBean, productDataReviewModelList);
                    resultBean.setDataAttrList(attrBeanList);
                    resultBean.setResultList(productDataReviewModelList);
                } else {
                    List<AttrBean> attrBeanList = this.getAttrBeanListMissingPartCommon(reqBean);
                    resultBean.setDataAttrList(attrBeanList);
                    resultBean.setResultList(missingPartList);
                }
            }

            if (!CollectionUtils.isEmpty(resultBean.getStationResultBeans())) {
                resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.TABLES.getValue());
                resultBean.setWcpcMap(!CollectionUtils.isEmpty(reqBean.getWcpcMap()) ? reqBean.getWcpcMap() : fsmCommonService.getCachedWorkCellParam(reqBean));
            } else {
                resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.TABLE.getValue());
            }

            this.sendMessage(reqBean, resultBean);
        }
    }

    private List<AttrBean> getAttrBeanListObjectCodeCommon(StationRequestBean reqBean, List<EpProductDataReviewModel> productDataReviewModelList) {
        List<EpProductDataReviewModel> showAllDataList = productDataReviewModelList.stream().
                filter(o -> (null != o && (StringUtils.isEmpty(o.getShowFlag()) || MesPcnEnumUtil.PART_CHECK_DATA_TYPE.ALL_DATA.getValue() == o.getShowFlag()))).collect(Collectors.toList());
        List<AttrBean> attrBeanList = new ArrayList<>();
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.OBJECT_CODE, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "对象代码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.CHECK_TIMES, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "实际检测次数"));
        if (CollectionUtils.isEmpty(showAllDataList)) {
            PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.LAST_DATA_RESULT, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "末检是否合格"));
        }
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.NEED_NUM, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "合格标准描述"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.REAL_NUM, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "实际有效数量"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.REVIEW_RESULT, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "是否合格"));
        return attrBeanList;
    }

    private List<AttrBean> getAttrBeanListMissingPartCommon(StationRequestBean reqBean) {
        List<AttrBean> attrBeanList = new ArrayList<>();
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.ITEM_PART_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "原材料物料编码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.ITEM_PART_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "原材料物料名称"));
        return attrBeanList;
    }

    private List<EpProductDataReviewModel> doHandleProductData(StationRequestBean reqBean, StationResultBean resultBean, String snColonRfidNo, MesWorkOrderExt workOrderExt) {
        String err;
        List<EpProductDataReviewModel> dataReviewList = null;
        Boolean isByPart = reqBean.getDataMap().containsKey(MesPcnExtConstWords.CENTER_PD_CHECK_IS_BY_PART) ?
                (Boolean) reqBean.getDataMap().get(MesPcnExtConstWords.CENTER_PD_CHECK_IS_BY_PART) : false;
        //获取物料校验项
        List<MesPartCheck> partCheckList = getPartCheckList(reqBean, isByPart, workOrderExt.getPartNo());

        if (CollectionUtils.isEmpty(partCheckList)) {
            log.info("数据采集生产线[{}]工位[{}]:生产线[{}]未配置物料校验项", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getWorkCenterCode());
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线[%s]未配置物料校验项"), reqBean.getWorkCenterCode());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return dataReviewList;
        }

        List<MesProductData> productDataDbList;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        if (!StringUtils.isEmpty(snColonRfidNo)) {
            DdlPreparedPack.getStringEqualPack(snColonRfidNo.split(MesPcnExtConstWords.COLON)[1], MesPcnExtConstWords.GROUP_NO, packBean);
            productDataDbList = productDataRepository.findByHqlWhere(packBean);
        } else {
//            DdlPreparedPack.getStringEqualPack(
//                    MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()
//                            ? reqBean.getSerialNumber() : new StringJoiner(MesPcnExtConstWords.AND).add(reqBean.getSerialNumber()).add(workOrderExt.getOrderNo()).toString(),
//                    MesPcnExtConstWords.GROUP_NO, packBean);
            DdlPreparedPack.getStringEqualPack(reqBean.getSerialNumber(), MesPcnExtConstWords.SERIAL_NUMBER, packBean);
            productDataDbList = productDataRepository.findByHqlWhere(packBean);
            productDataDbList = CollectionUtils.isEmpty(productDataDbList) ? null :
                    productDataDbList.stream().filter(o -> (null != o && (StringUtils.isEmpty(o.getOrderNo()) || o.getOrderNo().equals(workOrderExt.getOrderNo())))).collect(Collectors.toList());

        }

        List<EpProductDataModel> productDataList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productDataDbList)) {
            for (MesProductData productData : productDataDbList) {
                if (null != productData && !StringUtils.isEmpty(productData.getLineData())) {
                    List<ProductDataModel> productDataModelList = JSONArray.parseArray(productData.getLineData(), ProductDataModel.class);
                    if (CollectionUtils.isEmpty(productDataModelList)) {
                        continue;
                    }
                    EpProductDataModel productDataModel = new EpProductDataModel();
                    productDataModel.setObjectCode(productData.getObjectCode());
                    productDataModel.setProductDataModelList(productDataModelList);
                    productDataModel.setRowNo(productData.getRowNo());
                    productDataList.add(productDataModel);

                    if (StringUtils.isEmpty(productData.getOrderNo())) {
                        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == workOrderExt.getWorkOrderType()) {
                            //标记返工工单匹配值
                            productData.setGroupNo(new StringJoiner(MesPcnExtConstWords.AND).add(productData.getGroupNo()).add(workOrderExt.getOrderNo()).toString());
                        }
                        productData.setWorkCenterCode(workOrderExt.getWorkCenterCode());
                        productData.setWorkCenterName(collectDbProductDataStepService.doGetCenterOrCellName(reqBean.getOrganizeCode(), productData.getWorkCenterCode(), null));
                        productData.setWorkCellName(collectDbProductDataStepService.doGetCenterOrCellName(reqBean.getOrganizeCode(), productData.getWorkCenterCode(), productData.getWorkCellCode()));
                        productData.setOrderNo(workOrderExt.getOrderNo());
                        productData.setPartNo(workOrderExt.getPartNo());
                        productData.setPartDesc(workOrderExt.getPartNameRdd());
                        //非RFID工位的采集数据可直接同步到MES
                        productData.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
                        ConvertBean.serviceModelUpdate(productData, reqBean.getUserInfo());
                        productDataRepository.save(productData);
                    }
                }
            }
        }

        Map<String, List<EpProductDataModel>> objectCodeMap = CollectionUtils.isEmpty(productDataList) ? null :
                productDataList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getObjectCode())))
                        .sorted(Comparator.comparing(EpProductDataModel::getRowNo).reversed()).collect(Collectors.groupingBy(EpProductDataModel::getObjectCode));

        StandardEvaluationContext ctx = new StandardEvaluationContext();

        for (MesPartCheck partCheck : partCheckList) {
            if (null == partCheck || (StringUtils.isEmpty(partCheck.getRecordNumSpelExpress()) && StringUtils.isEmpty(partCheck.getLimitSpelExpress()))) {
                log.info("数据采集生产线[{}]工位[{}]:生产线[{}]配置物料校验项[{}}]错误", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), partCheck.getWorkCenterCode(), partCheck);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线[%s]物料[%s]对象代码[%s]物料校验项配置有误"),
                        partCheck.getWorkCenterCode(), partCheck.getPartNo(), partCheck.getObjectCode());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                continue;
            }
            if (StringUtils.isEmpty(partCheck.getLimitSpelExpress())) {
                if (StringUtils.isEmpty(partCheck.getTypeSpelExpress()) && StringUtils.isEmpty(partCheck.getCheckSpelExpress())) {
                    log.info("数据采集生产线[{}]工位[{}]:生产线[{}}]配置物料校验项[%s]错误", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), partCheck.getWorkCenterCode(), partCheck);
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线[%s]物料[%s]对象代码[%s]物料校验项配置有误"),
                            partCheck.getWorkCenterCode(), partCheck.getPartNo(), partCheck.getObjectCode());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                    continue;
                }
            }
            List<EpProductDataModel> productDataModelList = CollectionUtils.isEmpty(objectCodeMap) ? null : objectCodeMap.get(partCheck.getObjectCode());
            List<EpProductDataModel> curCheckModelList = getProductDataModelList(productDataModelList, partCheck);

            Boolean checkReviewFlag = true;
            Boolean checkLastDataFlag = true;
            EpProductDataReviewModel model = new EpProductDataReviewModel();
            model.setObjectCode(partCheck.getObjectCode());
            model.setNeedNum(partCheck.getRecordNumDesc());
            if (CollectionUtils.isEmpty(curCheckModelList)) {
                model.setRealNum("0");
                checkReviewFlag = false;
                checkLastDataFlag = false;
            } else {
                if (!StringUtils.isEmpty(partCheck.getRecordNumSpelExpress()) && (!StringUtils.isEmpty(partCheck.getCheckSpelExpress()) || !StringUtils.isEmpty(partCheck.getTypeSpelExpress()))) {
                    //封装总数量
                    Map<String, Object> totalMap = new HashMap<>();
                    totalMap.put(MesPcnExtConstWords.TOTAL, 0);
                    //按类型封装数量
                    Map<String, Object> realNumMap = new HashMap<>();

                    //获取类型字段
                    String typeField = getTypeField(partCheck);
                    //封装生产数据获取校验数据
                    checkLastDataFlag = packVariablesMap(reqBean, resultBean, ctx, totalMap, realNumMap, typeField, curCheckModelList, partCheck);

                    //校验记录数
                    checkReviewFlag = checkRecordNum(ctx, model, partCheck, totalMap, realNumMap);
                } else {
                    model.setRealNum(String.valueOf(curCheckModelList.size()));
                }
            }

            Boolean limitReviewFlag = checklimitReview(ctx, partCheck.getLimitSpelExpress(), CollectionUtils.isEmpty(productDataModelList) ? 0 : productDataModelList.size());

            model.setCheckTimes(CollectionUtils.isEmpty(productDataModelList) ? "0" : String.valueOf(productDataModelList.size()));

            String lastDataResult = (StringUtils.isEmpty(partCheck.getDataType()) || MesPcnEnumUtil.PART_CHECK_DATA_TYPE.ALL_DATA.getValue() == partCheck.getDataType()) ? null :
                    (checkLastDataFlag ? CommonEnumUtil.TRUE_OR_FALSE.TRUE.getDescription() : CommonEnumUtil.TRUE_OR_FALSE.FALSE.getDescription());
            model.setLastDataResult(lastDataResult);

            packReviewModel(model, checkReviewFlag && limitReviewFlag && checkLastDataFlag);

            model.setShowFlag(partCheck.getDataType());

            if (CollectionUtils.isEmpty(dataReviewList)) {
                dataReviewList = new ArrayList<>();
            }
            dataReviewList.add(model);
        }
        return dataReviewList;
    }

    private static void packReviewModel(EpProductDataReviewModel model, Boolean boo) {
        model.setReviewFlag(boo ? CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() : CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        model.setReviewResult(boo ? CommonEnumUtil.TRUE_OR_FALSE.TRUE.getDescription() : CommonEnumUtil.TRUE_OR_FALSE.FALSE.getDescription());
        model.setColor(boo ? MesPcnExtConstWords.GREEN : MesPcnExtConstWords.RED);
    }

    private Boolean checklimitReview(StandardEvaluationContext ctx, String limitSpelExpress, int total) {
        if (StringUtils.isEmpty(limitSpelExpress)) {
            return true;
        }
        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put(MesPcnExtConstWords.TOTAL, total);
        ExpressionParser parser = new SpelExpressionParser();
        ctx.setVariables(totalMap);
        return parser.parseExpression(limitSpelExpress).getValue(ctx, Boolean.class);
    }

    private Boolean checkRecordNum(StandardEvaluationContext ctx, EpProductDataReviewModel model, MesPartCheck partCheck, Map<String, Object> totalMap, Map<String, Object> realNumMap) {
        Boolean checkReviewFlag;
        //记录数量表达式
        if (StringUtils.isEmpty(partCheck.getTypeSpelExpress())) {
            ExpressionParser parser3 = new SpelExpressionParser();
            ctx.setVariables(totalMap);
            boolean boo3 = parser3.parseExpression(partCheck.getRecordNumSpelExpress()).getValue(ctx, Boolean.class);

            model.setRealNum(totalMap.get(MesPcnExtConstWords.TOTAL).toString());
            checkReviewFlag = boo3;
        } else {
            ExpressionParser parser4 = new SpelExpressionParser();
            ctx.setVariables(realNumMap);
            boolean boo4 = parser4.parseExpression(partCheck.getRecordNumSpelExpress()).getValue(ctx, Boolean.class);

            if (CollectionUtils.isEmpty(realNumMap)) {
                model.setRealNum("0");
            } else {
                String realNum = MesPcnExtConstWords.EMPTY;
                for (String typeValue : realNumMap.keySet()) {
                    if (!StringUtils.isEmpty(typeValue)) {
                        realNum += typeValue + MesPcnExtConstWords.COLON + realNumMap.get(typeValue) + MesPcnExtConstWords.COMMA;
                    }
                }
                model.setRealNum(realNum.substring(0, realNum.length() - 1));
            }
            checkReviewFlag = boo4;
        }
        return checkReviewFlag;
    }

    private Boolean packVariablesMap(StationRequestBean reqBean, StationResultBean resultBean,
                                     StandardEvaluationContext ctx, Map<String, Object> totalMap, Map<String, Object> realNumMap,
                                     String typeField, List<EpProductDataModel> curCheckModelList, MesPartCheck partCheck) {
        String err;
        Boolean checkLastDataFlag = true;
        //遍历对象代码对应的生产数据集合
        for (EpProductDataModel productDataModel : curCheckModelList) {
            if (null == productDataModel) {
                continue;
            }
            if (productDataModel.getIsNecessary()) {
                checkLastDataFlag = false;
            }
            //一条生产数据
            List<ProductDataModel> productDataModelList = productDataModel.getProductDataModelList();
            if (CollectionUtils.isEmpty(productDataModelList)) {
                continue;
            }
            Map<String, Object> dataMap = new HashMap<>();
            for (ProductDataModel item : productDataModelList) {
                if (null != item) {
                    dataMap.put(item.getFieldCode(), item.getFieldValue());
                    dataMap.put(item.getFieldName(), item.getFieldValue());
                }
            }
            ExpressionParser parser1 = new SpelExpressionParser();
            ctx.setVariables(dataMap);
            //校验表达式
            if (!StringUtils.isEmpty(partCheck.getCheckSpelExpress())) {
                boolean boo1 = parser1.parseExpression(partCheck.getCheckSpelExpress()).getValue(ctx, Boolean.class);
                if (!boo1) {
                    continue;
                }
            }

            //类型表达式
            if (StringUtils.isEmpty(partCheck.getTypeSpelExpress())) {
                totalMap.put(MesPcnExtConstWords.TOTAL, Integer.parseInt(totalMap.get(MesPcnExtConstWords.TOTAL).toString()) + 1);
            } else {
                ExpressionParser parser2 = new SpelExpressionParser();
                boolean boo2 = parser2.parseExpression(partCheck.getTypeSpelExpress()).getValue(ctx, Boolean.class);
                if (!boo2) {
                    continue;
                }

                //获取类型
                String typeValue = !StringUtils.isEmpty(typeField) ? dataMap.get(typeField).toString() : null;
                if (StringUtils.isEmpty(typeValue)) {
                    log.info("数据采集生产线[{}]工位[{}]:生产线[{}]物料[{}]对象代码[{}]类型表达式[{}]有误",
                            reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), partCheck.getWorkCenterCode(), partCheck.getPartNo(), partCheck.getObjectCode(), partCheck.getTypeSpelExpress());
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线[%s]物料[%s]对象代码[%s]类型表达式[%s]有误"),
                            partCheck.getWorkCenterCode(), partCheck.getPartNo(), partCheck.getObjectCode(), partCheck.getTypeSpelExpress());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                    continue;
                }
                if (realNumMap.containsKey(typeValue)) {
                    realNumMap.put(typeValue, Integer.parseInt(realNumMap.get(typeValue).toString()) + 1);
                } else {
                    realNumMap.put(typeValue, 1);
                }
            }
            if (productDataModel.getIsNecessary()) {
                checkLastDataFlag = true;
            }
        }
        return checkLastDataFlag;
    }

    private String getTypeField(MesPartCheck partCheck) {
        String typeField = null;
        if (!StringUtils.isEmpty(partCheck.getTypeSpelExpress())) {
            typeField = partCheck.getTypeSpelExpress().substring(1, partCheck.getTypeSpelExpress().indexOf(MesPcnExtConstWords.ONE_SPACE));
        }
        return typeField;
    }

    private List<EpProductDataModel> getProductDataModelList(List<EpProductDataModel> productDataModelList, MesPartCheck partCheck) {
        if (CollectionUtils.isEmpty(productDataModelList)) {
            return productDataModelList;
        }
        if (StringUtils.isEmpty(partCheck.getDataType()) || MesPcnEnumUtil.PART_CHECK_DATA_TYPE.ALL_DATA.getValue() == partCheck.getDataType()) {
            return productDataModelList;
        }
        productDataModelList.get(0).setIsNecessary(true);
        if (MesPcnEnumUtil.PART_CHECK_DATA_TYPE.ALL_DATA_AND_LAST_DATA.getValue() == partCheck.getDataType()) {
            return productDataModelList;
        }
        List<EpProductDataModel> curCheckModelList = new ArrayList<>();
        curCheckModelList.add(productDataModelList.get(0));
        return curCheckModelList;
    }

    private List<MesPartCheck> getPartCheckList(StationRequestBean reqBean, Boolean isByPart, String partNo) {
        List<MesPartCheck> partCheckList;
        if (!isByPart) {
            partCheckList = getPartCheckListOnlyByCenter(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
        } else {
            partCheckList = partCheckRepository.findByProperty(
                    new String[]{MesPcnConstWords.ORGANIZE_CODE, MesPcnConstWords.IS_VALID, MesPcnConstWords.IS_DELETED, MesPcnConstWords.WORK_CENTER_CODE, MesPcnConstWords.PART_NO},
                    new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), reqBean.getWorkCenterCode(), partNo});
            if (CollectionUtils.isEmpty(partCheckList)) {
                partCheckList = getPartCheckListOnlyByCenter(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
            }
        }
        return CollectionUtils.isEmpty(partCheckList) ? null : partCheckList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getObjectCode()))).collect(Collectors.toList());
    }

    private List<MesPartCheck> getPartCheckListOnlyByCenter(String organizeCode, String workCenterCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringNullPack(MesPcnExtConstWords.PART_NO, packBean);
        return partCheckRepository.findByHqlWhere(packBean);
    }

    private void insertProductDataReviewRecord(StationRequestBean reqBean, MesWorkOrderExt workOrderExt, String snColonRfidNo,
                                               Integer reviewResult, String err, List<EpProductDataReviewModel> productDataReviewModelList) {
        MesProductDataReviewRecord reviewRecord = new MesProductDataReviewRecord();
        reviewRecord.setWorkOrderNo(workOrderExt.getOrderNo());
        reviewRecord.setSerialNumber(reqBean.getSerialNumber());
        //非RFID工位标记匹配值标记返工工单
        reviewRecord.setGroupNo(!StringUtils.isEmpty(snColonRfidNo) ? snColonRfidNo.split(MesPcnExtConstWords.COLON)[1] :
                (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()
                        ? reqBean.getSerialNumber() : new StringJoiner(MesPcnExtConstWords.AND).add(reqBean.getSerialNumber()).add(workOrderExt.getOrderNo()).toString()));
        reviewRecord.setPartNo(workOrderExt.getPartNo());
        reviewRecord.setPartNameRdd(workOrderExt.getPartNameRdd());
        reviewRecord.setWorkCenterCode(reqBean.getWorkCenterCode());
        reviewRecord.setWorkCellCode(reqBean.getWorkCellCode());
        reviewRecord.setReviewTimes(1);
        reviewRecord.setReviewResult(reviewResult);
        reviewRecord.setLastReviewResult(reviewResult);
        if (!CollectionUtils.isEmpty(productDataReviewModelList)) {
            reviewRecord.setReviewData(JSONObject.toJSONString(productDataReviewModelList));
        } else {
            reviewRecord.setReviewData(err);
        }
        reviewRecord.setLastReviewData(reviewRecord.getReviewData());
        reviewRecord.setOrganizeCode(reqBean.getOrganizeCode());
        reviewRecord.setSystemSyncStatusExt(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(reviewRecord, reqBean.getUserInfo());
        productDataReviewRecordRepository.insert(reviewRecord);

    }

}
