From 34eca433a02bee81572159c946fc7a6b08b448b2 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期六, 22 八月 2026 13:46:33 +0800
Subject: [PATCH] fix: 领料编号排序修改

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 57 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
index 0ec5ca8..f3340f4 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
@@ -556,6 +556,40 @@
         return value == null ? BigDecimal.ZERO : value;
     }
 
+    /**
+     * 鎵规鍙锋寜鍓嶇紑鏃ユ湡鍗囧簭鎺掑簭锛堟牸寮忓 20260703-ed01-001锛屾棩鏈熸棭鐨勫湪鍓嶏級
+     */
+    private void sortBatchNoList(List<String> batchNoList) {
+        if (batchNoList == null || batchNoList.size() < 2) {
+            return;
+        }
+        batchNoList.sort(Comparator
+                .comparingLong(this::batchDateKey)
+                .thenComparing(batchNo -> batchNo == null ? "" : batchNo));
+    }
+
+    /**
+     * 鎻愬彇鎵规鍙峰紑澶寸殑鏁板瓧浣滀负鎺掑簭閿紙yyyyMMdd锛夛紝鏃犳暟瀛楁椂鏀惧埌鏈�鍚�
+     */
+    private long batchDateKey(String batchNo) {
+        if (batchNo == null) {
+            return Long.MAX_VALUE;
+        }
+        String trimmed = batchNo.trim();
+        int end = 0;
+        while (end < trimmed.length() && Character.isDigit(trimmed.charAt(end))) {
+            end++;
+        }
+        if (end == 0) {
+            return Long.MAX_VALUE;
+        }
+        try {
+            return Long.parseLong(trimmed.substring(0, end));
+        } catch (NumberFormatException e) {
+            return Long.MAX_VALUE;
+        }
+    }
+
     private void validateAndFillOrder(ProductionOrder productionOrder, ProductionOrder oldOrder) {
         // 鏍¢獙璁㈠崟鍙傛暟骞惰ˉ榻愰粯璁ゅ��
         if (productionOrder == null) {
@@ -1107,9 +1141,30 @@
             }
         }
 
-        // 杩囨护鍑洪潪鏍硅妭鐐癸紙瀹為檯棰嗘枡椤癸級
+        // 鏀堕泦璁㈠崟鎴愬搧瑙勬牸ID锛氬寘鍚敓浜ц鍗曟垚鍝佽鏍间笌BOM鏍硅妭鐐硅鏍硷紝鐢ㄤ簬鎺掗櫎鈥滆嚜宸扁�濊繖涓�鐗╂枡
+        Set<Long> finishedProductModelIds = new HashSet<>();
+        if (productionOrder != null && productionOrder.getProductModelId() != null) {
+            finishedProductModelIds.add(productionOrder.getProductModelId());
+        }
+        bomStructureList.stream()
+                .filter(s -> s != null && (s.getParentId() == null || s.getParentId() == 0))
+                .map(ProductionBomStructureVo::getProductModelId)
+                .filter(Objects::nonNull)
+                .forEach(finishedProductModelIds::add);
+
+        // 鏀堕泦鎵�鏈夎寮曠敤涓虹埗绾х殑鑺傜偣ID锛欱OM浠庢渶鍚庝竴閬撳伐搴忓線閲岄厤缃墠涓�閬撳伐搴忥紝
+        // 涓棿鑺傜偣鏄笂涓�閬撳伐搴忕殑浜у嚭锛堣嚜鍒讹級锛屽彧鏈夋瘡鏉″垎鏀渶閲屽眰鐨勫彾瀛愯妭鐐规墠鏄湡姝i渶瑕侀鐢ㄧ殑鐗╂枡銆�
+        Set<Long> parentNodeIds = bomStructureList.stream()
+                .filter(s -> s != null && s.getParentId() != null && s.getParentId() != 0)
+                .map(ProductionBomStructureVo::getParentId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        // 杩囨护鍑哄彾瀛愯妭鐐癸紙瀹為檯棰嗘枡椤癸級锛氶潪鏍硅妭鐐广�侀潪涓棿鑺傜偣锛屽苟鎺掗櫎涓庤鍗曟垚鍝佸悓瑙勬牸鐨勭墿鏂欙紙鑷埗鎴愬搧鏃犻渶棰嗘枡锛�
         List<ProductionBomStructureVo> childStructureList = bomStructureList.stream()
                 .filter(s -> s != null && s.getParentId() != null && s.getParentId() != 0)
+                .filter(s -> s.getId() == null || !parentNodeIds.contains(s.getId()))
+                .filter(s -> s.getProductModelId() == null || !finishedProductModelIds.contains(s.getProductModelId()))
                 .collect(Collectors.toList());
 
         // 閬嶅巻澶勭悊鏁版嵁骞剁粍瑁呯粨鏋�
@@ -1158,6 +1213,7 @@
                 List<String> batchNoList = stockBatchNoMap.get(productModelId) == null
                         ? Collections.emptyList()
                         : new ArrayList<>(stockBatchNoMap.get(productModelId));
+                sortBatchNoList(batchNoList);
                 vo.setBatchNoList(batchNoList);
                 vo.setStockQuantity(stockQuantityMap.getOrDefault(productModelId, BigDecimal.ZERO));
                 vo.setBom(true);

--
Gitblit v1.9.3