自动化立体仓库 - WMS系统
cl
2 天以前 5677fd88b56cd69e416b52144734f3997ef8f8f4
src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
@@ -7,6 +7,7 @@
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.api.controller.params.ChangeLocParams;
import com.zy.api.controller.params.ReassignLocParams;
import com.zy.api.controller.params.ReceviceTaskParams;
import com.zy.api.controller.params.StopOutTaskParams;
@@ -17,10 +18,13 @@
import com.zy.api.service.WcsApiService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.task.support.OutboundBatchSeqReleaseGuard;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.MesConstant;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.StartupDto;
import com.zy.common.model.CrnDepthRuleProfile;
import com.zy.common.model.enums.WorkNoType;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
@@ -32,6 +36,7 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -42,12 +47,31 @@
    private static final Long WCS_SYNC_USER = 9999L;
    private static final String YES = "Y";
    private static final String NO = "N";
    private static final long OUT_LOCK_REPORT_PENDING_WRK_STS = 13L;
    private static final long OUT_LOCK_REPORT_SUCCESS_WRK_STS = 21L;
    private static final long OUT_LOCK_REPORT_FAIL_WRK_STS = 22L;
    private static final long OUTBOUND_CRN_COMPLETE_WRK_STS = 25L;
    private static final long OUTBOUND_STATION_COMPLETE_WRK_STS = 26L;
    private static final String OUT_LOCK_REPORT_PENDING_FLAG = "P";
    /** 同一 WCS 路径、同一单号下一组下发的任务条数上限 */
    private static final int WCS_PUB_BATCH_SIZE = 20;
    /** 三方接口统计:本系统调用 WCS 的 namespace 约定 */
    private static final String NS_WMS_TO_WCS = "本系统请求WCS";
    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 final int CHANGE_LOC_IO_TYPE = 5;
    private static class ReassignCrnPool {
        private final List<Integer> crnNos;
        private ReassignCrnPool(List<Integer> crnNos) {
            this.crnNos = crnNos;
        }
    }
    @Autowired
    private LocMastService locMastService;
@@ -95,6 +119,10 @@
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private OutboundBatchSeqReleaseGuard outboundBatchSeqReleaseGuard;
    @Autowired
    private BasCrnDepthRuleService basCrnDepthRuleService;
    @Autowired
    private RowLastnoService rowLastnoService;
@@ -115,6 +143,10 @@
        String validateMsg = validatePubTask(params, wrkMast);
        if (!Cools.isEmpty(validateMsg)) {
            return R.error(validateMsg);
        }
        String batchBlockMsg = validateOutboundBatchSeqReady(params, wrkMast);
        if (!Cools.isEmpty(batchBlockMsg)) {
            return R.error(batchBlockMsg);
        }
        String url = resolveTaskPath(params);
        String requestJson = JSON.toJSONString(params);
