From 280261219bafff383fe37dfc24f148baca4672e1 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 28 八月 2026 20:40:17 +0800
Subject: [PATCH] fix: 操作日志根据操作时间倒序

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java |  174 +++++++++++++++++++++------------------------------------
 1 files changed, 65 insertions(+), 109 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 8c710cf..4d98b79 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
@@ -34,6 +34,7 @@
 import com.ruoyi.quality.pojo.QualityInspectFile;
 import com.ruoyi.quality.pojo.QualityInspectParam;
 import com.ruoyi.production.service.ProductionOrderService;
+import com.ruoyi.production.util.TaskPlanQuantityUtil;
 import com.ruoyi.sales.mapper.SalesLedgerMapper;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
@@ -250,7 +251,7 @@
                         .eq(TechnologyRoutingOperation::getTechnologyRoutingId, technologyRouting.getId())
                         .orderByDesc(TechnologyRoutingOperation::getDragSort)
                         .orderByDesc(TechnologyRoutingOperation::getId));
-        // Build task plan quantities from order BOM snapshot demand instead of recomputing from technology BOM units.
+        // 宸ュ崟璁″垝鏁伴噺鐩存帴浣跨敤璁㈠崟 BOM 蹇収涓殑闇�姹傞噺锛岄伩鍏嶄笌 BOM 椤甸潰灞曠ず鍙e緞涓嶄竴鑷淬��
         Long rootProductModelId = orderBom != null && orderBom.getProductModelId() != null
                 ? orderBom.getProductModelId()
                 : productionOrder.getProductModelId();
@@ -261,7 +262,9 @@
                         .eq(ProductionBomStructure::getProductionOrderBomId, orderBom.getId())
                         .orderByAsc(ProductionBomStructure::getId));
         Map<String, BigDecimal> operationDemandedQuantityMap =
