# LotInqManagerImpl.java

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

    import com.fa.sesa.i18n.I18nUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.server.automonitor.manager.LotAutoMonitorInqManager;
    import com.mycim.server.carrier.manager.CarrierInqManager;
    import com.mycim.server.carrier.manager.CarrierManager;
    import com.mycim.server.ctx.exec.manager.PollutionLevelContextValueManager;
    import com.mycim.server.ctx.exec.manager.ProcessOperationDescContextValueManager;
    import com.mycim.server.ctx.exec.manager.RecipeContextManager;
    import com.mycim.server.ctx.exec.manager.ResequenceContextValueManager;
    import com.mycim.server.ems.manager.EquipmentInqManager;
    import com.mycim.server.prp.manager.OperationInqManager;
    import com.mycim.server.prp.manager.ProcessInqManager;
    import com.mycim.server.prp.manager.ProductInqManager;
    import com.mycim.server.reticle.manager.LocationInqManager;
    import com.mycim.server.reticle.manager.ReticleFamilyInqManager;
    import com.mycim.server.reticle.manager.ReticleInqManager;
    import com.mycim.server.runcard.manager.RunCardLotInqManager;
    import com.mycim.server.wip.dao.LotInqDAO;
    import com.mycim.server.wip.manager.DiffBatchQueryManager;
    import com.mycim.server.wip.manager.LotAttributeQueryManager;
    import com.mycim.server.wip.manager.LotInqManager;
    import com.mycim.utils.WipUtils;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.consts.LotInfoConstants;
    import com.mycim.valueobject.ems.Carrier;
    import com.mycim.valueobject.prp.Item;
    import com.mycim.valueobject.wip.Lot;
    import com.mycim.valueobject.wip.LotStatus;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;

    /**
     * @author finatice.yang
     * @date 2021/12/13
     **/
    @Service
    public class LotInqManagerImpl implements LotInqManager {

        @Autowired
        LotInqDAO lotInqDAO;

        @Autowired
        RunCardLotInqManager runCardLotInqManager;

        @Autowired
        LotAutoMonitorInqManager lotAutoMonitorInqManager;

        @Autowired
        LotAttributeQueryManager lotAttributeQueryManager;

        @Autowired
        PollutionLevelContextValueManager pollutionLevelContextValueManager;

        @Autowired
        ProcessOperationDescContextValueManager operationDescContextValueManager;

        @Autowired
        ResequenceContextValueManager resequenceContextValueManager;

        @Autowired
        CarrierInqManager carrierInqManager;

        @Autowired
        EquipmentInqManager equipmentInqManager;

        @Autowired
        ProductInqManager productInqManager;

        @Autowired
        ProcessInqManager processInqManager;

        @Autowired
        OperationInqManager operationInqManager;

        @Autowired
        ReticleInqManager reticleInqManager;

        @Autowired
        ReticleFamilyInqManager reticleFamilyInqManager;

        @Autowired
        DiffBatchQueryManager diffBatchQueryManager;

        @Autowired
        RecipeContextManager recipeContextManager;

        @Autowired
        CarrierManager carrierManager;

        @Autowired
        LocationInqManager locationInqManager;

        @Override
        public String getLotId(Long lotRrn) {
            return lotInqDAO.getLotId(lotRrn);
        }

        @Override
        public Long getLotRrn(String lotId) {
            return lotInqDAO.getLotRrn(lotId);
        }

        @Override
        public Lot getLot(String lotId) {
            Lot lot = lotInqDAO.getLot(lotId);
            return fillLotInfo(lot);
        }

        @Override
        public Lot getLot(Long lotRrn) {
            Lot lot = lotInqDAO.getLot(lotRrn);
            return fillLotInfo(lot);
        }

        @Override
        public List<Lot> getLotListByCarrierRrn(Long carrierRrn) {
            List<Lot> lotList = lotInqDAO.getLotListByCarrierRrn(carrierRrn);
            for (Lot lot : lotList) {
                lot = fillLotInfo(lot);
            }
            return lotList;
        }

        @Override
        public List<Lot> getLotListByCarrierId(String carrierId) {
            Long carrierRrn = carrierInqManager.getCarrierRrn(carrierId);
            if (Objects.nonNull(carrierRrn) && carrierRrn.longValue() > 0) {
                return this.getLotListByCarrierRrn(carrierRrn);
            }
            return new ArrayList<>();
        }

        @Override
        public List<Lot> getLotListByJobRrn(Long jobRrn) {
            List<Lot> lotList = lotInqDAO.getLotListByJobRrn(jobRrn);
            for (Lot lot : lotList) {
                lot = fillLotInfo(lot);
            }
            return lotList;
        }

        @Override
        public List<Lot> getLotListByEqptRrn(Long eqptRrn) {
            List<Lot> lotList = lotInqDAO.getLotListByEqptRrn(eqptRrn);
            for (Lot lot : lotList) {
                lot = fillLotInfo(lot);
            }
            return lotList;
        }

        @Override
        public String getLotStatus(Long lotRrn) {
            return lotInqDAO.getLotStatus(lotRrn);
        }

        @Override
        public Lot fillLotSpec(Lot lot) {
            if (LotInfoConstants.SysType.isRunCardLot(lot.getSysType())) {
                return runCardLotInqManager.fillLotRunCardSpec(lot);
            } else if (LotInfoConstants.SysType.isAutoMonitorLot(lot.getSysType())) {
                return lotAutoMonitorInqManager.fillLotAutoMonitorSpec(lot);
            } else {
                return this.fillLotProcessSpec(lot);
            }
        }

        @Override
        public String checkLotCarrierType(String lotId, String actionPoint) {
            Lot lot = getLot(lotId);
            String message = StringUtils.EMPTY;
            if (lot != null && lot.getLotRrn() > 0) {
                Carrier carrier = carrierInqManager.getCarrier(lot.getCarrierRrn());
                String targetCarrierType = carrierManager.getTargetCarrierTypeByActionPoint(actionPoint, lot);
                if (StringUtils.isBlank(targetCarrierType)) {
                    message = I18nUtils.getMessage(MessageIdList.PROCESS_CARRIER_MUST_EMPTY,
                                                   "Process location for {} cannot be empty!", lotId);
                }
                if (!StringUtils.equalsIgnoreCase(carrier.getObjectSubtype(), targetCarrierType) &&
                        lot.getAutoSplitMergFlag() != 1) {
                    message = I18nUtils.getMessage(MessageIdList.PROCESS_NOT_MATCH_CASSETTE,
                                                   "Cassette type for {} not match,Please exchange Cassette!", lotId);
                }
            }
            return message;
        }

        /**
         * 填充normal lot的context value设定内容
         *
         * @author finatice.yang
         * @date 2021/12/20
         * @version 6.0.0
         **/
        private Lot fillLotProcessSpec(Lot lot) {
            String flowSeq = resequenceContextValueManager
                    .getFlowSeq(lot.getProcessRrn(), lot.getProcessVersion(), lot.getOperationRrn(), lot.getRouteRrn());
            lot.setFlowSeq(StringUtils.isNotBlank(lot.getFlowSeq()) ? lot.getFlowSeq() : flowSeq);

            lot.setStageId(lotAttributeQueryManager.getStageId(lot));
            lot.setWorkArea(lotAttributeQueryManager.getWorkArea(lot));
            lot.setStepType(lotAttributeQueryManager.getOperationType(lot));
            lot.setProcessLocationAfter(lotAttributeQueryManager.getProcessLocation(lot));
            lot.setPollutionLevelAfter(pollutionLevelContextValueManager.getPollutionLevelByLot(lot));

            String operationDesc = operationDescContextValueManager
                    .getOperationDesc(lot.getProductRrn(), lot.getProcessRrn(), lot.getProcessVersion(), lot.getRouteRrn(),
                                      lot.getOperationRrn());
            lot.setOperationDesc(operationDesc);

            String recipeId = StringUtils.EMPTY;
            long recipeLogicalRrn = lot.getRecipeLogicalRrn();
            if (LotStatus.isProcessingStatus(lot.getLotStatus())) {
                recipeId = lot.getRecipePhysicalId();
            } else {
                String recipeString = recipeContextManager.getRecipeString(lot);
                recipeId = recipeContextManager.parseRecipeId(recipeString);
            }
            lot.setRecipeId(recipeId);

            return lot;
        }

        /**
         * 内部填充lot operation设置数据方法
         *
         * @author finatice.yang
         * @date 2021/12/20
         * @version 6.0.0
         **/
        private Lot fillLotInfo(Lot lot) {
            if (Objects.isNull(lot)) {
                return null;
            }
            lot = lotInqDAO.getLotExt(lot);
            fillLotAttribute(lot);

            return fillLotSpec(lot);
        }

        private void fillLotAttribute(Lot lot) {
            Carrier carrier = carrierInqManager.getCarrier(lot.getCarrierRrn());
            if (Objects.nonNull(carrier)) {
                lot.setCarrierId(carrier.getInstanceId());
                lot.setCarrierMapRrn(carrier.getCarrierMapRrn());
            }

            lot.setEqptID(equipmentInqManager.getEquipmentId(lot.getEqptRrn()));

            Item product = productInqManager.getProduct(lot.getProductRrn());
            if (Objects.nonNull(product)) {
                lot.setProductId(product.getInstanceId());
                lot.setProductType(product.getObjectType());
            }

            lot.setProcessId(processInqManager.getProcessId(lot.getProcessRrn()));

            lot.setRouteRrn(Long.valueOf(WipUtils.getRouteByProcessStepVersion(lot.getProcessStepVersion())[0]));
            lot.setRouteId(WipUtils.getRouteByProcessStepVersion(lot.getProcessStepIdVersion())[0]);

            lot.setOperationId(operationInqManager.getOperationId(lot.getOperationRrn()));
            lot.setNextOperationId1(operationInqManager.getOperationId(lot.getNextOperationRrn1()));
            lot.setPrevOperation(operationInqManager.getOperationId(lot.getPrevOperationRrn()));

            lot.setReticleId(reticleInqManager.getReticleId(lot.getReticleRrn()));
            lot.setReticleGroupId(reticleFamilyInqManager.getReticleFamilyId(lot.getReticleGroupRrn()));

            if (StringUtils.equalsIgnoreCase(LotStatus.BANKED, lot.getLotStatus()) ||
                    StringUtils.equalsIgnoreCase(LotStatus.OUTSOURCING, lot.getLotStatus())) {
                lot.setPrevFlowSeq(resequenceContextValueManager
                                           .getFlowSeq(lot.getProcessRrn(), lot.getProcessVersion(), lot.getRouteRrn(),
                                                       lot.getPrevOperationRrn()));
                lot.setPrevOperationDesc(operationDescContextValueManager
                                                 .getOperationDesc(lot.getProductRrn(), lot.getProcessRrn(),
                                                                   lot.getProcessVersion(), lot.getRouteRrn(),
                                                                   lot.getPrevOperationRrn()));
                lot.setOperationId(operationInqManager.getOperationId(lot.getPrevOperationRrn()));
            }
            // 兼容查询 equipmentId 和 locationId
            lot.setLocation(locationInqManager.getLocationIdOrEquipmentId(lot.getLocationRrn()));
            lot.setpLocation(locationInqManager.getLocationIdOrEquipmentId(lot.getpLocationRrn()));

            lot.setBatchId(diffBatchQueryManager.getLotBatchId(lot.getLotRrn()));
        }

    }