From 6f0df10fdd0e55e5ed3e31b1720efd25e2bf02fb Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期三, 16 九月 2026 18:51:12 +0800
Subject: [PATCH] fix: 添加条件过滤以确保查询结果中合格数量大于零

---
 src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java |  158 ++++++++++++++++++++++++++++------------------------
 1 files changed, 86 insertions(+), 72 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 5cb898a..81fff7e 100644
--- a/src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/technology/service/impl/TechnologyBomStructureServiceImpl.java
@@ -2,135 +2,149 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.mapper.ProductModelMapper;
+import com.ruoyi.basic.pojo.ProductModel;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.technology.bean.dto.TechnologyBomStructureDto;
 import com.ruoyi.technology.bean.vo.TechnologyBomStructureVo;
+import com.ruoyi.technology.mapper.TechnologyBomMapper;
 import com.ruoyi.technology.mapper.TechnologyBomStructureMapper;
+import com.ruoyi.technology.mapper.TechnologyOperationMapper;
+import com.ruoyi.technology.pojo.TechnologyBom;
 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;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 @Service
 @RequiredArgsConstructor
 public class TechnologyBomStructureServiceImpl extends ServiceImpl<TechnologyBomStructureMapper, TechnologyBomStructure> implements TechnologyBomStructureService {
 
     private final TechnologyBomStructureMapper technologyBomStructureMapper;
+    private final TechnologyBomMapper technologyBomMapper;
+    private final ProductModelMapper productModelMapper;
+    private final TechnologyOperationMapper technologyOperationMapper;
 
     /**
-     * 淇濆瓨BOM缁撴瀯鏍戯紝澶勭悊鏂板銆佹洿鏂板拰鍒犻櫎鑺傜偣銆�
+     * 鍏ㄩ噺鍚屾 BOM 鐨勬墎骞崇墿鏂欐槑缁嗭細涓�琛屽彧琛ㄧず鈥滄煇涓墿鏂欒鏍煎湪鏌愰亾宸ュ簭琚秷鑰椻�濄��
+     * 涓嶅啀鏈夌埗瀛愬眰绾с�佸崟浣嶇敤閲忋�侀渶姹傛暟閲忓拰鍗曚綅閰嶇疆銆�
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean addTechnologyBomStructure(TechnologyBomStructureDto dto) {
         Long bomId = dto.getBomId();
-        List<TechnologyBomStructureDto> flatDtoList = new ArrayList<>();
-        flattenTree(dto.getChildren(), flatDtoList);
+        if (bomId == null) {
+            throw new ServiceException("BOM ID涓嶈兘涓虹┖");
+        }
+        TechnologyBom bom = technologyBomMapper.selectById(bomId);
+        if (bom == null) {
+            throw new ServiceException("BOM涓嶅瓨鍦�");
+        }
+
+        List<TechnologyBomStructureDto> detailList = dto.getDetailList() == null
+                ? Collections.emptyList()
+                : dto.getDetailList();
 
         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<>();
+        Set<Long> dbIds = new HashSet<>();
         for (TechnologyBomStructure dbItem : dbList) {
-            if (!frontendIds.contains(dbItem.getId())) {
-                deleteIds.add(dbItem.getId());
-            }
-        }
-        if (!deleteIds.isEmpty()) {
-            this.removeByIds(deleteIds);
+            dbIds.add(dbItem.getId());
         }
 
+        Set<Long> submittedIds = new HashSet<>();
+        Set<String> relationKeys = new HashSet<>();
         List<TechnologyBomStructure> insertList = new ArrayList<>();
         List<TechnologyBomStructure> updateList = new ArrayList<>();
-        Map<String, TechnologyBomStructure> tempEntityMap = new HashMap<>();
 
-        for (TechnologyBomStructureDto item : flatDtoList) {
+        for (TechnologyBomStructureDto item : detailList) {
+            Long productModelId = item.getProductModelId();
+            Long operationId = item.getOperationId();
+            validateMaterial(bom, productModelId);
+            validateOperation(operationId);
+            if (!relationKeys.add(productModelId + "#" + operationId)) {
+                throw new ServiceException("鍚屼竴鐗╂枡瑙勬牸鍦ㄥ悓涓�宸ュ簭涓嬩笉鑳介噸澶嶉厤缃�");
+            }
+
             TechnologyBomStructure entity = new TechnologyBomStructure();
-            BeanUtils.copyProperties(item, entity);
             entity.setBomId(bomId);
+            entity.setProductModelId(productModelId);
+            entity.setOperationId(operationId);
+            // 鎵佸钩 BOM 涓嶅啀浣跨敤灞傜骇鍜屾暟閲忚涔夛紝鍘嗗彶鍒楃粺涓�缃┖銆�
+            entity.setParentId(null);
+            entity.setUnitQuantity(null);
+            entity.setDemandedQuantity(null);
+            entity.setUnit(null);
+
             if (item.getId() == null) {
-                entity.setParentId(null);
                 insertList.add(entity);
-                tempEntityMap.put(item.getTempId(), entity);
             } else {
+                if (!dbIds.contains(item.getId())) {
+                    throw new ServiceException("BOM鐗╂枡鏄庣粏涓嶅瓨鍦ㄦ垨涓嶅睘浜庡綋鍓岯OM");
+                }
+                entity.setId(item.getId());
+                submittedIds.add(item.getId());
                 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);
+        Set<Long> deleteIds = new HashSet<>(dbIds);
+        deleteIds.removeAll(submittedIds);
+        if (!deleteIds.isEmpty()) {
+            this.removeByIds(deleteIds);
         }
         if (!updateList.isEmpty()) {
             this.updateBatchById(updateList);
+        }
+        if (!insertList.isEmpty()) {
+            this.saveBatch(insertList);
         }
         return true;
     }
 
     /**
-     * 鏍规嵁BOM鏌ヨ骞剁粍瑁呯粨鏋勬爲銆�
+     * 鏍规嵁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);
+        if (bomId == null) {
+            return new ArrayList<>();
         }
-
-        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;
+        return technologyBomStructureMapper.listByBomId(bomId);
     }
 
     /**
-     * 灏嗘爲褰㈢粨鏋勬媿骞虫垚鍒楄〃锛屼究浜庣粺涓�淇濆瓨銆�
+     * 鏍¢獙鐗╂枡瑙勬牸锛氬繀椤诲瓨鍦紝涓斾笉鑳芥槸 BOM 鑷韩鐨勬垚鍝佽鏍笺��
      */
-    private void flattenTree(List<TechnologyBomStructureDto> source, List<TechnologyBomStructureDto> result) {
-        if (source == null) {
-            return;
+    private void validateMaterial(TechnologyBom bom, Long productModelId) {
+        if (productModelId == null) {
+            throw new ServiceException("璇烽�夋嫨鐗╂枡瑙勬牸");
         }
-        for (TechnologyBomStructureDto node : source) {
-            result.add(node);
-            flattenTree(node.getChildren(), result);
+        ProductModel productModel = productModelMapper.selectById(productModelId);
+        if (productModel == null) {
+            throw new ServiceException("鐗╂枡瑙勬牸涓嶅瓨鍦�");
+        }
+        if (productModelId.equals(bom.getProductModelId())) {
+            throw new ServiceException("鎴愬搧瑙勬牸涓嶈兘浣滀负鑷韩鐨勭墿鏂�");
+        }
+    }
+
+    /**
+     * 鏍¢獙娑堣�楀伐搴忓繀濉笖瀛樺湪銆�
+     */
+    private void validateOperation(Long operationId) {
+        if (operationId == null) {
+            throw new ServiceException("璇烽�夋嫨娑堣�楀伐搴�");
+        }
+        if (technologyOperationMapper.selectById(operationId) == null) {
+            throw new ServiceException("娑堣�楀伐搴忎笉瀛樺湪");
         }
     }
 }

--
Gitblit v1.9.3