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

import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
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.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:EP条码关联工单-模式验证工步
 * @USER: xinwang.yi
 * @DATE: 2021-12-27 16:35
 */
@Slf4j
@Service("epSnBindOrderChooseProdTypeStepService")
public class EpSnBindOrderChooseProdTypeStepService extends BaseStepService {

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

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("epSnBindOrderChooseProdTypeStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String message;

        //当前模式
        String curProdType = (String) doHandleCurProdType(reqBean, 1, null);
        Integer curProTypeInt = null;
        //选择变更生成模式
        String chooseProdType = (String) redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_PROD_TYPE);

        if(StringUtils.isEmpty(curProdType) && StringUtils.isEmpty(chooseProdType)){
            message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"请选择生产模式"));
            this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            foundExThrowNoShowMsg();
        }

        if(!StringUtils.isEmpty(curProdType)){
            curProTypeInt = Integer.valueOf(curProdType);
        }
        
        if(!StringUtils.isEmpty(chooseProdType)){
            Integer chooseProdTypeIntValue = null;
            try {
                chooseProdTypeIntValue = Integer.valueOf(chooseProdType);
                if(StringUtils.isEmpty(MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.valueOfDescription(chooseProdTypeIntValue))){
                    doHandleCMDProdType(reqBean, 3, null);
                    log.info("当前选择生产模式:[%s]", chooseProdType);
                    message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"入参[模式]值无效,请重新选择生产模式"));
                    this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    foundExThrowNoShowMsg();
                }
            } catch (NumberFormatException e) {
                doHandleCMDProdType(reqBean, 3, null);
                log.info("当前选择生产模式:[%s]", chooseProdType);
                message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),"入参[模式]值无效,请重新选择生产模式"));
                this.sendMessage(reqBean,resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
            curProTypeInt = chooseProdTypeIntValue;
            curProdType = chooseProdType;
            //删除选择生产模式
            doHandleCMDProdType(reqBean, 3, null);
        }

        doHandleCurProdType(reqBean, 2, curProdType);
        message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "模式[%s]选择成功"), MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.valueOfDescription(curProTypeInt));
        this.sendMessage(reqBean, resultBean, message , MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        doHandleProduceSnModelList(reqBean, 3, null);
        //将枚举值传给下一个触发事件
        StepResult stepResult = StepResult.getSuccessComplete();
        if(curProTypeInt == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.SINGLE_BIND.getValue()){
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.SINGLE_BIND.name());
        }else if(curProTypeInt == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.BATCH_BIND.getValue()){
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.BATCH_BIND.name());
        }else if(curProTypeInt == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.SINGLE_UNBIND.getValue()){
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.SINGLE_UNBIND.name());
        }else{
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.BATCH_UNBIND.name());
        }

        return stepResult;
    }

    public synchronized Object doHandleCurProdType(StationRequestBean reqBean, Integer flag, String curProdType) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_PROD_TYPE);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_PROD_TYPE, curProdType, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_PROD_TYPE);
                return true;
            default:
                return null;
        }
    }

    public synchronized Object doHandleCMDProdType(StationRequestBean reqBean, Integer flag, String curProdType) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_PROD_TYPE);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_PROD_TYPE, curProdType, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CMD_PROD_TYPE);
                return true;
            default:
                return null;
        }
    }

    public synchronized Object doHandleProduceSnModelList(StationRequestBean reqBean, Integer flag, List<EpProduceSnExtModel> produceSnExtModelList) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST, produceSnExtModelList, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
                return true;
            default:
                return null;
        }
    }


}
