# WflLinkContextValueManagerImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.dao.WflLinkContextValueDao;
    import com.mycim.server.ctx.exec.manager.WflLinkContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.WflLinkContextValue;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/1
     **/
    @Service
    @Transactional
    public class WflLinkContextValueManagerImpl implements WflLinkContextValueManager {

        private final ContextValueManager contextValueManager;

        private final NamedObjectManager namedObjectManager;

        @Autowired
        private WflLinkContextValueDao wflLinkContextValueDao;

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

        @Override
        public void copyContextValueForProcessVersionWithActivatedEcn(long processRrn, int currentVersion) {
            WflLinkContextValue wflContextValue = buildNewWflLinkContextValue(processRrn, currentVersion);
            contextValueManager
                    .copyContextValueForContextKeyWithActivatedEcn(LocalContext.getUserRrn(), "copyByLastProcessVersion",
                                                                   wflContextValue, "CONTEXT_KEY14",
                                                                   StringUtils.toString(currentVersion + 1));
        }

        @Override
        public void deleteContextValueForProcessVersion(long instanceRrn, int instanceVersion) {
            contextValueManager.deleteContextValueByContextKey("deleteProcessVersion",
                                                               buildNewWflLinkContextValue(instanceRrn, instanceVersion));
        }

        @Override
        public List<WflLinkContextValue> getContextValues(long processRrn, int processVersion) {
            List<ContextValue> contextValues = contextValueManager
                    .getContextValuesByContextKey(buildNewWflLinkContextValue(processRrn, processVersion));
            List<WflLinkContextValue> wflLinkContextValues = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    WflLinkContextValue routeContextValue = new WflLinkContextValue();
                    routeContextValue.copy(contextValue);
                    wflLinkContextValues.add(routeContextValue);
                }
            }
            return wflLinkContextValues;
        }

        @Override
        public List<String> getParameterForLot(WflLinkContextValue ctx, List operationParam) {
            return wflLinkContextValueDao.getParameterForLot(ctx, operationParam);
        }

        @Override
        public String getEffectiveMultipathWflLinkId(Long facilityRrn, Map<String, Object> contextKey,
                                                     boolean isRouteLastStep) {
            WflLinkContextValue wflLinkContextValue = buildWflLinkByContextValue(facilityRrn, contextKey);

            if (isRouteLastStep) {
                wflLinkContextValue.setOperationRrn(null);
            }
            ContextValue contextValue = contextValueManager.getContextValueForMultipath(wflLinkContextValue);
            if (contextValue != null) {
                return contextValue.getResultValue1();
            } else {
                //手动的multipath在lbrd中会插入一条手动规则的multipath情景值
                //现在情景值rule不勾，生成sql都是value=' ' 导致有lotRrn的autoressign匹配不上有BUG,还要匹配手动multipath式
                WflLinkContextValue wflLinkContextValue4Manual = buildWflLinkByContextValue4Manual(facilityRrn, contextKey);
                if (isRouteLastStep) {
                    wflLinkContextValue4Manual.setOperationRrn(null);
                }
                contextValue = contextValueManager.getContextValueForMultipath(wflLinkContextValue4Manual);
                if (contextValue != null) {
                    return contextValue.getResultValue1();
                }
            }
            return StringUtils.EMPTY;
        }

        @Override
        public List<WflLinkContextValue> getContextValues(WflLinkContextValue ctx) {
            ctx.setContextRrn(namedObjectManager.getNamedObjectRrn(ctx.getContextId(), LocalContext.getFacilityRrn(),
                                                                   ObjectList.CONTEXT_KEY));

            List<ContextValue> contextValues = contextValueManager.getContextValuesByContextKey(ctx);
            List<WflLinkContextValue> wflLinkContextValues = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    WflLinkContextValue routeContextValue = new WflLinkContextValue();
                    routeContextValue.copy(contextValue);
                    wflLinkContextValues.add(routeContextValue);
                }
            }
            return wflLinkContextValues;
        }

        //auto式的multipath情景值规则
        private WflLinkContextValue buildWflLinkByContextValue(Long facilityRrn, Map<String, Object> contextKey) {
            WflLinkContextValue wflLinkContextValue = new WflLinkContextValue();
            wflLinkContextValue.setContextRrn(namedObjectManager
                                                      .getNamedObjectRrn(wflLinkContextValue.getContextId(), facilityRrn,
                                                                         ObjectList.CONTEXT_KEY));

            wflLinkContextValue.setProductRrn(MapUtils.getLong(contextKey, "productRrn")); // context_key1
            wflLinkContextValue.setProcessRrn(MapUtils.getLong(contextKey, "processRrn")); // context_key2
            wflLinkContextValue.setRouteRrn(MapUtils.getLong(contextKey, "routeRrn")); // context_key3
            wflLinkContextValue.setOperationRrn(MapUtils.getLong(contextKey, "operationRrn")); // context_key4
            //wflLinkContextValue.setContextKey5(MapUtils.getString(contextKey, "lotRrn"));

            wflLinkContextValue.setContextKey9(MapUtils.getString(contextKey, "recipeRrn"));
            //        wflLinkContextValue.setContextKey10(MapUtils.getString(contextKey, "parameter"));
            //        wflLinkContextValue.setContextKey11(MapUtils.getString(contextKey, "parameterValue")); //
            //        lower
            //        wflLinkContextValue.setContextKey12(MapUtils.getString(contextKey, "parameterValue")); //
            //        upper
            wflLinkContextValue.setContextKey15(MapUtils.getString(contextKey, "lotType"));
            wflLinkContextValue.setProcessVersion(MapUtils.getInteger(contextKey, "processVersion")); // context_key14

            return wflLinkContextValue;
        }

        //手动选择multipath情景值规则
        private WflLinkContextValue buildWflLinkByContextValue4Manual(Long facilityRrn, Map<String, Object> contextKey) {
            WflLinkContextValue wflLinkContextValue = new WflLinkContextValue();
            wflLinkContextValue.setContextRrn(namedObjectManager
                                                      .getNamedObjectRrn(wflLinkContextValue.getContextId(), facilityRrn,
                                                                         ObjectList.CONTEXT_KEY));

            wflLinkContextValue.setProductRrn(MapUtils.getLong(contextKey, "productRrn")); // context_key1
            wflLinkContextValue.setProcessRrn(MapUtils.getLong(contextKey, "processRrn")); // context_key2
            wflLinkContextValue.setRouteRrn(MapUtils.getLong(contextKey, "routeRrn")); // context_key3
            wflLinkContextValue.setOperationRrn(MapUtils.getLong(contextKey, "operationRrn")); // context_key4
            wflLinkContextValue.setContextKey5(MapUtils.getString(contextKey, "lotRrn"));

            //wflLinkContextValue.setContextKey9(MapUtils.getString(contextKey, "recipeRrn"));
            wflLinkContextValue.setContextKey15(MapUtils.getString(contextKey, "lotType"));
            wflLinkContextValue.setProcessVersion(MapUtils.getInteger(contextKey, "processVersion")); // context_key14

            return wflLinkContextValue;
        }

        private WflLinkContextValue buildNewWflLinkContextValue(Long processRrn, Integer processVersion) {
            WflLinkContextValue contextValue = new WflLinkContextValue();

            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(),
                                                                            LocalContext.getFacilityRrn(),
                                                                            ObjectList.CONTEXT_KEY));
            contextValue.setProcessRrn(processRrn);
            contextValue.setProcessVersion(processVersion);

            return contextValue;
        }

    }