From 9cc2ddd5012a448d6187250121ba7f0cd0c5663b Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期一, 10 八月 2026 10:17:25 +0800
Subject: [PATCH] 下发报错修改

---
 src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java |   80 +++++++++++++++++++++++---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java        |   62 +++++++++++++++++++-
 2 files changed, 129 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java
index 0513bdc..8278148 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java
@@ -31,6 +31,7 @@
 import com.ruoyi.technology.pojo.TechnologyOperationParam;
 import com.ruoyi.technology.pojo.TechnologyParam;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -49,6 +50,7 @@
  * @author 鑺杞欢锛堟睙鑻忥級鏈夐檺鍏徃
  * @since 2026-04-21 03:55:52
  */
+@Slf4j
 @Service
 @RequiredArgsConstructor()
 public class ProductionBomStructureServiceImpl extends ServiceImpl<ProductionBomStructureMapper, ProductionBomStructure> implements ProductionBomStructureService {
@@ -137,6 +139,9 @@
             entity.setProductionOrderBomId(orderBomId);
             if (item.getId() == null) {
                 entity.setParentId(null);
+                // 鏂板鑺傜偣鐨勯渶姹傞噺鐢卞悗绔寜BOM鏍戝舰缁撴瀯閲嶆柊璁$畻锛屼笉娓呯┖鍙兘瀵艰嚧鍓嶇浼犵殑鍊兼孩鍑�
+                entity.setDemandedQuantity(null);
+                entity.setId(null);
                 insertList.add(entity);
                 tempEntityMap.put(item.getTempId(), entity);
             } else {
@@ -146,6 +151,11 @@
 
         // 鎵归噺鏂板锛屾嬁鍒版暟鎹簱鐢熸垚鐨勭湡瀹濱D
         if (!insertList.isEmpty()) {
+            for (ProductionBomStructure entity : insertList) {
+                log.info("鍑嗗鏂板BOM缁撴瀯鑺傜偣: productModelId={}, technologyOperationId={}, unitQuantity={}, demandedQuantity={}, unit={}",
+                        entity.getProductModelId(), entity.getTechnologyOperationId(),
+                        entity.getUnitQuantity(), entity.getDemandedQuantity(), entity.getUnit());
+            }
             this.saveBatch(insertList);
         }
 
@@ -216,24 +226,76 @@
         if (structureList == null || structureList.isEmpty()) {
             return;
         }
-        List<ProductionBomStructure> updateList = new ArrayList<>();
-        BigDecimal lastProcessDemandedQuantity = orderQuantity;
+        log.info("寮�濮嬫寜BOM鏍戝舰缁撴瀯璁$畻闇�姹傞噺锛岃鍗曟暟閲�={}, BOM鑺傜偣鎬绘暟={}", orderQuantity, structureList.size());
+        // 鏋勫缓 id 鈫� structure 鏄犲皠鍜� parentId 鈫� children 鏄犲皠
+        Map<Long, ProductionBomStructure> structureById = new HashMap<>();
+        Map<Long, List<ProductionBomStructure>> childrenMap = new LinkedHashMap<>();
         for (ProductionBomStructure structure : structureList) {
             if (structure == null || structure.getId() == null) {
                 continue;
             }
+            structureById.put(structure.getId(), structure);
+            Long parentId = structure.getParentId();
+            childrenMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(structure);
+        }
 
-            BigDecimal demandedQuantity = lastProcessDemandedQuantity.multiply(defaultDecimal(structure.getUnitQuantity()));
-//            if (compareDecimal(structure.getDemandedQuantity(), demandedQuantity) == 0) {
-//                continue;
-//            }
+        List<ProductionBomStructure> updateList = new ArrayList<>();
+        // 浠庢牴鑺傜偣寮�濮嬶紝鎸夋爲褰㈠眰绾ч�愬眰璁$畻闇�姹傞噺
+        // 鏍硅妭鐐� parentId 涓� null 鎴� 0
+        Deque<Long> queue = new ArrayDeque<>();
+        List<ProductionBomStructure> rootNodes = childrenMap.get(null);
+        if (rootNodes != null) {
+            for (ProductionBomStructure root : rootNodes) {
+                queue.add(root.getId());
+            }
+        }
+        // parentId=0 鐨勪篃瑙嗕负鏍硅妭鐐�
+        List<ProductionBomStructure> zeroParentNodes = childrenMap.get(0L);
+        if (zeroParentNodes != null) {
+            for (ProductionBomStructure root : zeroParentNodes) {
+                if (!queue.contains(root.getId())) {
+                    queue.add(root.getId());
+                }
+            }
+        }
+
+        while (!queue.isEmpty()) {
+            Long nodeId = queue.poll();
+            ProductionBomStructure node = structureById.get(nodeId);
+            if (node == null) {
+                continue;
+            }
+            // 璁$畻褰撳墠鑺傜偣鐨勯渶姹傛暟閲� = 鐖惰妭鐐归渶姹傞噺 脳 鑷韩 unitQuantity
+            // 鏍硅妭鐐癸細闇�姹傞噺 = 璁㈠崟鏁伴噺 脳 unitQuantity
+            BigDecimal parentDemandedQuantity = orderQuantity;
+            Long parentId = node.getParentId();
+            if (parentId != null && parentId != 0L) {
+                ProductionBomStructure parent = structureById.get(parentId);
+                if (parent != null && parent.getDemandedQuantity() != null) {
+                    parentDemandedQuantity = parent.getDemandedQuantity();
+                }
+            }
+            BigDecimal demandedQuantity = parentDemandedQuantity.multiply(defaultDecimal(node.getUnitQuantity()));
+
+            log.info("BOM闇�姹傞噺璁$畻: nodeId={}, parentId={}, parentDemandedQuantity={}, unitQuantity={}, demandedQuantity={}",
+                    node.getId(), node.getParentId(), parentDemandedQuantity,
+                    defaultDecimal(node.getUnitQuantity()), demandedQuantity);
+
             ProductionBomStructure update = new ProductionBomStructure();
-            update.setId(structure.getId());
+            update.setId(node.getId());
             update.setDemandedQuantity(demandedQuantity);
             updateList.add(update);
-            structure.setDemandedQuantity(demandedQuantity);
-            lastProcessDemandedQuantity = demandedQuantity;
+            node.setDemandedQuantity(demandedQuantity);
+
+            // 灏嗗瓙鑺傜偣鍔犲叆闃熷垪
+            List<ProductionBomStructure> children = childrenMap.get(nodeId);
+            if (children != null) {
+                for (ProductionBomStructure child : children) {
+                    queue.add(child.getId());
+                }
+            }
         }
+
         if (!updateList.isEmpty()) {
             this.updateBatchById(updateList);
         }
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 1905d4a..95c7344 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
@@ -439,9 +439,54 @@
         productionOrderBomMapper.insert(orderBom);
 
         Map<Long, Long> idMap = new HashMap<>();
-        BigDecimal lastProcessDemandedQuantity = orderQuantity;
+        // 鏋勫缓 sourceId 鈫� sourceNode 鏄犲皠 鍜� parentId 鈫� children 鏄犲皠
+        Map<Long, TechnologyBomStructure> sourceById = new HashMap<>();
+        Map<Long, List<TechnologyBomStructure>> childrenMap = new LinkedHashMap<>();
         for (TechnologyBomStructure source : structureList) {
-            // 瀛愯妭鐐� parentId 闇�瑕佹槧灏勬垚鏂板揩鐓ц妭鐐� id锛屾墠鑳戒繚鐣欏師濮� BOM 灞傜骇銆�
+            if (source == null || source.getId() == null) {
+                continue;
+            }
+            sourceById.put(source.getId(), source);
+            Long parentId = source.getParentId();
+            childrenMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(source);
+        }
+
+        // BFS 浠庢牴鑺傜偣寮�濮嬶紝鐖惰妭鐐瑰厛浜庡瓙鑺傜偣澶勭悊
+        Deque<TechnologyBomStructure> queue = new ArrayDeque<>();
+        List<TechnologyBomStructure> rootNodes = childrenMap.get(null);
+        if (rootNodes != null) {
+            queue.addAll(rootNodes);
+        }
+        // parentId=0 鐨勪篃瑙嗕负鏍硅妭鐐�
+        List<TechnologyBomStructure> zeroParentNodes = childrenMap.get(0L);
+        if (zeroParentNodes != null) {
+            for (TechnologyBomStructure node : zeroParentNodes) {
+                if (!queue.contains(node)) {
+                    queue.add(node);
+                }
+            }
+        }
+
+        while (!queue.isEmpty()) {
+            TechnologyBomStructure source = queue.poll();
+            if (source == null || source.getId() == null) {
+                continue;
+            }
+            // 璁$畻闇�姹傞噺 = 鐖惰妭鐐归渶姹傞噺 脳 鑷韩 unitQuantity
+            BigDecimal parentDemandedQuantity = orderQuantity;
+            Long sourceParentId = source.getParentId();
+            if (sourceParentId != null && sourceParentId != 0L) {
+                Long parentTargetId = idMap.get(sourceParentId);
+                if (parentTargetId != null) {
+                    // 鐖惰妭鐐瑰凡缁忔彃鍏ワ紝鐢ㄧ埗鑺傜偣鐨� targetId 浠庡凡鎻掑叆鍒楄〃鎵�
+                    // 杩欓噷鎴戜滑鐩存帴鐢ㄥ瓨涓嬫潵鐨勭埗鑺傜偣闇�姹傞噺
+                    TechnologyBomStructure parentSource = sourceById.get(sourceParentId);
+                    if (parentSource != null && parentSource.getDemandedQuantity() != null) {
+                        parentDemandedQuantity = parentSource.getDemandedQuantity();
+                    }
+                }
+            }
+
             ProductionBomStructure target = new ProductionBomStructure();
             target.setProductionOrderId(productionOrder.getId());
             target.setProductionOrderBomId(orderBom.getId());
@@ -449,11 +494,20 @@
             target.setProductModelId(source.getProductModelId());
             target.setTechnologyOperationId(source.getOperationId());
             target.setUnitQuantity(source.getUnitQuantity());
-            target.setDemandedQuantity(lastProcessDemandedQuantity.multiply(source.getUnitQuantity()));
+            target.setDemandedQuantity(parentDemandedQuantity.multiply(defaultDecimal(source.getUnitQuantity())));
             target.setUnit(source.getUnit());
             productionBomStructureMapper.insert(target);
             idMap.put(source.getId(), target.getId());
-            lastProcessDemandedQuantity = target.getDemandedQuantity();
+            // 灏嗗綋鍓嶈妭鐐圭殑 demandedQuantity 鍥炲啓鍒� source 瀵硅薄涓婏紝渚涘瓙鑺傜偣璁$畻浣跨敤
+            source.setDemandedQuantity(target.getDemandedQuantity());
+
+            // 灏嗗瓙鑺傜偣鍔犲叆闃熷垪
+            List<TechnologyBomStructure> children = childrenMap.get(source.getId());
+            if (children != null) {
+                for (TechnologyBomStructure child : children) {
+                    queue.add(child);
+                }
+            }
         }
         return orderBom;
     }

--
Gitblit v1.9.3