# JobQueryManagerImpl.java

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

    import com.mycim.framework.jdbc.Page;
    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.manager.ProcessOperationDescContextValueManager;
    import com.mycim.server.ctx.exec.manager.RecipeContextManager;
    import com.mycim.server.ems.manager.ProductLayerMaskManager;
    import com.mycim.server.prp.manager.OperationManager;
    import com.mycim.server.system.manager.ReferenceFileManager;
    import com.mycim.server.wip.dao.JobQueryDAO;
    import com.mycim.server.wip.manager.JobQueryManager;
    import com.mycim.server.wip.manager.LotQueryManager;
    import com.mycim.server.wip.manager.WipWorkflowQueryManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.ems.Entity;
    import com.mycim.valueobject.prp.Operation;
    import com.mycim.valueobject.wip.Job;
    import com.mycim.valueobject.wip.LotStatus;
    import com.mycim.valueobject.wip.Run;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.*;

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/21
     **/
    @Service
    @Transactional
    public class JobQueryManagerImpl implements JobQueryManager {

        @Autowired
        private JobQueryDAO jobQueryDAO;

        @Autowired
        private ProcessOperationDescContextValueManager processOperationDescContextValueManager;

        @Autowired
        private ReferenceFileManager referenceFileManager;

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Autowired
        private ProductLayerMaskManager productLayerMaskManager;

        @Autowired
        private WipWorkflowQueryManager wipWorkflowQueryManager;

        @Autowired
        private LotQueryManager lotQueryManager;

        @Autowired
        private OperationManager operationManager;

        @Autowired
        private RecipeContextManager recipeContextManager;

        public JobQueryManagerImpl(JobQueryDAO jobQueryDAO, ReferenceFileManager referenceFileManager,
                                   NamedObjectManager namedObjectManager) {
            this.jobQueryDAO = jobQueryDAO;
            this.referenceFileManager = referenceFileManager;
            this.namedObjectManager = namedObjectManager;
        }

        @Override
        public List<Map> getJobList(long instanceRrn, String type) {
            Map operationInfo = new HashMap(3);
            if (StringUtils.equals(type, ObjectList.OPERATION_KEY)) {
                operationInfo = getOperaionInfo(instanceRrn);
            }
            List<Map> jobList = jobQueryDAO.getJobList(instanceRrn, type, operationInfo);

            for (Map job : jobList) {
                if (StringUtils.equals(type, ObjectList.EQUIPMENT_KEY)) {
                    operationInfo = getOperaionInfo(MapUtils.getLongValue(job, "operationRrn"));
                    job.put("operationId", MapUtils.getString(operationInfo, "operId"));
                    job.put("UOM1", MapUtils.getString(operationInfo, "UOM1"));
                    job.put("UOM2", MapUtils.getString(operationInfo, "UOM2"));
                } else {
                    job.put("operationId", namedObjectManager.getInstanceId(MapUtils.getLongValue(job, "operationRrn")));
                }

                job.put("recipeId", namedObjectManager.getNamedObjectId(MapUtils.getLongValue(job, "recipeRrn", 0)));
                job.put("processId", namedObjectManager.getNamedObjectId(MapUtils.getLongValue(job, "processRrn", 0)));
                job.put("productId", namedObjectManager.getNamedObjectId(MapUtils.getLongValue(job, "productRrn", 0)));

                String desc = processOperationDescContextValueManager.getOperationDesc(job);
                job.put("operationDesc", desc);
                job.put("reticleId", namedObjectManager.getNamedObjectId(MapUtils.getLong(job, "reticleRrn")));
                job.put("equipmentId", namedObjectManager.getNamedObjectId(MapUtils.getLong(job, "equipmentRrn")));
            }
            return jobList;
        }

        @Override
        public Job getJob(long jobRrn) {
            Job job = jobQueryDAO.getJob(jobRrn);
            if (job != null) {
                job.setEqptId(namedObjectManager.getNamedObjectId(job.getEqptRrn()));
                job.setOperationId(namedObjectManager.getNamedObjectId(job.getOperationRrn()));

                job.setUOM1(referenceFileManager
                                    .getReferenceDetailExchange("$$UNIT_OF_MEASURE", job.getUOM1(), "DATA_1_VALUE"));

                job.setUOM2(referenceFileManager
                                    .getReferenceDetailExchange("$$UNIT_OF_MEASURE", job.getUOM2(), "DATA_1_VALUE"));
            }
            return job;
        }

        @Override
        public long getJobRrn(long lotRrn) {
            return jobQueryDAO.getJobRrn(lotRrn);
        }

        @Override
        public Run getRun(Long runRrn) {
            Run run = jobQueryDAO.getRun(runRrn);

            run.setEqptId(namedObjectManager.getInstanceId(run.getEqptRrn()));
            run.setRecipeId(recipeContextManager.parseRecipeId(run.getRecipeString()));
            run.setRecipeParameter(recipeContextManager.parseRecipeParam(run.getRecipeString()));

            return run;
        }

        @Override
        public Page queryRunHistory(Page page, Long runRrn) {
            page = jobQueryDAO.queryRunHistory(page, runRrn);

            List<Map> results = (List<Map>) page.getResults();

            for (Map map : results) {
                map.put("carrier_id", namedObjectManager.getInstanceId((Long) map.get("carrier_rrn")));
                map.put("reticle_id", namedObjectManager.getInstanceId((Long) map.get("reticle_rrn")));
            }
            page.setResults(results);

            return page;
        }

        @Override
        public Run getRunByJob(long jobRrn) {
            Run run = jobQueryDAO.getRunByJob(jobRrn);

            if (run == null) {
                return null;
            }

            run.setEqptId(namedObjectManager.getInstanceId(run.getEqptRrn()));
            run.setRecipeId(recipeContextManager.parseRecipeId(run.getRecipeString()));
            run.setRecipeParameter(recipeContextManager.parseRecipeParam(run.getRecipeString()));

            return run;
        }

        @Override
        public List<Map> qryNextInfo(Long jobRrn) {
            List<Map> info = new ArrayList<>();

            List<Map> c = lotQueryManager.getLotInfo4Transaction(jobRrn, null);

            for (Map _m : c) {

                Long lotExecRrn = null;

                if (_m.get("executionRrn") != null) {
                    lotExecRrn = new Long((String) _m.get("executionRrn"));
                }

                if (lotExecRrn != null) {
                    Map m = wipWorkflowQueryManager.getProcessStepVersion(lotExecRrn);
                    m.put("lotId", _m.get("lotId"));
                    m.put("facilityRrn", _m.get("facilityRrn"));
                    m.put("qty", _m.get("qty"));
                    m.put("lotRrn", new Long(_m.get("lotRrn").toString()));
                    m.put("lotStatus", _m.get("lotStatus"));
                    info.add(m);
                }
            }

            return info;
        }

        @Override
        public boolean checkRunningJobByEquip(long equipRrn, long jobRrn) {
            return jobQueryDAO.checkRunningJobByEquip(equipRrn, jobRrn);
        }

        @Override
        public boolean checkRunning4Chamber(Entity entity, Long jobRrn) {
            String entityId = entity.getInstanceId();
            String chamberId = entityId.substring(entityId.length() - 1);
            Long entityParentRrn = entity.getParentEntityRrn();
            return checkRunning4Chamber(entityParentRrn, jobRrn, chamberId);
        }

        @Override
        public boolean checkRunning4Chamber(Long entityParentRrn, Long jobRrn, String chamberId) {
            return jobQueryDAO.checkIsRunning4Chamber(entityParentRrn, jobRrn, chamberId);
        }

        //当前设备rrn
        //以及要排除的rrn
        public boolean checkMainEqptIsRunning(long mainEqptRrn, List<Long> excludeJobRrn){
            return jobQueryDAO.checkMainEqptIsRunning(mainEqptRrn,excludeJobRrn);
        }

        @Override
        public String getSemiAutoParamsInExection(long exectionRrn) {
            return jobQueryDAO.getSemiAutoParamsInExection(exectionRrn);
        }

        private Map getOperaionInfo(long instanceRrn) {
            Map operationInfo = new HashMap(3);
            Operation operation = operationManager.getOperation(instanceRrn);
            String UOM1 = referenceFileManager
                    .referenceDetailExchange("$$UNIT_OF_MEASURE", operation.getUnitOfMeasure1(), "", "DATA_1_VALUE");
            String UOM2 = referenceFileManager
                    .referenceDetailExchange("$$UNIT_OF_MEASURE", operation.getUnitOfMeasure2(), "", "DATA_1_VALUE");
            String operId = namedObjectManager.getInstanceId(instanceRrn);
            operationInfo.put("UOM1", UOM1);
            operationInfo.put("UOM2", UOM2);
            operationInfo.put("operId", operId);
            return operationInfo;
        }

        private Map getReticle(Long executeRrn, Long productRrn, Long processRrn) {
            Map mask = new HashMap<>();
            Map stageLayer = wipWorkflowQueryManager.getWorkflowParamValueMap(executeRrn);
            if ((MapUtils.isNotEmpty(stageLayer)) && (stageLayer.get("layerId") != null)) {

                if (StringUtils.isNotEmpty(MapUtils.getString(stageLayer, "layerId"))) {
                    mask = productLayerMaskManager
                            .getProductLayerReticle(productRrn, processRrn, (String) stageLayer.get("layerId"));
                } else {
                    String layerId = lotQueryManager.getLayerId(executeRrn);
                    mask = productLayerMaskManager.getProductLayerReticle(productRrn, processRrn, layerId);

                }
            }
            return mask;
        }

        private Map getReticle(String layerId, Long productRrn, Long processRrn) {
            return productLayerMaskManager.getProductLayerReticle(productRrn, processRrn, layerId);
        }

    }