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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IConsistencyAffirmRecordService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
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.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.api.busi.ISxOutPutStatisticsTravelService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.bu3.IMesOuterPackageService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkCenterExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderInfoModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.InnerPackageRecordModel;
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.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
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.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
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.List;
import java.util.stream.Collectors;

/**
 * 工步业务----大包装小包装条码下线工步
 * @USER: steven.peng
 * @DATE: 2023-01-11 10:20
 * */
@Slf4j
@Service("sxOuterPackageOfflineStepService")
public class SxOuterPackageOfflineStepService extends BaseStepService {


    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IWorkCenterExtService workCenterExtService;


    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private IPartExtService partExtService;

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    @Autowired
    private MesOutPutStatisticsOqcRepository outPutStatisticsOqcRepository;

    @Autowired
    private ISxOutPutStatisticsTravelService outPutStatisticsTravelService;

    @Autowired
    private IMesOuterPackageService iMesOuterPackageService;

    @Autowired
    private SxProductOffLineRepository productOffLineRepository;

    @Autowired
    private MesPackageExtRepository packageExtRepository;

    @Autowired
    private MesProduceSnRepairRepository produceSnRepairRepository;

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Autowired
    private IConsistencyAffirmRecordService consistencyAffirmRecordService;

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

        String err;
        StationResultBean resultBean = new StationResultBean();

