From 2e183f8b71d7170b12346b8c231811c828819fda Mon Sep 17 00:00:00 2001
From: chenhj <1263187585@qq.com>
Date: 星期四, 30 四月 2026 15:48:38 +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/technology/service/impl/TechnologyBomStructureServiceImpl.java |  132 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 124 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java b/src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java
index 8fe52f2..5cb898a 100644
--- a/src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java
@@ -1,20 +1,136 @@
 package com.ruoyi.technology.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.technology.bean.dto.TechnologyBomStructureDto;
+import com.ruoyi.technology.bean.vo.TechnologyBomStructureVo;
 import com.ruoyi.technology.mapper.TechnologyBomStructureMapper;
 import com.ruoyi.technology.pojo.TechnologyBomStructure;
 import com.ruoyi.technology.service.TechnologyBomStructureService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-/**
- * <p>
- * BOM浜у搧缁撴瀯琛� 鏈嶅姟瀹炵幇绫�
- * </p>
- *
- * @author 鑺杞欢锛堟睙鑻忥級鏈夐檺鍏徃
- * @since 2026-04-20 10:06:17
- */
+import java.util.*;
+
 @Service
+@RequiredArgsConstructor
 public class TechnologyBomStructureServiceImpl extends ServiceImpl<TechnologyBomStructureMapper, TechnologyBomStructure> implements TechnologyBomStructureService {
 
+    private final TechnologyBomStructureMapper technologyBomStructureMapper;
+
+    /**
+     * 淇濆瓨BOM缁撴瀯鏍戯紝澶勭悊鏂板銆佹洿鏂板拰鍒犻櫎鑺傜偣銆�
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean addTechnologyBomStructure(TechnologyBomStructureDto dto) {
+        Long bomId = dto.getBomId();
+        List<TechnologyBomStructureDto> flatDtoList = new ArrayList<>();
+        flattenTree(dto.getChildren(), flatDtoList);
+
+        List<TechnologyBomStructure> dbList = this.list(new LambdaQueryWrapper<TechnologyBomStructure>()
+                .eq(TechnologyBomStructure::getBomId, bomId));
+
+        Set<Long> frontendIds = new HashSet<>();
+        for (TechnologyBomStructureDto item : flatDtoList) {
+            if (item.getId() != null) {
+                frontendIds.add(item.getId());
+            }
+        }
+
+        Set<Long> deleteIds = new HashSet<>();
+        for (TechnologyBomStructure dbItem : dbList) {
+            if (!frontendIds.contains(dbItem.getId())) {
+                deleteIds.add(dbItem.getId());
+            }
+        }
+        if (!deleteIds.isEmpty()) {
+            this.removeByIds(deleteIds);
+        }
+
+        List<TechnologyBomStructure> insertList = new ArrayList<>();
+        List<TechnologyBomStructure> updateList = new ArrayList<>();
+        Map<String, TechnologyBomStructure> tempEntityMap = new HashMap<>();
+
+        for (TechnologyBomStructureDto item : flatDtoList) {
+            TechnologyBomStructure entity = new TechnologyBomStructure();
+            BeanUtils.copyProperties(item, entity);
+            entity.setBomId(bomId);
+            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<TechnologyBomStructure> parentFixList = new ArrayList<>();
+        for (TechnologyBomStructureDto item : flatDtoList) {
+            if (item.getId() == null && item.getParentTempId() != null) {
+                TechnologyBomStructure child = tempEntityMap.get(item.getTempId());
+                if (child == null) {
+                    continue;
+                }
+                TechnologyBomStructure 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;
+    }
+
+    /**
+     * 鏍规嵁BOM鏌ヨ骞剁粍瑁呯粨鏋勬爲銆�
+     */
+    @Override
+    public List<TechnologyBomStructureVo> listByBomId(Long bomId) {
+        List<TechnologyBomStructureVo> list = technologyBomStructureMapper.listByBomId(bomId);
+        Map<Long, TechnologyBomStructureVo> map = new HashMap<>();
+        for (TechnologyBomStructureVo node : list) {
+            node.setChildren(new ArrayList<>());
+            map.put(node.getId(), node);
+        }
+
+        List<TechnologyBomStructureVo> tree = new ArrayList<>();
+        for (TechnologyBomStructureVo node : list) {
+            Long parentId = node.getParentId();
+            if (parentId == null || parentId == 0L) {
+                tree.add(node);
+                continue;
+            }
+            TechnologyBomStructureVo parent = map.get(parentId);
+            if (parent != null) {
+                parent.getChildren().add(node);
+            }
+        }
+        return tree;
+    }
+
+    /**
+     * 灏嗘爲褰㈢粨鏋勬媿骞虫垚鍒楄〃锛屼究浜庣粺涓�淇濆瓨銆�
+     */
+    private void flattenTree(List<TechnologyBomStructureDto> source, List<TechnologyBomStructureDto> result) {
+        if (source == null) {
+            return;
+        }
+        for (TechnologyBomStructureDto node : source) {
+            result.add(node);
+            flattenTree(node.getChildren(), result);
+        }
+    }
 }

--
Gitblit v1.9.3