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.IMesShippingQueueService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesEquipVariableCollectContext;
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.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.MesShippingQueue;
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 : 发运队列获取赛力斯RFID工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesShippingRfidSeresFindStepService")
public class MesShippingRfidSeresFindStepService extends BaseStepService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesShippingQueueService shippingQueueService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //获取工步参数
        Optional<Map<String, MesProdRouteOptParam>> stepParamMap = getStepParams(reqBean);

        //获取上下文发运队列信息
        MesShippingQueue shippingQueue = productionDispatchContextStepService.getShippingQueueContext(reqBean);
        if ((null == shippingQueue || StringUtils.isEmpty(shippingQueue.getVin()))) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog().checkRepeat(), stepResult,
                    false, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT, "当前未获取到缓存中有效的发运队列,即将重新获取!",
                    stepParamMap, MesPcnExtConstWords.READ_FAILURE_SLEEP, MesPcnExtConstWords.READ_FAILURE_SLEEP_DEFAULT_TIME);
        }

        //根据VIN查询赛力斯RFID信息
        MesCimSeresJisRfid cimSeresJisRfid = shippingQueueService.getCimSeresJisRfid(reqBean.getOrganizeCode(), shippingQueue.getVin());
        if (null == cimSeresJisRfid || StringUtils.isEmpty(cimSeresJisRfid.getRfid())) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog().checkRepeat(), stepResult,
                    false, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT,
                    String.format("当前零件发运组[%s]匹配的发运队列单号[%s]VIN[%s]未查询到有效的赛力斯RFID信息,请人工处理! 持续监听中...", shippingQueue.getShippingGroupCode(), shippingQueue.getShippingCode(), shippingQueue.getVin()),
                    stepParamMap, MesPcnExtConstWords.READ_FAILURE_SLEEP, MesPcnExtConstWords.READ_FAILURE_SLEEP_DEFAULT_TIME);
        }

        //解析rfidSn
        String rfidSn = shippingQueueService.getRfidSn(stepParamMap, cimSeresJisRfid.getRfid());
        if (StringUtils.isEmpty(rfidSn)) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog().checkRepeat(), stepResult,
                    false, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT,
                    String.format("当前零件发运组[%s]匹配的发运队列单号[%s]VIN[%s]查询到的赛力斯RFID[%s]解析错误,请人工处理! 持续监听中...", shippingQueue.getShippingGroupCode(), shippingQueue.getShippingCode(), shippingQueue.getVin(), cimSeresJisRfid.getRfid()),
                    stepParamMap, MesPcnExtConstWords.READ_FAILURE_SLEEP, MesPcnExtConstWords.READ_FAILURE_SLEEP_DEFAULT_TIME);
        }

        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()) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog().checkRepeat(), stepResult,
                    false, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT,
                    String.format("当前零件发运组[%s]匹配的发运队列单号[%s]VIN[%s]查询到的赛力斯RFID[%s]验证错误,请人工处理! 持续监听中...", shippingQueue.getShippingGroupCode(), shippingQueue.getShippingCode(), shippingQueue.getVin(), cimSeresJisRfid.getRfid()),
                    stepParamMap, MesPcnExtConstWords.READ_FAILURE_SLEEP, MesPcnExtConstWords.READ_FAILURE_SLEEP_DEFAULT_TIME);
        }

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

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult,
                String.format("零件发运组[%s]发运队列单号[%s]VIN[%s]已匹配到赛力斯RFID信息[%s]!", shippingQueue.getShippingGroupCode(), shippingQueue.getShippingCode(), shippingQueue.getVin(), cimSeresJisRfid.getRfid()));

    }

//    public static void main(String[] args) {
//        String rfid = "227S187103LM8F7J3B4SC028108002000466426#LRHA62726CC55####20##2##31122#66241H1H2########4###66###################################";
//        System.out.println(rfid.length());
//        System.out.println(rfid);
//        System.out.println(rfid.substring(0, 96) + "##################326312JSG1DJ05" + rfid.substring(128));
//        System.out.println(new StringBuilder().append(rfid, 0, 96).append("##################326312JSG1DJ05").append(rfid, 96+32, rfid.length()).toString());
//    }

}
