From 9568f67c00d69039d5701e50dab6f5e96cb68376 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 23 一月 2026 11:32:12 +0800
Subject: [PATCH] refactor: BOM产品结构保存与查看进行树形处理

---
 src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java |  148 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 128 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
index 310a723..8bd749f 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
@@ -1,16 +1,8 @@
 package com.ruoyi.production.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.basic.dto.ProductModelDto;
-import com.ruoyi.basic.dto.ProductTreeDto;
-import com.ruoyi.basic.mapper.ProductMapper;
-import com.ruoyi.basic.mapper.ProductModelMapper;
-import com.ruoyi.basic.pojo.Product;
-import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.production.dto.ProductStructureDto;
-import com.ruoyi.production.mapper.ProductBomMapper;
 import com.ruoyi.production.mapper.ProductStructureMapper;
 import com.ruoyi.production.pojo.ProductStructure;
 import com.ruoyi.production.service.ProductStructureService;
@@ -19,9 +11,10 @@
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 @RequiredArgsConstructor
@@ -29,23 +22,138 @@
 public class ProductStructureServiceImpl extends ServiceImpl<ProductStructureMapper, ProductStructure> implements ProductStructureService {
 
     @Autowired
-    private  ProductStructureMapper productStructureMapper;
-
+    private ProductStructureMapper productStructureMapper;
 
 
     @Override
-    public Boolean addProductStructureDto(ProductStructureDto productStructureDto) {
-        this.remove(new QueryWrapper<ProductStructure>().lambda().eq(ProductStructure::getBomId, productStructureDto.getBomId()));
-        productStructureDto.getProductStructureList().forEach(productStructure -> {
-            productStructure.setBomId(productStructureDto.getBomId());
-        });
-        return this.saveBatch(productStructureDto.getProductStructureList());
+    @Transactional
+    public Boolean addProductStructureDto(ProductStructureDto dto) {
+
+        Long bomId = dto.getBomId();
+
+        //  灏嗘爲鎵佸钩鍖�
+        List<ProductStructureDto> flatDtoList = new ArrayList<>();
+        flattenTree(dto.getChildren(), flatDtoList);
+
+        //  鏌ヨ鏁版嵁搴撲腑宸叉湁鐨� BOM 鏁版嵁
+        List<ProductStructure> dbList = this.list(new LambdaQueryWrapper<ProductStructure>().eq(ProductStructure::getBomId, bomId));
+
+        //  鏌ユ壘宸插瓨鍦ㄧ殑鑺傜偣 - ID
+        Set<Long> frontendIds = flatDtoList.stream()
+                .map(ProductStructureDto::getId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        //  闇�瑕佸垹闄ょ殑鑺傜偣 - ID
+        Set<Long> deleteIds = dbList.stream()
+                .map(ProductStructure::getId)
+                .filter(id -> !frontendIds.contains(id))
+                .collect(Collectors.toSet());
+
+        if (!deleteIds.isEmpty()) {
+            this.removeByIds(deleteIds);
+        }
+
+        //  鏂板 / 鏇存柊
+        List<ProductStructure> insertList = new ArrayList<>();
+        List<ProductStructure> updateList = new ArrayList<>();
+
+        // 鐢ㄤ簬鍥炲啓 parentId
+        Map<String, ProductStructure> tempEntityMap = new HashMap<>();
+
+        for (ProductStructureDto psDto : flatDtoList) {
+            ProductStructure entity = new ProductStructure();
+            BeanUtils.copyProperties(psDto, entity);
+            entity.setBomId(bomId);
+
+            if (psDto.getId() == null) {
+                // 鏂板
+                entity.setId(null);
+                entity.setParentId(null);
+                insertList.add(entity);
+                tempEntityMap.put(psDto.getTempId(), entity);
+            } else {
+                // 鏇存柊
+                updateList.add(entity);
+            }
+        }
+
+        //  鎻掑叆鏂拌妭鐐�
+        if (!insertList.isEmpty()) {
+            this.saveBatch(insertList);
+        }
+
+        //  鍥炲啓鏂板鑺傜偣 parentId
+        List<ProductStructure> parentFixList = new ArrayList<>();
+        //  鐪熷疄鐨勭埗鑺傜偣 ID
+        Long realParentId;
+        for (ProductStructureDto psDto : flatDtoList) {
+            if (psDto.getId() == null && psDto.getParentTempId() != null) {
+                ProductStructure child = tempEntityMap.get(psDto.getTempId());
+                if (tempEntityMap.containsKey(psDto.getParentTempId())) {
+                    // 鐖惰妭鐐规槸鏂拌妭鐐�
+                    realParentId = tempEntityMap.get(psDto.getParentTempId()).getId();
+                } else {
+                    // 鐖惰妭鐐规槸鑰佽妭鐐�
+                    realParentId = Long.valueOf(psDto.getParentTempId());
+                }
+
+                child.setParentId(realParentId);
+                parentFixList.add(child);
+            }
+        }
+
+        if (!parentFixList.isEmpty()) {
+            this.updateBatchById(parentFixList);
+        }
+
+        if (!updateList.isEmpty()) {
+            this.updateBatchById(updateList);
+        }
+
+        return true;
     }
+
+    /**
+     * 灏嗗墠绔紶鍏ョ殑鏍戣繘琛屾墎骞冲寲
+     *
+     * @param source 鏁版嵁鏍�
+     * @param result 鎵佸钩鍖栨暟鎹�
+     */
+    private void flattenTree(List<ProductStructureDto> source, List<ProductStructureDto> result) {
+        if (source == null) {
+            return;
+        }
+        for (ProductStructureDto node : source) {
+            result.add(node);
+            flattenTree(node.getChildren(), result);
+        }
+    }
+
 
     @Override
     public List<ProductStructureDto> listBybomId(Long bomId) {
-        List<ProductStructureDto> tree = productStructureMapper.listBybomId(bomId);
-        return tree;
+        List<ProductStructureDto> list = productStructureMapper.listBybomId(bomId);
 
+        Map<Long, ProductStructureDto> map = new HashMap<>();
+        for (ProductStructureDto node : list) {
+            node.setChildren(new ArrayList<>());
+            map.put(node.getId(), node);
+        }
+
+        List<ProductStructureDto> tree = new ArrayList<>();
+        for (ProductStructureDto node : list) {
+            Long parentId = node.getParentId();
+            if (parentId == null || parentId == 0) {
+                tree.add(node);
+            } else {
+                ProductStructureDto parent = map.get(parentId);
+                if (parent != null) {
+                    parent.getChildren().add(node);
+                }
+            }
+        }
+        return tree;
     }
+
 }

--
Gitblit v1.9.3