# PollutionLevelContextValueManagerImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.PollutionLevelContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.OperationDESCContextValue;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;

    /**
     * @author shijie.deng
     * @version 6.0.0
     * @date 2019/9/23
     **/
    @Service
    @Transactional
    public class PollutionLevelContextValueManagerImpl implements PollutionLevelContextValueManager {

        @Autowired
        private ContextValueManager contextValueManager;

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Override
        public String getPolluctionLevelByProcessInfo(long productRrn, long processRrn, long operationRrn, long routeRrn) {
            OperationDESCContextValue pollutionLevelContextValue = buildNewOperationDESCContextValue(productRrn, processRrn,
                                                                                                     operationRrn,
                                                                                                     routeRrn);
            ContextValue contextValue = contextValueManager.simulateContextValue(pollutionLevelContextValue);
            if (contextValue != null) {
                return contextValue.getResultValue3();
            }
            return null;
        }

        @Override
        public String getPolluctionLevelByProcessInfo(long productRrn, long processRrn, int processVersion,
                                                      long operationRrn, long routeRrn) {
            OperationDESCContextValue pollutionLevelContextValue = buildNewOperationDESCContextValue(productRrn, processRrn,
                                                                                                     operationRrn,
                                                                                                     routeRrn);
            pollutionLevelContextValue.setProcessVersion(processVersion);

            ContextValue contextValue = contextValueManager.simulateContextValue(pollutionLevelContextValue);
            if (contextValue != null) {
                return contextValue.getResultValue3();
            }
            return null;
        }

        @Override
        public List<ContextValue> getPollutionLevelContextValueList(long productRrn, long processRrn, int processVersion) {
            OperationDESCContextValue pollutionLevelContextValue = buildNewOperationDESCContextValue();
            if (NumberUtils.LONG_ZERO==processRrn){
                pollutionLevelContextValue.setProductRrn(productRrn);
            }
            pollutionLevelContextValue.setProcessRrn(processRrn);
            pollutionLevelContextValue.setProcessVersion(processVersion);

            return contextValueManager.getContextValuesByContextKey(pollutionLevelContextValue);
        }
        @Override
        public ContextValue getPollutionLevelContextValueByLot(Lot lot) {
            OperationDESCContextValue pollutionLevelContextValue = buildNewOperationDESCContextValue();
            // pollutionLevelContextValue.setProductRrn(lot.getProductRrn());
            pollutionLevelContextValue.setProcessRrn(lot.getProcessRrn());
            pollutionLevelContextValue.setProcessVersion(lot.getProcessVersion());
            pollutionLevelContextValue.setRouteRrn(lot.getRouteRrn());
            pollutionLevelContextValue.setOperationRrn(lot.getOperationRrn());
            ContextValue contextValue = contextValueManager
                    .getContextValue(pollutionLevelContextValue, LocalContext.getFacilityRrn());
            return contextValue;
        }

        @Override
        public String getPollutionLevelByLot(Lot lot) {
            ContextValue pollutionLevelContextValue = getPollutionLevelContextValueByLot(lot);
            return pollutionLevelContextValue == null ? lot.getPollutionLevel() : pollutionLevelContextValue
                    .getResultValue3();
        }

        private OperationDESCContextValue buildNewOperationDESCContextValue(long productRrn, long processRrn,
                                                                            long operationRrn, long routeRrn) {
            OperationDESCContextValue pollutionLevelContextValue = buildNewOperationDESCContextValue();
            pollutionLevelContextValue.setProductRrn(productRrn);
            pollutionLevelContextValue.setProcessRrn(processRrn);
            pollutionLevelContextValue.setOperationRrn(operationRrn);
            pollutionLevelContextValue.setRouteRrn(routeRrn);
            return pollutionLevelContextValue;
        }

        private OperationDESCContextValue buildNewOperationDESCContextValue() {
            OperationDESCContextValue contextValue = new OperationDESCContextValue();
            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(),
                                                                            LocalContext.getFacilityRrn(),
                                                                            ObjectList.CONTEXT_KEY));
            return contextValue;
        }

    }