From bd86068dd507b7ee076456ab71aa03394730076f Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 15 五月 2026 17:25:46 +0800
Subject: [PATCH] fix:1.工单产出数量根据bom需求数量赋值

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java |  102 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 65 insertions(+), 37 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 5a8ea9c..920188e 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
@@ -245,7 +245,18 @@
                         .eq(TechnologyRoutingOperation::getTechnologyRoutingId, technologyRouting.getId())
                         .orderByDesc(TechnologyRoutingOperation::getDragSort)
                         .orderByDesc(TechnologyRoutingOperation::getId));
-        Map<String, BigDecimal> operationDemandedQuantityMap = buildOperationDemandedQuantityMap(technologyRouting, productionOrder);
+        // 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()
@@ -279,7 +290,11 @@
                 ProductionOperationTask task = new ProductionOperationTask();
                 task.setProductionOrderRoutingOperationId(targetOperation.getId());
                 task.setProductionOrderId(productionOrder.getId());
-                task.setPlanQuantity(resolveTaskPlanQuantity(sourceOperation, operationDemandedQuantityMap, productionOrder));
+                task.setPlanQuantity(resolveTaskPlanQuantity(
+                        sourceOperation,
+                        operationDemandedQuantityMap,
+                        productionOrder,
+                        rootProductModelId));
                 task.setCompleteQuantity(BigDecimal.ZERO);
                 task.setWorkOrderNo(generateNextTaskNo());
                 task.setStatus(2);
@@ -314,47 +329,52 @@
         return syncedParamCount;
     }
 
-    private Map<String, BigDecimal> buildOperationDemandedQuantityMap(TechnologyRouting technologyRouting,
-                                                                      ProductionOrder productionOrder) {
-        if (technologyRouting == null || technologyRouting.getBomId() == null) {
+    private Map<String, BigDecimal> buildOperationDemandedQuantityMap(List<ProductionBomStructure> bomStructures,
+                                                                      Long rootProductModelId) {
+        if (bomStructures == null || bomStructures.isEmpty()) {
             return Collections.emptyMap();
         }
-        BigDecimal orderQuantity = defaultDecimal(productionOrder == null ? null : productionOrder.getQuantity());
-        List<TechnologyBomStructure> bomStructures = technologyBomStructureMapper.selectList(
-                Wrappers.<TechnologyBomStructure>lambdaQuery()
-                        .eq(TechnologyBomStructure::getBomId, technologyRouting.getBomId())
-                        .isNotNull(TechnologyBomStructure::getOperationId)
-                        .orderByAsc(TechnologyBomStructure::getId));
-        if (bomStructures.isEmpty()) {
-            return Collections.emptyMap();
-        }
-
-        Map<Long, TechnologyBomStructure> structureById = bomStructures.stream()
+        Map<Long, ProductionBomStructure> structureById = bomStructures.stream()
                 .filter(item -> item != null && item.getId() != null)
-                .collect(Collectors.toMap(TechnologyBomStructure::getId, item -> item, (left, right) -> left));
+                .collect(Collectors.toMap(ProductionBomStructure::getId, item -> item, (left, right) -> left));
         Map<String, BigDecimal> demandedQuantityMap = new HashMap<>();
-        for (TechnologyBomStructure bomStructure : bomStructures) {
-            if (bomStructure == null || bomStructure.getOperationId() == null) {
+        Set<String> mergedOutputNodeKeySet = new HashSet<>();
+        for (ProductionBomStructure bomStructure : bomStructures) {
+            if (bomStructure == null || bomStructure.getTechnologyOperationId() == null) {
                 continue;
             }
-            BigDecimal unitQuantity = bomStructure.getUnitQuantity();
-            if (unitQuantity == null) {
+            // 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;
             }
-            Long outputProductModelId = resolveOutputProductModelId(bomStructure, structureById, technologyRouting.getProductModelId());
-            String key = buildOperationDemandedQuantityKey(bomStructure.getOperationId(), outputProductModelId);
-            demandedQuantityMap.merge(key, unitQuantity.multiply(orderQuantity), BigDecimal::add);
+            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) {
+                                               ProductionOrder productionOrder,
+                                               Long rootProductModelId) {
         if (sourceOperation == null || operationDemandedQuantityMap == null || operationDemandedQuantityMap.isEmpty()) {
             return defaultDecimal(productionOrder == null ? null : productionOrder.getQuantity());
         }
-        String key = buildOperationDemandedQuantityKey(sourceOperation.getTechnologyOperationId(), sourceOperation.getProductModelId());
+        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());
     }
@@ -363,21 +383,29 @@
         return String.valueOf(operationId) + "#" + String.valueOf(outputProductModelId);
     }
 
-    private Long resolveOutputProductModelId(TechnologyBomStructure bomStructure,
-                                             Map<Long, TechnologyBomStructure> structureById,
-                                             Long routingProductModelId) {
+    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 routingProductModelId;
+            return null;
         }
-        Long parentId = bomStructure.getParentId();
-        if (parentId == null) {
-            return routingProductModelId != null ? routingProductModelId : bomStructure.getProductModelId();
+        // 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;
         }
-        TechnologyBomStructure parent = structureById.get(parentId);
-        if (parent != null && parent.getProductModelId() != null) {
-            return parent.getProductModelId();
+        ProductionBomStructure parent = structureById.get(bomStructure.getParentId());
+        return parent != null ? parent : bomStructure;
+    }
+
+    private Long resolveOutputProductModelId(ProductionBomStructure outputNode,
+                                             Long rootProductModelId) {
+        if (outputNode == null) {
+            return rootProductModelId;
         }
-        return routingProductModelId != null ? routingProductModelId : bomStructure.getProductModelId();
+        return outputNode.getProductModelId() != null ? outputNode.getProductModelId() : rootProductModelId;
     }
 
     private ProductionOrderBom syncProductionOrderBomSnapshot(ProductionOrder productionOrder, TechnologyRouting technologyRouting) {

--
Gitblit v1.9.3