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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesWorkOrderCutDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesQueueOrderPush;
import cn.estsh.i3plus.pojo.mes.bean.MesRawPartCharging;
import cn.estsh.i3plus.pojo.mes.bean.MesStation;
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.util.MesExtEnumUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description : 获取生产过程上下文对象接口实现【BUSI】
 * @Author : wangjie
 **/
@Slf4j
@Service("mesProductionDispatchContextStepService")
public class MesProductionDispatchContextStepService extends BaseStepService implements IMesProductionDispatchContextStepService {

    private String getContextKey(StationRequestBean reqBean) {
        return getFsmBusikey(reqBean, new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.BUSI_DATA_CONTEXT).toString());
    }

    private String getContextKey(String organizeCode, String workCenterCode, String workCellCode) {
        return new StringJoiner(MesPcnExtConstWords.COLON).add(organizeCode).add(workCenterCode).add(workCellCode).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.BUSI_DATA_CONTEXT).toString();
    }

    //清除上下文中的所有业务数据
    @Override
    public void flushProductionDispatchContext(StationRequestBean reqBean) {
        //清除上下文中的所有业务数据
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean));
    }

    //存储展示组件MODULE_CONTENT内容
    @Override
    public Boolean dispatchModuleContentContext(StationRequestBean reqBean, List<List<StationKvBean>> dataList) {
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MODULE_CONTENT_CONTEXT, JSONObject.toJSONString(dataList));
    }

    //获取展示组件MODULE_CONTENT内容
    @Override
    public List<List<StationKvBean>> getModuleContentContext(StationRequestBean reqBean) {
        String moduleContent = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MODULE_CONTENT_CONTEXT);
        return StringUtils.isEmpty(moduleContent) ? null : JSONObject.parseObject(moduleContent, new TypeReference<List<List<StationKvBean>>>() {});
    }

    //获取上下文零件数据信息
    @Override
    public Map<String, MesPart> getPartDataContext(StationRequestBean reqBean) {
        String partDataJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PART_DATA_CONTEXT);
        return !StringUtils.isEmpty(partDataJson) ? JSONObject.parseObject(partDataJson, new TypeReference<Map<String, MesPart>>() {}) : null;
    }

    //获取上下文零件数据信息
    @Override
    public Map<String, MesPartContext> getPartDataExtContext(StationRequestBean reqBean) {
        String partDataJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PART_DATA_CONTEXT);
        return !StringUtils.isEmpty(partDataJson) ? JSONObject.parseObject(partDataJson, new TypeReference<Map<String, MesPartContext>>() {}) : null;
    }

    //获取上下文零件数据信息
    @Override
    public Map<String, ? extends MesPart> getPartDataContext(StationRequestBean reqBean, Boolean isExt) {
        String partDataJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PART_DATA_CONTEXT);
        return !StringUtils.isEmpty(partDataJson) ?
                (!isExt ? JSONObject.parseObject(partDataJson, new TypeReference<Map<String, MesPart>>() {}) : JSONObject.parseObject(partDataJson, new TypeReference<Map<String, MesPartContext>>() {})) : null;
    }

    //保存上下文零件数据信息  [JSON]Map<String, MesPart>
    @Override
    public Boolean dispatchPartDataContext(StationRequestBean reqBean, Map<String, ? extends MesPart> partDataMap) {
        if (CollectionUtils.isEmpty(partDataMap)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PART_DATA_CONTEXT, JSONObject.toJSONString(partDataMap));
    }

    //删除上下文零件数据信息
    @Override
    public void removePartDataContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PART_DATA_CONTEXT);
    }

    //获取上下文加工结果
    @Override
    public String getProductResultContext(StationRequestBean reqBean) {
        return getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCT_RESULT_CONTEXT);
    }

    //获取上下文加工结果
    @Override
    public Integer getProductResultQcContext(StationRequestBean reqBean) {
        String productResult = getProductResultContext(reqBean);
        if (StringUtils.isEmpty(productResult)) return MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue();
        try {
            return Integer.valueOf(productResult);
        } catch (NumberFormatException e) {
            return MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue();
        }
    }

    //保存上下文加工结果    加工结果:成功/报废/可疑 【写入的值是变量类别代码对应的条码质量状态】【报废/可疑: 在保存逻辑条码时有特殊业务处理】
    @Override
    public Boolean dispatchProductResultContext(StationRequestBean reqBean, String productResult) {
        if (StringUtils.isEmpty(productResult)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCT_RESULT_CONTEXT, productResult);
    }

    //删除上下文加工结果
    @Override
    public void removeProductResultContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCT_RESULT_CONTEXT);
    }

    //获取上下文就绪信号
    @Override
    public String getReadySignalContext(StationRequestBean reqBean) {
        return getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.READY_SIGNAL_CONTEXT);
    }

    //保存上下文就绪信号   写1
    @Override
    public Boolean dispatchReadySignalContext(StationRequestBean reqBean) {
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.READY_SIGNAL_CONTEXT, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValueStr());
    }

    //删除上下文就绪信号
    @Override
    public void removeReadySignalContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.READY_SIGNAL_CONTEXT);
    }

    //获取上下文发送允许加工指令执行结果
    @Override
    public String getSendProcessCmdContext(StationRequestBean reqBean) {
        return getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SEND_PROCESS_CMD_CONTEXT);
    }

    //保存上下文发送允许加工指令执行结果    写接口逻辑的参考值
    @Override
    public Boolean dispatchSendProcessCmdContext(StationRequestBean reqBean, String value) {
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SEND_PROCESS_CMD_CONTEXT, value);
    }

    //删除上下文发送允许加工指令执行结果
    @Override
    public void removeSendProcessCmdContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SEND_PROCESS_CMD_CONTEXT);
    }

    //获取上下文头道模具号
    @Override
    public MesEquipVariableCollectContext getFirstMouldNoContext(StationRequestBean reqBean) {
        String firstMouldNo = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.FIRST_MOULD_NO_CONTEXT);
        return !StringUtils.isEmpty(firstMouldNo) ? JSONObject.parseObject(firstMouldNo, MesEquipVariableCollectContext.class) : null;
    }

    //验证上下文头道模具号是否存在
    @Override
    public Boolean checkFirstMouldNoIsExistContext(StationRequestBean reqBean) {
        String firstMouldNo = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.FIRST_MOULD_NO_CONTEXT);
        return !StringUtils.isEmpty(firstMouldNo) ? true : false;
    }

    //保存上下文头道模具号  [JSON]MesEquipVariableCollectContext::equipVariableValue
    @Override
    public Boolean dispatchFirstMouldNoContext(StationRequestBean reqBean, MesEquipVariableCollectContext firstMouldNo) {
        if (null == firstMouldNo) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.FIRST_MOULD_NO_CONTEXT, JSONObject.toJSONString(firstMouldNo));
    }

    //删除上下文头道模具号
    @Override
    public void removeFirstMouldNoContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.FIRST_MOULD_NO_CONTEXT);
    }

    //获取上下文产出零件信息
    @Override
    public List<MesProductionPartContext> getProductionPartContext(StationRequestBean reqBean) {
        String productionPartJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PART_CONTEXT);
        return !StringUtils.isEmpty(productionPartJson) ? JSONObject.parseArray(productionPartJson, MesProductionPartContext.class) : null;
    }

    //验证上下文产出零件信息是否存在
    @Override
    public Boolean checkProductionPartIsExistContext(StationRequestBean reqBean) {
        String productionPartJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PART_CONTEXT);
        return !StringUtils.isEmpty(productionPartJson) ? true : false;
    }

    //保存上下文产出零件信息  [JSON]List<MesProductionPartContext>
    @Override
    public Boolean dispatchProductionPartContext(StationRequestBean reqBean, List<MesProductionPartContext> productionPartContextList) {
        if (CollectionUtils.isEmpty(productionPartContextList)) return removeProductionPartContext(reqBean);
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PART_CONTEXT, JSONObject.toJSONString(productionPartContextList));
    }

    //删除上下文产出零件信息
    @Override
    public Boolean removeProductionPartContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PART_CONTEXT);
        return true;
    }

    //获取上下文模具号
    @Override
    public MesEquipVariableCollectContext getMouldNoContext(StationRequestBean reqBean) {
        String mouldNo = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MOULD_NO_CONTEXT);
        return !StringUtils.isEmpty(mouldNo) ? JSONObject.parseObject(mouldNo, MesEquipVariableCollectContext.class) : null;
    }

    //验证上下文模具号是否存在
    @Override
    public Boolean checkMouldNoIsExistContext(StationRequestBean reqBean) {
        String mouldNo = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MOULD_NO_CONTEXT);
        return !StringUtils.isEmpty(mouldNo) ? true : false;
    }

    //保存上下文模具号  [JSON]MesEquipVariableCollectContext::equipVariableValue
    @Override
    public Boolean dispatchMouldNoContext(StationRequestBean reqBean, MesEquipVariableCollectContext mouldNo) {
        if (null == mouldNo) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MOULD_NO_CONTEXT, JSONObject.toJSONString(mouldNo));
    }

    //删除上下文模具号
    @Override
    public void removeMouldNoContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MOULD_NO_CONTEXT);
    }

    //获取上下文扫/读信息:加工单
    @Override
    public List<MesEquipVariableCollectContext> getScanWorkOrderNoContext(StationRequestBean reqBean) {
        String productSnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_WORK_ORDER_NO_CONTEXT);
        return !StringUtils.isEmpty(productSnJson) ? JSONObject.parseArray(productSnJson, MesEquipVariableCollectContext.class) : null;
    }

    //验证上下文扫/读信息:加工单是否存在
    @Override
    public Boolean checkScanWorkOrderNoIsExistContext(StationRequestBean reqBean) {
        String productSnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_WORK_ORDER_NO_CONTEXT);
        return !StringUtils.isEmpty(productSnJson) ? true : false;
    }

    //保存上下文扫/读信息:加工单  [JSON]List<MesEquipVariableCollectContext>
    @Override
    public Boolean dispatchScanWorkOrderNoContext(StationRequestBean reqBean, List<MesEquipVariableCollectContext> workOrderNoList) {
        if (CollectionUtils.isEmpty(workOrderNoList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_WORK_ORDER_NO_CONTEXT, JSONObject.toJSONString(workOrderNoList));
    }

    //删除上下文扫/读信息:加工单
    @Override
    public void removeScanWorkOrderNoContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_WORK_ORDER_NO_CONTEXT);
    }

    //获取上下文扫/读信息:主条码
    @Override
    public List<MesEquipVariableCollectContext> getScanProductSnContext(StationRequestBean reqBean) {
        String productSnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_PRODUCT_SN_CONTEXT);
        return !StringUtils.isEmpty(productSnJson) ? JSONObject.parseArray(productSnJson, MesEquipVariableCollectContext.class) : null;
    }

    //验证上下文扫/读信息:主条码是否存在
    @Override
    public Boolean checkScanProductSnIsExistContext(StationRequestBean reqBean) {
        String productSnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_PRODUCT_SN_CONTEXT);
        return !StringUtils.isEmpty(productSnJson) ? true : false;
    }

    //保存上下文扫/读信息:主条码  [JSON]List<MesEquipVariableCollectContext>
    @Override
    public Boolean dispatchScanProductSnContext(StationRequestBean reqBean, List<MesEquipVariableCollectContext> productSnList) {
        if (CollectionUtils.isEmpty(productSnList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_PRODUCT_SN_CONTEXT, JSONObject.toJSONString(productSnList));
    }

    //删除上下文扫/读信息:主条码
    @Override
    public void removeScanProductSnContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_PRODUCT_SN_CONTEXT);
    }

    //获取上下文扫/读信息:装配件条码
    @Override
    public List<MesEquipVariableCollectContext> getScanAssemblySnContext(StationRequestBean reqBean) {
        String assemblySnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_ASSEMBLY_SN_CONTEXT);
        return !StringUtils.isEmpty(assemblySnJson) ? JSONObject.parseArray(assemblySnJson, MesEquipVariableCollectContext.class) : null;
    }

    //验证上下文扫/读信息:装配件条码是否存在
    @Override
    public Boolean checkScanAssemblySnIsExistContext(StationRequestBean reqBean) {
        String assemblySnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_ASSEMBLY_SN_CONTEXT);
        return !StringUtils.isEmpty(assemblySnJson) ? true : false;
    }

    //保存上下文扫/读信息:装配件条码  [JSON]List<MesEquipVariableCollectContext>
    @Override
    public Boolean dispatchScanAssemblySnContext(StationRequestBean reqBean, List<MesEquipVariableCollectContext> assemblySnList) {
        if (CollectionUtils.isEmpty(assemblySnList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_ASSEMBLY_SN_CONTEXT, JSONObject.toJSONString(assemblySnList));
    }

    //删除上下文扫/读信息:装配件条码
    @Override
    public void removeScanAssemblySnContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_ASSEMBLY_SN_CONTEXT);
    }

    //获取进料主条码数据信息
    @Override
    public List<MesProductionPsInContext> getProductionPsInContext(StationRequestBean reqBean) {
        String productionPsInJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_IN_CONTEXT);
        return !StringUtils.isEmpty(productionPsInJson) ? JSONObject.parseArray(productionPsInJson, MesProductionPsInContext.class) : null;
    }

    //验证上下文进料主条码数据集合是否存在
    @Override
    public Boolean checkProductionPsInIsExistContext(StationRequestBean reqBean) {
        String productionPsInJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_IN_CONTEXT);
        return !StringUtils.isEmpty(productionPsInJson) ? true : false;
    }

    //保存上下文进料主条码数据信息集合  [JSON]List<MesProductionPsInContext>
    @Override
    public Boolean dispatchProductionPsInContext(StationRequestBean reqBean, List<MesProductionPsInContext> productionPsInContextList) {
        if (CollectionUtils.isEmpty(productionPsInContextList)) return removeProductionPsInContext(reqBean);
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_IN_CONTEXT, JSONObject.toJSONString(productionPsInContextList));
    }

    //删除上下文进料主条码数据信息集合
    @Override
    public Boolean removeProductionPsInContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_IN_CONTEXT);
        return true;
    }

    //获取上下文产出条码数据信息集合
    @Override
    public List<MesProductionPsOutContext> getProductionPsOutContext(StationRequestBean reqBean) {
        String productionPsOutJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_OUT_CONTEXT);
        return !StringUtils.isEmpty(productionPsOutJson) ? JSONObject.parseArray(productionPsOutJson, MesProductionPsOutContext.class) : null;
    }

    //验证上下文产出条码数据信息集合是否存在
    @Override
    public Boolean checkProductionPsOutIsExistContext(StationRequestBean reqBean) {
        String productionPsOutJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_OUT_CONTEXT);
        return !StringUtils.isEmpty(productionPsOutJson) ? true : false;
    }

    //保存上下文产出条码数据信息集合  [JSON]List<MesProductionPsOutContext>
    @Override
    public Boolean dispatchProductionPsOutContext(StationRequestBean reqBean, List<MesProductionPsOutContext> productionPsOutContextList) {
        if (CollectionUtils.isEmpty(productionPsOutContextList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_OUT_CONTEXT, JSONObject.toJSONString(productionPsOutContextList));
    }

    //删除上下文产出条码数据信息集合
    @Override
    public void removeProductionPsOutContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_PS_OUT_CONTEXT);
    }

    //获取上下文产品加工规则信息集合
    @Override
    public List<MesProdRuleContext> getProdRuleDataContext(StationRequestBean reqBean) {
        String prodRuleNosortJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_RULE_DATA_CONTEXT);
        return !StringUtils.isEmpty(prodRuleNosortJson) ? JSONObject.parseArray(prodRuleNosortJson, MesProdRuleContext.class) : null;
    }

    //获取上下文产品加工规则信息集合
    @Override
    public List<MesProdRuleContext> getProdRuleDataContext(String organizeCode, String workCenterCode, String workCellCode) {
        String prodRuleNosortJson = getFsmBusiData(organizeCode, getContextKey(organizeCode, workCenterCode, workCellCode), MesPcnExtConstWords.PROD_RULE_DATA_CONTEXT);
        return !StringUtils.isEmpty(prodRuleNosortJson) ? JSONObject.parseArray(prodRuleNosortJson, MesProdRuleContext.class) : null;
    }

    //验证上下文产品加工规则信息集合是否存在
    @Override
    public Boolean checkProdRuleDataIsExistContext(StationRequestBean reqBean) {
        String prodRuleNosortJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_RULE_DATA_CONTEXT);
        return !StringUtils.isEmpty(prodRuleNosortJson) ? true : false;
    }

    //保存上下文产品加工规则信息集合  [JSON]List<MesProdRuleContext>
    @Override
    public Boolean dispatchProdRuleDataContext(StationRequestBean reqBean, List<MesProdRuleContext> prodRuleContextList) {
        if (CollectionUtils.isEmpty(prodRuleContextList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_RULE_DATA_CONTEXT, JSONObject.toJSONString(prodRuleContextList));
    }

    //删除上下文产品加工规则信息集合
    @Override
    public void removeProdRuleDataContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_RULE_DATA_CONTEXT);
    }

    //获取料桶原材料数据集合
    @Override
    public List<MesRawPartCharging> getMesRawPartChargingDataContext(StationRequestBean reqBean) {
        String mesRawPartChargingDataJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.RAW_PART_CHARGING_DATA_CONTEXT);
        return !StringUtils.isEmpty(mesRawPartChargingDataJson) ? JSONObject.parseArray(mesRawPartChargingDataJson, MesRawPartCharging.class) : null;
    }

    //保存料桶原材料数据集合
    @Override
    public Boolean dispatchMesRawPartChargingDataContext(StationRequestBean reqBean, List<MesRawPartCharging> mesRawPartChargingList) {
        if (CollectionUtils.isEmpty(mesRawPartChargingList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.RAW_PART_CHARGING_DATA_CONTEXT, JSONObject.toJSONString(mesRawPartChargingList));
    }

    //删除料桶原材料数据集合
    @Override
    public void removeMesRawPartChargingDataContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.RAW_PART_CHARGING_DATA_CONTEXT);
    }

    @Override
    public List<MesWorkOrderCutDetailModel> getMesWorkOrderCutFgDataContext(StationRequestBean reqBean) {
        String mesWorkOrderCutFgDataJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.WORK_ORDER_CUT_FG_DATA_CONTEXT);
        return !StringUtils.isEmpty(mesWorkOrderCutFgDataJson) ? JSONObject.parseArray(mesWorkOrderCutFgDataJson, MesWorkOrderCutDetailModel.class) : null;
    }

    @Override
    public Boolean dispatchMesWorkOrderCutFgDataContext(StationRequestBean reqBean, List<MesWorkOrderCutDetailModel> workOrderCutDetailModelList) {
        if (CollectionUtils.isEmpty(workOrderCutDetailModelList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.WORK_ORDER_CUT_FG_DATA_CONTEXT, JSONObject.toJSONString(workOrderCutDetailModelList));
    }

    @Override
    public void removeMesWorkOrderCutFgDataContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.WORK_ORDER_CUT_FG_DATA_CONTEXT);
    }

    @Override
    public Boolean dispatchOverEndlessLoopReadTimes(StationRequestBean reqBean, String maxTimes) {
        String item = new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.ENDLESS_LOOP_READ_TIMES).add(reqBean.getCurFsmState().getNextStatus()).toString();
        String endlessLoopReadTimes = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), item);
        if (StringUtils.isEmpty(endlessLoopReadTimes)) endlessLoopReadTimes = MesPcnExtConstWords.ZERO_STR;
        if (Integer.valueOf(endlessLoopReadTimes).compareTo(Integer.valueOf(maxTimes)) >= 0) {
            removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), item);
            return true;
        }
        dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), item, String.valueOf(Integer.valueOf(endlessLoopReadTimes) + 1));
        return false;
    }

    //获取上下文生产扫/读信息:可回用包装条码
    @Override
    public List<MesEquipVariableCollectContext> getScanRecyclablePackageContext(StationRequestBean reqBean) {
        String productSnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_RECYCLABLE_PACKAGE_CONTEXT);
        return !StringUtils.isEmpty(productSnJson) ? JSONObject.parseArray(productSnJson, MesEquipVariableCollectContext.class) : null;
    }

    //验证上下文扫/读信息:可回用包装条码是否存在
    @Override
    public Boolean checkScanRecyclablePackageIsExistContext(StationRequestBean reqBean) {
        String productSnJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_RECYCLABLE_PACKAGE_CONTEXT);
        return !StringUtils.isEmpty(productSnJson);
    }

    //保存上下文扫/读信息:可回用包装条码  [JSON]List<MesEquipVariableCollectContext>
    @Override
    public Boolean dispatchScanRecyclablePackageContext(StationRequestBean reqBean, List<MesEquipVariableCollectContext> productSnList) {
        if (CollectionUtils.isEmpty(productSnList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_RECYCLABLE_PACKAGE_CONTEXT, JSONObject.toJSONString(productSnList));
    }

    //删除上下文扫/读信息:可回用包装条码
    @Override
    public void removeScanRecyclablePackageContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SCAN_RECYCLABLE_PACKAGE_CONTEXT);
    }

    //获取进料可回用包装信息
    @Override
    public List<MesProductionRecyclablePackageContext> getProductionRecyclablePackageContext(StationRequestBean reqBean) {
        String productionPsInJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_RECYCLABLE_PACKAGE_CONTEXT);
        return !StringUtils.isEmpty(productionPsInJson) ? JSONObject.parseArray(productionPsInJson, MesProductionRecyclablePackageContext.class) : null;
    }

    //验证进料可回用包装是否存在
    @Override
    public Boolean checkProductionRecyclablePackageContextIsExist(StationRequestBean reqBean) {
        String productionPsInJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_RECYCLABLE_PACKAGE_CONTEXT);
        return !StringUtils.isEmpty(productionPsInJson);
    }

    //保存进料可回用包装
    @Override
    public Boolean dispatchProductionRecyclablePackageContext(StationRequestBean reqBean, List<MesProductionRecyclablePackageContext> recyclablePackageContextList) {
        if (CollectionUtils.isEmpty(recyclablePackageContextList))  return removeProductionRecyclablePackageContext(reqBean);
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_RECYCLABLE_PACKAGE_CONTEXT, JSONObject.toJSONString(recyclablePackageContextList));

    }

    //删除进料可回用包装
    @Override
    public Boolean removeProductionRecyclablePackageContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PRODUCTION_RECYCLABLE_PACKAGE_CONTEXT);
        return true;
    }

    //获取上下文临时数据信息集合
    @Override
    public List<MesProdTempDataContext> getProdTempDataContext(StationRequestBean reqBean) {
        String prodTempDataJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_TEMP_DATA_CONTEXT);
        return !StringUtils.isEmpty(prodTempDataJson) ? JSONObject.parseArray(prodTempDataJson, MesProdTempDataContext.class) : null;
    }

    //获取上下文临时数据信息集合
    @Override
    public List<MesProdTempDataContext> getProdTempDataContext(String organizeCode, String workCenterCode, String workCellCode) {
        String prodTempDataJson = getFsmBusiData(organizeCode, getContextKey(organizeCode, workCenterCode, workCellCode), MesPcnExtConstWords.PROD_TEMP_DATA_CONTEXT);
        return !StringUtils.isEmpty(prodTempDataJson) ? JSONObject.parseArray(prodTempDataJson, MesProdTempDataContext.class) : null;
    }

    //保存上下文临时数据信息集合
    @Override
    public Boolean dispatchProdTempDataContext(StationRequestBean reqBean, List<MesProdTempDataContext> prodTempDataContextList) {
        if (CollectionUtils.isEmpty(prodTempDataContextList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_TEMP_DATA_CONTEXT, JSONObject.toJSONString(prodTempDataContextList));
    }

    //保存上下文临时数据信息集合
    @Override
    public Boolean dispatchProdTempDataContext(String organizeCode, String workCenterCode, String workCellCode, List<MesProdTempDataContext> prodTempDataContextList) {
        if (CollectionUtils.isEmpty(prodTempDataContextList)) return false;
        return dispatchFsmBusiData(organizeCode, getContextKey(organizeCode, workCenterCode, workCellCode), MesPcnExtConstWords.PROD_TEMP_DATA_CONTEXT, JSONObject.toJSONString(prodTempDataContextList));
    }

    //删除上下文临时数据信息集合
    @Override
    public void removeProdTempDataContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.PROD_TEMP_DATA_CONTEXT);
    }

    //删除上下文临时数据信息集合
    @Override
    public void removeProdTempDataContext(String organizeCode, String workCenterCode, String workCellCode) {
        removeFsmBusiData(organizeCode, getContextKey(organizeCode, workCenterCode, workCellCode), MesPcnExtConstWords.PROD_TEMP_DATA_CONTEXT);
    }

    //获取上下文发运队列信息
    @Override
    public MesShippingQueue getShippingQueueContext(StationRequestBean reqBean) {
        String shippingQueueJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SHIPPING_QUEUE_CONTEXT);
        return !StringUtils.isEmpty(shippingQueueJson) ? JSONObject.parseObject(shippingQueueJson, MesShippingQueue.class) : null;
    }

    //保存上下文发运队列信息
    @Override
    public Boolean dispatchShippingQueueContext(StationRequestBean reqBean, MesShippingQueue shippingQueue) {
        if (null == shippingQueue) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.SHIPPING_QUEUE_CONTEXT, JSONObject.toJSONString(shippingQueue));
    }

    //获取上下文推单队列信息
    @Override
    public List<MesQueueOrderPush> getSortQueuePushContext(StationRequestBean reqBean) {
        String sortQueuePushJson = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.QUEUE_PUSH_CONTEXT);
        return !StringUtils.isEmpty(sortQueuePushJson) ? JSONObject.parseArray(sortQueuePushJson, MesQueueOrderPush.class) : null;
    }

    //保存上下文推单队列信息
    @Override
    public Boolean dispatchSortQueuePushContext(StationRequestBean reqBean, List<MesQueueOrderPush> queueOrderPushList) {
        if (CollectionUtils.isEmpty(queueOrderPushList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.QUEUE_PUSH_CONTEXT, JSONObject.toJSONString(queueOrderPushList));
    }

    //获取站点用于缺料时进行上料绑定
    @Override
    public List<MesStation> getMatchStationContext(StationRequestBean reqBean) {
        String stationContext = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MATCH_STATION_CONTEXT);
        return !StringUtils.isEmpty(stationContext) ? JSONObject.parseArray(stationContext, MesStation.class) : null;
    }

    //保存站点用于缺料时进行上料绑定
    @Override
    public void dispatchMatchStationContext(StationRequestBean reqBean, List<MesStation> stationList) {
        if (CollectionUtils.isEmpty(stationList)) return;
        dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MATCH_STATION_CONTEXT, JSONObject.toJSONString(stationList));
    }

    //获取站点用于扣料业务中进行LOCK
    @Override
    public Map<String, MesStation> getLockStationContext(StationRequestBean reqBean) {
        String stationContext = getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.LOCK_STATION_CONTEXT);
        return !StringUtils.isEmpty(stationContext) ? JSONObject.parseObject(stationContext, new TypeReference<Map<String, MesStation>>() {}) : null;
    }

    //保存站点用于后面扣料业务中进行LOCK
    @Override
    public void dispatchLockStationContext(StationRequestBean reqBean, Map<String, MesStation> stationMap2Lock) {
        if (CollectionUtils.isEmpty(stationMap2Lock)) return;
        dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.LOCK_STATION_CONTEXT, JSONObject.toJSONString(stationMap2Lock));
    }

    //获取站点用于缺料时进行上料绑定的容器条码
    @Override
    public String getMatchStationFeedContainerSnContext(StationRequestBean reqBean) {
        return getFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MATCH_STATION_FEED_CONTAINER_SN_CONTEXT);
    }

    //保存站点用于缺料时进行上料绑定的容器条码
    @Override
    public void dispatchMatchStationFeedContainerSnContext(StationRequestBean reqBean, String sn) {
        if (StringUtils.isEmpty(sn)) return;
        dispatchFsmBusiData(reqBean.getOrganizeCode(), getContextKey(reqBean), MesPcnExtConstWords.MATCH_STATION_FEED_CONTAINER_SN_CONTEXT, sn);
    }

}
