# LotAttributeQueryManagerImpl.java

    package com.mycim.server.wip.manager.impl;

    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.OperationAttributeSimulateManager;
    import com.mycim.server.prp.manager.OperationManager;
    import com.mycim.server.system.manager.ReferenceFileManager;
    import com.mycim.server.wip.manager.LotAttributeQueryManager;
    import com.mycim.valueobject.context.dto.QueryContextValueDTO;
    import com.mycim.valueobject.prp.Operation;
    import com.mycim.valueobject.prp.OperationDESCContextValue;
    import com.mycim.valueobject.prp.ProcessLocationContextValue;
    import com.mycim.valueobject.prp.StageContextValue;
    import com.mycim.valueobject.wip.Lot;
    import com.mycim.valueobject.wip.dto.LotProcessStepDto;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    /**
     * This class is only used to query the attribute of the context value
     *
     * @author Qiansheng.Wang
     * @since 2021-05-24
     */
    @Service
    public class LotAttributeQueryManagerImpl implements LotAttributeQueryManager {

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        ReferenceFileManager referenceFileManager;

        @Autowired
        OperationAttributeSimulateManager operationAttributeSimulateManager;

        @Autowired
        OperationManager operationManager;

        @Override
        public String getOperationType(Lot lot) {
            OperationDESCContextValue contextValue = operationAttributeSimulateManager
                    .simulateOperationDESCContextValue(buildQueryContextValue(lot));
            if (contextValue != null && StringUtils.isNotBlank(contextValue.getOperationType())) {
                return contextValue.getOperationType();
            }

            Operation operation = operationManager.getOperation(lot.getOperationRrn());
            if (operation != null && operation.getInstanceRrn() > 0 && StringUtils.isNotBlank(operation.getObjectType())) {
                return operation.getObjectType();
            }

            return StringUtils.EMPTY;
        }

        @Override
        public String getWorkArea(Lot lot) {
            OperationDESCContextValue contextValue = operationAttributeSimulateManager
                    .simulateOperationDESCContextValue(buildQueryContextValue(lot));
            if (contextValue != null && StringUtils.isNotBlank(contextValue.getWorkArea())) {
                return contextValue.getWorkArea();
            }

            Operation operation = operationManager.getOperation(lot.getOperationRrn());
            if (operation != null && operation.getInstanceRrn() > 0 && StringUtils.isNotBlank(operation.getWorkArea())) {
                return operation.getWorkArea();
            }

            return StringUtils.EMPTY;
        }

        @Override
        public String getStageId(Lot lot) {
            StageContextValue contextValue = operationAttributeSimulateManager
                    .simulateStageContextValue(buildQueryContextValue(lot));
            if (contextValue != null && StringUtils.isNotBlank(contextValue.getStageId())) {
                return contextValue.getStageId();
            }
            return StringUtils.EMPTY;
        }

        private String getProcessLocation(QueryContextValueDTO queryContextValue) {
            ProcessLocationContextValue contextValue = operationAttributeSimulateManager
                    .simulateProcessLocationContextValue(queryContextValue);

            if (contextValue != null && StringUtils.isNotBlank(contextValue.getProcessLocation())) {
                return contextValue.getProcessLocation();
            }

            return StringUtils.EMPTY;
        }

        @Override
        public String getProcessLocation(Lot lot) {
            return getProcessLocation(buildQueryContextValue(lot));
        }

        @Override
        public String getProcessLocation(LotProcessStepDto lotProcessStep) {
            return getProcessLocation(buildQueryContextValue(lotProcessStep));
        }

        private QueryContextValueDTO buildQueryContextValue(LotProcessStepDto lotProcessStep) {
            QueryContextValueDTO queryContextValue = new QueryContextValueDTO();

            queryContextValue.setProductRrn(lotProcessStep.getProductRrn());
            queryContextValue.setProductVersion(lotProcessStep.getProductVersion());
            queryContextValue.setProcessRrn(lotProcessStep.getProcessRrn());
            queryContextValue.setProcessVersion(lotProcessStep.getProcessVersion());
            queryContextValue.setRouteRrn(lotProcessStep.getRouteRrn());
            queryContextValue.setRouteVersion(lotProcessStep.getRouteVersion());
            queryContextValue.setOperationRrn(lotProcessStep.getOperationRrn());

            return queryContextValue;
        }

        private QueryContextValueDTO buildQueryContextValue(Lot lot) {
            QueryContextValueDTO queryContextValue = new QueryContextValueDTO();

            queryContextValue.setFacilityRrn(lot.getFacilityRrn());
            queryContextValue.setLotRrn(lot.getLotRrn());
            queryContextValue.setProductRrn(lot.getProductRrn());
            queryContextValue.setProductVersion(lot.getProductVersion());
            queryContextValue.setProcessRrn(lot.getProcessRrn());
            queryContextValue.setProcessVersion(lot.getProcessVersion());
            queryContextValue.setRouteRrn(lot.getRouteRrn());
            queryContextValue.setRouteVersion(lot.getRouteVersion());
            queryContextValue.setOperationRrn(lot.getOperationRrn());

            return queryContextValue;
        }

    }