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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep.EpSnLineOffModuleService;
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.CommonEnumUtil;
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 : 工步业务-EP打包下线绑定关键件之前判断条码解绑校验工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2020-05-27
 * @Modify:
 **/
@Slf4j
@Service("epSnLineOffBeforeBindKeyCheckSnUnBindStepService")
public class EpSnLineOffBeforeBindKeyCheckSnUnBindStepService extends BaseStepService {

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private EpSnLineOffModuleService snLineOffModuleService;

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

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("EpSnLineOffBeforeBindKeyCheckSnUnBindStepService[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();
        }

        String curWorkOrderNo = (String) snLineOffModuleService.doHandleCurWorkOrderNo(reqBean, 1, null);
        if (StringUtils.isEmpty(curWorkOrderNo)) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单已经失效,请重新选择工单");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        Boolean flag = (null != produceSnExt && CommonEnumUtil.IS_VAILD.VAILD.getValue() == produceSnExt.getIsValid() && produceSnExt.getLastWorkOrderNo().equals(curWorkOrderNo));
        if (!flag) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]未绑定工单[%s]"), reqBean.getSerialNumber(), curWorkOrderNo);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_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();
    }

}
