自动化立体仓库 - WMS系统
1.双伸空托盘入库规则完善
2.完善双伸只找一边的问题
3.完善重新分配库位问题
1个文件已添加
6个文件已修改
1053 ■■■■ 已修改文件
src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java 261 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/service/impl/BasCrnpServiceImpl.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/service/CommonService.java 294 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/zy/api/service/impl/WcsApiServiceImplTest.java 265 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/zy/asrs/service/impl/BasCrnpServiceImplTest.java 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/zy/common/service/CommonServiceDoubleExtensionSameGoodsTest.java 143 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/zy/common/service/CommonServiceLocTypeStrategyTest.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
@@ -24,15 +24,12 @@
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.io.IOException;
import java.math.BigDecimal;
@@ -59,8 +56,18 @@
    /** 三方接口统计:本系统调用 WCS 的 namespace 约定 */
    private static final String NS_WMS_TO_WCS = "本系统请求WCS";
    private static final String REASSIGN_CRN_LOCK_KEY_PREFIX = "wcs:reassign:inbound:crn:";
    private static final long REASSIGN_CRN_LOCK_SECONDS = 180L;
    private static final String EMPTY_PALLET_MATNR = "emptyPallet";
    private static final int EMPTY_PALLET_REASSIGN_TARGET_LEVEL = 8;
    private static final short EMPTY_PALLET_REASSIGN_LOC_TYPE1 = 3;
    private static final short EMPTY_PALLET_REASSIGN_LOC_TYPE2 = 0;
    private static class ReassignCrnPool {
        private final List<Integer> crnNos;
        private ReassignCrnPool(List<Integer> crnNos) {
            this.crnNos = crnNos;
        }
    }
    @Autowired
    private LocMastService locMastService;
@@ -107,10 +114,6 @@
    private BasCrnpService basCrnpService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private RowLastnoService rowLastnoService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OutboundBatchSeqReleaseGuard outboundBatchSeqReleaseGuard;
@@ -844,28 +847,37 @@
            return R.error("当前目标库位不存在");
        }
        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc);
        List<Integer> areaOrder = buildReassignAreaOrder(wrkMast, currentLoc);
        if (Cools.isEmpty(areaOrder)) {
            return R.error("无法确定任务所属库区");
        boolean emptyPallet = isReassignEmptyPallet(wrkMast);
        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc, emptyPallet);
        BasDevp sourceStation = basDevpService.selectById(wrkMast.getSourceStaNo());
        if (Cools.isEmpty(sourceStation)) {
            return R.error("源站未配置入库优先池");
        }
        List<ReassignCrnPool> poolOrder;
        try {
            poolOrder = buildReassignCrnPoolOrder(sourceStation, wrkMast.getCrnNo());
        } catch (CoolException e) {
            return R.error(e.getMessage());
        }
        if (Cools.isEmpty(poolOrder)) {
            return R.error("源站未配置入库优先池");
        }
        StartupDto startupDto = null;
        Integer preferredArea = null;
        for (Integer area : areaOrder) {
            List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(area, wrkMast.getCrnNo());
        Integer targetLevel = resolveReassignTargetLevel(emptyPallet);
        for (ReassignCrnPool pool : poolOrder) {
            List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(pool.crnNos, wrkMast.getCrnNo());
            if (candidateCrnNos.isEmpty()) {
                continue;
            }
            startupDto = commonService.findRun2InboundLocByCandidateCrnNos(
                    wrkMast.getSourceStaNo(), wrkMast.getIoType(), area, candidateCrnNos, locTypeDto);
                    wrkMast.getSourceStaNo(), wrkMast.getIoType(), candidateCrnNos, locTypeDto, targetLevel);
            if (startupDto != null && !Cools.isEmpty(startupDto.getLocNo())) {
                preferredArea = area;
                break;
            }
        }
        if (startupDto == null || Cools.isEmpty(startupDto.getLocNo())) {
            return R.error("当前库区没有可重新分配的空库位");
            return R.error("当前优先池没有可重新分配的空库位");
        }
        LocMast targetLoc = locMastService.selectById(startupDto.getLocNo());
