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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionProcessContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
import cn.estsh.i3plus.mes.pcn.util.StringUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
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.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 org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description : 保存加工结果工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesProductionDataSaveStepService")
public class MesProductionDataSaveStepService extends BaseStepService {

    @Autowired
    public IMesProductionProcessContextStepService productionProcessContextStepService;

    //【非排序线】保存结果结果工步集合
    //保存开模记录工步>>保存零件条码信息工步>>加工异常处理工步>>生成加工记录工步>>保存装配记录工步>>保存工单信息工步
    private static final LinkedList<String> noSortStepList = new LinkedList<>(Stream.of(
            "mesMouldRecordGenerateStepService", "mesProductSnSaveStepService", "mesProductResultErrorHandleStepService",
            "mesProductionRecordGenerateStepService", "mesAssemblySaveStepService", "mesWorkOrderSaveStepService").collect(Collectors.toList()));

    //【排序线】保存结果结果工步集合
    //保存开模记录工步>>保存零件条码信息工步>>保存工位队列信息工步>>加工异常处理工步>>生成加工记录工步>>保存工单信息工步
    private static final LinkedList<String> sortStepList = new LinkedList<>(Stream.of(
            "mesMouldRecordGenerateStepService", "mesProductSnSaveStepService", "mesWorkOrderQueueSaveStepService",
            "mesProductResultErrorHandleStepService", "mesProductionRecordGenerateStepService", "mesWorkOrderSaveStepService").collect(Collectors.toList()));

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //根据生产线类型赋值工步集合
        Boolean isSort = MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue() == productionProcessContextStepService.getCenterType(reqBean) ? true : false;
        LinkedList<String> stepList = !isSort ? noSortStepList : sortStepList;

        for (String stepService : stepList) {
            if (StringUtils.isEmpty(stepService)) continue;
            StepResult innerStepResult = ((IStepService) SpringContextsUtil.getBean(stepService)).executeInState(reqBean);
            if (!StringUtils.isEmpty(innerStepResult.getNextTriggerEvent())) stepResult.nextTriggerEvent(innerStepResult.getNextTriggerEvent());
            if (!StringUtils.isEmpty(innerStepResult.getStepAfterState())) stepResult.stepAfterState(innerStepResult.getStepAfterState());
            if (!innerStepResult.isCompleted()) return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, "保存加工结果失败!");
        }

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

    }
}
