From f57064d2fa563ee4cfeeccd715850ba8b8aa4f60 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期一, 18 五月 2026 09:44:33 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New_pro' into dev_宁夏_英泽防锈

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java |  107 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 101 insertions(+), 6 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 344800c..ecdb37c 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
@@ -245,6 +245,18 @@
                         .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.
+        Long rootProductModelId = orderBom != null && orderBom.getProductModelId() != null
+                ? orderBom.getProductModelId()
+                : productionOrder.getProductModelId();
+        List<ProductionBomStructure> orderBomStructureList = orderBom == null || orderBom.getId() == null
+                ? Collections.emptyList()
+                : productionBomStructureMapper.selectList(
+                Wrappers.<ProductionBomStructure>lambdaQuery()
+                        .eq(ProductionBomStructure::getProductionOrderBomId, orderBom.getId())
+                        .orderByAsc(ProductionBomStructure::getId));
+        Map<String, BigDecimal> operationDemandedQuantityMap =
+                buildOperationDemandedQuantityMap(orderBomStructureList, rootProductModelId);
         Map<Long, String> operationNameMap = technologyOperationMapper.selectBatchIds(
         // 閬嶅巻澶勭悊鏁版嵁骞剁粍瑁呯粨鏋�
                         routingOperations.stream()
@@ -278,7 +290,11 @@
                 ProductionOperationTask task = new ProductionOperationTask();
                 task.setProductionOrderRoutingOperationId(targetOperation.getId());
                 task.setProductionOrderId(productionOrder.getId());
-                task.setPlanQuantity(defaultDecimal(productionOrder.getQuantity()));
+                task.setPlanQuantity(resolveTaskPlanQuantity(
+                        sourceOperation,
+                        operationDemandedQuantityMap,
+                        productionOrder,
+                        rootProductModelId));
                 task.setCompleteQuantity(BigDecimal.ZERO);
                 task.setWorkOrderNo(generateNextTaskNo());
                 task.setStatus(2);
@@ -313,6 +329,85 @@
         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) {
@@ -342,6 +437,7 @@
         productionOrderBomMapper.insert(orderBom);
 
         Map<Long, Long> idMap = new HashMap<>();
+        BigDecimal lastProcessDemandedQuantity = orderQuantity;
         for (TechnologyBomStructure source : structureList) {
             // 瀛愯妭鐐� parentId 闇�瑕佹槧灏勬垚鏂板揩鐓ц妭鐐� id锛屾墠鑳戒繚鐣欏師濮� BOM 灞傜骇銆�
             ProductionBomStructure target = new ProductionBomStructure();
@@ -351,10 +447,11 @@
             target.setProductModelId(source.getProductModelId());
             target.setTechnologyOperationId(source.getOperationId());
             target.setUnitQuantity(source.getUnitQuantity());
-            target.setDemandedQuantity(source.getUnitQuantity().multiply(orderQuantity));
+            target.setDemandedQuantity(lastProcessDemandedQuantity.multiply(source.getUnitQuantity()));
             target.setUnit(source.getUnit());
             productionBomStructureMapper.insert(target);
             idMap.put(source.getId(), target.getId());
+            lastProcessDemandedQuantity = target.getDemandedQuantity();
         }
         return orderBom;
     }
@@ -788,12 +885,10 @@
                 if (reportOutput == null) {
                     continue;
                 }
-                Long reportMainId = reportOutput.getProductionProductMainId() != null
-                        ? reportOutput.getProductionProductMainId()
-                        : reportOutput.getProductMainId();
-                if (reportMainId == null) {
+                if (reportOutput.getProductionProductMainId() == null) {
                     continue;
                 }
+                Long reportMainId = reportOutput.getProductionProductMainId();
                 reportOutputMap.computeIfAbsent(reportMainId, k -> new ArrayList<>()).add(reportOutput);
             }
 

--
Gitblit v1.9.3