# ReticleFamilyContextManagerImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.beans.BeanUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.ReticleFamilyContextManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.ReticleFamilyContextValue;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * @author Luopeng.Wang
     * @version 6.0.0
     * @date 2019/9/9
     **/

    @Service
    @Transactional
    public class ReticleFamilyContextManagerImpl implements ReticleFamilyContextManager {

        @Autowired
        private ContextValueManager contextValueManager;

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Override
        public ContextValue getReticleFamilyContextValue(Long productRrn, Integer productVersion, Long processRrn,
                                                         Integer processVersion, Long routeRrn, Long operationRrn) {
            ReticleFamilyContextValue reticleFamilyContextValue = buildNewReticleFamilyContextValueForProcess(productRrn,
                                                                                                              productVersion,
                                                                                                              processRrn,
                                                                                                              processVersion,
                                                                                                              routeRrn,
                                                                                                              operationRrn);
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(reticleFamilyContextValue);
            return contextValue;
        }

        @Override
        public long getReticleFamilyRrn(long productRrn, Integer productVersion, long processRrn, int processVersion,
                                        long routeRrn, long operationRrn) {
            long reticleFamliyRrn = 0;
            ContextValue reticleFamilyContextValue = getReticleFamilyContextValue(productRrn, productVersion, processRrn,
                                                                                  processVersion, routeRrn, operationRrn);
            if (reticleFamilyContextValue != null && StringUtils.isNotBlank(reticleFamilyContextValue.getResultValue1())) {
                reticleFamliyRrn = NumberUtils.toLong(reticleFamilyContextValue.getResultValue1(), 0);
            }
            return reticleFamliyRrn;
        }

        @Override
        public List<ContextValue> getReticleFamilyContextValueList(long productRrn, Integer productVersion, long processRrn,
                                                                   int processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            ReticleFamilyContextValue reticleFamilyContextValue = new ReticleFamilyContextValue();
            reticleFamilyContextValue.setContextRrn(namedObjectManager
                                                            .getNamedObjectRrn(reticleFamilyContextValue.getContextId(),
                                                                               facilityRrn, ObjectList.CONTEXT_KEY));
            reticleFamilyContextValue.setProductRrn(productRrn);
            reticleFamilyContextValue.setProcessRrn(processRrn);
            reticleFamilyContextValue.setProcessVersion(processVersion);

            return contextValueManager.getContextValuesByContextKey(reticleFamilyContextValue);
        }

        @Override
        public List<ContextValue> getReticleFamilyContextValueList(Long processRrn, Integer processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            ReticleFamilyContextValue reticleFamilyContextValue = new ReticleFamilyContextValue();
            reticleFamilyContextValue.setContextRrn(namedObjectManager
                                                            .getNamedObjectRrn(reticleFamilyContextValue.getContextId(),
                                                                               facilityRrn, ObjectList.CONTEXT_KEY));
            reticleFamilyContextValue.setProcessRrn(processRrn);
            reticleFamilyContextValue.setProcessVersion(processVersion);

            return contextValueManager.getContextValuesByContextKey(reticleFamilyContextValue);
        }

        @Override
        public boolean isRequiredReticle(long productRrn, Integer productVersion, long processRrn, int processVersion,
                                         long routeRrn, long operationRrn) {
            ContextValue reticleFamilyContextValue = getReticleFamilyContextValue(productRrn, productVersion, processRrn,
                                                                                  processVersion, routeRrn, operationRrn);
            return reticleFamilyContextValue != null &&
                    StringUtils.isNotBlank(reticleFamilyContextValue.getResultValue1()) &&
                    NumberUtils.toLong(reticleFamilyContextValue.getResultValue1(), 0) > 0;
        }

        @Override
        public Map buildContextValueForReticleFamily(Long facilityRrn, Map contextValueInfo) {

            ReticleFamilyContextValue reticleFamilyContextValue = new ReticleFamilyContextValue();
            BeanUtils.copyMapToBean(reticleFamilyContextValue, contextValueInfo);
            reticleFamilyContextValue.setProductRrn(MapUtils.getLong(contextValueInfo, "productRrn"));
            reticleFamilyContextValue.setProductVersion(MapUtils.getInteger(contextValueInfo, "productVersion"));
            reticleFamilyContextValue.setProcessRrn(MapUtils.getLong(contextValueInfo, "processRrn"));
            reticleFamilyContextValue.setOperationRrn(MapUtils.getLong(contextValueInfo, "operationRrn"));
            reticleFamilyContextValue.setRouteRrn(MapUtils.getLong(contextValueInfo, "routeRrn"));
            reticleFamilyContextValue.setContextRrn(namedObjectManager
                                                            .getNamedObjectRrn(reticleFamilyContextValue.getContextId(),
                                                                               facilityRrn, ObjectList.CONTEXT_KEY));

            // 未按照规则取值，取值不需要seq，置为空，后续需要seq的话，直接释放即可
            reticleFamilyContextValue.setOperationSeq("");
            reticleFamilyContextValue.setRouteSeq("");
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(reticleFamilyContextValue);
            if (contextValue != null) {
                contextValueInfo.put("reticleGroupRrn", contextValue.getResultValue1());
                contextValue = contextValueManager.filterContextValueToId(contextValue);
                contextValueInfo.put("reticleFamilyId", 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;
        }

        public ReticleFamilyContextValue buildNewReticleFamilyContextValue() {
            ReticleFamilyContextValue contextValue = new ReticleFamilyContextValue();
            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(),
                                                                            LocalContext.getFacilityRrn(),
                                                                            ObjectList.CONTEXT_KEY));
            return contextValue;
        }

        private ReticleFamilyContextValue buildNewReticleFamilyContextValueForProcess(Long productRrn,
                                                                                      Integer productVersion,
                                                                                      Long processRrn,
                                                                                      Integer processVersion, Long routeRrn,
                                                                                      Long operationRrn) {
            ReticleFamilyContextValue contextValue = buildNewReticleFamilyContextValue();

            contextValue.setProductRrn(productRrn);
            contextValue.setProductVersion(productVersion);
            contextValue.setProcessRrn(processRrn);
            contextValue.setProcessVersion(processVersion);
            contextValue.setRouteRrn(routeRrn);
            contextValue.setOperationRrn(operationRrn);
            return contextValue;
        }

    }