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 |  146 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 138 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..81fff7e 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,150 @@
 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.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.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-/**
- * <p>
- * BOM浜у搧缁撴瀯琛� 鏈嶅姟瀹炵幇绫�
- * </p>
- *
- * @author 鑺杞欢锛堟睙鑻忥級鏈夐檺鍏徃
- * @since 2026-04-20 10:06:17
- */
+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 鐨勬墎骞崇墿鏂欐槑缁嗭細涓�琛屽彧琛ㄧず鈥滄煇涓墿鏂欒鏍煎湪鏌愰亾宸ュ簭琚秷鑰椻�濄��
+     * 涓嶅啀鏈夌埗瀛愬眰绾с�佸崟浣嶇敤閲忋�侀渶姹傛暟閲忓拰鍗曚綅閰嶇疆銆�
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean addTechnologyBomStructure(TechnologyBomStructureDto dto) {
+        Long bomId = dto.getBomId();
+        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> dbIds = new HashSet<>();
+        for (TechnologyBomStructure dbItem : dbList) {
+            dbIds.add(dbItem.getId());
+        }
+
+        Set<Long> submittedIds = new HashSet<>();
+        Set<String> relationKeys = new HashSet<>();
+        List<TechnologyBomStructure> insertList = new ArrayList<>();
+        List<TechnologyBomStructure> updateList = new ArrayList<>();
+
+        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();
+            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) {
+                insertList.add(entity);
+            } else {
+                if (!dbIds.contains(item.getId())) {
+                    throw new ServiceException("BOM鐗╂枡鏄庣粏涓嶅瓨鍦ㄦ垨涓嶅睘浜庡綋鍓岯OM");
+                }
+                entity.setId(item.getId());
+                submittedIds.add(item.getId());
+                updateList.add(entity);
+            }
+        }
+
+        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鏌ヨ鎵佸钩鐗╂枡鏄庣粏锛屾寜绋冲畾椤哄簭杩斿洖銆�
+     */
+    @Override
+    public List<TechnologyBomStructureVo> listByBomId(Long bomId) {
+        if (bomId == null) {
+            return new ArrayList<>();
+        }
+        return technologyBomStructureMapper.listByBomId(bomId);
+    }
+
+    /**
+     * 鏍¢獙鐗╂枡瑙勬牸锛氬繀椤诲瓨鍦紝涓斾笉鑳芥槸 BOM 鑷韩鐨勬垚鍝佽鏍笺��
+     */
+    private void validateMaterial(TechnologyBom bom, Long productModelId) {
+        if (productModelId == null) {
+            throw new ServiceException("璇烽�夋嫨鐗╂枡瑙勬牸");
+        }
+        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