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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.*;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.*;
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 cn.estsh.i3plus.pojo.mes.repository.MesProductionAssemblyRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
/**
 * @Description : 裁片工单保存加工结果工步【非排序】
 * @Author : wangjie
 **/
@Slf4j
@Service("mesWorkOrderCutSaveStepService")
public class MesWorkOrderCutSaveStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private MesProductionAssemblyRepository productionAssemblyRepository;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        //保存开模记录工步
        ((IStepService) SpringContextsUtil.getBean("mesMouldRecordGenerateStepService")).executeInState(reqBean);

        //保存裁片工单条码工步
        ((IStepService) SpringContextsUtil.getBean("mesWorkOrderCutProductSnSaveStepService")).executeInState(reqBean);

        //生成裁片工单包装条码工步
        ((IStepService) SpringContextsUtil.getBean("mesWorkOrderCutPackageSnSaveStepService")).executeInState(reqBean);

        //生成加工记录工步
        ((IStepService) SpringContextsUtil.getBean("mesProductionRecordGenerateStepService")).executeInState(reqBean);

        //修改裁片工单、工单信息工步
        ((IStepService) SpringContextsUtil.getBean("mesWorkOrderCutWorkOrderSaveStepService")).executeInState(reqBean);

        return stepSuccessCompleteAndSendMsgReturn(reqBean, new StationResultBean().writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "保存加工结果成功!");

    }
    private void saveProductionAssembly(StationRequestBean reqBean, MesProductionProcessContext productionProcessContext,
                                        MesCellEquipContext cellEquipContext, MesProdRuleContext mesProdRuleContext,
                                        MesProductionPsOutContext productionPsOutContext) {

        MesProductionAssembly productionAssembly = new MesProductionAssembly();

        productionAssembly.setAssemblySn("");
        productionAssembly.setDataSource(MesExtEnumUtil.PRODUCTION_ASSEMBLY_DATA_SOURCE.NOSORT.getValue());
        productionAssembly.setIsOrigSn(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        productionAssembly.setIsSkip(CommonEnumUtil.FALSE);

        productionAssembly.setAreaCode(reqBean.getAreaCode());
        productionAssembly.setWorkCenterCode(reqBean.getWorkCenterCode());
        productionAssembly.setWorkCellCode(reqBean.getWorkCellCode());
        productionAssembly.setProcessCode(reqBean.getProcessCode());

        productionAssembly.setProcessName(productionProcessContext.getProcessName());
        productionAssembly.setCraftCode(productionProcessContext.getCraftCode());
        productionAssembly.setCraftName(productionProcessContext.getCraftName());

        productionAssembly.setEquipmentCode(cellEquipContext.getEquipmentCode());

        productionAssembly.setProductionRecordId(mesProdRuleContext.getProductionRecordId());
        productionAssembly.setMouldNo(mesProdRuleContext.getMouldNo());

        if (null != productionPsOutContext) {
            productionAssembly.setPartNo(productionPsOutContext.getPartNo());
            productionAssembly.setPartName(productionPsOutContext.getPartName());
            productionAssembly.setSerialNumber(productionPsOutContext.getSerialNumber());
            productionAssembly.setProductSn(productionPsOutContext.getProductSn());
            productionAssembly.setCustSn(productionPsOutContext.getCustSn());
        }

        MesScanMonitorContext scanMonitorContext = productionProcessContextStepService.dispatchScanMonitorContext(reqBean, true);
        if (null != scanMonitorContext) productionAssembly.setMouldRecordId(scanMonitorContext.getMouldRecordId());

        productionAssembly.setOrganizeCode(reqBean.getOrganizeCode());
        ConvertBean.serviceModelInitialize(productionAssembly, reqBean.getUserInfo());

        productionAssembly.setFid(UUID.randomUUID().toString());

        productionAssemblyRepository.insert(productionAssembly);
    }

}
