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.api.busi.IMesWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.step.method.MesPartDataMapSaveStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesEquipVariableCollectContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPartContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionProcessContext;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
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 java.util.List;

/**
 * @Description : 加工单验证工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesWorkOrderCheckStepService")
public class MesWorkOrderCheckStepService extends BaseStepService {

    @Autowired
    public IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    public IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    public MesPartDataMapSaveStepService partDataMapSaveStepService;

    @Autowired
    public IMesWorkOrderExtService workOrderExtService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        //排序线 加工单验证工步
        if (MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue() == productionProcessContextStepService.getCenterType(reqBean)) return ((IStepService) SpringContextsUtil.getBean("mesWorkOrderCheckSortStepService")).executeInState(reqBean);

        //非排序线 加工单验证工步
        return ((IStepService) SpringContextsUtil.getBean("mesWorkOrderCheckNosortStepService")).executeInState(reqBean);

    }

    public List<MesProductionPartContext> removeBusiData(StationRequestBean reqBean) {
        productionDispatchContextStepService.removeProductionPartContext(reqBean);
        productionDispatchContextStepService.removeProductionPsInContext(reqBean);
        productionDispatchContextStepService.removeProductionPsOutContext(reqBean);
        productionDispatchContextStepService.removeProdRuleDataContext(reqBean);
        return null;
    }

    //验证扫描信息是否属于工艺强过码
    public Boolean matchCraftJumpCode(MesProductionProcessContext productionProcessContext, List<MesEquipVariableCollectContext> equipVariableCollectContextList) {
        if (equipVariableCollectContextList.size() != 1 || MesExtEnumUtil.CELL_MESSAGE_SOURCE.SCAN.getValue() != equipVariableCollectContextList.get(0).getMessageSource()) return false;
        if (!equipVariableCollectContextList.get(0).getEquipVariableValue().equals(productionProcessContext.getCraftJumpCode())) return false;
        return true;
    }

}
