# StageContextValueManagerImpl.java

    package com.mycim.server.ctx.exec.manager.impl;

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.StageContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.StageContextValue;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * @author shijie.deng
     * @version 6.0.0
     * @date 2019/9/1
     **/
    @Service
    @Transactional
    public class StageContextValueManagerImpl implements StageContextValueManager {

        private final ContextValueManager contextValueManager;

        private final NamedObjectManager namedObjectManager;

        public StageContextValueManagerImpl(ContextValueManager contextValueManager,
                                            NamedObjectManager namedObjectManager) {
            this.contextValueManager = contextValueManager;
            this.namedObjectManager = namedObjectManager;
        }

        @Override
        public String getStage(Long productRrn, Long processRrn, Integer processVersion, Long routeRrn, Long operationRrn) {
            StageContextValue stageContextValue = buildNewStageContextValue(productRrn, processRrn, processVersion,
                                                                            routeRrn, operationRrn);
            ContextValue contextValueNoFilter = contextValueManager.getContextValueNoFilter(stageContextValue);
            if (contextValueNoFilter != null) {
                return contextValueNoFilter.getResultValue1();
            }
            return null;
        }

        @Override
        public ContextValue getStageContextValue(Long productRrn, Long processRrn, Integer processVersion, Long routeRrn,
                                                 Long operationRrn) {
            StageContextValue stageContextValue = buildNewStageContextValue(productRrn, processRrn, processVersion,
                                                                            routeRrn, operationRrn);
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(stageContextValue);
            return contextValue;
        }

        @Override
        public String getStageIdByContext(Lot lot, Long facilityRrn) {
            StageContextValue stageContextValue = new StageContextValue();

            stageContextValue.setProductRrn(lot.getProductRrn());
            stageContextValue.setProcessRrn(lot.getProcessRrn());
            stageContextValue.setProcessVersion(lot.getProcessVersion());
            stageContextValue.setRouteRrn(lot.getRouteRrn());
            stageContextValue.setRouteSeq(lot.getRouteSeq());
            stageContextValue.setOperationRrn(lot.getOperationRrn());
            stageContextValue.setOperationSeq(lot.getOperationSeq());
            stageContextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(stageContextValue.getContextId(),
                                                                                 namedObjectManager
                                                                                         .getNamedSpace(facilityRrn,
                                                                                                        ObjectList.CONTEXT_KEY),
                                                                                 ObjectList.CONTEXT_KEY));
            ContextValue contextValue = contextValueManager.simulateContextValue(stageContextValue);

            return contextValue == null ? "" : contextValue.getResultValue1();
        }

        @Override
        public List<ContextValue> getStageContextValueList(long productRrn, long processRrn, int processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            StageContextValue stageContextValue = new StageContextValue();
            stageContextValue.setContextRrn(namedObjectManager
                                                    .getNamedObjectRrn(stageContextValue.getContextId(), facilityRrn,
                                                                       ObjectList.CONTEXT_KEY));
            stageContextValue.setProductRrn(productRrn);
            stageContextValue.setProcessRrn(processRrn);
            stageContextValue.setProcessVersion(processVersion);
            return contextValueManager.getContextValuesByContextKey(stageContextValue);
        }

        @Override
        public String getStageId(Lot lot) {
            StageContextValue stageContextValue = buildNewStageContextValue(lot);
            ContextValue contextValue = contextValueManager.simulateContextValue(stageContextValue);
            if (contextValue == null) {
                return null;
            }
            return contextValue.getResultValue1();
        }

        @Override
        public Map buildContextValueForStage(Long facilityRrn, Map contextValueInfo) {
            StageContextValue stageContextValue = new StageContextValue();
            stageContextValue.setProductRrn(MapUtils.getLong(contextValueInfo, "productRrn"));
            stageContextValue.setProcessRrn(MapUtils.getLong(contextValueInfo, "processRrn"));
            stageContextValue.setOperationRrn(MapUtils.getLong(contextValueInfo, "operationRrn"));
            stageContextValue.setRouteRrn(MapUtils.getLong(contextValueInfo, "routeRrn"));
            stageContextValue.setProcessVersion(MapUtils.getIntValue(contextValueInfo, "processVersion"));
            stageContextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(stageContextValue.getContextId(),
                                                                                 namedObjectManager
                                                                                         .getNamedSpace(facilityRrn,
                                                                                                        ObjectList.CONTEXT_KEY),
                                                                                 ObjectList.CONTEXT_KEY));
            //未按照规则取值，取值不需要seq，置为空，后续需要seq的话，直接释放即可
            // stageContextValue.setOperationSeq("");
            // stageContextValue.setRouteSeq("");
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(stageContextValue);
            if (contextValue != null) {
                // contextValue = this.getPrpManager().FilterContextValueToId(contextValue);
                contextValueInfo.put("stageId", contextValue.getResultValue1());
                contextValueInfo.put("activeStageId", contextValue.getResultValue1());
                if (contextValue.getEcnRrn() != null && contextValue.getEcnRrn() > 0) {
                    contextValueInfo.put("ecnRrn", contextValue.getEcnRrn());
                    contextValueInfo.put("status", contextValue.getStatus());
                    contextValueInfo.put("effectiveDateFrom", contextValue.getEffectiveDateFrom());
                    contextValueInfo.put("effectiveDateTo", contextValue.getEffectiveDateTo());
                }
            }

            return contextValueInfo;
        }

        private StageContextValue buildNewStageContextValue(Lot lot) {
            StageContextValue stageContextValue = new StageContextValue();

            stageContextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(stageContextValue.getContextId(),
                                                                                 LocalContext.getFacilityRrn(),
                                                                                 ObjectList.CONTEXT_KEY));
            stageContextValue.setProductRrn(lot.getProductRrn());
            stageContextValue.setProcessRrn(lot.getProcessRrn());
            stageContextValue.setProcessVersion(lot.getProcessVersion());
            stageContextValue.setRouteRrn(lot.getRouteRrn());
            stageContextValue.setRouteSeq(lot.getRouteSeq());
            stageContextValue.setOperationRrn(lot.getOperationRrn());
            stageContextValue.setOperationSeq(lot.getOperationSeq());

            return stageContextValue;
        }

        private StageContextValue buildNewStageContextValue(Long productRrn, Long processRrn, Integer processVersion,
                                                            Long routeRrn, Long operationRrn) {
            StageContextValue stageContextValue = new StageContextValue();

            stageContextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(stageContextValue.getContextId(),
                                                                                 LocalContext.getFacilityRrn(),
                                                                                 ObjectList.CONTEXT_KEY));
            stageContextValue.setProductRrn(productRrn);
            stageContextValue.setProcessRrn(processRrn);
            stageContextValue.setRouteRrn(routeRrn);
            stageContextValue.setOperationRrn(operationRrn);
            stageContextValue.setProcessVersion(processVersion);

            return stageContextValue;
        }

    }