From 4031cede20c17c15772d8c4f17064dbc1f90234f Mon Sep 17 00:00:00 2001
From: zwl <1051256694@qq.com>
Date: 星期一, 20 四月 2026 21:43:23 +0800
Subject: [PATCH] 1.12天入库体积统计

---
 src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java |  198 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 168 insertions(+), 30 deletions(-)

diff --git a/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java b/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
index fd6422b..3d39bb7 100644
--- a/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
+++ b/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
@@ -35,6 +35,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;
@@ -203,6 +204,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()) {
@@ -211,8 +213,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;
             }
 
@@ -464,6 +480,56 @@
         return outboundPltSlotReleasedInWms(userNo, batchGroup, minPlt - 1);
     }
 
+    private String validateOutboundBatchSeqReady(List<WorkTaskParams> chunk, Map<String, WrkMast> wrkMastMap) {
+        if (chunk == null || chunk.isEmpty()) {
+            return null;
+        }
+        WorkTaskParams head = chunk.get(0);
+        if (!"out".equalsIgnoreCase(head.getType())) {
+            return null;
+        }
+        WrkMast headMast = wrkMastMap.get(head.getTaskNo());
+        String userNo = sortUserNoForPub(head, headMast);
+        if (Cools.isEmpty(userNo)) {
+            return null;
+        }
+        String batchSeq = sortBatchGroupForPub(head, headMast);
+        String blockingBatchSeq = findFirstUnfinishedOutboundBatchSeq(userNo);
+        if (blockingBatchSeq == null || compareBatchSeqNatural(batchSeq, blockingBatchSeq) == 0) {
+            return null;
+        }
+        return "鍑哄簱鎵规鏈畬鎴愶紝鏆傚仠鍚庣画涓嬪彂, userNo=" + userNo
+                + ", blockingBatchSeq=" + normalizeBatchSeq(blockingBatchSeq)
+                + ", nextBatchSeq=" + normalizeBatchSeq(batchSeq);
+    }
+
+    private String findFirstUnfinishedOutboundBatchSeq(String userNo) {
+        EntityWrapper<WrkMast> wrapper = new EntityWrapper<>();
+        if (Cools.isEmpty(userNo)) {
+            wrapper.isNull("user_no");
+        } else {
+            wrapper.eq("user_no", userNo);
+        }
+        wrapper.eq("io_type", 101);
+        wrapper.last(" and (wrk_sts < 14 or wrk_sts in ("
+                + OUT_LOCK_REPORT_SUCCESS_WRK_STS + "," + OUT_LOCK_REPORT_FAIL_WRK_STS + "))");
+        List<WrkMast> rows = wrkMastService.selectList(wrapper);
+        if (rows == null || rows.isEmpty()) {
+            return null;
+        }
+        String firstBatchSeq = null;
+        for (WrkMast row : rows) {
+            if (row == null) {
+                continue;
+            }
+            String batchSeq = sortBatchGroupForPub(null, row);
+            if (firstBatchSeq == null || compareBatchSeqNatural(batchSeq, firstBatchSeq) < 0) {
+                firstBatchSeq = batchSeq;
+            }
+        }
+        return firstBatchSeq;
+    }
+
     private boolean outboundPltSlotReleasedInWms(String userNo, String batchSeq, int pltType) {
         EntityWrapper<WrkMast> mastWrapper = new EntityWrapper<>();
         mastWrapper.eq("user_no", userNo);
@@ -521,7 +587,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())), 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));
     }
 
@@ -545,6 +611,64 @@
             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) {
@@ -608,23 +732,7 @@
                 //wcs浠诲姟鍙栨秷鎺ュ彛
             } else if (params.getMsgType().equals("task_cancel")) {
                 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();
     }
@@ -706,19 +814,26 @@
             return R.error("褰撳墠鐩爣搴撲綅涓嶅瓨鍦�");
         }
 
-        Integer preferredArea = resolveReassignArea(wrkMast, currentLoc);
-        if (preferredArea == null) {
+        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc);
+        List<Integer> areaOrder = buildReassignAreaOrder(wrkMast, currentLoc);
+        if (Cools.isEmpty(areaOrder)) {
             return R.error("鏃犳硶纭畾浠诲姟鎵�灞炲簱鍖�");
         }
 
-        List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(preferredArea, wrkMast.getCrnNo());
-        if (candidateCrnNos.isEmpty()) {
-            return R.error("褰撳墠搴撳尯娌℃湁鍏朵粬鍫嗗灈鏈哄彲渚涢噸鍒嗛厤");
+        StartupDto startupDto = null;
+        Integer preferredArea = null;
+        for (Integer area : areaOrder) {
+            List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(area, wrkMast.getCrnNo());
+            if (candidateCrnNos.isEmpty()) {
+                continue;
+            }
+            startupDto = commonService.findRun2InboundLocByCandidateCrnNos(
+                    wrkMast.getSourceStaNo(), wrkMast.getIoType(), area, candidateCrnNos, locTypeDto);
+            if (startupDto != null && !Cools.isEmpty(startupDto.getLocNo())) {
+                preferredArea = area;
+                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("褰撳墠搴撳尯娌℃湁鍙噸鏂板垎閰嶇殑绌哄簱浣�");
         }
@@ -801,15 +916,19 @@
     }
 
     private Integer resolveReassignArea(WrkMast wrkMast, LocMast currentLoc) {
-        Integer stationArea = Utils.getStationStorageArea(wrkMast.getSourceStaNo());
-        if (belongsToArea(stationArea, wrkMast.getCrnNo(), currentLoc)) {
-            return stationArea;
+        List<Integer> stationAreas = Utils.getStationStorageAreas(wrkMast.getSourceStaNo());
+        if (!Cools.isEmpty(stationAreas)) {
+            for (Integer area : stationAreas) {
+                if (belongsToArea(area, wrkMast.getCrnNo(), currentLoc)) {
+                    return area;
+                }
+            }
         }
         Integer fallbackArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
         if (fallbackArea != null) {
             return fallbackArea;
         }
-        return stationArea;
+        return Utils.getStationStorageArea(wrkMast.getSourceStaNo());
     }
 
     private Integer findAreaByCurrentTask(Integer currentCrnNo, LocMast currentLoc) {
@@ -821,6 +940,25 @@
         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);
+        }
+        Integer currentArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
+        if (currentArea != null) {
+            areaOrder.add(currentArea);
+        }
+        if (areaOrder.isEmpty()) {
+            Integer resolvedArea = resolveReassignArea(wrkMast, currentLoc);
+            if (resolvedArea != null) {
+                areaOrder.add(resolvedArea);
+            }
+        }
+        return new ArrayList<>(areaOrder);
+    }
+
     private boolean belongsToArea(Integer area, Integer currentCrnNo, LocMast currentLoc) {
         if (area == null || area <= 0) {
             return false;

--
Gitblit v1.9.1