From 19effb2444eaf331f61fd0d43d3735f8d1baf768 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期三, 18 三月 2026 10:14:44 +0800
Subject: [PATCH] fix: BOM的子集取消第一层限制

---
 src/main/java/com/ruoyi/production/dto/BomImportDto.java                         |   16 +++-
 src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java       |   92 ++++++++++++++++++++----------
 src/main/resources/mapper/production/ProductStructureMapper.xml                  |    8 ++
 src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java |   19 ++++-
 src/main/java/com/ruoyi/production/controller/ProductBomController.java          |    9 +++
 5 files changed, 102 insertions(+), 42 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/controller/ProductBomController.java b/src/main/java/com/ruoyi/production/controller/ProductBomController.java
index 5bfae99..dee173d 100644
--- a/src/main/java/com/ruoyi/production/controller/ProductBomController.java
+++ b/src/main/java/com/ruoyi/production/controller/ProductBomController.java
@@ -1,5 +1,6 @@
 package com.ruoyi.production.controller;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -101,6 +102,14 @@
         return AjaxResult.success(productBoms);
     }
 
+    @GetMapping("/getDictCode")
+    @Log(title = "BOM-鏍规嵁閫夋嫨鐨勪骇鍝佸瀷鍙锋煡璇㈠瓨鍦ㄧ殑bom", businessType = BusinessType.OTHER)
+    @ApiOperation("BOM-鏍规嵁閫夋嫨鐨勪骇鍝佸瀷鍙锋煡璇㈠瓨鍦ㄧ殑bom")
+    public AjaxResult getDictCode(Long dictCode) {
+        List<ProductBom> bomList = productBomService.list(new LambdaQueryWrapper<ProductBom>().eq(ProductBom::getDictCode, dictCode));
+        return AjaxResult.success(bomList);
+    }
+
 
     @PostMapping("uploadBom")
     @PreAuthorize("@ss.hasPermi('product:bom:import')")
diff --git a/src/main/java/com/ruoyi/production/dto/BomImportDto.java b/src/main/java/com/ruoyi/production/dto/BomImportDto.java
index e53fd4f..7f8689b 100644
--- a/src/main/java/com/ruoyi/production/dto/BomImportDto.java
+++ b/src/main/java/com/ruoyi/production/dto/BomImportDto.java
@@ -8,11 +8,17 @@
 @Data
 public class BomImportDto {
 
-    @Excel(name = "缂栧彿")
-    private String code;
+    @Excel(name = "鐖堕」浜у搧鍚嶇О")
+    private String parentName;
 
-    @Excel(name = "鐖堕」缂栧彿")
-    private String parentCode;
+    @Excel(name = "鐖堕」浜у搧瑙勬牸")
+    private String parentSpec;
+
+    @Excel(name = "瀛愰」浜у搧鍚嶇О")
+    private String childName;
+
+    @Excel(name = "瀛愰」浜у搧瑙勬牸")
+    private String childSpec;
 
     @Excel(name = "鍗曚綅鐢ㄩ噺")
     private BigDecimal unitQty;
@@ -22,4 +28,4 @@
 
     @Excel(name = "澶囨敞")
     private String remark;
-}
\ No newline at end of file
+}
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 b5c9562..c16401e 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -4,6 +4,8 @@
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.production.pojo.ProductMaterial;
+import com.ruoyi.production.pojo.ProductMaterialSku;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -42,12 +44,17 @@
     @Autowired
     private ProductBomMapper productBomMapper;
 
-
     @Autowired
     private ProductStructureService productStructureService;
 
     @Autowired
     private ProductProcessService productProcessService;
+
+    @Autowired
+    private ProductMaterialService productMaterialService;
+
+    @Autowired
+    private ProductMaterialSkuService productMaterialSkuService;
 
     @Autowired
     private SysDictDataMapper sysDictDataMapper;
