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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionCustomContextStepService;
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.IMesShippingQueueService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesEquipVariableCollectContext;
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.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
import cn.estsh.i3plus.pojo.mes.bean.seres.MesCimSeresJisRfid;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagementDetail;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingQueue;
import cn.estsh.i3plus.pojo.mes.model.StationKvBean;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description : 发运队列工位人工扫描发运单号工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesShippingCodeScanStepService")
public class MesShippingCodeScanStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesProductionCustomContextStepService productionCustomContextStepService;

    @Autowired
    private IMesShippingQueueService shippingQueueService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult;

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

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

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

        //扫描信息置空
        String scanInfo = reqBean.resetScanInfo(reqBean.getScanInfo());

        //验证发运组信息是否有效
        List<StationKvBean> resultList = null;

        if (StringUtils.isEmpty(scanInfo)) stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "[人工模式]请扫描发运单号!");

        //发送工步内容
        productionCustomContextStepService.sendStepContextMessage(reqBean, scanInfo, MesExtEnumUtil.CELL_MESSAGE_SOURCE.SCAN);

        //根据发运代码查询创建时间正序创建状态的一条发运队列
        MesShippingQueue shippingQueue = shippingQueueService.getShippingQueueByCode(reqBean.getOrganizeCode(), scanInfo);
        String vinCode = (null == shippingQueue || StringUtils.isEmpty(shippingQueue.getVin())) ? null : shippingQueue.getVin();
        if (StringUtils.isEmpty(vinCode)) {
            MesShippingOrderManagement shipping = shippingQueueService.getShipping(reqBean.getOrganizeCode(), scanInfo);
            MesShippingOrderManagementDetail shippingDetail = null == shipping ? null : shippingQueueService.getShippingDetail(reqBean.getOrganizeCode(), shipping.getId());
            vinCode = null == shippingDetail ? null : shippingDetail.getVin();
        }

        if (StringUtils.isEmpty(vinCode)) {
            this.sendMessage(reqBean, new StationResultBean().writeDbLog().scanInfo(scanInfo), String.format("当前扫描信息发运单号[%s]信息无效!", scanInfo), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "[人工模式]请扫描发运单号!");
        }

        //根据VIN查询赛力斯RFID信息
        MesCimSeresJisRfid cimSeresJisRfid = shippingQueueService.getCimSeresJisRfid(reqBean.getOrganizeCode(), vinCode);
        if (null == cimSeresJisRfid || StringUtils.isEmpty(cimSeresJisRfid.getRfid())) {
            this.sendMessage(reqBean, new StationResultBean().writeDbLog().scanInfo(scanInfo), String.format("当前扫描信息发运单号[%s]信息关联的VIN[%s]未查询到有效的赛力斯RFID信息!", scanInfo, vinCode), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "[人工模式]请扫描发运单号!");
        }

        //获取工步参数
        Optional<Map<String, MesProdRouteOptParam>> stepParamMap = getStepParams(reqBean);
        //解析rfidSn
        String rfidSn = shippingQueueService.getRfidSn(stepParamMap, cimSeresJisRfid.getRfid());
        if (StringUtils.isEmpty(rfidSn)) {
            this.sendMessage(reqBean, new StationResultBean().writeDbLog().scanInfo(scanInfo), String.format("当前扫描信息发运单号[%s]信息关联的VIN[%s]查询到的赛力斯RFID[%s]解析错误!", scanInfo, vinCode, cimSeresJisRfid.getRfid()), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "[人工模式]请扫描发运单号!");
        }

        List<MesEquipVariableCollectContext> equipVariableCollectContextList = new ArrayList<>();
        equipVariableCollectContextList.add(new MesEquipVariableCollectContext(reqBean.getOrganizeCode(), rfidSn, MesExtEnumUtil.CELL_MESSAGE_SOURCE.READ.getValue()));
        //保存上下文扫/读信息:主条码
        productionDispatchContextStepService.dispatchScanProductSnContext(reqBean, equipVariableCollectContextList);

        //主条码验证工步 【非排序】
        stepResult = ((IStepService) SpringContextsUtil.getBean("mesProductSnCheckStepService")).executeInState(reqBean);

        if (!stepResult.isCompleted()) {
            this.sendMessage(reqBean, new StationResultBean().writeDbLog().scanInfo(scanInfo), String.format("当前扫描信息发运单号[%s]信息关联的VIN[%s]查询到的赛力斯RFID[%s]验证错误!", scanInfo, vinCode, cimSeresJisRfid.getRfid()), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "[人工模式]请扫描发运单号!");
        }

        //保存上下文发运队列信息
        if (null != shippingQueue) {
            //保存上下文发运队列信息
            shippingQueue.setPidStr(cimSeresJisRfid.getId().toString());
            shippingQueue.setRfidSn(rfidSn);
            productionDispatchContextStepService.dispatchShippingQueueContext(reqBean, shippingQueue);
        }

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("当前扫描信息发运单号[%s]VIN[%s]已匹配到赛力斯RFID信息[%s]!", scanInfo, vinCode, cimSeresJisRfid.getRfid()));

    }

}
