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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionCustomContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.*;
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.util.StationKvBeanUtil;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.StationKvBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicReference;

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

    //发送设备质量信息
    @Override
    public void sendEquipQualityMessage(StationRequestBean reqBean, MesCellEquipContext cellEquipContext) {
        StationResultBean resultBean = new StationResultBean()
                .busiType(MesPcnEnumUtil.STATION_BUSI_TYPE.QUALITY_MODULE.getValue()).dataType(MesPcnEnumUtil.STATION_DATA_TYPE.COLOR.getValue()).resultObj(MesExtEnumUtil.EQUIP_LOG_QUALITY.valueOfColor(cellEquipContext.getQuality()));
        this.sendMessage(reqBean, resultBean);
    }

    //工位加工锁KEY
    private String getProductionLockContextKey(StationRequestBean reqBean) { return getFsmBusikey(reqBean, new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.PRODUCTION_LOCK_CONTEXT).toString()); }

    //验证上下文工位加工锁是否存在
    @Override
    public Boolean checkProductionLockIsExistContext(StationRequestBean reqBean) {
        String value = getFsmBusiData(reqBean.getOrganizeCode(), getProductionLockContextKey(reqBean));
        return !StringUtils.isEmpty(value) ? true : false;
    }

    //保存上下文加工锁
    @Override
    public Boolean dispatchProductionLockContext(StationRequestBean reqBean, String value) {
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getProductionLockContextKey(reqBean), value);
    }

    //删除上下文加工锁
    @Override
    public void removeProductionLockContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getProductionLockContextKey(reqBean));
    }

    //上个扫描/读取信息KEY
    private String getLastScanContextKey(StationRequestBean reqBean) { return getFsmBusikey(reqBean, new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.LAST_SCAN_INFO_CONTEXT).toString()); }

    //存储上个扫描/读取信息
    @Override
    public Boolean dispatchLastScanContext(StationRequestBean reqBean, List<StationKvBean> dataList) {
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getLastScanContextKey(reqBean), JSONObject.toJSONString(dataList));
    }

    //获取上个扫描/读取信息
    @Override
    public List<StationKvBean> getLastScanContext(StationRequestBean reqBean) {
        String lastScanContent = getFsmBusiData(reqBean.getOrganizeCode(), getLastScanContextKey(reqBean));
        return StringUtils.isEmpty(lastScanContent) ? new ArrayList<>() : JSONObject.parseArray(lastScanContent, StationKvBean.class);
    }

    //发送工步内容
    @Override
    public void sendStepContextMessage(StationRequestBean reqBean) {
        sendStepContextMessage(reqBean, getLastScanContext(reqBean));
    }

    //发送工步内容
    @Override
    public void sendStepContextMessage(StationRequestBean reqBean, String scanInfo, MesExtEnumUtil.CELL_MESSAGE_SOURCE source) {

        List<StationKvBean> lastScanContext = getLastScanContext(reqBean);

        lastScanContext = StationKvBeanUtil.addStationKvBeanLimix100ThenHalf(lastScanContext, new StationKvBean().name(source.getDescription()).value(scanInfo));

        dispatchLastScanContext(reqBean, lastScanContext);

        sendStepContextMessage(reqBean, lastScanContext);

    }

    //发送工步内容
    @Override
    public void sendStepContextMessage(StationRequestBean reqBean, List<StationKvBean> lastScanContext) {
        StationResultBean resultBean = new StationResultBean()
                .busiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue()).dataType(MesPcnEnumUtil.STATION_DATA_TYPE.KEY_VALUE.getValue())
                .resultList(StationKvBeanUtil.addStationKvBeanList(new ArrayList<>(),
                        lastScanContext.size() > 1 ? lastScanContext.get(1).name("上次" + lastScanContext.get(1).getName()).key(MesPcnExtConstWords.LAST_SCAN_INFO) : null,
                        lastScanContext.size() > 0 ? lastScanContext.get(0).name("当前" + lastScanContext.get(0).getName()).key(MesPcnExtConstWords.SCAN_INFO) : null));
        this.sendMessage(reqBean, resultBean);
    }

    //开班信息KEY 【PS:生产线级别】
    private String getProdShiftContextKey(String orgainzeCode, String workCenterCode) { return new StringJoiner(MesPcnExtConstWords.COLON).add(orgainzeCode).add(MesPcnExtConstWords.PROD_SHIFT_DATA_CONTEXT).add(workCenterCode).toString(); }

    //获取上下文班次班组信息
    @Override
    public List<StationKvBean> getProdShiftDataContext(String orgainzeCode, String workCenterCode) {
        String prodShiftJson = getFsmBusiData(orgainzeCode, getProdShiftContextKey(orgainzeCode, workCenterCode));
        return !StringUtils.isEmpty(prodShiftJson) ? JSONObject.parseArray(prodShiftJson, StationKvBean.class) : null;
    }

    //验证上下文班次班组信息是否存在
    @Override
    public Boolean checkProdShiftDataIsExistContext(String orgainzeCode, String workCenterCode) {
        String prodShiftJson = getFsmBusiData(orgainzeCode, getProdShiftContextKey(orgainzeCode, workCenterCode));
        return !StringUtils.isEmpty(prodShiftJson) ? true : false;
    }

    //保存上下文班次班组信息  [JSON]List<StationKvBean>
    @Override
    public Boolean dispatchProdShiftDataContext(String organizeCode, String workCenterCode, List<StationKvBean> prodShiftDataList) {
        if (CollectionUtils.isEmpty(prodShiftDataList)) return false;
        return dispatchFsmBusiData(organizeCode, getProdShiftContextKey(organizeCode, workCenterCode), JSONObject.toJSONString(prodShiftDataList));
    }

    //删除上下文班次班组信息
    @Override
    public void removeProdShiftDataContext(String orgainzeCode, String workCenterCode) {
        removeFsmBusiData(orgainzeCode, getProdShiftContextKey(orgainzeCode, workCenterCode));
    }

    //加工次数key
    private String getProductionStatisticsContextKey(StationRequestBean reqBean) { return getFsmBusikey(reqBean, new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.PRODUCTION_STATISTICS_CONTEXT).toString()); }

    //获取上下文工位加工数统计信息
    @Override
    public List<StationKvBean> getProductionStatisticsContext(StationRequestBean reqBean) {
        String productionStatisticsJson = getFsmBusiData(reqBean.getOrganizeCode(), getProductionStatisticsContextKey(reqBean));
        return !StringUtils.isEmpty(productionStatisticsJson) ? JSONObject.parseArray(productionStatisticsJson, StationKvBean.class) : null;
    }

    //封装加工数信息
    @Override
    public List<StationKvBean> getProductionStatisticsContext(MesProdShiftContext mesProdShiftKvBean, String countStr) {
        return StationKvBeanUtil.addStationKvBeanList(new ArrayList<>(),
                new StationKvBean(mesProdShiftKvBean.getShiftGroup(), "加工数", countStr), new StationKvBean(MesPcnExtConstWords.COLOR, "颜色", MesExtEnumUtil.COLOR.BLACK.getValue()));
    }

    //保存上下文工位加工数统计信息  [JSON]List<StationKvBean>
    @Override
    public Boolean dispatchProductionStatisticsContext(StationRequestBean reqBean, List<StationKvBean> productionStatisticsList) {
        if (CollectionUtils.isEmpty(productionStatisticsList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getProductionStatisticsContextKey(reqBean), JSONObject.toJSONString(productionStatisticsList));
    }

    //删除上下文工位加工数统计信息
    @Override
    public void removeProductionStatisticsContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getProductionStatisticsContextKey(reqBean));
    }

    @Override
    public Integer dispatchProductionStatisticsContext(StationRequestBean reqBean, int count, MesProdShiftContext mesProdShiftKvBean) {

        if (null == mesProdShiftKvBean) return null;

        List<StationKvBean> stationKvBeans = getProductionStatisticsContext(reqBean);

        String countStr = String.valueOf(count);
        AtomicReference<Integer> amount = new AtomicReference<>();
        amount.set(count);

        if (CollectionUtils.isEmpty(stationKvBeans)) {
            stationKvBeans = getProductionStatisticsContext(mesProdShiftKvBean, countStr);
        } else {
            AtomicReference<Boolean> flag = new AtomicReference<>(false);
            stationKvBeans.stream().forEach(stationKvBean -> {
                if (stationKvBean.getKey().contains(mesProdShiftKvBean.getShiftGroup())) {
                    stationKvBean.setValue(stationKvBean == null ? countStr : String.valueOf(Integer.valueOf(stationKvBean.getValue()) + count));
                    amount.set(Integer.valueOf(stationKvBean.getValue()));
                    flag.set(true);
                }
            });
            if (!flag.get()) stationKvBeans = getProductionStatisticsContext(mesProdShiftKvBean, countStr);
        }

        dispatchProductionStatisticsContext(reqBean, stationKvBeans);

        return amount.get();
    }

    //可复用条码KEY
    private String getRepeatAssemblySnContextKey(String organizeCode, String workCenterCode, String workCellCode) {
        return new StringJoiner(MesPcnExtConstWords.COLON).add(organizeCode).add(workCenterCode).add(workCellCode).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.REPEAT_ASSEMBLY_SN_CONTEXT).toString();
    }

    //根据装配件信息获取可复用条码
    @Override
    public String getRepeatAssemblySn(String organizeCode, String workCenterCode, String workCellCode, MesProductionAssemblyContext productionAssemblyContext) {
        if (productionAssemblyContext.getAssemblyStatus().compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue()) != 0) return null;
        if (null == productionAssemblyContext || StringUtils.isEmpty(productionAssemblyContext.getIsRepeat()) || productionAssemblyContext.getIsRepeat().compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) != 0) return null;
        return getRepeatAssemblySn(organizeCode, workCenterCode, workCellCode, productionAssemblyContext.repeatKeyToString());
    }

    //根据装配件信息获取可复用条码
    @Override
    public String getRepeatAssemblySn(String organizeCode, String workCenterCode, String workCellCode, String repeatKey) {
        String repeatAssemblySnJson = getFsmBusiData(organizeCode, getRepeatAssemblySnContextKey(organizeCode, workCenterCode, workCellCode), repeatKey);
        return repeatAssemblySnJson;
    }

    //根据设备代码获取可复用条码的个数
    @Override
    public Integer getRepeatAssemblySnCount(String organizeCode, String workCenterCode, String workCellCode, String equipmentCode) {
        String countStr = getFsmBusiData(organizeCode, getRepeatAssemblySnContextKey(organizeCode, workCenterCode, workCellCode), equipmentCode);
        return !StringUtils.isEmpty(countStr) ? Integer.valueOf(countStr) : MesPcnExtConstWords.ZERO;
    }

    //保存可复用条码到上下文
    @Override
    public Boolean dispatchRepeatAssemblySn(String organizeCode, String workCenterCode, String workCellCode, MesProductionAssemblyContext productionAssemblyContext) {
        String repeatKey = productionAssemblyContext.repeatKeyToString();
        if (StringUtils.isEmpty(getRepeatAssemblySn(organizeCode, workCenterCode, workCellCode, repeatKey))) {
            Integer count = getRepeatAssemblySnCount(organizeCode, workCenterCode, workCellCode, productionAssemblyContext.getEquipmentCode()) + 1;
            dispatchFsmBusiData(organizeCode, getRepeatAssemblySnContextKey(organizeCode, workCenterCode, workCellCode), productionAssemblyContext.getEquipmentCode(), count.toString());
        }
        return dispatchFsmBusiData(organizeCode, getRepeatAssemblySnContextKey(organizeCode, workCenterCode, workCellCode), repeatKey, productionAssemblyContext.getAssemblySn());
    }

    //删除上下文可复用条码信息
    @Override
    public void removeRepeatAssemblySn(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getRepeatAssemblySnContextKey(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode()));
    }

    //删除上下文可复用条码信息
    @Override
    public void removeRepeatAssemblySn(String organizeCode, String workCenterCode, String workCellCode) {
        removeFsmBusiData(organizeCode, getRepeatAssemblySnContextKey(organizeCode, workCenterCode, workCellCode));
    }

    //设备计数KEY
    private String getShortCounterContextKey(StationRequestBean reqBean) {
        return new StringJoiner(MesPcnExtConstWords.COLON).add(reqBean.getOrganizeCode()).add(reqBean.getWorkCenterCode()).add(reqBean.getWorkCellCode()).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.SHORT_COUNTER_CONTEXT).toString();
    }

    //获取设备计数
    @Override
    public Integer getShortCounter(StationRequestBean reqBean) {
        String countStr = getFsmBusiData(reqBean.getOrganizeCode(), getShortCounterContextKey(reqBean));
        return !StringUtils.isEmpty(countStr) ? Integer.valueOf(countStr) : MesPcnExtConstWords.ZERO;
    }

    @Override
    public void dispatchShortCounter(StationRequestBean reqBean, Integer shortCounter) {
        dispatchFsmBusiData(reqBean.getOrganizeCode(), getShortCounterContextKey(reqBean), shortCounter.toString());
    }

    //打包数据KEY
    private String getPackageDataContextKey(StationRequestBean reqBean) {
        return new StringJoiner(MesPcnExtConstWords.COLON).add(reqBean.getOrganizeCode()).add(reqBean.getWorkCenterCode()).add(reqBean.getWorkCellCode()).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.PACKAGE_DATA_CONTEXT).toString();
    }

    //保存打包数据  [JSON]List<MesPackageDataContext>
    @Override
    public Boolean dispatchPackageDataContext(StationRequestBean reqBean, Map<String, String> packageDataContextMap) {
        if (CollectionUtils.isEmpty(packageDataContextMap)) {
            removePackageDataContext(reqBean);
            return true;
        }
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getPackageDataContextKey(reqBean), JSONObject.toJSONString(packageDataContextMap));
    }

    //获取打包数据  [JSON]List<MesPackageDataContext>
    @Override
    public Map<String, String> getPackageDataContext(StationRequestBean reqBean) {
        String contextStr = getFsmBusiData(reqBean.getOrganizeCode(), getPackageDataContextKey(reqBean));
        return !StringUtils.isEmpty(contextStr) ? JSONObject.parseObject(contextStr, Map.class) : null;
    }

    //删除打包数据
    @Override
    public void removePackageDataContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getPackageDataContextKey(reqBean));
    }

    //上个扫描/读取信息KEY
    private String getShippingGroupContextKey(StationRequestBean reqBean) { return getFsmBusikey(reqBean, new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.PRODUCTION_PROCESS_CONTEXT).add(MesPcnExtConstWords.FUNCTION_CHOOSE_SHIPPING_GROUP).toString()); }

    //获取选择/扫描零件发运组信息上下文
    @Override
    public List<StationKvBean> getFunctionChooseShippingGroupContext(StationRequestBean reqBean) {
        String chooseCavityOrderJson = getFsmBusiData(reqBean.getOrganizeCode(), getShippingGroupContextKey(reqBean));
        return !StringUtils.isEmpty(chooseCavityOrderJson) ? JSONObject.parseArray(chooseCavityOrderJson, StationKvBean.class) : null;
    }

    //保存选择/扫描零件发运组信息上下文   腔数=工单个数  数据格式: [StationKvBean(key=shippingGroupCode, name=零件发运组代码, value=xxx, seq=0), StationKvBean(key=cellMessageSource, name=工位信息来源, value=10, seq=1)]
    @Override
    public Boolean dispatchFunctionChooseShippingGroupContext(StationRequestBean reqBean, List<StationKvBean> resultList) {
        if (CollectionUtils.isEmpty(resultList)) return false;
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getShippingGroupContextKey(reqBean), JSONObject.toJSONString(resultList));
    }

    //删除选择/扫描零件发运组信息上下文
    @Override
    public void removeFunctionChooseShippingGroupContext(StationRequestBean reqBean) {
        removeFsmBusiData(reqBean.getOrganizeCode(), getShippingGroupContextKey(reqBean));
    }

    //排序线工单队列推送锁数据KEY 【PS:生产线级别】
    private String getSortQueuePushLockContextKey(StationRequestBean reqBean) { return new StringJoiner(MesPcnExtConstWords.COLON).add(reqBean.getOrganizeCode()).add(reqBean.getWorkCenterCode()).add(MesPcnExtConstWords.QUEUE_PUSH_LOCK_CONTEXT).toString(); }

    //获取排序线工单队列推送锁数据, 返回的值是工位代码
    @Override
    public String getSortQueuePushLockContext(StationRequestBean reqBean, String queuePushId) {
        if (StringUtils.isEmpty(queuePushId)) return null;
        return getFsmBusiData(reqBean.getOrganizeCode(), getSortQueuePushLockContextKey(reqBean), queuePushId);
    }

    //获取排序线工单队列推送锁数据, 返回的值是工单队列ID集合
    @Override
    public List<String> getSortQueuePushLockContext(StationRequestBean reqBean) {
        String queuePushIdListJson = getFsmBusiData(reqBean.getOrganizeCode(), getSortQueuePushLockContextKey(reqBean), reqBean.getWorkCellCode());
        return StringUtils.isEmpty(queuePushIdListJson) ? null : JSONObject.parseArray(queuePushIdListJson, String.class);
    }

    //保存排序线工单队列推送锁数据【保存LOCK信息:item=id,value=工位】【保存当前工位锁定的信息:item=工位,value=idList】
    @Override
    public Boolean dispatchSortQueuePushLockContext(StationRequestBean reqBean, List<String> queuePushIdList) {
        if (CollectionUtils.isEmpty(queuePushIdList)) return false;
        queuePushIdList.forEach(o -> dispatchFsmBusiData(reqBean.getOrganizeCode(), getSortQueuePushLockContextKey(reqBean), o, reqBean.getWorkCellCode()));
        return dispatchFsmBusiData(reqBean.getOrganizeCode(), getSortQueuePushLockContextKey(reqBean), reqBean.getWorkCellCode(), JSONObject.toJSONString(queuePushIdList));
    }

    //删除排序线工单队列推送锁数据
    @Override
    public void removeSortQueuePushLockContext(StationRequestBean reqBean, String queuePushId) {
        if (!StringUtils.isEmpty(queuePushId)) return;
        removeFsmBusiData(reqBean.getOrganizeCode(), getSortQueuePushLockContextKey(reqBean), queuePushId);
    }

    //工单完成数KEY【noCalcOrderQty=true代表工位维度的统计,最终不一定会实际累加到工单上】【noCalcOrderQty=false代表生产线维度的统计】
    private String getWorkOrderCompleteQtyContextCellKey(StationRequestBean reqBean, Boolean noCalcOrderQty, String workOrderNo) {
        StringJoiner sj = new StringJoiner(MesPcnExtConstWords.COLON).add(reqBean.getOrganizeCode()).add(reqBean.getWorkCenterCode());
        if (noCalcOrderQty) sj.add(reqBean.getWorkCellCode());
        return sj.add(MesPcnExtConstWords.WORK_ORDER_COMPLETE_CONTEXT).add(workOrderNo).toString();
    }

    //获取生产线维度的工单完成数上下文[查询]
    @Override
    public MesWorkOrderCompleteQtyContext getWorkOrderCompleteQtyContext(StationRequestBean reqBean, MesProductionPartContext productionPartContext) {
        String key = getWorkOrderCompleteQtyContextCellKey(reqBean, false, productionPartContext.getWorkOrderNo());
        String context = getFsmBusiData(reqBean.getOrganizeCode(), key);
        MesWorkOrderCompleteQtyContext workOrderCompleteQtyContext = !StringUtils.isEmpty(context)
                ? JSONObject.parseObject(context, MesWorkOrderCompleteQtyContext.class)
                : new MesWorkOrderCompleteQtyContext(productionPartContext.getWorkOrderNo(), productionPartContext.getQty(), productionPartContext.getCompleteQty());
        return workOrderCompleteQtyContext;
    }

    //根据noCalcOrderQty判断维度处理处理工单完成数上下文[查询], 有效期三天
    @Override
    public MesWorkOrderCompleteQtyContext dispatchWorkOrderCompleteQtyContext(StationRequestBean reqBean, Boolean noCalcOrderQty, MesProductionPartContext productionPartContext) {
        String key = getWorkOrderCompleteQtyContextCellKey(reqBean, noCalcOrderQty, productionPartContext.getWorkOrderNo());
        String context = getFsmBusiData(reqBean.getOrganizeCode(), key);
        MesWorkOrderCompleteQtyContext workOrderCompleteQtyContext = !StringUtils.isEmpty(context)
                ? JSONObject.parseObject(context, MesWorkOrderCompleteQtyContext.class)
                : new MesWorkOrderCompleteQtyContext(productionPartContext.getWorkOrderNo(), productionPartContext.getQty(), productionPartContext.getCompleteQty());
        //假如缓存中的完成数小于当前传参工单的完成数,则覆盖
        Boolean isNeedSave = !StringUtils.isEmpty(context) && MathOperation.compareTo(productionPartContext.getCompleteQty(), workOrderCompleteQtyContext.getCompleteQty()) > 0;
        if (isNeedSave) workOrderCompleteQtyContext.overrideCompleteQty(productionPartContext.getCompleteQty());
        //判断是否需要存储
        if (StringUtils.isEmpty(context) || isNeedSave) dispatchFsmBusiData(reqBean.getOrganizeCode(), key, JSONObject.toJSONString(workOrderCompleteQtyContext), MesPcnEnumUtil.EXPIRE_TIME.THREE_DAY.getValue());
        return workOrderCompleteQtyContext;
    }

    //根据noCalcOrderQty判断维度处理处理工单完成数上下文[保存], 有效期三天
    //【noCalcOrderQty=true代表工位维度的统计,最终不一定会实际累加到工单上】【noCalcOrderQty=false代表生产线维度的统计】
    @Override
    public MesWorkOrderCompleteQtyContext dispatchWorkOrderCompleteQtyContext(StationRequestBean reqBean, Boolean noCalcOrderQty, String workOrderNo, Double calcCompleteQty) {
        String key = getWorkOrderCompleteQtyContextCellKey(reqBean, noCalcOrderQty, workOrderNo);
        String context = getFsmBusiData(reqBean.getOrganizeCode(), key);
        MesWorkOrderCompleteQtyContext workOrderCompleteQtyContext = !StringUtils.isEmpty(context) ? JSONObject.parseObject(context, MesWorkOrderCompleteQtyContext.class) : null;
        if (null == workOrderCompleteQtyContext) return null;
        workOrderCompleteQtyContext.overrideCompleteQty(MathOperation.add(workOrderCompleteQtyContext.getCompleteQty(), calcCompleteQty));
        dispatchFsmBusiData(reqBean.getOrganizeCode(), key, JSONObject.toJSONString(workOrderCompleteQtyContext), MesPcnEnumUtil.EXPIRE_TIME.THREE_DAY.getValue());
        return workOrderCompleteQtyContext;
    }

}