@@ -880,7 +892,6 @@
        updateReassignTargetLoc(targetLoc, wrkMast, currentLoc, now);
        updateReassignWorkMast(wrkMast, startupDto, now);
        releaseOldReservedLocIfNeeded(currentLoc, targetLoc.getLocNo(), now);
        lockReassignedCrnAfterCommit(preferredArea, targetLoc.getCrnNo(), wrkMast.getWrkNo());
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("locNo", Utils.WMSLocToWCSLoc(targetLoc.getLocNo()));
@@ -948,115 +959,102 @@
        return null;
    }
    private Integer resolveReassignArea(WrkMast wrkMast, LocMast currentLoc) {
        List<Integer> stationAreas = Utils.getStationStorageAreas(wrkMast.getSourceStaNo());
        if (!Cools.isEmpty(stationAreas)) {
            for (Integer area : stationAreas) {
                if (belongsToArea(area, wrkMast.getCrnNo(), currentLoc)) {
                    return area;
                }
            }
    private List<ReassignCrnPool> buildReassignCrnPoolOrder(BasDevp sourceStation, Integer currentCrnNo) {
        List<Integer> firstPoolCrnNos = Utils.distinctCrnNos(sourceStation.getInFirstCrnCsv());
        List<Integer> secondPoolCrnNos = excludeReassignPoolCrnNos(
                Utils.distinctCrnNos(sourceStation.getInSecondCrnCsv()), firstPoolCrnNos);
        if (Cools.isEmpty(firstPoolCrnNos) && Cools.isEmpty(secondPoolCrnNos)) {
            throw new CoolException("源站未配置入库优先池");
        }
        Integer fallbackArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
        if (fallbackArea != null) {
            return fallbackArea;
        boolean inFirstPool = firstPoolCrnNos.contains(currentCrnNo);
        boolean inSecondPool = secondPoolCrnNos.contains(currentCrnNo);
        if (!inFirstPool && !inSecondPool) {
            throw new CoolException("当前任务堆垛机未配置在源站入库优先池");
        }
        return Utils.getStationStorageArea(wrkMast.getSourceStaNo());
        List<ReassignCrnPool> poolOrder = new ArrayList<>();
        if (inFirstPool) {
            poolOrder.add(new ReassignCrnPool(firstPoolCrnNos));
            poolOrder.add(new ReassignCrnPool(secondPoolCrnNos));
            return poolOrder;
        }
        poolOrder.add(new ReassignCrnPool(secondPoolCrnNos));
        poolOrder.add(new ReassignCrnPool(firstPoolCrnNos));
        return poolOrder;
    }
    private Integer findAreaByCurrentTask(Integer currentCrnNo, LocMast currentLoc) {
        for (int area = 1; area <= 3; area++) {
            if (belongsToArea(area, currentCrnNo, currentLoc)) {
                return area;
    private List<Integer> excludeReassignPoolCrnNos(List<Integer> crnNos, List<Integer> excludedCrnNos) {
        List<Integer> result = new ArrayList<>();
        if (Cools.isEmpty(crnNos)) {
            return result;
        }
        LinkedHashSet<Integer> excluded = new LinkedHashSet<>(Utils.distinctCrnNos(excludedCrnNos));
        for (Integer crnNo : Utils.distinctCrnNos(crnNos)) {
            if (crnNo == null || excluded.contains(crnNo)) {
                continue;
            }
            result.add(crnNo);
        }
        return result;
    }
    private List<Integer> buildReassignCandidateCrnNos(List<Integer> poolCrnNos, Integer currentCrnNo) {
        return buildReassignCrnSearchOrder(poolCrnNos, currentCrnNo);
    }
    private List<Integer> buildReassignCrnSearchOrder(List<Integer> poolCrnNos, Integer currentCrnNo) {
        List<Integer> orderedCrnNos = Utils.distinctCrnNos(poolCrnNos);
        Collections.sort(orderedCrnNos);
        if (Cools.isEmpty(orderedCrnNos) || currentCrnNo == null) {
            return orderedCrnNos;
        }
        List<Integer> searchOrder = new ArrayList<>();
        for (int index = orderedCrnNos.size() - 1; index >= 0; index--) {
            Integer crnNo = orderedCrnNos.get(index);
            if (crnNo == null || crnNo >= currentCrnNo) {
                continue;
            }
            searchOrder.add(crnNo);
        }
        for (int index = orderedCrnNos.size() - 1; index >= 0; index--) {
            Integer crnNo = orderedCrnNos.get(index);
            if (crnNo == null || crnNo <= 0 || crnNo.equals(currentCrnNo) || crnNo < currentCrnNo) {
                continue;
            }
            searchOrder.add(crnNo);
        }
        return searchOrder;
    }
    private Integer resolveReassignTargetLevel(boolean emptyPallet) {
        if (emptyPallet) {
            return EMPTY_PALLET_REASSIGN_TARGET_LEVEL;
        }
        return null;
    }
    private List<Integer> buildReassignAreaOrder(WrkMast wrkMast, LocMast currentLoc) {
        LinkedHashSet<Integer> areaOrder = new LinkedHashSet<>();
        List<Integer> stationAreas = Utils.getStationStorageAreas(wrkMast.getSourceStaNo());
        if (!Cools.isEmpty(stationAreas)) {
            areaOrder.addAll(stationAreas);
    private boolean isReassignEmptyPallet(WrkMast wrkMast) {
        if (wrkMast == null || wrkMast.getWrkNo() == null) {
            return false;
        }
        Integer currentArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
        if (currentArea != null) {
            areaOrder.add(currentArea);
        List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
        if (Cools.isEmpty(wrkDetls)) {
            return false;
        }
        if (areaOrder.isEmpty()) {
            Integer resolvedArea = resolveReassignArea(wrkMast, currentLoc);
            if (resolvedArea != null) {
                areaOrder.add(resolvedArea);
        for (WrkDetl wrkDetl : wrkDetls) {
            if (wrkDetl != null && EMPTY_PALLET_MATNR.equalsIgnoreCase(String.valueOf(wrkDetl.getMatnr()).trim())) {
                return true;
            }
        }
        return new ArrayList<>(areaOrder);
        return false;
    }
    private boolean belongsToArea(Integer area, Integer currentCrnNo, LocMast currentLoc) {
        if (area == null || area <= 0) {
            return false;
        }
        RowLastno areaRowLastno = rowLastnoService.selectById(area);
        if (areaRowLastno == null) {
            return false;
        }
        Integer startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
        Integer endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
        if (currentCrnNo != null && currentCrnNo >= startCrnNo && currentCrnNo <= endCrnNo) {
            return true;
        }
        Integer row = currentLoc == null ? null : currentLoc.getRow1();
        Integer startRow = areaRowLastno.getsRow();
        Integer endRow = areaRowLastno.geteRow();
        return row != null && startRow != null && endRow != null && row >= startRow && row <= endRow;
    }
    private List<Integer> buildReassignCandidateCrnNos(Integer area, Integer currentCrnNo) {
        RowLastno areaRowLastno = rowLastnoService.selectById(area);
        if (areaRowLastno == null) {
            throw new CoolException("未找到库区轮询规则");
        }
        int startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
        int endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
        if (currentCrnNo == null || currentCrnNo < startCrnNo || currentCrnNo > endCrnNo) {
            throw new CoolException("当前任务堆垛机不在所属库区范围内");
        }
        List<Integer> candidateCrnNos = new ArrayList<>();
        for (int crnNo = currentCrnNo - 1; crnNo >= startCrnNo; crnNo--) {
            addUnlockedReassignCandidate(candidateCrnNos, area, crnNo);
        }
        for (int crnNo = endCrnNo; crnNo > currentCrnNo; crnNo--) {
            addUnlockedReassignCandidate(candidateCrnNos, area, crnNo);
        }
        return candidateCrnNos;
    }
    private void addUnlockedReassignCandidate(List<Integer> candidateCrnNos, Integer area, int crnNo) {
        if (isReassignCrnLocked(area, crnNo)) {
            log.info("skip locked reassign crane. area={}, crnNo={}, ttl={}s",
                    area, crnNo, redisUtil.getExpire(buildReassignCrnLockKey(area, crnNo)));
            return;
        }
        candidateCrnNos.add(crnNo);
    }
    private int resolveAreaStartCrnNo(RowLastno areaRowLastno) {
        if (areaRowLastno.getsCrnNo() != null && areaRowLastno.getsCrnNo() > 0) {
            return areaRowLastno.getsCrnNo();
        }
        return 1;
    }
    private int resolveAreaEndCrnNo(RowLastno areaRowLastno, int startCrnNo) {
        if (areaRowLastno.geteCrnNo() != null && areaRowLastno.geteCrnNo() >= startCrnNo) {
            return areaRowLastno.geteCrnNo();
        }
        int crnQty = areaRowLastno.getCrnQty() == null || areaRowLastno.getCrnQty() <= 0 ? 1 : areaRowLastno.getCrnQty();
        return startCrnNo + crnQty - 1;
    }
    private LocTypeDto buildReassignLocTypeDto(LocMast currentLoc) {
    private LocTypeDto buildReassignLocTypeDto(LocMast currentLoc, boolean emptyPallet) {
        LocTypeDto locTypeDto = new LocTypeDto();
        if (emptyPallet) {
            locTypeDto.setLocType1(EMPTY_PALLET_REASSIGN_LOC_TYPE1);
            locTypeDto.setLocType2(EMPTY_PALLET_REASSIGN_LOC_TYPE2);
            return locTypeDto;
        }
        if (currentLoc == null) {
            return locTypeDto;
        }
@@ -1122,43 +1120,6 @@
        if (!locMastService.updateById(currentLoc)) {
            throw new CoolException("释放原目标库位失败");
        }
    }
    private boolean isReassignCrnLocked(Integer area, Integer crnNo) {
        if (area == null || crnNo == null) {
            return false;
        }
        return redisUtil.hasKey(buildReassignCrnLockKey(area, crnNo));
    }
    private String buildReassignCrnLockKey(Integer area, Integer crnNo) {
        return REASSIGN_CRN_LOCK_KEY_PREFIX + area + ":" + crnNo;
    }
    private void lockReassignedCrnAfterCommit(Integer area, Integer crnNo, Integer wrkNo) {
        if (area == null || crnNo == null) {
            return;
        }
        Runnable action = () -> {
            String key = buildReassignCrnLockKey(area, crnNo);
            boolean locked = redisUtil.set(key, String.valueOf(wrkNo), REASSIGN_CRN_LOCK_SECONDS);
            if (!locked) {
                log.warn("failed to lock reassigned crane in redis. area={}, crnNo={}, wrkNo={}", area, crnNo, wrkNo);
                return;
            }
            log.info("locked reassigned crane in redis. area={}, crnNo={}, wrkNo={}, ttl={}s",
                    area, crnNo, wrkNo, REASSIGN_CRN_LOCK_SECONDS);
        };
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    action.run();
                }
            });
            return;
        }
        action.run();
    }
    /**
src/main/java/com/zy/asrs/service/impl/BasCrnpServiceImpl.java
@@ -31,10 +31,10 @@
        if (Cools.isEmpty(crnp)) {
            throw new CoolException(crnId + "号堆垛机不存在");
        }
        if ("N".equals(crnp.getInEnable())) {
        if (!isEnabled(crnp.getInEnable())) {
            throw new CoolException(crnId + "堆垛机不可入");
        }
        if ("N".equals(crnp.getOutEnable())) {
        if (!isEnabled(crnp.getOutEnable())) {
            throw new CoolException(crnId + "堆垛机不可出");
        }
        return crnp;
@@ -69,12 +69,12 @@
//                return false;
//            }
            if ("N".equals(crnp.getInEnable())) {
            if (!isEnabled(crnp.getInEnable())) {
                log.error("{}号堆垛机不可入", crnNo);
                return false;
            }
        } else {
            if ("N".equals(crnp.getOutEnable())) {
            if (!isEnabled(crnp.getOutEnable())) {
                log.error("{}号堆垛机不可出", crnNo);
                return false;
            }
@@ -82,4 +82,8 @@
        return true;
    }
    private boolean isEnabled(String flag) {
        return "Y".equalsIgnoreCase(flag);
    }
}
src/main/java/com/zy/common/service/CommonService.java
@@ -28,6 +28,7 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -224,12 +225,32 @@
    }
    /**
     * 供 6.15 重分配接口复用:按外部指定的堆垛机顺序,在指定库区内找新的入库位。
     * 供重分配接口复用:按外部指定的堆垛机顺序找新的入库位。
     *
     * 这里不推进 row_lastno 游标,只负责一次性的路径校验 + 设备校验 + 空库位搜索。
     */
    public StartupDto findRun2InboundLocByCandidateCrnNos(Integer sourceStaNo, Integer staDescId,
                                                          List<Integer> candidateCrnNos, LocTypeDto locTypeDto) {
        return findRun2InboundLocByCandidateCrnNos(sourceStaNo, staDescId, null, candidateCrnNos, locTypeDto, null);
    }
    public StartupDto findRun2InboundLocByCandidateCrnNos(Integer sourceStaNo, Integer staDescId,
                                                          List<Integer> candidateCrnNos, LocTypeDto locTypeDto,
                                                          Integer targetLev) {
        return findRun2InboundLocByCandidateCrnNos(sourceStaNo, staDescId, null, candidateCrnNos, locTypeDto, targetLev);
    }
    /**
     * 兼容旧调用:按外部指定的堆垛机顺序,在指定库区内找新的入库位。
     */
    public StartupDto findRun2InboundLocByCandidateCrnNos(Integer sourceStaNo, Integer staDescId, Integer preferredArea,
                                                          List<Integer> candidateCrnNos, LocTypeDto locTypeDto) {
        return findRun2InboundLocByCandidateCrnNos(sourceStaNo, staDescId, preferredArea, candidateCrnNos, locTypeDto, null);
    }
    private StartupDto findRun2InboundLocByCandidateCrnNos(Integer sourceStaNo, Integer staDescId, Integer preferredArea,
                                                           List<Integer> candidateCrnNos, LocTypeDto locTypeDto,
                                                           Integer targetLev) {
        if (sourceStaNo == null) {
            throw new CoolException("源站不能为空");
        }
@@ -253,12 +274,9 @@
        if (Cools.isEmpty(rowLastnoType)) {
            throw new CoolException("数据异常,请联系管理员===》库位规则类型未知");
        }
        if (rowLastnoType.getType() != 1 && rowLastnoType.getType() != 2) {
            throw new CoolException("当前仓库不支持重新分配入库位");
        }
        StartupDto startupDto = new StartupDto();
        LocMast locMast = findRun2EmptyLocByCrnNos(searchRowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                staDescId, sourceStaNo, startupDto, preferredArea, null, "reassign-inbound");
                staDescId, sourceStaNo, startupDto, preferredArea, null, "reassign-inbound", targetLev);
        if (Cools.isEmpty(locMast) || !"O".equals(locMast.getLocSts())) {
            return null;
        }
@@ -1569,6 +1587,22 @@
    private LocMast findRun2EmptyLocByCrnNos(RowLastno rowLastno, RowLastnoType rowLastnoType, List<Integer> candidateCrnNos,
                                             LocTypeDto locTypeDto, Integer staDescId, Integer sourceStaNo, StartupDto startupDto,
                                             Integer preferredArea, FindLocNoAttributeVo findLocNoAttributeVo, String stage, boolean routeRequired) {
        return findRun2EmptyLocByCrnNos(rowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                staDescId, sourceStaNo, startupDto, preferredArea, findLocNoAttributeVo, stage, routeRequired, null);
    }
    private LocMast findRun2EmptyLocByCrnNos(RowLastno rowLastno, RowLastnoType rowLastnoType, List<Integer> candidateCrnNos,
                                             LocTypeDto locTypeDto, Integer staDescId, Integer sourceStaNo, StartupDto startupDto,
                                             Integer preferredArea, FindLocNoAttributeVo findLocNoAttributeVo, String stage,
                                             Integer targetLev) {
        return findRun2EmptyLocByCrnNos(rowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                staDescId, sourceStaNo, startupDto, preferredArea, findLocNoAttributeVo, stage, true, targetLev);
    }
    private LocMast findRun2EmptyLocByCrnNos(RowLastno rowLastno, RowLastnoType rowLastnoType, List<Integer> candidateCrnNos,
                                             LocTypeDto locTypeDto, Integer staDescId, Integer sourceStaNo, StartupDto startupDto,
                                             Integer preferredArea, FindLocNoAttributeVo findLocNoAttributeVo, String stage,
                                             boolean routeRequired, Integer targetLev) {
        if (Cools.isEmpty(candidateCrnNos)) {
            log.warn("run2 skip empty candidate list. stage={}, sourceStaNo={}, preferredArea={}, spec={}",
                    stage, sourceStaNo, preferredArea, JSON.toJSONString(locTypeDto));
@@ -1580,7 +1614,7 @@
        List<Integer> locTypeBlockedCrns = new ArrayList<>();
        return findRun2EmptyLocByCrnNosRecursively(rowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                staDescId, sourceStaNo, startupDto, preferredArea, findLocNoAttributeVo, stage, routeRequired, 0,
                crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns);
                crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns, targetLev);
    }
    private LocMast findRun2EmptyLocByCrnNosRecursively(RowLastno rowLastno, RowLastnoType rowLastnoType,
@@ -1589,7 +1623,8 @@
                                                        Integer preferredArea, FindLocNoAttributeVo findLocNoAttributeVo,
                                                        String stage, boolean routeRequired, int index,
                                                        List<Integer> crnErrorCrns, List<Integer> routeBlockedCrns,
                                                        List<Integer> noEmptyCrns, List<Integer> locTypeBlockedCrns) {
                                                        List<Integer> noEmptyCrns, List<Integer> locTypeBlockedCrns,
                                                        Integer targetLev) {
        if (index >= candidateCrnNos.size()) {
            logRun2NoMatch(stage, sourceStaNo, preferredArea, candidateCrnNos, locTypeDto,
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns);
@@ -1600,18 +1635,18 @@
            crnErrorCrns.add(candidateCrnNo);
            return findRun2EmptyLocByCrnNosRecursively(rowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                    staDescId, sourceStaNo, startupDto, preferredArea, findLocNoAttributeVo, stage, routeRequired, index + 1,
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns);
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns, targetLev);
        }
        Integer targetStaNo = routeRequired ? resolveTargetStaNo(rowLastno, staDescId, sourceStaNo, candidateCrnNo) : null;
        if (routeRequired && Utils.BooleanWhsTypeSta(rowLastno, staDescId) && targetStaNo == null) {
            routeBlockedCrns.add(candidateCrnNo);
            return findRun2EmptyLocByCrnNosRecursively(rowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                    staDescId, sourceStaNo, startupDto, preferredArea, findLocNoAttributeVo, stage, routeRequired, index + 1,
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns);
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns, targetLev);
        }
        Integer preferredNearRow = getCrnStartRow(rowLastno, candidateCrnNo);
        LocMast candidateLoc = findConfiguredEmptyLocForCrn(rowLastno, rowLastnoType, candidateCrnNo,
                preferredNearRow, locTypeDto, findLocNoAttributeVo);
                preferredNearRow, locTypeDto, findLocNoAttributeVo, targetLev);
        if (Cools.isEmpty(candidateLoc)) {
            int availableLocCount = countAssignableLocForCrn(rowLastno, rowLastnoType, candidateCrnNo,
                    preferredNearRow == null ? 0 : preferredNearRow, locTypeDto);
@@ -1622,7 +1657,7 @@
            }
            return findRun2EmptyLocByCrnNosRecursively(rowLastno, rowLastnoType, candidateCrnNos, locTypeDto,
                    staDescId, sourceStaNo, startupDto, preferredArea, findLocNoAttributeVo, stage, routeRequired, index + 1,
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns);
                    crnErrorCrns, routeBlockedCrns, noEmptyCrns, locTypeBlockedCrns, targetLev);
        }
        if (targetStaNo != null) {
            startupDto.setStaNo(targetStaNo);
@@ -1907,6 +1942,10 @@
        return locTypeDto == null || VersionUtils.locMoveCheckLocTypeComplete(locMast, locTypeDto);
    }
    private boolean matchesTargetLev(LocMast locMast, Integer targetLev) {
        return targetLev == null || targetLev <= 0 || (locMast != null && Objects.equals(locMast.getLev1(), targetLev));
    }
    /**
     * 查询某一排上的所有空库位,并按单伸/双伸策略与频次排序。
     */
@@ -1924,6 +1963,13 @@
    private List<LocMast> findOpenLocsByRow(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer row,
                                            Integer crnNo, LocTypeDto locTypeDto, FindLocNoAttributeVo findLocNoAttributeVo,
                                            boolean singleExtension, boolean ignoreFreqType) {
        return findOpenLocsByRow(rowLastno, rowLastnoType, row, crnNo, locTypeDto, findLocNoAttributeVo,
                singleExtension, ignoreFreqType, null);
    }
    private List<LocMast> findOpenLocsByRow(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer row,
                                            Integer crnNo, LocTypeDto locTypeDto, FindLocNoAttributeVo findLocNoAttributeVo,
                                            boolean singleExtension, boolean ignoreFreqType, Integer targetLev) {
        List<LocMast> result = new ArrayList<LocMast>();
        if (row == null) {
            return result;
@@ -1933,11 +1979,14 @@
        if (crnNo != null) {
            wrapper.eq("crn_no", crnNo);
        }
        if (targetLev != null && targetLev > 0) {
            wrapper.eq("lev1", targetLev);
        }
        applyLocTypeFilters(wrapper, locTypeDto, true);
        List<LocMast> locMasts = locMastService.selectList(wrapper);
        List<LocMast> sortedLocMasts = sortLocCandidates(locMasts, findLocNoAttributeVo, ignoreFreqType);
        for (LocMast locMast : sortedLocMasts) {
            if (matchesLocType(locMast, locTypeDto)) {
            if (matchesLocType(locMast, locTypeDto) && matchesTargetLev(locMast, targetLev)) {
                result.add(locMast);
            }
        }
@@ -2046,26 +2095,27 @@
    /**
     * 双伸堆垛机同货优先:
     * 先找深库位中 standby1 相同且状态为 F 的货位,再检查其对应浅库位是否为空。
     * 先找深库位中同货且状态为 F 的货位,再检查其对应浅库位是否为空。
     * 空托盘请求则按 matnr=emptyPallet 识别,深位已有空托盘时优先返回对应浅位。
     */
    private LocMast findDoubleExtensionSameGoodsPreferredLoc(RowLastno rowLastno, RowLastnoType rowLastnoType,
                                                             Integer crnNo, CrnDepthRuleProfile profile,
                                                             LocTypeDto locTypeDto, FindLocNoAttributeVo findLocNoAttributeVo) {
        if (profile == null || !profile.isDoubleExtension() || findLocNoAttributeVo == null
                || Cools.isEmpty(findLocNoAttributeVo.getStandby1())) {
        if (profile == null || !profile.isDoubleExtension() || findLocNoAttributeVo == null) {
            return null;
        }
        boolean emptyPalletRequest = "emptyPallet".equalsIgnoreCase(findLocNoAttributeVo.getMatnr());
        String standby1 = findLocNoAttributeVo.getStandby1();
        LinkedHashSet<Integer> processedDeepRows = new LinkedHashSet<Integer>();
        for (Integer searchRow : profile.getSearchRows()) {
            if (searchRow == null || !profile.isDeepRow(searchRow) || !processedDeepRows.add(searchRow)) {
        if (!emptyPalletRequest && Cools.isEmpty(standby1)) {
            return null;
        }
        List<Integer> orderedShallowRows = orderDoubleExtensionShallowRows(rowLastno, rowLastnoType, crnNo, profile);
        for (Integer shallowRow : orderedShallowRows) {
            Integer deepRow = profile.getPairedDeepRow(shallowRow);
            if (deepRow == null) {
                continue;
            }
            Integer shallowRow = profile.getPairedShallowRow(searchRow);
            if (shallowRow == null) {
                continue;
            }
            List<LocMast> deepLocs = findOccupiedLocsByRow(searchRow, crnNo, findLocNoAttributeVo);
            List<LocMast> deepLocs = findOccupiedLocsByRow(deepRow, crnNo, findLocNoAttributeVo);
            if (Cools.isEmpty(deepLocs)) {
                continue;
            }
@@ -2079,9 +2129,14 @@
            if (Cools.isEmpty(deepLocNos)) {
                continue;
            }
            List<LocDetl> sameGoodsLocDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
                    .eq("standby1", standby1)
                    .in("loc_no", deepLocNos));
            EntityWrapper<LocDetl> detlWrapper = new EntityWrapper<LocDetl>();
            detlWrapper.in("loc_no", deepLocNos);
            if (emptyPalletRequest) {
                detlWrapper.eq("matnr", findLocNoAttributeVo.getMatnr());
            } else {
                detlWrapper.eq("standby1", standby1);
            }
            List<LocDetl> sameGoodsLocDetls = locDetlService.selectList(detlWrapper);
            if (Cools.isEmpty(sameGoodsLocDetls)) {
                continue;
            }
@@ -2107,6 +2162,115 @@
            }
        }
        return null;
    }
    /**
     * 双伸堆垛机按当前两侧负载排序浅位搜索顺序。
     *
     * 负载越低的浅/深配对越优先,避免长期只命中同一侧。
     */
    private List<Integer> orderDoubleExtensionShallowRows(RowLastno rowLastno, RowLastnoType rowLastnoType,
                                                          Integer crnNo, CrnDepthRuleProfile profile) {
        List<Integer> orderedShallowRows = new ArrayList<Integer>();
        if (profile == null || !profile.isDoubleExtension() || Cools.isEmpty(profile.getShallowRows())) {
            return orderedShallowRows;
        }
        orderedShallowRows.addAll(profile.getShallowRows());
        final Map<Integer, Integer> originalOrder = new HashMap<Integer, Integer>();
        for (int index = 0; index < orderedShallowRows.size(); index++) {
            originalOrder.put(orderedShallowRows.get(index), index);
        }
        final Map<Integer, Integer> pairLoadCache = new HashMap<Integer, Integer>();
        for (Integer shallowRow : orderedShallowRows) {
            pairLoadCache.put(shallowRow, countDoubleExtensionPairLoad(rowLastno, rowLastnoType, crnNo, profile, shallowRow));
        }
        orderedShallowRows.sort(Comparator
                .comparingInt((Integer row) -> pairLoadCache.getOrDefault(row, Integer.MAX_VALUE))
                .thenComparingInt(row -> originalOrder.getOrDefault(row, Integer.MAX_VALUE)));
        return rotateEqualLoadGroupsByCurrentRow(orderedShallowRows, rowLastno, pairLoadCache);
    }
    /**
     * 等载时,按当前排游标旋转组内顺序,避免长期固定命中同一侧深库位。
     */
    private List<Integer> rotateEqualLoadGroupsByCurrentRow(List<Integer> orderedRows, RowLastno rowLastno,
                                                            Map<Integer, Integer> pairLoadCache) {
        if (Cools.isEmpty(orderedRows) || rowLastno == null) {
            return orderedRows;
        }
        Integer currentRow = rowLastno.getCurrentRow();
        if (currentRow == null || currentRow <= 0) {
            return orderedRows;
        }
        List<Integer> rotatedRows = new ArrayList<Integer>(orderedRows.size());
        int index = 0;
        while (index < orderedRows.size()) {
            Integer currentLoad = pairLoadCache.get(orderedRows.get(index));
            int groupStart = index;
            while (index < orderedRows.size() && Objects.equals(pairLoadCache.get(orderedRows.get(index)), currentLoad)) {
                index++;
            }
            List<Integer> sameLoadRows = new ArrayList<Integer>(orderedRows.subList(groupStart, index));
            rotateShallowRowsByCurrentRow(sameLoadRows, currentRow);
            rotatedRows.addAll(sameLoadRows);
        }
        return rotatedRows;
    }
    private void rotateShallowRowsByCurrentRow(List<Integer> shallowRows, Integer currentRow) {
        if (Cools.isEmpty(shallowRows) || shallowRows.size() <= 1 || currentRow == null) {
            return;
        }
        int pivotIndex = -1;
        for (int i = 0; i < shallowRows.size(); i++) {
            Integer row = shallowRows.get(i);
            if (row != null && row > currentRow) {
                pivotIndex = i;
                break;
            }
        }
        if (pivotIndex <= 0) {
            return;
        }
        List<Integer> rotated = new ArrayList<Integer>(shallowRows.size());
        rotated.addAll(shallowRows.subList(pivotIndex, shallowRows.size()));
        rotated.addAll(shallowRows.subList(0, pivotIndex));
        shallowRows.clear();
        shallowRows.addAll(rotated);
    }
    /**
     * 统计双伸浅/深配对的当前已占用库位数量。
     */
    private int countDoubleExtensionPairLoad(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer crnNo,
                                             CrnDepthRuleProfile profile, Integer shallowRow) {
        if (profile == null || shallowRow == null) {
            return Integer.MAX_VALUE;
        }
        int count = countOccupiedLocsByRow(rowLastno, rowLastnoType, shallowRow, crnNo);
        Integer deepRow = profile.getPairedDeepRow(shallowRow);
        if (deepRow != null) {
            count += countOccupiedLocsByRow(rowLastno, rowLastnoType, deepRow, crnNo);
        }
        return count;
    }
    /**
     * 统计某一排当前已占用/预约的库存数量。
     *
     * 这里把 S/R/P/Q/D/X 都算作负载,避免批量入库时只看 F 导致刚预约出去的库位不参与均衡。
     */
    private int countOccupiedLocsByRow(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer row, Integer crnNo) {
        if (row == null) {
            return Integer.MAX_VALUE;
        }
        Wrapper<LocMast> wrapper = new EntityWrapper<LocMast>()
                .eq("row1", row)
                .in("loc_sts", Arrays.asList("F", "S", "R", "P", "Q", "D", "X"));
        if (crnNo != null) {
            wrapper.eq("crn_no", crnNo);
        }
        return locMastService.selectCount(wrapper);
    }
    /**
@@ -2143,7 +2307,13 @@
    private LocMast findPairAssignableLoc(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer crnNo,
                                          Integer shallowRow, Integer deepRow, LocTypeDto locTypeDto,
                                          FindLocNoAttributeVo findLocNoAttributeVo) {
        List<LocMast> shallowOpenLocs = findOpenLocsByRow(rowLastno, rowLastnoType, shallowRow, crnNo, locTypeDto, findLocNoAttributeVo, false);
        return findPairAssignableLoc(rowLastno, rowLastnoType, crnNo, shallowRow, deepRow, locTypeDto, findLocNoAttributeVo, null);
    }
    private LocMast findPairAssignableLoc(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer crnNo,
                                          Integer shallowRow, Integer deepRow, LocTypeDto locTypeDto,
                                          FindLocNoAttributeVo findLocNoAttributeVo, Integer targetLev) {
        List<LocMast> shallowOpenLocs = findOpenLocsByRow(rowLastno, rowLastnoType, shallowRow, crnNo, locTypeDto, findLocNoAttributeVo, false, false, targetLev);
        if (Cools.isEmpty(shallowOpenLocs)) {
            return null;
        }
@@ -2186,7 +2356,21 @@
    private LocMast findConfiguredEmptyLocForCrn(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer crnNo,
                                                 Integer preferredNearRow, LocTypeDto locTypeDto, FindLocNoAttributeVo findLocNoAttributeVo,
                                                 Integer targetLev) {
        return findConfiguredEmptyLocForCrn(rowLastno, rowLastnoType, crnNo, preferredNearRow, locTypeDto, findLocNoAttributeVo,
                false, targetLev);
    }
    private LocMast findConfiguredEmptyLocForCrn(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer crnNo,
                                                 Integer preferredNearRow, LocTypeDto locTypeDto, FindLocNoAttributeVo findLocNoAttributeVo,
                                                 boolean ignoreFreqType) {
        return findConfiguredEmptyLocForCrn(rowLastno, rowLastnoType, crnNo, preferredNearRow, locTypeDto, findLocNoAttributeVo,
                ignoreFreqType, null);
    }
    private LocMast findConfiguredEmptyLocForCrn(RowLastno rowLastno, RowLastnoType rowLastnoType, Integer crnNo,
                                                 Integer preferredNearRow, LocTypeDto locTypeDto, FindLocNoAttributeVo findLocNoAttributeVo,
                                                 boolean ignoreFreqType, Integer targetLev) {
        if (rowLastno == null || crnNo == null) {
            return null;
        }
@@ -2196,43 +2380,55 @@
        }
        LocMast sameGoodsPreferredLoc = findDoubleExtensionSameGoodsPreferredLoc(rowLastno, rowLastnoType, crnNo,
                profile, locTypeDto, findLocNoAttributeVo);
        if (!Cools.isEmpty(sameGoodsPreferredLoc)) {
        if (!Cools.isEmpty(sameGoodsPreferredLoc) && matchesTargetLev(sameGoodsPreferredLoc, targetLev)) {
            return sameGoodsPreferredLoc;
        }
        LinkedHashSet<Integer> processedShallowRows = new LinkedHashSet<Integer>();
        boolean singleExtension = profile.isSingleExtension();
        if (!singleExtension) {
            List<Integer> orderedShallowRows = orderDoubleExtensionShallowRows(rowLastno, rowLastnoType, crnNo, profile);
            for (Integer shallowRow : orderedShallowRows) {
                if (shallowRow == null) {
                    continue;
                }
                LocMast candidateLoc = findPairAssignableLoc(rowLastno, rowLastnoType, crnNo, shallowRow,
                        profile.getPairedDeepRow(shallowRow), locTypeDto, findLocNoAttributeVo, targetLev);
                if (!Cools.isEmpty(candidateLoc)) {
                    return candidateLoc;
                }
            }
            return null;
        }
        LinkedHashSet<Integer> processedShallowRows = new LinkedHashSet<Integer>();
        for (Integer searchRow : profile.getSearchRows()) {
            if (searchRow == null) {
                continue;
            }
            if (!singleExtension) {
                if (profile.isShallowRow(searchRow)) {
                    if (!processedShallowRows.add(searchRow)) {
            if (profile.isShallowRow(searchRow)) {
                if (!processedShallowRows.add(searchRow)) {
                    continue;
                }
                LocMast candidateLoc = findPairAssignableLoc(rowLastno, rowLastnoType, crnNo, searchRow,
                        profile.getPairedDeepRow(searchRow), locTypeDto, findLocNoAttributeVo, targetLev);
                if (!Cools.isEmpty(candidateLoc)) {
                    return candidateLoc;
                }
                continue;
            }
            if (profile.isDeepRow(searchRow)) {
                Integer shallowRow = profile.getPairedShallowRow(searchRow);
                if (shallowRow != null) {
                    if (!processedShallowRows.add(shallowRow)) {
                        continue;
                    }
                    LocMast candidateLoc = findPairAssignableLoc(rowLastno, rowLastnoType, crnNo, searchRow,
                            profile.getPairedDeepRow(searchRow), locTypeDto, findLocNoAttributeVo);
                    LocMast candidateLoc = findPairAssignableLoc(rowLastno, rowLastnoType, crnNo, shallowRow,
                            searchRow, locTypeDto, findLocNoAttributeVo, targetLev);
                    if (!Cools.isEmpty(candidateLoc)) {
                        return candidateLoc;
                    }
                    continue;
                }
                if (profile.isDeepRow(searchRow)) {
                    Integer shallowRow = profile.getPairedShallowRow(searchRow);
                    if (shallowRow != null) {
                        if (!processedShallowRows.add(shallowRow)) {
                            continue;
                        }
                        LocMast candidateLoc = findPairAssignableLoc(rowLastno, rowLastnoType, crnNo, shallowRow,
                                searchRow, locTypeDto, findLocNoAttributeVo);
                        if (!Cools.isEmpty(candidateLoc)) {
                            return candidateLoc;
                        }
                        continue;
                    }
                }
            }
            List<LocMast> locMasts = findOpenLocsByRow(rowLastno, rowLastnoType, searchRow, crnNo, locTypeDto, findLocNoAttributeVo, singleExtension, ignoreFreqType);
            List<LocMast> locMasts = findOpenLocsByRow(rowLastno, rowLastnoType, searchRow, crnNo, locTypeDto, findLocNoAttributeVo, singleExtension, ignoreFreqType, targetLev);
            if (!Cools.isEmpty(locMasts)) {
                return locMasts.get(0);
            }
src/test/java/com/zy/api/service/impl/WcsApiServiceImplTest.java
@@ -1,18 +1,38 @@
package com.zy.api.service.impl;
import com.zy.api.controller.params.ReassignLocParams;
import com.zy.api.controller.params.ReceviceTaskParams;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkDetl;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WorkService;
import com.zy.asrs.service.WrkDetlService;
import com.zy.asrs.service.WrkMastService;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -25,6 +45,18 @@
    @Mock
    private WorkService workService;
    @Mock
    private WrkDetlService wrkDetlService;
    @Mock
    private LocMastService locMastService;
    @Mock
    private BasDevpService basDevpService;
    @Mock
    private CommonService commonService;
    private WcsApiServiceImpl service;
    @BeforeEach
@@ -32,6 +64,10 @@
        service = new WcsApiServiceImpl();
        ReflectionTestUtils.setField(service, "wrkMastService", wrkMastService);
        ReflectionTestUtils.setField(service, "workService", workService);
        ReflectionTestUtils.setField(service, "wrkDetlService", wrkDetlService);
        ReflectionTestUtils.setField(service, "locMastService", locMastService);
        ReflectionTestUtils.setField(service, "basDevpService", basDevpService);
        ReflectionTestUtils.setField(service, "commonService", commonService);
    }
    @Test
@@ -64,6 +100,174 @@
        verify(wrkMastService).updateById(mast);
    }
    @Test
    @SuppressWarnings("unchecked")
    void buildReassignCrnSearchOrder_currentThree_shouldSearchSmallerThenWrapDescending() {
        List<Integer> result = ReflectionTestUtils.invokeMethod(
                service, "buildReassignCrnSearchOrder", Arrays.asList(1, 2, 3, 4, 5, 6), 3);
        assertEquals(Arrays.asList(2, 1, 6, 5, 4), result);
    }
    @Test
    @SuppressWarnings("unchecked")
    void buildReassignCrnSearchOrder_currentOne_shouldWrapFromMax() {
        List<Integer> result = ReflectionTestUtils.invokeMethod(
                service, "buildReassignCrnSearchOrder", Arrays.asList(1, 2, 3, 4, 5, 6), 1);
        assertEquals(Arrays.asList(6, 5, 4, 3, 2), result);
    }
    @Test
    @SuppressWarnings("unchecked")
    void buildReassignCrnSearchOrder_currentSix_shouldSearchDescending() {
        List<Integer> result = ReflectionTestUtils.invokeMethod(
                service, "buildReassignCrnSearchOrder", Arrays.asList(1, 2, 3, 4, 5, 6), 6);
        assertEquals(Arrays.asList(5, 4, 3, 2, 1), result);
    }
    @Test
    @SuppressWarnings("unchecked")
    void buildReassignCrnSearchOrder_nonContinuousPool_shouldKeepDescendingCircularOrder() {
        List<Integer> result = ReflectionTestUtils.invokeMethod(
                service, "buildReassignCrnSearchOrder", Arrays.asList(1, 3, 6), 3);
        assertEquals(Arrays.asList(1, 6), result);
    }
    @Test
    @SuppressWarnings("unchecked")
    void buildReassignCrnSearchOrder_singleCurrentCrane_shouldReturnEmpty() {
        List<Integer> result = ReflectionTestUtils.invokeMethod(
                service, "buildReassignCrnSearchOrder", Collections.singletonList(3), 3);
        assertEquals(Collections.emptyList(), result);
    }
    @Test
    @SuppressWarnings("unchecked")
    void buildReassignCrnSearchOrder_currentNotInPool_shouldStartFromLargestSmallerCrane() {
        List<Integer> result = ReflectionTestUtils.invokeMethod(
                service, "buildReassignCrnSearchOrder", Arrays.asList(1, 4, 6), 3);
        assertEquals(Arrays.asList(1, 6, 4), result);
    }
    @Test
    void reassignInboundLoc_currentCraneInFirstPool_shouldUseFirstPoolDescendingOrder() {
        WrkMast mast = inboundMast(3, "0100101");
        LocMast currentLoc = loc("0100101", 3, "S");
        LocMast targetLoc = loc("0200101", 2, "O");
        StartupDto startupDto = startup("0200101", 2);
        prepareSuccessfulReassign(mast, currentLoc, targetLoc, station("1,2,3,4,5,6", "7,8"));
        when(commonService.findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), anyList(), any(LocTypeDto.class), isNull()))
                .thenReturn(startupDto);
        service.reassignInboundLoc(reassignParams());
        ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
        verify(commonService).findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), captor.capture(), any(LocTypeDto.class), isNull());
        assertEquals(Arrays.asList(2, 1, 6, 5, 4), captor.getValue());
    }
    @Test
    void reassignInboundLoc_currentCraneInSecondPool_shouldUseSecondPoolBeforeFirstPool() {
        WrkMast mast = inboundMast(5, "0100101");
        LocMast currentLoc = loc("0100101", 5, "S");
        LocMast targetLoc = loc("0200101", 4, "O");
        StartupDto startupDto = startup("0200101", 4);
        prepareSuccessfulReassign(mast, currentLoc, targetLoc, station("1,2,3", "4,5,6"));
        when(commonService.findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), anyList(), any(LocTypeDto.class), isNull()))
                .thenReturn(startupDto);
        service.reassignInboundLoc(reassignParams());
        ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
        verify(commonService).findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), captor.capture(), any(LocTypeDto.class), isNull());
        assertEquals(Arrays.asList(4, 6), captor.getValue());
    }
    @Test
    void reassignInboundLoc_firstPoolNoLocation_shouldFallbackToSecondPool() {
        WrkMast mast = inboundMast(3, "0100101");
        LocMast currentLoc = loc("0100101", 3, "S");
        LocMast targetLoc = loc("0200101", 6, "O");
        StartupDto startupDto = startup("0200101", 6);
        prepareSuccessfulReassign(mast, currentLoc, targetLoc, station("1,2,3", "4,5,6"));
        when(commonService.findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), anyList(), any(LocTypeDto.class), isNull()))
                .thenReturn(null)
                .thenReturn(startupDto);
        service.reassignInboundLoc(reassignParams());
        ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
        verify(commonService, org.mockito.Mockito.times(2))
                .findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), captor.capture(), any(LocTypeDto.class), isNull());
        assertEquals(Arrays.asList(2, 1), captor.getAllValues().get(0));
        assertEquals(Arrays.asList(6, 5, 4), captor.getAllValues().get(1));
    }
    @Test
    void reassignInboundLoc_emptyPalletDetail_shouldSearchOnlyLevelEight() {
        WrkMast mast = inboundMast(3, "0100101");
        LocMast currentLoc = loc("0100101", 3, "S");
        currentLoc.setLocType1((short) 2);
        currentLoc.setLocType2((short) 0);
        currentLoc.setLocType3(null);
        LocMast targetLoc = loc("0200108", 2, "O");
        targetLoc.setLev1(8);
        targetLoc.setLocType1((short) 3);
        targetLoc.setLocType2((short) 0);
        StartupDto startupDto = startup("0200108", 2);
        prepareSuccessfulReassign(mast, currentLoc, targetLoc, station("1,2,3,4,5,6", ""));
        when(wrkDetlService.selectByWrkNo(7597)).thenReturn(Collections.singletonList(wrkDetl("emptyPallet")));
        when(commonService.findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), anyList(), any(LocTypeDto.class), eq(8)))
                .thenReturn(startupDto);
        service.reassignInboundLoc(reassignParams());
        ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
        ArgumentCaptor<LocTypeDto> locTypeCaptor = ArgumentCaptor.forClass(LocTypeDto.class);
        verify(commonService).findRun2InboundLocByCandidateCrnNos(eq(1001), eq(1), captor.capture(), locTypeCaptor.capture(), eq(8));
        assertEquals(Arrays.asList(2, 1, 6, 5, 4), captor.getValue());
        assertEquals(Short.valueOf((short) 3), locTypeCaptor.getValue().getLocType1());
        assertEquals(Short.valueOf((short) 0), locTypeCaptor.getValue().getLocType2());
    }
    @Test
    void reassignInboundLoc_emptyMasterFlagWithoutEmptyPalletDetail_shouldNotForceLevelEight() {
        WrkMast mast = inboundMast(3, "0100101");
        mast.setIoType(10);
        mast.setEmptyMk("Y");
        LocMast currentLoc = loc("0100101", 3, "S");
        LocMast targetLoc = loc("0200101", 2, "O");
        StartupDto startupDto = startup("0200101", 2);
        prepareSuccessfulReassign(mast, currentLoc, targetLoc, station("1,2,3,4,5,6", ""));
        when(wrkDetlService.selectByWrkNo(7597)).thenReturn(Collections.singletonList(wrkDetl("MAT-001")));
        when(commonService.findRun2InboundLocByCandidateCrnNos(eq(1001), eq(10), anyList(), any(LocTypeDto.class), isNull()))
                .thenReturn(startupDto);
        service.reassignInboundLoc(reassignParams());
        verify(commonService).findRun2InboundLocByCandidateCrnNos(eq(1001), eq(10), anyList(), any(LocTypeDto.class), isNull());
    }
    @Test
    void reassignInboundLoc_currentCraneNotInAnyPool_shouldNotSearchLocation() {
        WrkMast mast = inboundMast(3, "0100101");
        LocMast currentLoc = loc("0100101", 3, "S");
        when(wrkMastService.selectOne(any())).thenReturn(mast);
        when(locMastService.selectById("0100101")).thenReturn(currentLoc);
        when(basDevpService.selectById(1001)).thenReturn(station("1,2", "4,5"));
        Object result = service.reassignInboundLoc(reassignParams());
        assertNotNull(result);
        verify(commonService, never())
                .findRun2InboundLocByCandidateCrnNos(any(), any(), anyList(), any(LocTypeDto.class), any());
    }
    private static WrkMast outboundMast(Long wrkSts) {
        WrkMast mast = new WrkMast();
        mast.setWrkNo(7597);
@@ -71,4 +275,65 @@
        mast.setWrkSts(wrkSts);
        return mast;
    }
    private static WrkMast inboundMast(Integer crnNo, String locNo) {
        WrkMast mast = new WrkMast();
        mast.setWrkNo(7597);
        mast.setIoType(1);
        mast.setWrkSts(2L);
        mast.setCrnNo(crnNo);
        mast.setLocNo(locNo);
        mast.setSourceStaNo(1001);
        mast.setBarcode("BC-001");
        return mast;
    }
    private static LocMast loc(String locNo, Integer crnNo, String locSts) {
        LocMast locMast = new LocMast();
        locMast.setLocNo(locNo);
        locMast.setCrnNo(crnNo);
        locMast.setLocSts(locSts);
        locMast.setLocType1((short) 1);
        locMast.setLocType2((short) 2);
        locMast.setLocType3((short) 1);
        return locMast;
    }
    private static BasDevp station(String firstCrnCsv, String secondCrnCsv) {
        BasDevp station = new BasDevp();
        station.setDevNo(1001);
        station.setInFirstCrnCsv(firstCrnCsv);
        station.setInSecondCrnCsv(secondCrnCsv);
        return station;
    }
    private static StartupDto startup(String locNo, Integer crnNo) {
        StartupDto startupDto = new StartupDto();
        startupDto.setLocNo(locNo);
        startupDto.setCrnNo(crnNo);
        startupDto.setStaNo(2001);
        return startupDto;
    }
    private static WrkDetl wrkDetl(String matnr) {
        WrkDetl wrkDetl = new WrkDetl();
        wrkDetl.setWrkNo(7597);
        wrkDetl.setMatnr(matnr);
        return wrkDetl;
    }
    private static ReassignLocParams reassignParams() {
        ReassignLocParams params = new ReassignLocParams();
        params.setTaskNo("7597");
        return params;
    }
    private void prepareSuccessfulReassign(WrkMast mast, LocMast currentLoc, LocMast targetLoc, BasDevp station) {
        when(wrkMastService.selectOne(any())).thenReturn(mast);
        when(locMastService.selectById(currentLoc.getLocNo())).thenReturn(currentLoc);
        when(locMastService.selectById(targetLoc.getLocNo())).thenReturn(targetLoc);
        when(locMastService.updateById(any(LocMast.class))).thenReturn(true);
        when(wrkMastService.updateById(mast)).thenReturn(true);
        when(basDevpService.selectById(mast.getSourceStaNo())).thenReturn(station);
    }
}
src/test/java/com/zy/asrs/service/impl/BasCrnpServiceImplTest.java
New file
@@ -0,0 +1,64 @@
package com.zy.asrs.service.impl;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.mapper.BasCrnpMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class BasCrnpServiceImplTest {
    @Mock
    private BasCrnpMapper basCrnpMapper;
    private BasCrnpServiceImpl service;
    @BeforeEach
    void setUp() {
        service = new BasCrnpServiceImpl();
        ReflectionTestUtils.setField(service, "baseMapper", basCrnpMapper);
    }
    @Test
    void checkSiteError_shouldRejectNullInboundFlag() {
        BasCrnp basCrnp = buildCrnp(null, "Y");
        when(basCrnpMapper.selectById(22)).thenReturn(basCrnp);
        assertFalse(service.checkSiteError(22, true));
    }
    @Test
    void checkSiteError_shouldAllowExplicitInboundFlagY() {
        BasCrnp basCrnp = buildCrnp("Y", "Y");
        when(basCrnpMapper.selectById(22)).thenReturn(basCrnp);
        assertTrue(service.checkSiteError(22, true));
    }
    @Test
    void checkSiteStatus_shouldRejectNullInboundFlag() {
        BasCrnp basCrnp = buildCrnp(null, "Y");
        when(basCrnpMapper.selectById(22)).thenReturn(basCrnp);
        assertThrows(RuntimeException.class, () -> service.checkSiteStatus(22));
    }
    private BasCrnp buildCrnp(String inEnable, String outEnable) {
        BasCrnp basCrnp = new BasCrnp();
        basCrnp.setCrnNo(22);
        basCrnp.setCrnSts(3);
        basCrnp.setCrnErr(0L);
        basCrnp.setInEnable(inEnable);
        basCrnp.setOutEnable(outEnable);
        return basCrnp;
    }
}
src/test/java/com/zy/common/service/CommonServiceDoubleExtensionSameGoodsTest.java
@@ -19,7 +19,9 @@
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
@@ -80,25 +82,135 @@
    }
    @Test
    void doubleExtension_shouldFallBackWhenSameStandby1DeepLocHasNoEmptyShallowPartner() {
    void doubleExtension_shouldReturnShallowLocWhenDeepLocContainsEmptyPallet() {
        RowLastno rowLastno = buildRowLastno();
        CrnDepthRuleProfile profile = buildDoubleExtensionProfile();
        when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile);
        LocMast deepLoc = buildLoc("D-002", 1, 1, 1, "F", 1);
        LocMast shallowLoc = buildLoc("S-002", 2, 1, 1, "O", 1);
        LocDetl deepDetl = buildDetl("D-002", "PO-002");
        deepDetl.setMatnr("emptyPallet");
        when(locMastService.selectList(any())).thenReturn(Arrays.asList(deepLoc));
        when(locDetlService.selectList(any())).thenReturn(Arrays.asList(deepDetl));
        when(locMastService.selectOne(any())).thenReturn(shallowLoc);
        FindLocNoAttributeVo attributeVo = buildAttributeVo("PO-002");
        attributeVo.setMatnr("emptyPallet");
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, buildRowLastnoType(), 1, 2, null, attributeVo, false);
        assertEquals("S-002", result.getLocNo());
        assertEquals(Integer.valueOf(2), result.getRow1());
        assertEquals("O", result.getLocSts());
    }
    @Test
    void orderDoubleExtensionShallowRows_shouldPreferLessLoadedPair() {
        RowLastno rowLastno = buildRowLastno();
        CrnDepthRuleProfile profile = buildDoubleExtensionTwoPairProfile();
        when(locMastService.selectCount(any())).thenReturn(2, 2, 0, 0);
        @SuppressWarnings("unchecked")
        List<Integer> orderedRows = ReflectionTestUtils.invokeMethod(commonService, "orderDoubleExtensionShallowRows",
                rowLastno, buildRowLastnoType(), 1, profile);
        assertEquals(Arrays.asList(3, 2), orderedRows);
    }
    @Test
    void orderDoubleExtensionShallowRows_shouldRotateEqualLoadPairByCurrentRow() {
        RowLastno rowLastno = buildRowLastno();
        CrnDepthRuleProfile profile = buildDoubleExtensionTwoPairProfile();
        when(locMastService.selectCount(any())).thenReturn(1, 1, 1, 1);
        @SuppressWarnings("unchecked")
        List<Integer> orderedRows = ReflectionTestUtils.invokeMethod(commonService, "orderDoubleExtensionShallowRows",
                rowLastno, buildRowLastnoType(), 1, profile);
        assertEquals(Arrays.asList(3, 2), orderedRows);
    }
    @Test
    void doubleExtension_shouldSwitchPairAfterReservationLoadIncreases() {
        RowLastno rowLastno = buildRowLastno();
        CrnDepthRuleProfile profile = buildDoubleExtensionTwoPairProfile();
        when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile);
        when(locMastService.selectCount(any())).thenReturn(
                1, 1, 1, 1,
                1, 1, 1, 2);
        LocMast firstPairShallowLoc = buildLoc("S-003", 3, 1, 1, "O", 1);
        LocMast firstPairDeepLoc = buildLoc("D-004", 4, 1, 1, "O", 1);
        LocMast secondPairShallowLoc = buildLoc("S-002", 2, 1, 1, "O", 1);
        LocMast secondPairDeepLoc = buildLoc("D-001", 1, 1, 1, "O", 1);
        when(locMastService.selectList(any())).thenReturn(
                Arrays.asList(firstPairShallowLoc),
                Arrays.asList(secondPairShallowLoc));
        when(locMastService.selectOne(any())).thenReturn(firstPairDeepLoc, secondPairDeepLoc);
        FindLocNoAttributeVo attributeVo = new FindLocNoAttributeVo();
        LocMast firstResult = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, buildRowLastnoType(), 1, 2, null, attributeVo, false);
        LocMast secondResult = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, buildRowLastnoType(), 1, 2, null, attributeVo, false);
        assertEquals("D-004", firstResult.getLocNo());
        assertEquals(Integer.valueOf(4), firstResult.getRow1());
        assertEquals("D-001", secondResult.getLocNo());
        assertEquals(Integer.valueOf(1), secondResult.getRow1());
    }
    @Test
    void doubleExtension_shouldUseLessLoadedPairFirstWhenBothSidesAreOpen() {
        RowLastno rowLastno = buildRowLastno();
        CrnDepthRuleProfile profile = buildDoubleExtensionTwoPairProfile();
        when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile);
        when(locMastService.selectCount(any())).thenReturn(2, 2, 0, 0);
        LocMast openShallowLoc = buildLoc("S-003", 3, 1, 1, "O", 1);
        LocMast openDeepLoc = buildLoc("D-004", 4, 1, 1, "O", 1);
        when(locMastService.selectList(any())).thenReturn(
                Collections.<LocMast>emptyList(),
                Collections.<LocMast>emptyList(),
                Arrays.asList(openShallowLoc));
        when(locMastService.selectOne(any())).thenReturn(openDeepLoc);
        FindLocNoAttributeVo attributeVo = buildAttributeVo("PO-003");
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, buildRowLastnoType(), 1, 2, null, attributeVo, false);
        assertEquals("D-004", result.getLocNo());
        assertEquals(Integer.valueOf(4), result.getRow1());
        assertEquals("O", result.getLocSts());
    }
    @Test
    void doubleExtension_shouldFallBackWhenSameStandby1DeepLocHasNoEmptyShallowPartner() {
        RowLastno rowLastno = buildRowLastno();
        CrnDepthRuleProfile profile = buildDoubleExtensionTwoPairProfile();
        when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile);
        LocMast sameGoodsDeepLoc = buildLoc("D-001", 1, 1, 1, "F", 1);
        LocMast genericShallowLoc = buildLoc("S-002", 2, 2, 1, "O", 1);
        LocMast genericDeepFallbackLoc = buildLoc("D-002", 1, 2, 1, "F", 1);
        LocMast genericShallowLoc = buildLoc("S-003", 3, 1, 1, "O", 1);
        LocMast genericDeepFallbackLoc = buildLoc("D-004", 4, 1, 1, "O", 1);
        LocDetl deepDetl = buildDetl("D-001", "PO-001");
        when(locMastService.selectList(any())).thenReturn(Arrays.asList(sameGoodsDeepLoc), Arrays.asList(genericShallowLoc));
        when(locMastService.selectList(any())).thenReturn(
                Collections.<LocMast>emptyList(),
                Arrays.asList(sameGoodsDeepLoc),
                Arrays.asList(genericShallowLoc));
        when(locDetlService.selectList(any())).thenReturn(Arrays.asList(deepDetl));
        when(locMastService.selectOne(any())).thenReturn(null, null, genericDeepFallbackLoc);
        when(locMastService.selectOne(any())).thenReturn(null, genericDeepFallbackLoc);
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, buildRowLastnoType(), 1, 2, null, buildAttributeVo("PO-001"), false);
        assertEquals("S-002", result.getLocNo());
        assertEquals(Integer.valueOf(2), result.getRow1());
        assertEquals("D-004", result.getLocNo());
        assertEquals(Integer.valueOf(4), result.getRow1());
        assertEquals("O", result.getLocSts());
    }
@@ -152,6 +264,23 @@
        return profile;
    }
    private CrnDepthRuleProfile buildDoubleExtensionTwoPairProfile() {
        CrnDepthRuleProfile profile = new CrnDepthRuleProfile();
        profile.setLayoutType(2);
        profile.setSearchRows(Arrays.asList(2, 3, 1, 4));
        profile.setShallowRows(Arrays.asList(2, 3));
        profile.setDeepRows(Arrays.asList(1, 4));
        LinkedHashMap<Integer, Integer> shallowToDeep = new LinkedHashMap<Integer, Integer>();
        shallowToDeep.put(2, 1);
        shallowToDeep.put(3, 4);
        LinkedHashMap<Integer, Integer> deepToShallow = new LinkedHashMap<Integer, Integer>();
        deepToShallow.put(1, 2);
        deepToShallow.put(4, 3);
        profile.setShallowToDeepRow(shallowToDeep);
        profile.setDeepToShallowRow(deepToShallow);
        return profile;
    }
    private CrnDepthRuleProfile buildSingleExtensionProfile() {
        CrnDepthRuleProfile profile = new CrnDepthRuleProfile();
        profile.setLayoutType(1);
src/test/java/com/zy/common/service/CommonServiceLocTypeStrategyTest.java
@@ -160,4 +160,18 @@
        assertEquals(Short.valueOf((short) 0), stages.get(2).getLocType2());
        assertEquals(Short.valueOf((short) 2), stages.get(2).getLocType3());
    }
    @Test
    void matchesTargetLev_shouldOnlyAcceptRequestedLevel() {
        LocMast locMast = new LocMast();
        locMast.setLev1(8);
        Boolean matchEight = ReflectionTestUtils.invokeMethod(commonService, "matchesTargetLev", locMast, 8);
        Boolean matchSeven = ReflectionTestUtils.invokeMethod(commonService, "matchesTargetLev", locMast, 7);
        Boolean matchNull = ReflectionTestUtils.invokeMethod(commonService, "matchesTargetLev", locMast, null);
        assertTrue(Boolean.TRUE.equals(matchEight));
        assertFalse(Boolean.TRUE.equals(matchSeven));
        assertTrue(Boolean.TRUE.equals(matchNull));
    }
}