@@ -192,6 +224,7 @@
        List<String> failMsgs = new ArrayList<>();
        List<WorkTaskParams> lastSentChunk = null;
        String skipGroupKey = null;
        Set<String> blockedOutboundUserKeys = new HashSet<>();
        for (List<WorkTaskParams> chunk : chunks) {
            if (chunk == null || chunk.isEmpty()) {
@@ -200,8 +233,22 @@
            WorkTaskParams head = chunk.get(0);
            WrkMast headMast = wrkMastMap.get(head.getTaskNo());
            String key = buildBatchGroupKey(head, headMast);
            String outboundUserKey = buildOutboundUserKey(head, headMast);
            if (skipGroupKey != null && skipGroupKey.equals(key)) {
                continue;
            }
            if (outboundUserKey != null && blockedOutboundUserKeys.contains(outboundUserKey)) {
                continue;
            }
            String batchBlockMsg = validateOutboundBatchSeqReady(chunk, wrkMastMap);
            if (!Cools.isEmpty(batchBlockMsg)) {
                skipMsgs.add(batchBlockMsg);
                skipGroupKey = key;
                if (outboundUserKey != null) {
                    blockedOutboundUserKeys.add(outboundUserKey);
                }
                continue;
            }
@@ -295,7 +342,7 @@
    }
    /**
     * 出库:仅当单号、序号均有效时做跳号校验;单号空或序号无效仍下发。入库/移库不处理。
     * 出库:仅当单号、批次、序号均有效时做批次内跳号校验;无效时仍下发。入库/移库不处理。
     */
    private List<WorkTaskParams> filterOutboundByContiguousPlt(List<WorkTaskParams> accepted, Map<String, WrkMast> wrkMastMap, List<String> skipMsgs) {
        Map<String, Integer> reachCache = new HashMap<>();
@@ -307,12 +354,14 @@
            }
            WrkMast w = wrkMastMap.get(p.getTaskNo());
            String userNo = sortUserNoForPub(p, w);
            String batchGroup = sortBatchGroupForPub(p, w);
            Integer plt = sortPltForPub(p, w);
            if (Cools.isEmpty(userNo) || plt == null || plt <= 0) {
                kept.add(p);
                continue;
            }
            int maxReach = reachCache.computeIfAbsent(userNo, wrkMastService::outboundSeqMaxContiguousPlt);
            String cacheKey = buildOutboundBatchCacheKey(userNo, batchGroup);
            int maxReach = reachCache.computeIfAbsent(cacheKey, key -> wrkMastService.outboundSeqMaxContiguousPlt(userNo, batchGroup));
            if (plt > maxReach) {
                skipMsgs.add(buildTaskMsg(p, "出库序号跳号,跳过"));
                continue;
@@ -356,7 +405,7 @@
    }
    /**
     * 同单下一组:优先 WCS queryTask;失败或无数据则主表已非 11 或已进历史表。
     * 同单同批下一组:优先 WCS queryTask;失败或无数据则主表已非 11 或已进历史表。
     */
    private boolean sameOrderNextChunkAllowed(List<WorkTaskParams> lastSentChunk) {
        if (lastSentChunk == null || lastSentChunk.isEmpty()) {
@@ -419,7 +468,7 @@
    }
    /**
     * 出库每组下发前:本组有有效最小序号且&gt;1 时,只校验「最小序号-1」一档;序号全无则跳过本条件。
     * 出库每组下发前:本组有有效最小序号且&gt;1 时,只校验「同单同批的最小序号-1」一档;序号全无则跳过本条件。
     */
    private boolean outboundChunkPredecessorPltReady(List<WorkTaskParams> chunk, Map<String, WrkMast> wrkMastMap) {
        if (chunk == null || chunk.isEmpty()) {
@@ -431,6 +480,7 @@
        }
        WrkMast headMast = wrkMastMap.get(head.getTaskNo());
        String userNo = sortUserNoForPub(head, headMast);
        String batchGroup = sortBatchGroupForPub(head, headMast);
        if (Cools.isEmpty(userNo)) {
            return true;
        }
@@ -447,14 +497,40 @@
        if (minPlt == Integer.MAX_VALUE || minPlt <= 1) {
            return true;
        }
        return outboundPltSlotReleasedInWms(userNo, minPlt - 1);
        return outboundPltSlotReleasedInWms(userNo, batchGroup, minPlt - 1);
    }
    private boolean outboundPltSlotReleasedInWms(String userNo, int pltType) {
        List<WrkMast> rows = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("user_no", userNo)
                .eq("io_type", 101)
                .eq("plt_type", pltType));
    private String validateOutboundBatchSeqReady(List<WorkTaskParams> chunk, Map<String, WrkMast> wrkMastMap) {
        if (chunk == null || chunk.isEmpty()) {
            return null;
        }
        WorkTaskParams head = chunk.get(0);
        return validateOutboundBatchSeqReady(head, wrkMastMap.get(head.getTaskNo()));
    }
    private String validateOutboundBatchSeqReady(WorkTaskParams params, WrkMast wrkMast) {
        if (params == null || !"out".equalsIgnoreCase(params.getType())) {
            return null;
        }
        if (wrkMast == null || !Objects.equals(wrkMast.getIoType(), 101)) {
            return null;
        }
        return outboundBatchSeqReleaseGuard.validateReady(
                sortUserNoForPub(params, wrkMast),
                sortBatchGroupForPub(params, wrkMast));
    }
    private boolean outboundPltSlotReleasedInWms(String userNo, String batchSeq, int pltType) {
        EntityWrapper<WrkMast> mastWrapper = new EntityWrapper<>();
        mastWrapper.eq("user_no", userNo);
        mastWrapper.eq("io_type", 101);
        mastWrapper.eq("plt_type", pltType);
        if (batchSeq == null) {
            mastWrapper.isNull("batch_seq");
        } else {
            mastWrapper.eq("batch_seq", batchSeq);
        }
        List<WrkMast> rows = wrkMastService.selectList(mastWrapper);
        if (rows != null && !rows.isEmpty()) {
            for (WrkMast m : rows) {
                if (m != null && m.getWrkSts() != null && Objects.equals(m.getWrkSts(), 11L)) {
@@ -463,10 +539,16 @@
            }
            return true;
        }
        int logCnt = wrkMastLogService.selectCount(new EntityWrapper<WrkMastLog>()
                .eq("user_no", userNo)
                .eq("io_type", 101)
                .eq("plt_type", pltType));
        EntityWrapper<WrkMastLog> logWrapper = new EntityWrapper<>();
        logWrapper.eq("user_no", userNo);
        logWrapper.eq("io_type", 101);
        logWrapper.eq("plt_type", pltType);
        if (batchSeq == null) {
            logWrapper.isNull("batch_seq");
        } else {
            logWrapper.eq("batch_seq", batchSeq);
        }
        int logCnt = wrkMastLogService.selectCount(logWrapper);
        return logCnt > 0;
    }
@@ -495,6 +577,7 @@
        return Comparator
                .comparing((WorkTaskParams p) -> Optional.ofNullable(p.getType()).orElse(""), String.CASE_INSENSITIVE_ORDER)
                .thenComparing(p -> sortUserNoForPub(p, wrkMastMap.get(p.getTaskNo())), Comparator.nullsLast(String::compareTo))
                .thenComparing(p -> sortBatchGroupForPub(p, wrkMastMap.get(p.getTaskNo())), WcsApiServiceImpl::compareBatchSeqNatural)
                .thenComparing(p -> sortPltForPub(p, wrkMastMap.get(p.getTaskNo())), Comparator.nullsLast(Integer::compareTo));
    }
@@ -511,6 +594,77 @@
            return wrkMast.getPltType();
        }
        return p.getBatchSeq();
    }
    private static String sortBatchGroupForPub(WorkTaskParams p, WrkMast wrkMast) {
        if (wrkMast != null) {
            return wrkMast.getBatchSeq();
        }
        return null;
    }
    private static String buildOutboundUserKey(WorkTaskParams params, WrkMast wrkMast) {
        if (params == null || !"out".equalsIgnoreCase(params.getType())) {
            return null;
        }
        String userNo = sortUserNoForPub(params, wrkMast);
        if (Cools.isEmpty(userNo)) {
            return null;
        }
        return resolveSafeKey(userNo);
    }
    private static int compareBatchSeqNatural(String left, String right) {
        String safeLeft = normalizeBatchSeq(left);
        String safeRight = normalizeBatchSeq(right);
        int leftIndex = 0;
        int rightIndex = 0;
        while (leftIndex < safeLeft.length() && rightIndex < safeRight.length()) {
            char leftChar = safeLeft.charAt(leftIndex);
            char rightChar = safeRight.charAt(rightIndex);
            if (Character.isDigit(leftChar) && Character.isDigit(rightChar)) {
                int leftStart = leftIndex;
                int rightStart = rightIndex;
                while (leftIndex < safeLeft.length() && Character.isDigit(safeLeft.charAt(leftIndex))) {
                    leftIndex++;
                }
                while (rightIndex < safeRight.length() && Character.isDigit(safeRight.charAt(rightIndex))) {
                    rightIndex++;
                }
                String leftNumber = safeLeft.substring(leftStart, leftIndex);
                String rightNumber = safeRight.substring(rightStart, rightIndex);
                int compare = new BigInteger(leftNumber).compareTo(new BigInteger(rightNumber));
                if (compare != 0) {
                    return compare;
                }
                compare = Integer.compare(leftNumber.length(), rightNumber.length());
                if (compare != 0) {
                    return compare;
                }
                continue;
            }
            int compare = Character.compare(leftChar, rightChar);
            if (compare != 0) {
                return compare;
            }
            leftIndex++;
            rightIndex++;
        }
        return Integer.compare(safeLeft.length(), safeRight.length());
    }
    private static String normalizeBatchSeq(String value) {
        return Cools.isEmpty(value) ? "" : value;
    }
    private static String resolveSafeKey(String value) {
        return Cools.isEmpty(value) ? "_EMPTY_" : value;
    }
    private static String buildOutboundBatchCacheKey(String userNo, String batchSeq) {
        String safeUserNo = Cools.isEmpty(userNo) ? "_NO_USER_" : userNo;
        String safeBatchSeq = Cools.isEmpty(batchSeq) ? "_NO_BATCH_" : batchSeq;
        return safeUserNo + "#" + safeBatchSeq;
    }
    /**
@@ -537,13 +691,44 @@
        }
        if (params.getNotifyType().equals("task")) {
        if (isOutboundCrnTaskRun(params)) {
            // WCS出库任务开始:堆垛机开始执行出库任务,工作状态 12 -> 13。
            if (isOutboundTask(mast) && Objects.equals(mast.getWrkSts(), 12L)) {
                mast.setWrkSts(OUT_LOCK_REPORT_PENDING_WRK_STS);
                mast.setExpTime(0D);
                mast.setLogMk(OUT_LOCK_REPORT_PENDING_FLAG);
                mast.setLogErrMemo(null);
                mast.setLogErrTime(null);
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (isOutboundCrnTaskComplete(params)) {
            // WCS出库任务完成:堆垛机出库任务执行完成,工作状态 -> 25。
            if (isOutboundTask(mast) && canMarkOutboundCrnComplete(mast)) {
                mast.setWrkSts(OUTBOUND_CRN_COMPLETE_WRK_STS);
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (isOutboundStationTaskRunComplete(params)) {
            // WCS输送站点出库任务运行完成:托盘已到目的地,工作状态 -> 26。
            if (isOutboundTask(mast) && canMarkOutboundStationComplete(mast)) {
                mast.setWrkSts(OUTBOUND_STATION_COMPLETE_WRK_STS);
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if ("task".equalsIgnoreCase(params.getNotifyType())) {
            //任务
            if (params.getMsgType().equals("task_complete")) {
            if ("task_complete".equalsIgnoreCase(params.getMsgType())) {
                if (mast.getIoType() == 1 || mast.getIoType() == 2 ||mast.getIoType() == 10) {
                if (mast.getIoType() == 1 || mast.getIoType() == 2 || mast.getIoType() == 10 || mast.getIoType() == CHANGE_LOC_IO_TYPE) {
                    mast.setWrkSts(4L);
                } else if ((mast.getIoType() == 101||mast.getIoType()==110) && mast.getWrkSts()<14) {
                } else if (isOutboundTask(mast) && canMarkOutboundTaskComplete(mast)) {
                    mast.setWrkSts(14L);
                    if(Cools.isEmpty(mast.getStaNo())){
                        mast.setOveMk("Y");
@@ -553,27 +738,63 @@
                    throw new CoolException("任务状态修改失败!!");
                }
                //wcs任务取消接口
            } else if (params.getMsgType().equals("task_cancel")) {
            } else if ("task_cancel".equalsIgnoreCase(params.getMsgType())) {
                workService.cancelWrkMast(String.valueOf(mast.getWrkNo()), 9955L);
            } else if (params.getMsgType().equals("task_arrive")) {
                //到达目的地
                //如果出库任务是跨区则需要生成新的入库任务入库
                if(!Cools.isEmpty(mast.getLocNo())){
                    mast.setOnlineYn("N");//等待生成跨区入库任务
                }
                mast.setWrkSts(14L);
                if(Cools.isEmpty(mast.getStaNo())){
                    mast.setOveMk("Y");
                }
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (params.getNotifyType().equals("weight")) {
        }
        return R.ok();
    }
    private boolean isOutboundCrnTaskRun(ReceviceTaskParams params) {
        return params != null
                && "Crn".equalsIgnoreCase(params.getNotifyType())
                && "crn_out_task_run".equalsIgnoreCase(params.getMsgType());
    }
    private boolean isOutboundCrnTaskComplete(ReceviceTaskParams params) {
        return params != null
                && "Crn".equalsIgnoreCase(params.getNotifyType())
                && "crn_out_task_complete".equalsIgnoreCase(params.getMsgType());
    }
    private boolean isOutboundStationTaskRunComplete(ReceviceTaskParams params) {
        return params != null
                && "Devp".equalsIgnoreCase(params.getNotifyType())
                && "station_out_task_run_complete".equalsIgnoreCase(params.getMsgType());
    }
    private boolean isOutboundTask(WrkMast mast) {
        return mast != null && mast.getIoType() != null && (mast.getIoType() == 101 || mast.getIoType() == 110);
    }
    private boolean canMarkOutboundCrnComplete(WrkMast mast) {
        if (mast == null || mast.getWrkSts() == null) {
            return false;
        }
        return mast.getWrkSts() < 14
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_SUCCESS_WRK_STS)
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS);
    }
    private boolean canMarkOutboundTaskComplete(WrkMast mast) {
        if (mast == null || mast.getWrkSts() == null) {
            return false;
        }
        return mast.getWrkSts() < 14
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_SUCCESS_WRK_STS)
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS)
                || mast.getWrkSts().equals(OUTBOUND_CRN_COMPLETE_WRK_STS)
                || mast.getWrkSts().equals(OUTBOUND_STATION_COMPLETE_WRK_STS);
    }
    private boolean canMarkOutboundStationComplete(WrkMast mast) {
        if (mast == null || mast.getWrkSts() == null) {
            return false;
        }
        return mast.getWrkSts() < 14
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_SUCCESS_WRK_STS)
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS)
                || mast.getWrkSts().equals(OUTBOUND_CRN_COMPLETE_WRK_STS);
    }
    @Override
@@ -634,21 +855,37 @@
            return R.error("当前目标库位不存在");
        }
        Integer preferredArea = resolveReassignArea(wrkMast, currentLoc);
        if (preferredArea == null) {
            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("源站未配置入库优先池");
        }
        List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(preferredArea, wrkMast.getCrnNo());
        if (candidateCrnNos.isEmpty()) {
            return R.error("当前库区没有其他堆垛机可供重分配");
        StartupDto startupDto = null;
        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(), candidateCrnNos, locTypeDto, targetLevel);
            if (startupDto != null && !Cools.isEmpty(startupDto.getLocNo())) {
                break;
            }
        }
        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc);
        StartupDto startupDto = commonService.findRun2InboundLocByCandidateCrnNos(
                wrkMast.getSourceStaNo(), wrkMast.getIoType(), preferredArea, candidateCrnNos, locTypeDto);
        if (startupDto == null || Cools.isEmpty(startupDto.getLocNo())) {
            return R.error("当前库区没有可重新分配的空库位");
            return R.error("当前优先池没有可重新分配的空库位");
        }
        LocMast targetLoc = locMastService.selectById(startupDto.getLocNo());
@@ -667,6 +904,215 @@
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("locNo", Utils.WMSLocToWCSLoc(targetLoc.getLocNo()));
        return R.ok("操作成功").add(result);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R changeLoc(ChangeLocParams params) {
        if (params == null || Cools.isEmpty(params.getLocNo())) {
            return R.error("locNo不能为空");
        }
        LocMast sourceLoc = locMastService.selectById(params.getLocNo());
        if (sourceLoc == null) {
            return R.error("当前库位不存在");
        }
        CrnDepthRuleProfile profile = resolveChangeLocProfile(sourceLoc);
        String validateMsg = validateChangeLocSource(sourceLoc, profile);
        if (!Cools.isEmpty(validateMsg)) {
            return R.error(validateMsg);
        }
        List<Integer> candidateRows = resolveChangeLocRows(params.getRow(), profile);
        LocMast targetLoc = selectChangeLocTarget(sourceLoc, profile, candidateRows);
        if (targetLoc == null) {
            return R.error("当前堆垛机无可用移库目标位");
        }
        Integer workNo = commonService.getWorkNo(WorkNoType.PICK.type);
        Date now = new Date();
        createChangeLocTask(workNo, sourceLoc, targetLoc, now);
        reserveChangeLocTargetLoc(targetLoc, sourceLoc, now);
        reserveChangeLocSourceLoc(sourceLoc, now);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("locNo", Utils.WMSLocToWCSLoc(targetLoc.getLocNo()));
        result.put("taskNo", String.valueOf(workNo));
        return R.ok("操作成功").add(result);
    }
    private CrnDepthRuleProfile resolveChangeLocProfile(LocMast sourceLoc) {
        RowLastno rowLastno = rowLastnoService.selectById(sourceLoc.getWhsType());
        return basCrnDepthRuleService.resolveProfile(rowLastno, sourceLoc.getCrnNo(), sourceLoc.getRow1());
    }
    private String validateChangeLocSource(LocMast sourceLoc, CrnDepthRuleProfile profile) {
        if (sourceLoc.getCrnNo() == null) {
            return "当前库位未绑定堆垛机";
        }
        if (profile == null || !profile.isDoubleExtension()) {
            return "当前堆垛机非双深库位,不支持移库";
        }
        if (sourceLoc.getRow1() == null || profile == null || !profile.isShallowRow(sourceLoc.getRow1())) {
            return "当前库位不是浅库位";
        }
        if (!isChangeLocInStockLocSts(sourceLoc.getLocSts())) {
            return "当前库位状态不允许移库";
        }
        return null;
    }
    private List<Integer> resolveChangeLocRows(List<Integer> requestRows, CrnDepthRuleProfile profile) {
        if (!Cools.isEmpty(requestRows)) {
            return requestRows.stream()
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
        }
        return profile == null ? Collections.emptyList() : profile.getSearchRows();
    }
    private LocMast selectChangeLocTarget(LocMast sourceLoc, CrnDepthRuleProfile profile, List<Integer> candidateRows) {
        List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                .eq("crn_no", sourceLoc.getCrnNo())
                .eq("loc_type1",2)
                .eq("loc_sts", "O")
                .orderBy("row1", true)
                .orderBy("lev1", true)
                .orderBy("bay1", true));
        if (Cools.isEmpty(locMasts)) {
            return null;
        }
//        for (LocMast locMast : locMasts) {
//            if (!isChangeLocRowAllowed(locMast, candidateRows)) {
//                continue;
//            }
//            if (locMast.getRow1() != null && Utils.isDeepLoc(slaveProperties, locMast.getRow1())) {
//                return locMast;
//            }
//        }
//        for (LocMast locMast : locMasts) {
//            if (!isChangeLocRowAllowed(locMast, candidateRows)) {
//                continue;
//            }
//            if (locMast.getRow1() == null || !Utils.isShallowLoc(slaveProperties, locMast.getRow1())) {
//                continue;
//            }
//            String deepLocNo = Utils.getDeepLoc(slaveProperties, locMast.getLocNo());
//            LocMast deepLoc = locMastService.selectById(deepLocNo);
//            if (deepLoc != null && ("F".equals(deepLoc.getLocSts()) || "D".equals(deepLoc.getLocSts()))) {
//                return locMast;
//            }
//        }
        LocMast shallowFallback = null;
        for (LocMast locMast : locMasts) {
            //排过滤
            if (!isChangeLocRowAllowed(locMast, candidateRows)) {
                continue;
            }
            //深位优先
            if (isChangeLocDeepCandidate(locMast, profile)) {
                return locMast;
            }
            //浅位兜底
            if (shallowFallback == null && isChangeLocShallowFallbackCandidate(locMast, profile)) {
                shallowFallback = locMast;
            }
        }
        return shallowFallback;
    }
    private boolean isChangeLocRowAllowed(LocMast locMast, List<Integer> candidateRows) {
        if (Cools.isEmpty(candidateRows)) {
            return true;
        }
        return locMast.getRow1() != null && candidateRows.contains(locMast.getRow1());
    }
    private boolean isChangeLocDeepCandidate(LocMast locMast, CrnDepthRuleProfile profile) {
        return locMast.getRow1() != null
                && profile != null
                && profile.isDeepRow(locMast.getRow1());
    }
    private boolean isChangeLocShallowFallbackCandidate(LocMast locMast, CrnDepthRuleProfile profile) {
        if (locMast.getRow1() == null || profile == null || !profile.isShallowRow(locMast.getRow1())) {
            return false;
        }
        Integer deepRow = profile.getPairedDeepRow(locMast.getRow1());
        if (deepRow == null) {
            return false;
        }
        LocMast deepLoc = locMastService.selectOne(new EntityWrapper<LocMast>()
                .eq("crn_no", locMast.getCrnNo())
                .eq("row1", deepRow)
                .eq("bay1", locMast.getBay1())
                .eq("lev1", locMast.getLev1()));
        return deepLoc != null && isChangeLocInStockLocSts(deepLoc.getLocSts());
    }
    private boolean isChangeLocInStockLocSts(String locSts) {
        return "F".equals(locSts) || "D".equals(locSts);
    }
    private void createChangeLocTask(Integer workNo, LocMast sourceLoc, LocMast targetLoc, Date now) {
        List<LocDetl> sourceDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLoc.getLocNo()));
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(11L);
        wrkMast.setIoType(CHANGE_LOC_IO_TYPE);
        wrkMast.setIoPri(10D);
        wrkMast.setCrnNo(sourceLoc.getCrnNo());
        wrkMast.setSourceLocNo(sourceLoc.getLocNo());
        wrkMast.setLocNo(targetLoc.getLocNo());
        wrkMast.setFullPlt(Cools.isEmpty(sourceDetls) ? "N" : "Y");
        wrkMast.setPicking("N");
        wrkMast.setExitMk("N");
        wrkMast.setEmptyMk("D".equals(sourceLoc.getLocSts()) ? "Y" : "N");
        wrkMast.setBarcode(sourceLoc.getBarcode());
        wrkMast.setLinkMis("N");
        wrkMast.setMemo("CHANGE_LOC");
        wrkMast.setAppeUser(WCS_SYNC_USER);
        wrkMast.setAppeTime(now);
        wrkMast.setModiUser(WCS_SYNC_USER);
        wrkMast.setModiTime(now);
        if (!wrkMastService.insert(wrkMast)) {
            throw new CoolException("保存工作档失败");
        }
        if (Cools.isEmpty(sourceDetls)) {
            return;
        }
        for (LocDetl sourceDetl : sourceDetls) {
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(sourceDetl);
            wrkDetl.setWrkNo(workNo);
            wrkDetl.setIoTime(now);
            wrkDetl.setAnfme(sourceDetl.getAnfme());
            wrkDetl.setAppeTime(now);
            wrkDetl.setAppeUser(WCS_SYNC_USER);
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(WCS_SYNC_USER);
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
            }
        }
    }
    private void reserveChangeLocTargetLoc(LocMast targetLoc, LocMast sourceLoc, Date now) {
        targetLoc.setLocSts("S");
        targetLoc.setBarcode(sourceLoc.getBarcode());
        targetLoc.setScWeight(sourceLoc.getScWeight() == null ? BigDecimal.ZERO : sourceLoc.getScWeight());
        targetLoc.setModiUser(WCS_SYNC_USER);
        targetLoc.setModiTime(now);
        if (!locMastService.updateById(targetLoc)) {
            throw new CoolException("更新目标库位状态失败");
        }
    }
    private void reserveChangeLocSourceLoc(LocMast sourceLoc, Date now) {
        sourceLoc.setLocSts("R");
        sourceLoc.setModiUser(WCS_SYNC_USER);
        sourceLoc.setModiTime(now);
        if (!locMastService.updateById(sourceLoc)) {
            throw new CoolException("更新源库位状态失败");
        }
    }
    private boolean requiresOutboundErpConfirm(WrkMast wrkMast) {
@@ -698,6 +1144,9 @@
            if ("Y".equalsIgnoreCase(wrkMast.getPauseMk())) {
                return "task paused";
            }
            if (Objects.equals(wrkMast.getIoType(), 101) && Cools.isEmpty(wrkMast.getBatchSeq())) {
                return "出库进仓编号(batchSeq)为空,跳过下发";
            }
            if (requiresOutboundErpConfirm(wrkMast) && !"Y".equalsIgnoreCase(wrkMast.getPdcType())) {
                return "task not confirmed by erp";
            }
@@ -727,83 +1176,102 @@
        return null;
    }
    private Integer resolveReassignArea(WrkMast wrkMast, LocMast currentLoc) {
        Integer stationArea = Utils.getStationStorageArea(wrkMast.getSourceStaNo());
        if (belongsToArea(stationArea, wrkMast.getCrnNo(), currentLoc)) {
            return stationArea;
    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 stationArea;
        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 boolean belongsToArea(Integer area, Integer currentCrnNo, LocMast currentLoc) {
        if (area == null || area <= 0) {
    private boolean isReassignEmptyPallet(WrkMast wrkMast) {
        if (wrkMast == null || wrkMast.getWrkNo() == null) {
            return false;
        }
        RowLastno areaRowLastno = rowLastnoService.selectById(area);
        if (areaRowLastno == null) {
        List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
        if (Cools.isEmpty(wrkDetls)) {
            return false;
        }
        Integer startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
        Integer endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
        if (currentCrnNo != null && currentCrnNo >= startCrnNo && currentCrnNo <= endCrnNo) {
            return true;
        for (WrkDetl wrkDetl : wrkDetls) {
            if (wrkDetl != null && EMPTY_PALLET_MATNR.equalsIgnoreCase(String.valueOf(wrkDetl.getMatnr()).trim())) {
                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;
        return false;
    }
    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--) {
            candidateCrnNos.add(crnNo);
        }
        for (int crnNo = endCrnNo; crnNo > currentCrnNo; crnNo--) {
            candidateCrnNos.add(crnNo);
        }
        return candidateCrnNos;
    }
    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;
        }
@@ -939,21 +1407,23 @@
     * <p>
     * 分组规则:
     * 1. 先按接口路径区分,避免不同任务类型混用同一个 WCS 接口;
     * 2. 再按 userNo 区分,确保相同 userNo 的任务一起上报。
     * 2. 再按 userNo + batchSeq 区分,确保相同订单同批次的任务一起上报。
     * <p>
     * 正常情况下 userNo 取自 work_mast.user_no;
     * 如果当前没查到工作档,则回退到请求里的 batch 字段,保证兼容已有调用。
     * batchSeq 取自 work_mast.batch_seq;如果当前没查到工作档,则只按 userNo 回退兼容已有调用。
     */
    private String buildBatchGroupKey(WorkTaskParams params, WrkMast wrkMast) {
        String path = resolveTaskPath(params);
        String userNo = wrkMast == null ? null : wrkMast.getUserNo();
        String batchGroup = wrkMast == null ? null : wrkMast.getBatchSeq();
        if (Cools.isEmpty(userNo)) {
            userNo = params.getBatch();
        }
        if (Cools.isEmpty(userNo)) {
            userNo = "_NO_USER_";
        }
        return path + "#" + userNo;
        String batchKey = Cools.isEmpty(batchGroup) ? "_NO_BATCH_" : batchGroup;
        return path + "#" + userNo + "#" + batchKey;
    }
    /**
@@ -994,10 +1464,12 @@
        if (!Cools.isEmpty(params.getStaNo())) {
            task.put("staNo", params.getStaNo());
        }
        if (!Cools.isEmpty(params.getBatch())) {
        boolean includeOutBatch = !"out".equalsIgnoreCase(params.getType())
                || (params.getBatchSeq() != null && params.getBatchSeq() > 0);
        if (includeOutBatch && !Cools.isEmpty(params.getBatch())) {
            task.put("batch", params.getBatch());
        }
        if (!Objects.isNull(params.getBatchSeq())) {
        if (includeOutBatch && !Objects.isNull(params.getBatchSeq())) {
            task.put("batchSeq", params.getBatchSeq());
        }
        return task;
@@ -1141,6 +1613,8 @@
            // crn_sts 本地表存的是“堆垛机模式(手动/自动/电脑)”,因此必须写 mode,不能写 status。
            basCrnp.setCrnSts(defaultZero(crnProtocol.getMode()));
            basCrnp.setWrkNo(defaultZero(crnProtocol.getTaskNo()));
            basCrnp.setBay(crnProtocol.getBay());
            basCrnp.setLevel(crnProtocol.getLevel());
            basCrnp.setCrnErr(crnProtocol.getAlarm() == null ? 0L : Long.valueOf(crnProtocol.getAlarm()));
            basCrnp.setModiUser(WCS_SYNC_USER);
            basCrnp.setModiTime(now);