@@ -61,12 +68,12 @@
     @Transactional(rollbackFor = Exception.class)
     public AjaxResult add(ProductBom productBom) {
         if (productBom == null || productBom.getDictCode() == null) {
-            throw new ServiceException("鏂板澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
+            throw new ServiceException("鏂板BOM澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
         }
 
         SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode());
         if (sysDictData == null) {
-            throw new ServiceException("鏂板澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
+            throw new ServiceException("鏂板BOM澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
         }
 
         boolean save = productBomMapper.insert(productBom) > 0;
@@ -90,7 +97,6 @@
         if (sysDictData == null) {
             return AjaxResult.error("瀵煎叆澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
         }
-
         ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
         List<BomImportDto> list;
         try {
@@ -99,68 +105,81 @@
             return AjaxResult.error("Excel瑙f瀽澶辫触");
         }
 
-        if (list == null || list.isEmpty()) return AjaxResult.error("鏁版嵁涓虹┖");
+        if (list == null || list.isEmpty()) {
+            return AjaxResult.error("鏁版嵁涓虹┖");
+        }
 
-        //  澶勭悊瀛楁娓呯悊
+        //  澶勭悊宸ュ簭
         list.forEach(dto -> {
-            dto.setParentCode(clean(dto.getParentCode()));
-            dto.setCode(clean(dto.getCode()));
+            dto.setParentName(clean(dto.getParentName()));
+            dto.setParentSpec(clean(dto.getParentSpec()));
+            dto.setChildName(clean(dto.getChildName()));
+            dto.setChildSpec(clean(dto.getChildSpec()));
         });
         handleProcess(list);
         Map<String, Long> processMap = productProcessService.list().stream()
                 .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.setVersion("1.0");
         bom.setDictCode(dictCode);
+        bom.setVersion("1.0");
         productBomMapper.insert(bom);
         bom.setBomNo("BM." + String.format("%05d", bom.getId()));
         productBomMapper.updateById(bom);
 
-        // 璁板綍宸茬粡鎻掑叆缁撴瀯鐨勮妭鐐癸細Key = "缂栫爜", Value = structure_id
+        // 璁板綍宸茬粡鎻掑叆缁撴瀯鐨勮妭鐐癸細Key = "鍚嶇О+瑙勬牸", Value = structure_id
         Map<String, Long> treePathMap = new HashMap<>();
 
         for (int i = 0; i < list.size(); i++) {
             BomImportDto dto = list.get(i);
-            String currentCode = dto.getCode();
-            String parentCode = dto.getParentCode();
+            String parentKey = dto.getParentName() + "|" + dto.getParentSpec();
+            String childKey = dto.getChildName() + "|" + dto.getChildSpec();
 
-            // 澶勭悊鏍硅妭鐐癸細涓�鑸寚绗竴琛屼笖娌℃湁鐖堕」缂栧彿
-            if (i == 0 && StringUtils.isBlank(parentCode)) {
+            //澶勭悊鏍硅妭鐐�,绗竴琛屼笖瀛愰」涓虹┖
+            if (i == 0 && StringUtils.isBlank(dto.getChildName())) {
                 ProductStructure rootNode = new ProductStructure();
                 rootNode.setBomId(bom.getId());
                 rootNode.setParentId(null); // 椤跺眰娌℃湁鐖惰妭鐐�
-                if (processMap.containsKey(dto.getProcess())) {
-                    rootNode.setProcessId(processMap.get(dto.getProcess()));
-                }
+                rootNode.setProductModelId(rootModel.getId());
                 rootNode.setUnitQuantity(BigDecimal.ONE);
+                ProductMaterial rootMaterial = productMaterialService.getById(rootModel.getProductId());
+                rootNode.setUnit(rootMaterial != null ? rootMaterial.getUnit() : null);
                 productStructureService.save(rootNode);
 
-                treePathMap.put(currentCode, rootNode.getId());
+                treePathMap.put(parentKey, rootNode.getId());
                 continue;
             }
 
             //  澶勭悊瀛愬眰绾ц妭鐐�
             //  鎵惧埌鐖惰妭鐐瑰湪鏁版嵁搴撻噷鐨� ID
-            Long parentStructureId = treePathMap.get(parentCode);
+            Long parentStructureId = treePathMap.get(parentKey);
             if (parentStructureId == null) {
                 // 濡傛灉 Map 閲屾壘涓嶅埌锛岃鏄� Excel 椤哄簭涔变簡鎴栬�呮暟鎹湁璇�
-                throw new ServiceException("瀵煎叆澶辫触: 鐖堕」[" + parentCode + "]蹇呴』鍦ㄥ叾瀛愰」涔嬪墠瀹氫箟");
+                throw new ServiceException("瀵煎叆澶辫触: 鐖堕」[" + dto.getParentName() + "]蹇呴』鍦ㄥ叾瀛愰」涔嬪墠瀹氫箟");
             }
+
+            //  鑾峰彇瀛愰」妯″瀷淇℃伅
+            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()));
             }
             productStructureService.save(node);
 
-            //  鎶婂綋鍓嶉」璁板綍鍒� Map, 浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
-            treePathMap.put(currentCode, node.getId());
+            //  鎶婂綋鍓嶅瓙椤硅褰曞埌 Map,浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
+            //  鍚屼竴鐖堕」涓嬬殑鍚屽悕瀛愰」涓嶉渶瑕侀噸澶嶈褰�
+            treePathMap.put(childKey, node.getId());
         }
 
         return AjaxResult.success("BOM瀵煎叆鎴愬姛");
@@ -189,7 +208,8 @@
         for (ProductStructureDto root : treeData) {
             //  娣诲姞鏍硅妭鐐�
             BomImportDto rootRow = new BomImportDto();
-            rootRow.setCode(root.getId().toString());
+            rootRow.setParentName(root.getProductName());
+            rootRow.setParentSpec(root.getModel());
             rootRow.setUnitQty(root.getUnitQuantity());
             rootRow.setRemark("");
             exportList.add(rootRow);
@@ -211,14 +231,14 @@
                 }
 
                 BomImportDto row = new BomImportDto();
-                // 鐖剁被缂栧彿
-                row.setParentCode(parent.getId().toString());
-                // 鏈韩缂栧彿
-                row.setCode(child.getId().toString());
+                // 鐖剁被淇℃伅
+                row.setParentName(parent.getProductName());
+                row.setParentSpec(parent.getModel());
+                // 瀛愮被淇℃伅
+                row.setChildName(child.getProductName());
+                row.setChildSpec(child.getModel());
                 row.setUnitQty(child.getUnitQuantity());
                 row.setProcess(child.getProcessName());
-//                row.setProcess();
-                row.setRemark("");
 
                 exportList.add(row);
 
@@ -241,6 +261,18 @@
             map.put(node.getId(), node);
             populateMap(node.getChildren(), map);
         }
+    }
+
+    private ProductMaterialSku findModel(String name, String spec) {
+        ProductMaterial product = productMaterialService.getOne(new LambdaQueryWrapper<ProductMaterial>()
+                .eq(ProductMaterial::getProductName, name).last("limit 1"));
+        if (product == null) throw new ServiceException("浜у搧鏈淮鎶わ細" + name);
+
+        ProductMaterialSku model = productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>()
+                .eq(ProductMaterialSku::getProductId, product.getId())
+                .eq(ProductMaterialSku::getModel, spec).last("limit 1"));
+        if (model == null) throw new ServiceException("瑙勬牸鏈淮鎶わ細" + name + "[" + spec + "]");
+        return model;
     }
 
     private void handleProcess(List<BomImportDto> list) {
@@ -283,5 +315,3 @@
         return s.replaceAll("[\\u00A0\\u3000]", "").trim();
     }
 }
