# CtxInstructionContextValueManagerImpl.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.MapUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.dao.InstructionContextValueManagerDao;
    import com.mycim.server.ctx.exec.manager.CtxInstructionContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.consts.ActionPointList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.CtxInstructionContextValue;
    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 Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/24
     **/
    @Service
    @Transactional
    public class CtxInstructionContextValueManagerImpl implements CtxInstructionContextValueManager {

        private final NamedObjectManager namedObjectManager;

        private final ContextValueManager contextValueManager;

        @Autowired
        private InstructionContextValueManagerDao instructionContextValueManagerDao;

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

        @Override
        public List<ContextValue> getInstructionByProcess(long processRrn, long routeRrn, long operationRrn,
                                                          String actionPoint) {
            CtxInstructionContextValue ctxInstructionContextValue = buildNewCtxInstructionContextValue(processRrn, routeRrn,
                                                                                                       operationRrn);

            setActionPoint(ctxInstructionContextValue, actionPoint);

            return contextValueManager.simulateContextValues(ctxInstructionContextValue);
        }

        @Override
        public List<ContextValue> getInstructionByLot(long lotRrn, String routeSeq, String operationSeq, long productRrn,
                                                      long processRrn, long routeRrn, long operationRrn,
                                                      String actionPoint) {
            CtxInstructionContextValue ctxInstructionContextValue = buildNewCtxInstructionContextValue(lotRrn, routeSeq,
                                                                                                       operationSeq,
                                                                                                       productRrn,
                                                                                                       processRrn, routeRrn,
                                                                                                       operationRrn);

            setActionPoint(ctxInstructionContextValue, actionPoint);

            return contextValueManager.simulateContextValues(ctxInstructionContextValue);
        }

        @Override
        public List<ContextValue> getInstructionByProduct(long productRrn, long processRrn, long routeRrn,
                                                          long operationRrn, String actionPoint) {
            CtxInstructionContextValue ctxInstructionContextValue = buildNewCtxInstructionContextValue(productRrn,
                                                                                                       processRrn, routeRrn,
                                                                                                       operationRrn);

            setActionPoint(ctxInstructionContextValue, actionPoint);

            return contextValueManager.simulateContextValues(ctxInstructionContextValue);
        }

        private CtxInstructionContextValue buildNewCtxInstructionContextValue(long lotRrn, String routeSeq,
                                                                              String operationSeq, long productRrn,
                                                                              long processRrn, long routeRrn,
                                                                              long operationRrn) {
            CtxInstructionContextValue ctxInstructionContextValue = buildNewCtxInstructionContextValue(productRrn,
                                                                                                       processRrn, routeRrn,
                                                                                                       operationRrn);
            ctxInstructionContextValue.setLotRrn(lotRrn);
            ctxInstructionContextValue.setRouteSeq(routeSeq);
            ctxInstructionContextValue.setOperationSeq(operationSeq);

            return ctxInstructionContextValue;
        }

        private void setActionPoint(ContextValue contextValue, String actionPoint) {
            if (StringUtils.isEmpty(actionPoint) || StringUtils.equalsIgnoreCase(actionPoint, ActionPointList.MOVEIN_KEY)) {
                contextValue.setContextKey7(ActionPointList.MOVEIN_KEY);
            } else {
                contextValue.setContextKey7(ActionPointList.MOVEOUT_KEY);
            }
        }

        private CtxInstructionContextValue buildNewCtxInstructionContextValue(long productRrn, long processRrn,
                                                                              long routeRrn, long operationRrn) {
            CtxInstructionContextValue ctxInstructionContextValue = buildNewCtxInstructionContextValue(processRrn, routeRrn,
                                                                                                       operationRrn);

            ctxInstructionContextValue.setProductRrn(productRrn);

            return ctxInstructionContextValue;
        }

        private CtxInstructionContextValue buildNewCtxInstructionContextValue(long processRrn, long routeRrn,
                                                                              long operationRrn) {
            CtxInstructionContextValue ctxInstructionContextValue = buildNewCtxInstructionContextValue();
            ctxInstructionContextValue.setProcessRrn(processRrn);
            ctxInstructionContextValue.setRouteRrn(routeRrn);
            ctxInstructionContextValue.setOperationRrn(operationRrn);

            return ctxInstructionContextValue;
        }

        private CtxInstructionContextValue buildNewCtxInstructionContextValue() {
            CtxInstructionContextValue contextValue = new CtxInstructionContextValue();
            long contextRrn = namedObjectManager
                    .getNamedObjectRrn(contextValue.getContextId(), LocalContext.getFacilityRrn(), ObjectList.CONTEXT_KEY);
            contextValue.setContextRrn(contextRrn);
            return contextValue;
        }

        @Override
        public List getInstructionByProduct(long refRrn, long productRrn) {
            return instructionContextValueManagerDao.getInstructionByProduct(refRrn, productRrn);
        }

        @Override
        public List getInstructionByLot(long refRrn, long productRrn, long lotRrn) {
            return instructionContextValueManagerDao.getInstructionByLot(refRrn, productRrn, lotRrn);
        }

        @Override
        public List getInstructionByProcess(long refRrn, long processRrn) {
            List<Map> temp = instructionContextValueManagerDao.getInstructionByProcess(refRrn, processRrn);
            for (Map map : temp) {
                paddingData(map);
            }
            return temp;
        }

        private Map paddingData(Map map) {
            // 补充数据
            long processRrn = MapUtils.getLongValue(map, "processRrn");
            if (processRrn != 0) {
                map.put("processId", namedObjectManager.getInstanceId(processRrn));
            }

            long routeRrn = MapUtils.getLongValue(map, "routeRrn");
            if (routeRrn != 0) {
                map.put("routeId", namedObjectManager.getInstanceId(routeRrn));
            }

            long operationRrn = MapUtils.getLongValue(map, "operationRrn");
            if (operationRrn != 0) {
                map.put("operationId", namedObjectManager.getInstanceId(operationRrn));
                map.put("operationDesc", namedObjectManager.getNamedObjectDesc(operationRrn));
            }
            return map;
        }

    }