-                buildOperationDemandedQuantityMap(orderBomStructureList, rootProductModelId);
+                TaskPlanQuantityUtil.buildOperationDemandedQuantityMap(orderBomStructureList, rootProductModelId);
+        Map<Long, Set<Long>> operationOutputProductModelMap =
+                TaskPlanQuantityUtil.buildOperationOutputProductModelMap(orderBomStructureList, rootProductModelId);
         Map<Long, String> operationNameMap = technologyOperationMapper.selectBatchIds(
                         // 閬嶅巻澶勭悊鏁版嵁骞剁粍瑁呯粨鏋�
                         routingOperations.stream()
@@ -281,7 +284,12 @@
             targetOperation.setProductionOrderId(productionOrder.getId());
             targetOperation.setTechnologyRoutingOperationId(sourceOperation.getId());
             targetOperation.setOrderRoutingId(orderRouting.getId());
-            targetOperation.setProductModelId(sourceOperation.getProductModelId());
+            Long outputProductModelId = TaskPlanQuantityUtil.resolveOperationOutputProductModelId(
+                    sourceOperation.getTechnologyOperationId(),
+                    sourceOperation.getProductModelId(),
+                    operationOutputProductModelMap,
+                    rootProductModelId);
+            targetOperation.setProductModelId(outputProductModelId);
             targetOperation.setDragSort(sourceOperation.getDragSort());
             targetOperation.setIsProduction(sourceOperation.getIsProduction());
             targetOperation.setIsQuality(sourceOperation.getIsQuality());
@@ -295,10 +303,10 @@
                 ProductionOperationTask task = new ProductionOperationTask();
                 task.setProductionOrderRoutingOperationId(targetOperation.getId());
                 task.setProductionOrderId(productionOrder.getId());
-                task.setPlanQuantity(resolveTaskPlanQuantity(
-                        sourceOperation,
+                task.setPlanQuantity(TaskPlanQuantityUtil.resolveTaskPlanQuantity(
+                        targetOperation,
                         operationDemandedQuantityMap,
-                        productionOrder,
+                        productionOrder.getQuantity(),
                         rootProductModelId));
                 task.setCompleteQuantity(BigDecimal.ZERO);
                 task.setWorkOrderNo(generateNextTaskNo());
@@ -334,85 +342,6 @@
         return syncedParamCount;
     }
 
-    private Map<String, BigDecimal> buildOperationDemandedQuantityMap(List<ProductionBomStructure> bomStructures,
-                                                                      Long rootProductModelId) {
-        if (bomStructures == null || bomStructures.isEmpty()) {
-            return Collections.emptyMap();
-        }
-        Map<Long, ProductionBomStructure> structureById = bomStructures.stream()
-                .filter(item -> item != null && item.getId() != null)
-                .collect(Collectors.toMap(ProductionBomStructure::getId, item -> item, (left, right) -> left));
-        Map<String, BigDecimal> demandedQuantityMap = new HashMap<>();
-        Set<String> mergedOutputNodeKeySet = new HashSet<>();
-        for (ProductionBomStructure bomStructure : bomStructures) {
-            if (bomStructure == null || bomStructure.getTechnologyOperationId() == null) {
-                continue;
-            }
-            // The BOM row points to the producing operation; task quantity should come from that operation's output node.
-            ProductionBomStructure outputNode = resolveOperationOutputNode(bomStructure, structureById);
-            Long outputProductModelId = resolveOutputProductModelId(outputNode, rootProductModelId);
-            if (outputProductModelId == null) {
-                continue;
-            }
-            String mergedOutputNodeKey = buildOperationOutputNodeKey(
-                    bomStructure.getTechnologyOperationId(),
-                    outputNode == null ? null : outputNode.getId(),
-                    outputProductModelId);
-            if (!mergedOutputNodeKeySet.add(mergedOutputNodeKey)) {
-                continue;
-            }
-            // demandedQuantity is already the order-level required output quantity for the current output node.
-            BigDecimal demandedQuantity = defaultDecimal(outputNode == null ? null : outputNode.getDemandedQuantity());
-            String key = buildOperationDemandedQuantityKey(bomStructure.getTechnologyOperationId(), outputProductModelId);
-            demandedQuantityMap.merge(key, demandedQuantity, BigDecimal::add);
-        }
-        return demandedQuantityMap;
-    }
-
-    private BigDecimal resolveTaskPlanQuantity(TechnologyRoutingOperation sourceOperation,
-                                               Map<String, BigDecimal> operationDemandedQuantityMap,
-                                               ProductionOrder productionOrder,
-                                               Long rootProductModelId) {
-        if (sourceOperation == null || operationDemandedQuantityMap == null || operationDemandedQuantityMap.isEmpty()) {
-            return defaultDecimal(productionOrder == null ? null : productionOrder.getQuantity());
-        }
-        Long outputProductModelId = sourceOperation.getProductModelId() != null
-                ? sourceOperation.getProductModelId()
-                : rootProductModelId;
-        String key = buildOperationDemandedQuantityKey(sourceOperation.getTechnologyOperationId(), outputProductModelId);
-        BigDecimal planQuantity = operationDemandedQuantityMap.get(key);
-        return planQuantity != null ? planQuantity : defaultDecimal(productionOrder == null ? null : productionOrder.getQuantity());
-    }
-
-    private String buildOperationDemandedQuantityKey(Long operationId, Long outputProductModelId) {
-        return String.valueOf(operationId) + "#" + String.valueOf(outputProductModelId);
-    }
-
-    private String buildOperationOutputNodeKey(Long operationId, Long outputNodeId, Long outputProductModelId) {
-        return String.valueOf(operationId) + "#" + String.valueOf(outputNodeId) + "#" + String.valueOf(outputProductModelId);
-    }
-
-    private ProductionBomStructure resolveOperationOutputNode(ProductionBomStructure bomStructure,
-                                                              Map<Long, ProductionBomStructure> structureById) {
-        if (bomStructure == null) {
-            return null;
-        }
-        // The root node is the first output node; child rows use their direct parent as the current operation output.
-        if (bomStructure.getParentId() == null) {
-            return bomStructure;
-        }
-        ProductionBomStructure parent = structureById.get(bomStructure.getParentId());
-        return parent != null ? parent : bomStructure;
-    }
-
-    private Long resolveOutputProductModelId(ProductionBomStructure outputNode,
-                                             Long rootProductModelId) {
-        if (outputNode == null) {
-            return rootProductModelId;
-        }
-        return outputNode.getProductModelId() != null ? outputNode.getProductModelId() : rootProductModelId;
-    }
-
     private ProductionOrderBom syncProductionOrderBomSnapshot(ProductionOrder productionOrder, TechnologyRouting technologyRouting) {
         // 鍚屾璁㈠崟BOM蹇収缁撴瀯
         if (technologyRouting.getBomId() == null) {
@@ -442,7 +371,6 @@
         productionOrderBomMapper.insert(orderBom);
 
         Map<Long, Long> idMap = new HashMap<>();
-        BigDecimal lastProcessDemandedQuantity = orderQuantity;
         for (TechnologyBomStructure source : structureList) {
             // 瀛愯妭鐐� parentId 闇�瑕佹槧灏勬垚鏂板揩鐓ц妭鐐� id锛屾墠鑳戒繚鐣欏師濮� BOM 灞傜骇銆�
             ProductionBomStructure target = new ProductionBomStructure();
@@ -452,11 +380,11 @@
             target.setProductModelId(source.getProductModelId());
             target.setTechnologyOperationId(source.getOperationId());
             target.setUnitQuantity(source.getUnitQuantity());
-            target.setDemandedQuantity(lastProcessDemandedQuantity.multiply(source.getUnitQuantity()));
+            target.setDemandedQuantity(defaultDecimal(orderQuantity)
+                    .multiply(defaultDecimal(source.getUnitQuantity())));
             target.setUnit(source.getUnit());
             productionBomStructureMapper.insert(target);
             idMap.put(source.getId(), target.getId());
-            lastProcessDemandedQuantity = target.getDemandedQuantity();
         }
         return orderBom;
     }
@@ -554,6 +482,40 @@
     private BigDecimal defaultDecimal(BigDecimal value) {
         // 灏嗙┖鏁伴噺鍏滃簳涓�0锛岄伩鍏嶇┖鎸囬拡寮傚父
         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) {
@@ -1074,7 +1036,7 @@
             return Collections.emptyList();
         }
 
-        // 鏌ヨ瀹屾暣鐨凚OM缁撴瀯锛堝寘鎷牴鑺傜偣锛夛紝鐢ㄤ簬璁$畻灞傜骇闇�姹傛暟閲�
+        // 鏌ヨ瀹屾暣鐨凚OM缁撴瀯锛堝寘鎷牴鑺傜偣锛夛紝姣忎釜鑺傜偣鐙珛璁$畻闇�姹傛暟閲�
         List<ProductionBomStructureVo> bomStructureList = productionBomStructureMapper.listByBomId(orderBom.getId());
         if (bomStructureList == null || bomStructureList.isEmpty()) {
             return Collections.emptyList();
@@ -1084,26 +1046,10 @@
         ProductionOrder productionOrder = productionOrderMapper.selectById(productionOrderId);
         BigDecimal orderQuantity = productionOrder != null ? defaultDecimal(productionOrder.getQuantity()) : BigDecimal.ZERO;
 
-        // 鏋勫缓鏍戝舰缁撴瀯骞惰绠楀眰绾ч渶姹傛暟閲�
-        Map<Long, ProductionBomStructureVo> structureByIdMap = bomStructureList.stream()
-                .filter(s -> s != null && s.getId() != null)
-                .collect(Collectors.toMap(ProductionBomStructureVo::getId, s -> s));
-
-        // 鎸夊眰绾ц绠楅渶姹傛暟閲忥細瀛愮骇闇�姹傛暟閲� = 鐖剁骇闇�姹傛暟閲� 脳 瀛愮骇鍗曚綅浜у嚭鎵�闇�鏁伴噺
         for (ProductionBomStructureVo structure : bomStructureList) {
-            if (structure == null) continue;
-
-            if (structure.getParentId() == null || structure.getParentId() == 0) {
-                // 鏍硅妭鐐癸細闇�姹傛暟閲� = 璁㈠崟鏁伴噺
-                structure.setDemandedQuantity(orderQuantity);
-            } else {
-                // 瀛愯妭鐐癸細闇�姹傛暟閲� = 鐖剁骇闇�姹傛暟閲� 脳 瀛愮骇鍗曚綅浜у嚭鎵�闇�鏁伴噺
-                ProductionBomStructureVo parent = structureByIdMap.get(structure.getParentId());
-                if (parent != null) {
-                    BigDecimal parentDemandedQty = defaultDecimal(parent.getDemandedQuantity());
-                    BigDecimal unitQuantity = defaultDecimal(structure.getUnitQuantity());
-                    structure.setDemandedQuantity(parentDemandedQty.multiply(unitQuantity));
-                }
+            if (structure != null) {
+                structure.setDemandedQuantity(orderQuantity
+                        .multiply(defaultDecimal(structure.getUnitQuantity())));
             }
         }
 
@@ -1118,9 +1064,18 @@
                 .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());
 
@@ -1170,6 +1125,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