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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.jx.SxNoumenonSnPopUpOnlineModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
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.impp.framework.boot.util.ImppRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @Description : 本体包装是否重选工单校验工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2020-05-27
 * @Modify:
 **/
@Slf4j
@Service("sxNoumenonSnCheckReselectOrderStepService")
public class SxNoumenonSnCheckReselectOrderStepService extends BaseStepService {

    @Autowired
    private SxNoumenonSnPopUpOnlineModuleService snPopUpOnlineModuleService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Resource(name = MesPcnExtConstWords.REDIS_RES_PCN)
    private ImppRedis redisMesPcn;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxNoumenonSnCheckReselectOrderStepService[execute] 参数：{}", reqBean);
        StationResultBean resultBean = new StationResultBean();
        String err;

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少条码信息 SerialNumber");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工单信息 WorkOrderNo");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getDataMap().get(MesPcnExtConstWords.WORK_ORDER_NO))) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参[DataMap]缺少工单信息 WorkOrderNo");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (!reqBean.getWorkOrderNo().equals(reqBean.getDataMap().get(MesPcnExtConstWords.WORK_ORDER_NO))) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单已变更为[%s]"), reqBean.getWorkOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            snPopUpOnlineModuleService.putLastSelectOrder(reqBean);

            String bindKeyPartCacheKey = getCacheKey(reqBean, MesPcnExtConstWords.BIND_KEY_PART);
            redisMesPcn.deleteKey(bindKeyPartCacheKey);
            String repairBindKeyPartCacheKey = getCacheKey(reqBean, MesPcnExtConstWords.REPAIR_BIND_KEY_PART);
            redisMesPcn.deleteKey(repairBindKeyPartCacheKey);

            //跳过整个工序
            reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
            reqBean.setInterfaceType(MesPcnExtConstWords.SHIPPING);
            reqBean.setBusiType(MesPcnConstWords.WS_CMD_DO_SCAN);
            reqBean.setForceJumpProcess(true);
            shippingDispatchService.doSendScanQueueNextExec(reqBean);

            foundExThrowNoShowMsg();
        }

        MesProduceSnExt produceSnExtDb = produceSnExtService.getProduceSnExtBySnAndOrderNo(reqBean.getOrganizeCode(), reqBean.getSerialNumber(), reqBean.getWorkOrderNo());
        if(produceSnExtDb == null){
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前条码[%s]已不关联当前工单[%s]"), reqBean.getSerialNumber(), reqBean.getWorkOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

            String bindKeyPartCacheKey = getCacheKey(reqBean, MesPcnExtConstWords.BIND_KEY_PART);
            redisMesPcn.deleteKey(bindKeyPartCacheKey);

            //跳过整个工序
            reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
            reqBean.setInterfaceType(MesPcnExtConstWords.SHIPPING);
            reqBean.setBusiType(MesPcnConstWords.WS_CMD_DO_SCAN);
            reqBean.setForceJumpProcess(true);
            shippingDispatchService.doSendScanQueueNextExec(reqBean);

            foundExThrowNoShowMsg();
        }

        return StepResult.getSuccessComplete();
    }

}
