From 398551392afc2ab879f837f60f95f9c9d8cf3c98 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期一, 27 四月 2026 13:08:24 +0800
Subject: [PATCH] Merge branch 'dev_New_pro' of http://114.132.189.42:9002/r/product-inventory-management-after into dev_New_pro

---
 src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java |   93 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 89 insertions(+), 4 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 00ab057..cbae0dc 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java
@@ -1,17 +1,18 @@
 package com.ruoyi.production.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.production.bean.dto.ProductionBomStructureDto;
 import com.ruoyi.production.bean.vo.ProductionBomStructureVo;
 import com.ruoyi.production.mapper.ProductionBomStructureMapper;
 import com.ruoyi.production.pojo.ProductionBomStructure;
 import com.ruoyi.production.service.ProductionBomStructureService;
 import lombok.RequiredArgsConstructor;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * <p>
@@ -54,4 +55,88 @@
         return tree;
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean addProductionBomStructure(ProductionBomStructureDto dto) {
+        Long orderBomId = dto.getProductionOrderBomId();
+        List<ProductionBomStructureDto> flatDtoList = new ArrayList<>();
+        flattenTree(dto.getChildren(), flatDtoList);
+
+        List<ProductionBomStructure> dbList = this.list(new LambdaQueryWrapper<ProductionBomStructure>()
+                .eq(ProductionBomStructure::getProductionOrderBomId, orderBomId));
+
+        Set<Long> frontendIds = new HashSet<>();
+        for (ProductionBomStructureDto item : flatDtoList) {
+            if (item.getId() != null) {
+                frontendIds.add(item.getId());
+            }
+        }
+
+        Set<Long> deleteIds = new HashSet<>();
+        for (ProductionBomStructure dbItem : dbList) {
+            if (!frontendIds.contains(dbItem.getId())) {
+                deleteIds.add(dbItem.getId());
+            }
+        }
+        if (!deleteIds.isEmpty()) {
+            this.removeByIds(deleteIds);
+        }
+
+        List<ProductionBomStructure> insertList = new ArrayList<>();
+        List<ProductionBomStructure> updateList = new ArrayList<>();
+        Map<String, ProductionBomStructure> tempEntityMap = new HashMap<>();
+
+        for (ProductionBomStructureDto item : flatDtoList) {
+            ProductionBomStructure entity = new ProductionBomStructure();
+            BeanUtils.copyProperties(item, entity);
+            entity.setProductionOrderBomId(orderBomId);
+            if (item.getId() == null) {
+                entity.setParentId(null);
+                insertList.add(entity);
+                tempEntityMap.put(item.getTempId(), entity);
+            } else {
+                updateList.add(entity);
+            }
+        }
+
+        if (!insertList.isEmpty()) {
+            this.saveBatch(insertList);
+        }
+
+        List<ProductionBomStructure> parentFixList = new ArrayList<>();
+        for (ProductionBomStructureDto item : flatDtoList) {
+            if (item.getId() == null && item.getParentTempId() != null) {
+                ProductionBomStructure child = tempEntityMap.get(item.getTempId());
+                if (child == null) {
+                    continue;
+                }
+                ProductionBomStructure parent = tempEntityMap.get(item.getParentTempId());
+                Long realParentId = parent != null ? parent.getId() : Long.valueOf(item.getParentTempId());
+                child.setParentId(realParentId);
+                parentFixList.add(child);
+            }
+        }
+
+        if (!parentFixList.isEmpty()) {
+            this.updateBatchById(parentFixList);
+        }
+        if (!updateList.isEmpty()) {
+            this.updateBatchById(updateList);
+        }
+        return true;
+    }
+
+    /**
+     * 灏嗘爲褰㈢粨鏋勬媿骞虫垚鍒楄〃锛屼究浜庣粺涓�淇濆瓨銆�
+     */
+    private void flattenTree(List<ProductionBomStructureDto> source, List<ProductionBomStructureDto> result) {
+        if (source == null) {
+            return;
+        }
+        for (ProductionBomStructureDto node : source) {
+            result.add(node);
+            flattenTree(node.getChildren(), result);
+        }
+    }
+
 }

--
Gitblit v1.9.3