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.IMesQueueOrderPushService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPartContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionProcessContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPsOutContext;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.StringUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesQueueOrderPushCellCfg;
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 lombok.extern.slf4j.Slf4j;
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;

/**
 * @Description : 工位工单推送工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesWorkOrderQueuePushStepService")
public class MesWorkOrderQueuePushStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesQueueOrderPushService queueOrderPushService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

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

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

        //存储生产过程上下文对象
        productionProcessContextStepService.dispatchProductionProcessContext(reqBean, productionProcessContext);

        //处理排序线推单配置上下文, 返回推送工位类型对应的配置信息 【来源工位】
        List<MesQueueOrderPushCellCfg> queueOrderPushCellCfgList = productionProcessContextStepService.dispatchQueueOrderPushCellCfgContext(reqBean, MesExtEnumUtil.QUEUE_ORDER_CELL_PUSH_TYPE.SOURCE.getValue());
        if (CollectionUtils.isEmpty(queueOrderPushCellCfgList)) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()),
                    stepResult, true, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT,
                    String.format("生产线[%s]工位[%s]未配置[%s]类型的生产队列推送工位配置信息,当前跳过工位工单推送!", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), MesExtEnumUtil.QUEUE_ORDER_CELL_PUSH_TYPE.SOURCE.getDescription()));
        }

        //获取上下文产出条码信息
        List<MesProductionPsOutContext> productionPsOutContextList = productionDispatchContextStepService.getProductionPsOutContext(reqBean);
        //获取上下文产出零件信息
        List<MesProductionPartContext> productionPartContextList = productionDispatchContextStepService.getProductionPartContext(reqBean);
        if (CollectionUtils.isEmpty(productionPsOutContextList) || CollectionUtils.isEmpty(productionPartContextList)) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), "当前不存在产出零件条码信息,请重置工序解决!");

        //写入工位工单推送信息
        productionPsOutContextList.stream().forEach(o ->
                queueOrderPushService.insertQueueOrderPush(reqBean.getOrganizeCode(), reqBean.getUserInfo(), queueOrderPushCellCfgList, o,
                        productionPartContextList.stream().filter(i -> (null != o && !StringUtils.isEmpty(i.getForeignKey()) && i.getForeignKey().compareTo(o.getForeignKey()) == 0)).findFirst().get())
        );

        log.info("工厂{}生产线{}工位{}:FSM STATE DISPATCHER --- DO STEP --- {} EXEC --- ORDER:{} --- PUSH_SOURCE_CODE:{}",
                reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), StringUtil.toLowerCaseFirst(this.getClass().getSimpleName()),
                productionPsOutContextList.stream().filter(o -> null != o).map(MesProductionPsOutContext::getWorkOrderNo).collect(Collectors.toList()).toString(),
                queueOrderPushCellCfgList.stream().filter(o -> null != o).map(MesQueueOrderPushCellCfg::getPushSourceCode).collect(Collectors.toList()).toString());

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, "工位工单推送成功!");

    }

}
