From bc116c55a44bc344e5575b5bdd4c2591bc0475aa Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期二, 17 三月 2026 16:43:36 +0800
Subject: [PATCH] fix: BOM模块的导入、模板下载、新增、编辑修改

---
 src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java |  131 ++++++++++++++++---------------------------
 1 files changed, 50 insertions(+), 81 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 2554b5e..b5c9562 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -4,10 +4,6 @@
 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.basic.pojo.Product;
-import com.ruoyi.basic.pojo.ProductModel;
-import com.ruoyi.basic.service.IProductModelService;
-import com.ruoyi.basic.service.IProductService;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -19,9 +15,9 @@
 import com.ruoyi.production.pojo.ProductBom;
 import com.ruoyi.production.pojo.ProductProcess;
 import com.ruoyi.production.pojo.ProductStructure;
-import com.ruoyi.production.service.ProductBomService;
-import com.ruoyi.production.service.ProductProcessService;
-import com.ruoyi.production.service.ProductStructureService;
+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;
@@ -44,14 +40,8 @@
 public class ProductBomServiceImpl extends ServiceImpl<ProductBomMapper, ProductBom> implements ProductBomService {
 
     @Autowired
-    private IProductService productService;
-
-    @Autowired
     private ProductBomMapper productBomMapper;
 
-
-    @Autowired
-    private IProductModelService productModelService;
 
     @Autowired
     private ProductStructureService productStructureService;
@@ -59,38 +49,31 @@
     @Autowired
     private ProductProcessService productProcessService;
 
+    @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("鏂板澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
+        }
+
+        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode());
+        if (sysDictData == null) {
+            throw new ServiceException("鏂板澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
+        }
+
         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("璇烽�夋嫨浜у搧妯″瀷");
-            }
-
-            ProductModel productModel = productModelService.getById(productBom.getProductModelId());
-            if (productModel == null) {
-                throw new ServiceException("閫夋嫨鐨勪骇鍝佹ā鍨嬩笉瀛樺湪");
-            }
-
-            //  娣诲姞鍒濆鐨勪骇鍝佺粨鏋�
-            ProductStructure productStructure = new ProductStructure();
-            productStructure.setProductModelId(productBom.getProductModelId());
-            productStructure.setUnit(productModel.getUnit());
-            productStructure.setUnitQuantity(BigDecimal.valueOf(1));
-            productStructure.setBomId(productBom.getId());
-
-            productStructureService.save(productStructure);
 
             return AjaxResult.success();
         }
@@ -99,7 +82,15 @@
 
     @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 {
@@ -110,75 +101,66 @@
 
         if (list == null || list.isEmpty()) return AjaxResult.error("鏁版嵁涓虹┖");
 
-        //  澶勭悊宸ュ簭
+        //  澶勭悊瀛楁娓呯悊
         list.forEach(dto -> {
-            dto.setParentName(clean(dto.getParentName()));
-            dto.setParentSpec(clean(dto.getParentSpec()));
-            dto.setChildName(clean(dto.getChildName()));
-            dto.setChildSpec(clean(dto.getChildSpec()));
+            dto.setParentCode(clean(dto.getParentCode()));
+            dto.setCode(clean(dto.getCode()));
         });
         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);
-        ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec());
         ProductBom bom = new ProductBom();
-        bom.setProductModelId(rootModel.getId());
         bom.setVersion("1.0");
+        bom.setDictCode(dictCode);
         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 parentKey = dto.getParentName() + "|" + dto.getParentSpec();
-            String childKey = dto.getChildName() + "|" + dto.getChildSpec();
+            String currentCode = dto.getCode();
+            String parentCode = dto.getParentCode();
 
