# DiffBatchQueryServiceImpl.java

    package com.mycim.server.wip.service;

    import com.alipay.sofa.runtime.api.annotation.SofaService;
    import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.wip.manager.ArgRecipeQueryManager;
    import com.mycim.server.wip.manager.DiffBatchQueryManager;
    import com.mycim.server.wip.manager.LotQueryManager;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.prp.ArgRecipeInfo;
    import com.mycim.valueobject.wip.BatchLotStore;
    import com.mycim.valueobject.wip.EqpRcpDiffBatchSetupInfo;
    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.*;

    /**
     * @author finatice.yang
     * @date 2021/6/30
     **/
    @Service
    @SofaService(interfaceType = DiffBatchQueryService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
    public class DiffBatchQueryServiceImpl implements DiffBatchQueryService {

        @Autowired
        DiffBatchQueryManager diffBatchQueryManager;

        @Autowired
        LotQueryManager lotQueryManager;

        @Autowired
        ArgRecipeQueryManager argRecipeQueryManager;

        @Override
        public List<EqpRcpDiffBatchSetupInfo> getEqpRecipeDiffBatchSet(Long eqptRrn) {
            return diffBatchQueryManager.getEqpRecipeDiffBatchSet(eqptRrn);
        }

        @Override
        public Page getEqpRcpBatchSetupHistory(Map<String, Object> condition, Page page) {
            return diffBatchQueryManager.getEqpRcpBatchSetupHistory(condition, page);
        }

        @Override
        public String getLotArgRecipe(Long lotRrn, Long eqptRrn, String recipeId) {
            List<BatchLotStore> batchLotStoreList = getBatchStoreListByLot(lotRrn);

            return getArgRecipeId(batchLotStoreList,eqptRrn,recipeId);
        }

        @Override
        public String getArgRecipeId(List<BatchLotStore> batchLotStoreList, Long eqptRrn, String recipeId) {
            Integer allQty = NumberUtils.INTEGER_ZERO;
            for (BatchLotStore batchLotStore : batchLotStoreList) {
                Lot lot = lotQueryManager.getLot(batchLotStore.getLotRrn());
                allQty = allQty.intValue() + lot.getInt_qty1();
            }

            ArgRecipeInfo condition = new ArgRecipeInfo();
            condition.setEqptRrn(eqptRrn);
            condition.setFlowRecipeId(recipeId);
            List<ArgRecipeInfo> list = argRecipeQueryManager.getArgInfoList(condition);
            if(CollectionUtils.isEmpty(list)) {
                return recipeId;
            }

            String argRecipeId = StringUtils.EMPTY;
            for (ArgRecipeInfo argInfo : list) {
                if (isWithinScope(allQty, argInfo.getLowerWaferCount(), argInfo.getUpperWaferCount())) {
                    argRecipeId = argInfo.getArgRecipeId();
                    break;
                }
            }
            Assert.state(StringUtils.isNotBlank(argRecipeId),
                         Errors.create().key(MessageIdList.BATCH_CHECKWAFERCOUNT).build());

            return argRecipeId;
        }

        /**
         * @param dispatchWaferQty
         * @param argLowerWaferCount
         * @param argUpperWaferCount
         * @return
         */
        private boolean isWithinScope(long dispatchWaferQty, Integer argLowerWaferCount, Integer argUpperWaferCount) {
            boolean isInLine = false;

            // TODO Maybe NullPointerException
            if (argLowerWaferCount == null && dispatchWaferQty <= argUpperWaferCount) {
                isInLine = true;
            } else if (argUpperWaferCount == null && dispatchWaferQty > argLowerWaferCount) {
                isInLine = true;
            } else if (dispatchWaferQty > argLowerWaferCount && dispatchWaferQty <= argUpperWaferCount) {
                isInLine = true;
            }

            return isInLine;
        }

        @Override
        public List<Map<String, Object>> getBatchLotStoreHistory(BatchLotStore storeCondition, Boolean showAll) {
            return diffBatchQueryManager.getBatchLotStoreHistory(storeCondition, showAll);
        }

        @Override
        public List<BatchLotStore> getBatchStoreListByLot(Long lotRrn) {
            return diffBatchQueryManager.getBatchStoreListByLot(lotRrn);
        }

        @Override
        public List<BatchLotStore> getBatchStoreListByBatchId(String batchId) {
            return diffBatchQueryManager.getBatchStoreListByBatchId(batchId);
        }

        @Override
        public List<Lot> getBatchLotList(Long lotRrn) {
            return diffBatchQueryManager.getBatchLotList(lotRrn);
        }

        @Override
        public Boolean checkLotInBatch(Long lotRrn) {
            return diffBatchQueryManager.checkLotInBatch(lotRrn);
        }

        @Override
        public Boolean checkLotIsBatchMonitorLot(Long lotRrn) {
            return diffBatchQueryManager.checkLotIsBatchMonitorLot(lotRrn);
        }

        @Override
        public Boolean checkLotIsBatchMonitorLot(Long lotRrn, Equipment equipment) {
            return diffBatchQueryManager.checkLotIsBatchMonitorLot(lotRrn, equipment);
        }

        @Override
        public String getLotBatchId(Long lotRrn) {
            return diffBatchQueryManager.getLotBatchId(lotRrn);
        }

        @Override
        public String buildBatchId(String prefix) {
            return diffBatchQueryManager.buildBatchId(prefix);
        }

    }