From 10570a9fa5e87231210a449eddc732c7d0599349 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期一, 24 八月 2026 16:02:55 +0800
Subject: [PATCH] fix: 报工计算修改

---
 src/main/java/com/ruoyi/production/util/TaskPlanQuantityUtil.java |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 53 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/util/TaskPlanQuantityUtil.java b/src/main/java/com/ruoyi/production/util/TaskPlanQuantityUtil.java
index e82d7e4..1e1696e 100644
--- a/src/main/java/com/ruoyi/production/util/TaskPlanQuantityUtil.java
+++ b/src/main/java/com/ruoyi/production/util/TaskPlanQuantityUtil.java
@@ -88,6 +88,49 @@
         return demandedQuantityMap;
     }
 
+    public Map<Long, Set<Long>> buildOperationOutputProductModelMap(List<ProductionBomStructure> bomStructures,
+                                                                     Long rootProductModelId) {
+        if (bomStructures == null || bomStructures.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        Map<Long, ProductionBomStructure> structureById = new HashMap<>();
+        for (ProductionBomStructure item : bomStructures) {
+            if (item != null && item.getId() != null) {
+                structureById.put(item.getId(), item);
+            }
+        }
+        Map<Long, Set<Long>> outputProductModelMap = new HashMap<>();
+        for (ProductionBomStructure bomStructure : bomStructures) {
+            if (bomStructure == null || bomStructure.getTechnologyOperationId() == null) {
+                continue;
+            }
+            ProductionBomStructure outputNode = resolveOperationOutputNode(bomStructure, structureById);
+            Long outputProductModelId = resolveOutputProductModelId(outputNode, rootProductModelId);
+            if (outputProductModelId != null) {
+                outputProductModelMap.computeIfAbsent(
+                        bomStructure.getTechnologyOperationId(), key -> new LinkedHashSet<>())
+                        .add(outputProductModelId);
+            }
+        }
+        return outputProductModelMap;
+    }
+
+    public Long resolveOperationOutputProductModelId(Long operationId,
+                                                      Long preferredProductModelId,
+                                                      Map<Long, Set<Long>> outputProductModelMap,
+                                                      Long rootProductModelId) {
+        Set<Long> candidates = outputProductModelMap == null ? null : outputProductModelMap.get(operationId);
+        if (candidates != null && !candidates.isEmpty()) {
+            if (preferredProductModelId != null && candidates.contains(preferredProductModelId)) {
+                return preferredProductModelId;
+            }
+            if (candidates.size() == 1) {
+                return candidates.iterator().next();
+            }
+        }
+        return preferredProductModelId != null ? preferredProductModelId : rootProductModelId;
+    }
+
     /**
      * 鏋勫缓宸ュ簭闇�姹傞噺key
      */
@@ -110,13 +153,22 @@
         if (bomStructure == null) {
             return null;
         }
-        if (bomStructure.getParentId() == null) {
+        if (bomStructure.getParentId() == null || hasChildren(bomStructure, structureById)) {
             return bomStructure;
         }
         ProductionBomStructure parent = structureById.get(bomStructure.getParentId());
         return parent != null ? parent : bomStructure;
     }
 
+    private boolean hasChildren(ProductionBomStructure bomStructure,
+                                Map<Long, ProductionBomStructure> structureById) {
+        if (bomStructure == null || bomStructure.getId() == null || structureById == null) {
+            return false;
+        }
+        return structureById.values().stream()
+                .anyMatch(child -> child != null && Objects.equals(child.getParentId(), bomStructure.getId()));
+    }
+
     /**
      * 瑙f瀽杈撳嚭浜у搧瑙勬牸ID
      */

--
Gitblit v1.9.3