From 5599c23fa95fea51cab371e6615e83c44d0f7a95 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 09 十月 2026 11:49:21 +0800
Subject: [PATCH] fix: 允许下发数量超出

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java |  130 +++++++++++++-----------------------------
 1 files changed, 41 insertions(+), 89 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 00a20de..defce97 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());
@@ -332,85 +340,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) {
@@ -876,9 +805,6 @@
                 BigDecimal percentage = task.getCompletionStatus() == null
                         ? BigDecimal.ZERO
                         : task.getCompletionStatus();
-                if (percentage.compareTo(new BigDecimal("100")) > 0) {
-                    percentage = new BigDecimal("100");
-                }
                 status.setPercentage(percentage);
                 statusMap.computeIfAbsent(task.getProductionOrderId(), key -> new ArrayList<>()).add(status);
             }
@@ -919,8 +845,17 @@
                 // 閬嶅巻澶勭悊鏁版嵁骞剁粍瑁呯粨鏋�
                 : workOrderPage.getRecords().stream()
                 .filter(Objects::nonNull)
-                .sorted(Comparator.comparing(ProductionOperationTaskVo::getId, Comparator.nullsLast(Comparator.naturalOrder())))
                 .toList();
+        if (!workOrderList.isEmpty()) {
+            // 宸ュ簭杩涘害闇�瑕佹寜宸ヨ壓璺嚎宸ュ簭椤哄簭锛坉rag_sort 鍗囧簭锛変粠宸﹀埌鍙冲睍绀猴紝浠诲姟 id 浠呬綔涓哄厹搴曟帓搴�
+            Map<Long, Integer> routingOperationDragSortMap = loadRoutingOperationDragSortMap(workOrderList);
+            Comparator<ProductionOperationTaskVo> workOrderComparator = Comparator
+                    .comparing((ProductionOperationTaskVo task) -> routingOperationDragSortMap.get(task.getProductionOrderRoutingOperationId()),
+                            Comparator.nullsLast(Comparator.<Integer>naturalOrder()))
+                    .thenComparing(ProductionOperationTaskVo::getId,
+                            Comparator.nullsLast(Comparator.<Long>naturalOrder()));
+            workOrderList = workOrderList.stream().sorted(workOrderComparator).toList();
+        }
         if (workOrderList.isEmpty()) {
             detailVo.setWorkOrderList(Collections.emptyList());
             return detailVo;
@@ -1069,6 +1004,23 @@
         return detailVo;
     }
 
+    /**
+     * 鏌ヨ宸ュ崟瀵瑰簲宸ヨ壓璺嚎宸ュ簭鐨勬嫋鍔ㄦ帓搴忥紝鐢ㄤ簬鎸夊伐搴忓厛鍚庨『搴忓睍绀哄伐搴忚繘搴�
+     */
+    private Map<Long, Integer> loadRoutingOperationDragSortMap(List<ProductionOperationTaskVo> workOrderList) {
+        Set<Long> routingOperationIds = workOrderList.stream()
+                .map(ProductionOperationTaskVo::getProductionOrderRoutingOperationId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+        if (routingOperationIds.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        return productionOrderRoutingOperationMapper.selectBatchIds(routingOperationIds).stream()
+                .filter(Objects::nonNull)
+                .filter(operation -> operation.getId() != null && operation.getDragSort() != null)
+                .collect(Collectors.toMap(ProductionOrderRoutingOperation::getId, ProductionOrderRoutingOperation::getDragSort, (a, b) -> a));
+    }
+
     private Long resolveProductionOrderId(ProductionOrderDto dto) {
         // 浠庡叆鍙備腑瑙f瀽鐢熶骇璁㈠崟ID骞舵牎楠�
         if (dto == null) {

--
Gitblit v1.9.3