        // 获取当前工单，并判断当前工单类型
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null == workOrderExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]无效,不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MathOperation.compareTo(workOrderExt.getCompleteQty(), workOrderExt.getQty()) >= 0) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]数量已完成,不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]状态为[%s],不能操作"),
                    reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        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();
        }
        WorkCenterExtModel centerModel = new WorkCenterExtModel();
        BeanUtils.copyProperties(workCenterExt, centerModel, MesPcnExtConstWords.ID);

        String outerPackageNo = (String)reqBean.getDataMap().get("outerPackageNo");
        List<InnerPackageRecordModel> innerPackageRecordModels = (List<InnerPackageRecordModel>)reqBean.getDataMap().get("packageNoList");
        // 将完工数量（大包装主表里面的qty）叠加到产量统计表,产量出货统计表
        MesOuterPackage mesOuterPackage = iMesOuterPackageService.getOutPackageBySn(reqBean.getOrganizeCode(),outerPackageNo);
        if(null == mesOuterPackage){
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码[%s]不存在"), outerPackageNo);
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        // 小包装条码、大包装条码报工（写进报工表）【判断是否试制工单，若是不报工】->修改工单的完成数量mes_work_order，
        // 将完工数量（大包装主表里面的qty）叠加到产量统计表（不用加字段，写工单号）

        if(StringUtils.isEmpty(outerPackageNo) || (null == innerPackageRecordModels) || (innerPackageRecordModels.size() == 0)){
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), ""), reqBean.getPartNo());
            sendMessage(reqBean, resultBean,err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        //修改工单完成的改造数量
        List<InnerPackageRecordModel> remakeSnList = innerPackageRecordModels.stream().filter(o -> (null != o && MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REMAKE_SN.getValue() == o.getSnType())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(remakeSnList))
            workOrderExt.setFiCompleteQty(StringUtils.isEmpty(workOrderExt.getFiCompleteQty()) ? new Double(remakeSnList.size()) : MathOperation.add(workOrderExt.getFiCompleteQty(), new Double(remakeSnList.size())));

        // 修改工单的完成数量mes_work_order
        workOrderExt.setCompleteQty(MathOperation.add(workOrderExt.getCompleteQty(), new Double(innerPackageRecordModels.size())));
        ConvertBean.serviceModelUpdate(workOrderExt, reqBean.getUserInfo());
        workOrderExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(workOrderExt).convertBean(workOrderExt);
        workOrderExtRepository.save(workOrderExt);
        for(InnerPackageRecordModel im : innerPackageRecordModels) {
            String offLineTime = TimeTool.getNowTime(true);
            // 通过小包装查询得到包装详细信息和条码信息
            MesPackageDetail packageDetail = packageExtService.getPackageDetailByPackNo(reqBean.getOrganizeCode(), im.getPackageNo());
            if (null == packageDetail) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]查询本体条码失败"), reqBean.getScanInfo());
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonCompleteStop(err);
            }

            //  // 校验小包装条码对应的本体条码是否归于此工单 查询出本体条码的信息
            MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), packageDetail.getSerialNumber());
            if (produceSnExt == null) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]无效"), reqBean.getSerialNumber());
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonCompleteStop(err);
            }

            // 如果是正常工单
            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(), im.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);
            }
        }

        MesPartExt partExt = partExtService.getPartExt(reqBean.getOrganizeCode(), reqBean.getPartNo());
        if (null == partExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料编码[%s]无效"), reqBean.getPartNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        ProduceSnExtModel snModel = (ProduceSnExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.PRODUCE_SN);
        if (null == snModel ) {
            snModel = getProduceSnExtModel(reqBean);
        }
       // MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());

        MesOutPutStatistics outPutStatistics = outPutStatisticsRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE},
                new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        reqBean.getWorkOrderNo(), workCenterExt.getWorkTime(), workCenterExt.getShiftCode()});
        if (null == outPutStatistics) {
            outPutStatistics = new MesOutPutStatistics();
            outPutStatistics.setWorkCenterCode(reqBean.getWorkCenterCode());
            outPutStatistics.setWorkOrderNo(reqBean.getWorkOrderNo());
            outPutStatistics.setPartNo(reqBean.getPartNo());
            outPutStatistics.setPartNameRdd(reqBean.getPartName());
            outPutStatistics.setShiftCode(workCenterExt.getShiftCode());
            outPutStatistics.setWorkTime(workCenterExt.getWorkTime());
            outPutStatistics.setOrderStartTime(workOrderExt.getStartTime());
            outPutStatistics.setQty(mesOuterPackage.getQty());
            outPutStatistics.setPlanQty(workOrderExt.getQty());
            outPutStatistics.setPlanRemakeQty(workOrderExt.getPlanQty());
            if (MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REMAKE_SN.getValue() == snModel.getSnType()) {
                outPutStatistics.setRemakeQty(mesOuterPackage.getQty());
            } else {
                outPutStatistics.setRemakeQty(0.0);
            }
            outPutStatistics.setWorkOrderType(workOrderExt.getWorkOrderType());
            outPutStatistics.setOrganizeCode(reqBean.getOrganizeCode());
            outPutStatistics.setCategoryCode(workCenterExt.getCategoryCode());
            outPutStatistics.setCareerCode(workCenterExt.getCareerCode());
            outPutStatistics.setOutPutFlag(workOrderExt.getOutPutFlag());
            outPutStatistics.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            ConvertBean.serviceModelInitialize(outPutStatistics, reqBean.getUserInfo());
            outPutStatistics.setStartTime(outPutStatistics.getModifyDatetime());
            outPutStatistics.setEndTime(outPutStatistics.getStartTime());

            MesOutPutStatisticsOqc outPutStatisticsOqc = new MesOutPutStatisticsOqc();
            BeanUtils.copyProperties(outPutStatistics, outPutStatisticsOqc);
            outPutStatisticsOqc.setCheckLimit(0.0);
            outPutStatisticsOqc.setSpotCheckQty(0.0);
            outPutStatisticsOqc.setCheckResult(MesPcnExtEnumUtil.OQC_TOTAL_STATUS.UNTREATED.getValue());
            outPutStatisticsOqc.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            outPutStatisticsOqc.setPid(outPutStatisticsRepository.insert(outPutStatistics).getId());
            outPutStatisticsOqcRepository.insert(outPutStatisticsOqc);
        } else {
            outPutStatistics.setQty(MathOperation.add(outPutStatistics.getQty(), mesOuterPackage.getQty()));
            if (MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REMAKE_SN.getValue() == snModel.getSnType()) {
                outPutStatistics.setRemakeQty(MathOperation.add(outPutStatistics.getRemakeQty(), mesOuterPackage.getQty()));
            }
            ConvertBean.serviceModelUpdate(outPutStatistics, reqBean.getUserInfo());
            outPutStatistics.setEndTime(outPutStatistics.getModifyDatetime());
            outPutStatistics.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            outPutStatisticsRepository.save(outPutStatistics);
        }
        outPutStatisticsTravelService.insertOutPutStatisticsTravel(outPutStatistics);

        // 小包装条码、大包装条码报工（写进报工表）
        // return dealOffline(outerPackageNo, innerPackageRecordModels,reqBean,workCenterExt,workOrderExt, snModel);
        return dealOffline(outerPackageNo, innerPackageRecordModels,reqBean,workCenterExt,workOrderExt);

    }

    private ProduceSnExtModel getProduceSnExtModel(StationRequestBean reqBean) {
        String sn = "";
        if(StringUtils.isEmpty(reqBean.getSerialNumber())){
            sn = reqBean.getScanInfo();
        }else{
            sn = reqBean.getSerialNumber();
        }
        ProduceSnExtModel snModel = null;
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), sn);
        if (null != produceSnExt) {
            snModel = new ProduceSnExtModel();
            BeanUtils.copyProperties(produceSnExt, snModel, MesPcnExtConstWords.ID);
        }
        return snModel;
    }


    public StepResult dealOffline(String outerPackageNo, List<InnerPackageRecordModel> outerPackageRecordModels, StationRequestBean reqBean, MesWorkCenterExt workCenterExt, MesWorkOrderExt workOrderExt ){
        MesPartExt partExt = partExtService.getPartExt(reqBean.getOrganizeCode(), reqBean.getPartNo());
        StationResultBean resultBean = new StationResultBean();
        String err;
       for(InnerPackageRecordModel o :outerPackageRecordModels) {
           // 通过小包装查询得到包装详细信息和条码信息
           MesPackageDetail packageDetail = packageExtService.getPackageDetailByPackNo(reqBean.getOrganizeCode(), o.getPackageNo());
           // 校验小包装条码对应的本体条码是否归于此工单 查询出本体条码的信息
           if (null == packageDetail) {
               String err2 = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "通过小包装条码[%s]查询本体条码失败"),
                       o.getPackageNo(), reqBean.getScanInfo());
               sendMessage(reqBean, resultBean, err2, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
               return StepResult.getNonComplete(err2);
           }
           MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), packageDetail.getSerialNumber());
           if (null == produceSnExt) {
               String err2 = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "通过条码[%s]查询条码表查询不到信息"),
                       packageDetail.getSerialNumber(), reqBean.getScanInfo());
               sendMessage(reqBean, resultBean, err2, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
               return StepResult.getNonComplete(err2);
           }

           SxProductOffLine productOffLine = new SxProductOffLine();
           productOffLine.setWorkOrderNo(reqBean.getWorkOrderNo());
           productOffLine.setPackageNo(o.getPackageNo());
           productOffLine.setSerialNumber(o.getSerialNumber());
           productOffLine.setPartNo(reqBean.getPartNo());
           productOffLine.setPartName(reqBean.getPartName());
           productOffLine.setQty(1.0);
           productOffLine.setOuterPackageNo(outerPackageNo);
           productOffLine.setIsWaterPurifier(MesPcnExtEnumUtil.IS_WATER_PURIFIER.YES.getValue());
           productOffLine.setLotNo(workCenterExt.getWorkTime());
           productOffLine.setShiftCode(workCenterExt.getShiftCode());
           productOffLine.setWorkCenterCode(reqBean.getWorkCenterCode());
           productOffLine.setErpWarehouse(workOrderExt.getErpWarehouse());

           if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()) {
               productOffLine.setSnType(produceSnExt.getSnType());
           } else {
               productOffLine.setSnType(MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REWORK_SN.getValue());
           }

           if (null != partExt) {
               productOffLine.setUnit(partExt.getUnit());
               productOffLine.setProdType(getProdType(partExt.getCategoryCode1()));
           }
           productOffLine.setSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
           productOffLine.setOrganizeCode(reqBean.getOrganizeCode());
           ConvertBean.serviceModelInitialize(productOffLine, reqBean.getUserInfo());
           productOffLineRepository.insert(productOffLine);
        }


/*        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]关联包装条码[%s]已报工"),
                reqBean.getSerialNumber(), reqBean.getPackageNo());*/
        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码[%s]对应的小包装条码[%s]已报工"),
                outerPackageNo,reqBean.getScanInfo());
        sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        // 若达到工单所需做的数量，则关闭工单
        if (MathOperation.compareTo(workOrderExt.getCompleteQty(), workOrderExt.getQty()) == 0) {
            this.sendMessage(reqBean, resultBean, "", MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getSuccessComplete();
        } else {
            return StepResult.getNonComplete();
        }
    }

    private Integer getProdType(String categoryCode1) {
        if (StringUtils.isEmpty(categoryCode1)) {
            return null;
        } else {
            try {
                return Integer.valueOf(categoryCode1);
            } catch (NumberFormatException e) {
                return null;
            }
        }
    }

}