-            //澶勭悊鏍硅妭鐐�,绗竴琛屼笖瀛愰」涓虹┖
-            if (i == 0 && StringUtils.isBlank(dto.getChildName())) {
+            // 澶勭悊鏍硅妭鐐癸細涓�鑸寚绗竴琛屼笖娌℃湁鐖堕」缂栧彿
+            if (i == 0 && StringUtils.isBlank(parentCode)) {
                 ProductStructure rootNode = new ProductStructure();
                 rootNode.setBomId(bom.getId());
                 rootNode.setParentId(null); // 椤跺眰娌℃湁鐖惰妭鐐�
-                rootNode.setProductModelId(rootModel.getId());
+                if (processMap.containsKey(dto.getProcess())) {
+                    rootNode.setProcessId(processMap.get(dto.getProcess()));
+                }
                 rootNode.setUnitQuantity(BigDecimal.ONE);
-                rootNode.setUnit(rootModel.getUnit());
                 productStructureService.save(rootNode);
 
-                treePathMap.put(parentKey, rootNode.getId());
+                treePathMap.put(currentCode, rootNode.getId());
                 continue;
             }
 
             //  澶勭悊瀛愬眰绾ц妭鐐�
             //  鎵惧埌鐖惰妭鐐瑰湪鏁版嵁搴撻噷鐨� ID
-            Long parentStructureId = treePathMap.get(parentKey);
+            Long parentStructureId = treePathMap.get(parentCode);
             if (parentStructureId == null) {
                 // 濡傛灉 Map 閲屾壘涓嶅埌锛岃鏄� Excel 椤哄簭涔变簡鎴栬�呮暟鎹湁璇�
-                throw new ServiceException("瀵煎叆澶辫触: 鐖堕」[" + dto.getParentName() + "]蹇呴』鍦ㄥ叾瀛愰」涔嬪墠瀹氫箟");
+                throw new ServiceException("瀵煎叆澶辫触: 鐖堕」[" + parentCode + "]蹇呴』鍦ㄥ叾瀛愰」涔嬪墠瀹氫箟");
             }
-
-            //  鑾峰彇瀛愰」妯″瀷淇℃伅
-            ProductModel 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());
-            node.setUnit(childModel.getUnit());
             if (processMap.containsKey(dto.getProcess())) {
                 node.setProcessId(processMap.get(dto.getProcess()));
             }
             productStructureService.save(node);
 
-            //  鎶婂綋鍓嶅瓙椤硅褰曞埌 Map,浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
-            //  鍚屼竴鐖堕」涓嬬殑鍚屽悕瀛愰」涓嶉渶瑕侀噸澶嶈褰�
-            treePathMap.put(childKey, node.getId());
+            //  鎶婂綋鍓嶉」璁板綍鍒� Map, 浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
+            treePathMap.put(currentCode, node.getId());
         }
 
         return AjaxResult.success("BOM瀵煎叆鎴愬姛");
@@ -191,7 +173,7 @@
             return;
         }
 
-        List<ProductStructureDto> treeData = productStructureService.listBybomId(bomId);
+        List<ProductStructureDto> treeData = productStructureService.listByBomId(bomId);
         if (treeData == null || treeData.isEmpty()) {
             return;
         }
@@ -207,8 +189,7 @@
         for (ProductStructureDto root : treeData) {
             //  娣诲姞鏍硅妭鐐�
             BomImportDto rootRow = new BomImportDto();
-            rootRow.setParentName(root.getProductName());
-            rootRow.setParentSpec(root.getModel());
+            rootRow.setCode(root.getId().toString());
             rootRow.setUnitQty(root.getUnitQuantity());
             rootRow.setRemark("");
             exportList.add(rootRow);
@@ -230,14 +211,14 @@
                 }
 
                 BomImportDto row = new BomImportDto();
-                // 鐖剁被淇℃伅
-                row.setParentName(parent.getProductName());
-                row.setParentSpec(parent.getModel());
-                // 瀛愮被淇℃伅
-                row.setChildName(child.getProductName());
-                row.setChildSpec(child.getModel());
+                // 鐖剁被缂栧彿
+                row.setParentCode(parent.getId().toString());
+                // 鏈韩缂栧彿
+                row.setCode(child.getId().toString());
                 row.setUnitQty(child.getUnitQuantity());
                 row.setProcess(child.getProcessName());
+//                row.setProcess();
+                row.setRemark("");
 
                 exportList.add(row);
 
@@ -260,18 +241,6 @@
             map.put(node.getId(), node);
             populateMap(node.getChildren(), map);
         }
-    }
-
-    private ProductModel findModel(String name, String spec) {
-        Product product = productService.getOne(new LambdaQueryWrapper<Product>()
-                .eq(Product::getProductName, name).last("limit 1"));
-        if (product == null) throw new ServiceException("浜у搧鏈淮鎶わ細" + name);
-
-        ProductModel model = productModelService.getOne(new LambdaQueryWrapper<ProductModel>()
-                .eq(ProductModel::getProductId, product.getId())
-                .eq(ProductModel::getModel, spec).last("limit 1"));
-        if (model == null) throw new ServiceException("瑙勬牸鏈淮鎶わ細" + name + "[" + spec + "]");
-        return model;
     }
 
     private void handleProcess(List<BomImportDto> list) {

--
Gitblit v1.9.3