-
-
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 2dfd0f3..22f248e 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
@@ -86,17 +86,27 @@
         //  鐪熷疄鐨勭埗鑺傜偣 ID
         Long realParentId;
         for (ProductStructureDto psDto : flatDtoList) {
-            if (psDto.getId() == null && psDto.getParentTempId() != null) {
+            String parentTempId = psDto.getParentTempId();
+            if (psDto.getId() == null && parentTempId != null && !parentTempId.isEmpty()) {
                 ProductStructure child = tempEntityMap.get(psDto.getTempId());
-                if (tempEntityMap.containsKey(psDto.getParentTempId())) {
+                if (tempEntityMap.containsKey(parentTempId)) {
                     // 鐖惰妭鐐规槸鏂拌妭鐐�
-                    realParentId = tempEntityMap.get(psDto.getParentTempId()).getId();
+                    realParentId = tempEntityMap.get(parentTempId).getId();
                 } else {
                     // 鐖惰妭鐐规槸鑰佽妭鐐�
-                    realParentId = Long.valueOf(psDto.getParentTempId());
+                    try {
+                        realParentId = Long.valueOf(parentTempId);
+                    } catch (NumberFormatException e) {
+                        realParentId = 0L;
+                    }
                 }
 
                 child.setParentId(realParentId);
+                parentFixList.add(child);
+            } else if (psDto.getId() == null) {
+                // 濡傛灉 parentTempId 涓虹┖锛屽垯鏄《绾ц妭鐐�
+                ProductStructure child = tempEntityMap.get(psDto.getTempId());
+                child.setParentId(0L);
                 parentFixList.add(child);
             }
         }
@@ -104,7 +114,6 @@
         if (!parentFixList.isEmpty()) {
             this.updateBatchById(parentFixList);
         }
-
         if (!updateList.isEmpty()) {
             this.updateBatchById(updateList);
         }
diff --git a/src/main/resources/mapper/production/ProductStructureMapper.xml b/src/main/resources/mapper/production/ProductStructureMapper.xml
index f92a9d0..18a4f89 100644
--- a/src/main/resources/mapper/production/ProductStructureMapper.xml
+++ b/src/main/resources/mapper/production/ProductStructureMapper.xml
@@ -18,9 +18,15 @@
                ps.bom_id,
                ps.unit_quantity,
                ps.process_id,
-               pp.name AS process_name
+               pp.name         AS process_name,
+               pm.product_name AS productName,
+               pm.unit,
+               pms.model
+
         FROM product_structure ps
                  LEFT JOIN product_process pp ON ps.process_id = pp.id
+                 LEFT JOIN product_material_sku pms ON pms.id = ps.product_model_id
+                 LEFT JOIN product_material pm ON pm.id = pms.product_id
         WHERE ps.bom_id = #{bomId}
         ORDER BY ps.id
     </select>

--
Gitblit v1.9.3