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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionProcessContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesCellEquipContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesEquipVariableCollectContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionProcessContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesScanMonitorContext;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProdMouldRecord;
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.MesProdMouldRecordRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description : 生成开模记录工步
 * @Author : zxw
 **/
@Slf4j
@Service("mesMouldRecordGenerateStepService")
public class MesMouldRecordGenerateStepService extends BaseStepService {

    @Autowired
    private MesProdMouldRecordRepository prodMouldRecordRepository;

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        //获取上下文信息
        MesProductionProcessContext productionProcessContext = productionProcessContextStepService.getProductionProcessContext(reqBean);

        //配置错误 抛出异常
        if (!productionProcessContext.getSuccess()) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), productionProcessContext.getMessage());

        //获取工位设备信息
        MesCellEquipContext cellEquipContext = productionProcessContext.getCurCellEquip();

        //获取头道模具号
        MesEquipVariableCollectContext firstMouldNoContext = productionDispatchContextStepService.getFirstMouldNoContext(reqBean);

        //获取模具号
        MesEquipVariableCollectContext mouldNoContext = productionDispatchContextStepService.getMouldNoContext(reqBean);

        //获取上下文工位扫描监控信息
        MesScanMonitorContext scanMonitorContext = productionProcessContextStepService.dispatchScanMonitorContext(reqBean, true);

        MesProdMouldRecord prodMouldRecord = new MesProdMouldRecord();
        prodMouldRecord.setId(scanMonitorContext.getMouldRecordId());
        prodMouldRecord.setEquipId(cellEquipContext.getEquipId());
        prodMouldRecord.setEquipmentCode(cellEquipContext.getEquipmentCode());
        if (null != firstMouldNoContext) prodMouldRecord.setMouldNo(firstMouldNoContext.getEquipVariableValue());
        else if (null != mouldNoContext) prodMouldRecord.setMouldNo(mouldNoContext.getEquipVariableValue());
        prodMouldRecord.setOrganizeCode(reqBean.getOrganizeCode());
        ConvertBean.serviceModelInitialize(prodMouldRecord, reqBean.getUserInfo());

        prodMouldRecordRepository.save(prodMouldRecord);

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), String.format("开模记录生成成功! ID:%s", scanMonitorContext.getMouldRecordId()));

    }

}
