From 5ac4f450b6b4bd726cefb57a626a46c56dbbc42e Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 02 四月 2026 16:01:12 +0800
Subject: [PATCH] fix: BOM导入去除单层限制

---
 src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java |  152 +++++++++++++++++++++++++++++---------------------
 1 files changed, 87 insertions(+), 65 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
index 43a8942..9659cdd 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -18,6 +18,8 @@
 import com.ruoyi.production.pojo.ProductProcess;
 import com.ruoyi.production.pojo.ProductStructure;
 import com.ruoyi.production.service.*;
+import com.ruoyi.project.system.domain.SysDictData;
+import com.ruoyi.project.system.mapper.SysDictDataMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -42,7 +44,6 @@
     @Autowired
     private ProductBomMapper productBomMapper;
 
-
     @Autowired
     private ProductStructureService productStructureService;
 
@@ -55,39 +56,31 @@
     @Autowired
     private ProductMaterialSkuService productMaterialSkuService;
 
+    @Autowired
+    private SysDictDataMapper sysDictDataMapper;
+
     @Override
-    public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) {
+    public IPage<ProductBomDto> listPage(Page<ProductBom> page, ProductBomDto productBomDto) {
         return productBomMapper.listPage(page, productBomDto);
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public AjaxResult add(ProductBom productBom) {
+        if (productBom == null || productBom.getDictCode() == null) {
+            throw new ServiceException("鏂板BOM澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
+        }
+
+        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode());
+        if (sysDictData == null) {
+            throw new ServiceException("鏂板BOM澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
+        }
+
         boolean save = productBomMapper.insert(productBom) > 0;
         if (save) {
             String no = "BM." + String.format("%05d", productBom.getId());
             productBom.setBomNo(no);
             productBomMapper.updateById(productBom);
-
-            //  鏌ヨ鍑轰骇鍝佹ā鍨嬩俊鎭�
-            if (productBom.getProductModelId() == null) {
-                throw new ServiceException("璇烽�夋嫨浜у搧妯″瀷");
-            }
-
-            ProductMaterialSku productModel = productMaterialSkuService.getById(productBom.getProductModelId());
-            if (productModel == null) {
-                throw new ServiceException("閫夋嫨鐨勪骇鍝佹ā鍨嬩笉瀛樺湪");
-            }
-            ProductMaterial productMaterial = productMaterialService.getById(productModel.getProductId());
-
-            //  娣诲姞鍒濆鐨勪骇鍝佺粨鏋�
-            ProductStructure productStructure = new ProductStructure();
-            productStructure.setProductModelId(productBom.getProductModelId());
-            productStructure.setUnit(productMaterial != null ? productMaterial.getUnit() : null);
-            productStructure.setUnitQuantity(BigDecimal.valueOf(1));
-            productStructure.setBomId(productBom.getId());
-
-            productStructureService.save(productStructure);
 
             return AjaxResult.success();
         }
@@ -96,7 +89,14 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public AjaxResult uploadBom(MultipartFile file) {
+    public AjaxResult uploadBom(MultipartFile file, Long dictCode) {
+        if (dictCode == null) {
+            return AjaxResult.error("瀵煎叆澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
+        }
+        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(dictCode);
+        if (sysDictData == null) {
+            return AjaxResult.error("瀵煎叆澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
+        }
         ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
         List<BomImportDto> list;
         try {
@@ -105,7 +105,9 @@
             return AjaxResult.error("Excel瑙f瀽澶辫触");
         }
 
-        if (list == null || list.isEmpty()) return AjaxResult.error("鏁版嵁涓虹┖");
+        if (list == null || list.isEmpty()) {
+            return AjaxResult.error("鏁版嵁涓虹┖");
+        }
 
         //  澶勭悊宸ュ簭
         list.forEach(dto -> {
@@ -119,10 +121,8 @@
                 .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1));
 
         //  鍒涘缓 BOM 鏁版嵁
-        BomImportDto first = list.get(0);
-        ProductMaterialSku rootModel = findModel(first.getParentName(), first.getParentSpec());
         ProductBom bom = new ProductBom();
-        bom.setProductModelId(rootModel.getId());
+        bom.setDictCode(dictCode);
         bom.setVersion("1.0");
         productBomMapper.insert(bom);
         bom.setBomNo("BM." + String.format("%05d", bom.getId()));
@@ -131,56 +131,72 @@
         // 璁板綍宸茬粡鎻掑叆缁撴瀯鐨勮妭鐐癸細Key = "鍚嶇О+瑙勬牸", Value = structure_id
         Map<String, Long> treePathMap = new HashMap<>();
 
-        for (int i = 0; i < list.size(); i++) {
-            BomImportDto dto = list.get(i);
-            String parentKey = dto.getParentName() + "|" + dto.getParentSpec();
-            String childKey = dto.getChildName() + "|" + dto.getChildSpec();
+        for (BomImportDto dto : list) {
+            String parentName = dto.getParentName();
+            String parentSpec = dto.getParentSpec();
+            String childName = dto.getChildName();
+            String childSpec = dto.getChildSpec();
 
-            //澶勭悊鏍硅妭鐐�,绗竴琛屼笖瀛愰」涓虹┖
-            if (i == 0 && StringUtils.isBlank(dto.getChildName())) {
-                ProductStructure rootNode = new ProductStructure();
-                rootNode.setBomId(bom.getId());
-                rootNode.setParentId(null); // 椤跺眰娌℃湁鐖惰妭鐐�
-                rootNode.setProductModelId(rootModel.getId());
-                rootNode.setUnitQuantity(BigDecimal.ONE);
-                ProductMaterial rootMaterial = productMaterialService.getById(rootModel.getProductId());
-                rootNode.setUnit(rootMaterial != null ? rootMaterial.getUnit() : null);
-                productStructureService.save(rootNode);
+            String parentKey = parentName + "|" + (parentSpec == null ? "" : parentSpec);
+            String childKey = childName + "|" + (childSpec == null ? "" : childSpec);
 
-                treePathMap.put(parentKey, rootNode.getId());
+            //  澶勭悊椤剁骇鑺傜偣 (瀛愰」鍙婂叾瑙勬牸鍧囦负绌哄綋浣滈《绾ц妭鐐瑰畾涔�)
+            if (StringUtils.isBlank(childName) && StringUtils.isBlank(childSpec)) {
+                if (!treePathMap.containsKey(parentKey)) {
+                    ProductMaterialSku model = findModel(parentName, parentSpec);
+                    ProductStructure node = saveStructure(bom.getId(), null, model, dto.getUnitQty(), null);
+                    treePathMap.put(parentKey, node.getId());
+                } else {
+                    // 濡傛灉宸茬粡瀛樺湪锛屼粎鍦ㄥ畠鏄《绾ц妭鐐规椂鏇存柊
+                    Long structureId = treePathMap.get(parentKey);
+                    ProductStructure node = productStructureService.getById(structureId);
+                    if (node != null && node.getParentId() == null && dto.getUnitQty() != null) {
+                        node.setUnitQuantity(dto.getUnitQty());
+                        productStructureService.updateById(node);
+                    }
+                }
                 continue;
             }
 
-            //  澶勭悊瀛愬眰绾ц妭鐐�
-            //  鎵惧埌鐖惰妭鐐瑰湪鏁版嵁搴撻噷鐨� ID
+            //  澶勭悊鐖跺瓙灞傜骇鍏崇郴
             Long parentStructureId = treePathMap.get(parentKey);
             if (parentStructureId == null) {
-                // 濡傛灉 Map 閲屾壘涓嶅埌锛岃鏄� Excel 椤哄簭涔变簡鎴栬�呮暟鎹湁璇�
-                throw new ServiceException("瀵煎叆澶辫触: 鐖堕」[" + dto.getParentName() + "]蹇呴』鍦ㄥ叾瀛愰」涔嬪墠瀹氫箟");
+                // 濡傛灉 Map 閲屾壘涓嶅埌鐖惰妭鐐癸紝瑙嗕负椤剁骇鐖惰妭鐐�
+                ProductMaterialSku parentModel = findModel(parentName, parentSpec);
+                ProductStructure parentNode = saveStructure(bom.getId(), null, parentModel, BigDecimal.ONE, null);
+                parentStructureId = parentNode.getId();
+                treePathMap.put(parentKey, parentStructureId);
             }
 
             //  鑾峰彇瀛愰」妯″瀷淇℃伅
-            ProductMaterialSku childModel = findModel(dto.getChildName(), dto.getChildSpec());
-
-            //  鎻掑叆缁撴瀯琛�
-            ProductStructure node = new ProductStructure();
-            node.setBomId(bom.getId());
-            node.setParentId(parentStructureId); // 鐖惰妭鐐笽D
-            node.setProductModelId(childModel.getId());
-            node.setUnitQuantity(dto.getUnitQty());
-            ProductMaterial childMaterial = productMaterialService.getById(childModel.getProductId());
-            node.setUnit(childMaterial != null ? childMaterial.getUnit() : null);
-            if (processMap.containsKey(dto.getProcess())) {
-                node.setProcessId(processMap.get(dto.getProcess()));
+            ProductMaterialSku childModel = findModel(childName, childSpec);
+            Long processId = null;
+            if (StringUtils.isNotBlank(dto.getProcess())) {
+                processId = processMap.get(dto.getProcess());
             }
-            productStructureService.save(node);
-
-            //  鎶婂綋鍓嶅瓙椤硅褰曞埌 Map,浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
-            //  鍚屼竴鐖堕」涓嬬殑鍚屽悕瀛愰」涓嶉渶瑕侀噸澶嶈褰�
-            treePathMap.put(childKey, node.getId());
+            // 鎻掑叆瀛愯妭鐐圭粨鏋勮〃
+            ProductStructure childNode = saveStructure(bom.getId(), parentStructureId, childModel, dto.getUnitQty(), processId);
+            // 鎶婂綋鍓嶅瓙椤硅褰曞埌 Map,浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
+            treePathMap.put(childKey, childNode.getId());
         }
 
         return AjaxResult.success("BOM瀵煎叆鎴愬姛");
+    }
+
+    /**
+     * 淇濆瓨缁撴瀯鑺傜偣
+     */
+    private ProductStructure saveStructure(Integer bomId, Long parentId, ProductMaterialSku model, BigDecimal qty, Long processId) {
+        ProductStructure node = new ProductStructure();
+        node.setBomId(bomId);
+        node.setParentId(parentId);
+        node.setProductModelId(model.getId());
+        node.setUnitQuantity(qty != null ? qty : BigDecimal.ONE);
+        ProductMaterial material = productMaterialService.getById(model.getProductId());
+        node.setUnit(material != null ? material.getUnit() : null);
+        node.setProcessId(processId);
+        productStructureService.save(node);
+        return node;
     }
 
 
@@ -251,6 +267,14 @@
         util.exportExcel(response, exportList, "BOM缁撴瀯瀵煎嚭");
     }
 
+    @Override
+    public String strengthById(Long bomId) {
+        if (bomId == null) {
+            return null;
+        }
+        return baseMapper.selectStrengthById(bomId);
+    }
+
     private void populateMap(List<ProductStructureDto> nodes, Map<Long, ProductStructureDto> map) {
         if (nodes == null || nodes.isEmpty()) {
             return;
@@ -313,5 +337,3 @@
         return s.replaceAll("[\\u00A0\\u3000]", "").trim();
     }
 }
-
-

--
Gitblit v1.9.3