From 5640d1300795c002438962386049bb65fb4f2216 Mon Sep 17 00:00:00 2001
From: 吴羡 <16976746+Xian-TT3@user.noreply.gitee.com>
Date: 星期二, 22 九月 2026 18:05:34 +0800
Subject: [PATCH] feat: 采购台账产品层级自动注册与材质字段、大类删除限制、审批模板启用后仅可修改状态

---
 src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java |   98 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 89 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
index 2b9eb40..ba6b4d6 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
@@ -89,21 +89,27 @@
 
     @Override
     public int addOrEditProduct(ProductDto productDto) {
-        if (ObjectUtils.isEmpty(productDto.getParentId())) {
-            throw new IllegalArgumentException("璇烽�夋嫨鐖惰妭鐐�");
-        }
         String productName = StringUtils.trim(productDto.getProductName());
         if (StringUtils.isEmpty(productName)) {
             throw new IllegalArgumentException("浜у搧鍚嶇О涓嶈兘涓虹┖");
+        }
+        // 鐖惰妭鐐逛负绌鸿〃绀烘柊澧炰竴绾уぇ绫�
+        if (ObjectUtils.isEmpty(productDto.getParentId())) {
+            if (productDto.getId() != null) {
+                throw new IllegalArgumentException("璇烽�夋嫨鐖惰妭鐐�");
+            }
+            productDto.setParentId(null);
         }
         productDto.setProductName(productName);
         checkProductNameUnique(productDto.getParentId(), productName, productDto.getId());
         if (productDto.getId() == null) {
             // 鏂板浜у搧閫昏緫
-            // 妫�鏌ョ埗鑺傜偣鏄惁瀛樺湪锛堝彲閫夛紝鏍规嵁涓氬姟闇�姹傦級
-            Product parent = productMapper.selectById(productDto.getParentId());
-            if (parent == null) {
-                throw new IllegalArgumentException("鐖惰妭鐐逛笉瀛樺湪锛屾棤娉曟坊鍔犲瓙浜у搧");
+            // 妫�鏌ョ埗鑺傜偣鏄惁瀛樺湪锛堜竴绾уぇ绫绘棤鐖惰妭鐐癸級
+            if (productDto.getParentId() != null) {
+                Product parent = productMapper.selectById(productDto.getParentId());
+                if (parent == null) {
+                    throw new IllegalArgumentException("鐖惰妭鐐逛笉瀛樺湪锛屾棤娉曟坊鍔犲瓙浜у搧");
+                }
             }
             return productMapper.insert(productDto);
         } else {
@@ -119,8 +125,12 @@
 
     private void checkProductNameUnique(Long parentId, String productName, Long currentId) {
         LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(Product::getParentId, parentId)
-                .eq(Product::getProductName, productName)
+        if (parentId == null) {
+            queryWrapper.isNull(Product::getParentId);
+        } else {
+            queryWrapper.eq(Product::getParentId, parentId);
+        }
+        queryWrapper.eq(Product::getProductName, productName)
                 .ne(currentId != null, Product::getId, currentId)
                 .last("limit 1");
         Product duplicateProduct = productMapper.selectOne(queryWrapper);
@@ -131,6 +141,26 @@
 
     @Override
     public int delProductByIds(Long[] ids) {
+        // 瀛樺湪瀛愮被鏃朵笉鍏佽鍒犻櫎
+        LambdaQueryWrapper<Product> childWrapper = new LambdaQueryWrapper<>();
+        childWrapper.in(Product::getParentId, ids).last("limit 1");
+        Product childProduct = productMapper.selectOne(childWrapper);
+        if (childProduct != null) {
+            throw new IllegalArgumentException("璇ュぇ绫讳笅瀛樺湪瀛愮被锛屼笉鑳藉垹闄�");
+        }
+
+        // 澶х被涓嬪瓨鍦ㄨ鏍煎瀷鍙锋暟鎹椂涓嶅厑璁稿垹闄�
+        for (Product product : productMapper.selectBatchIds(Arrays.asList(ids))) {
+            if (product.getParentId() != null) {
+                continue;
+            }
+            LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>();
+            modelWrapper.eq(ProductModel::getProductId, product.getId()).last("limit 1");
+            if (productModelMapper.selectOne(modelWrapper) != null) {
+                throw new IllegalArgumentException("璇ュぇ绫讳笅瀛樺湪鏁版嵁锛屼笉鑳藉垹闄�");
+            }
+        }
+
         // 1. 鍒犻櫎瀛愯〃 product_model 涓叧鑱旂殑鏁版嵁
         LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.in(ProductModel::getProductId, ids);
@@ -142,4 +172,54 @@
         return deleteCount;
     }
 
+    @Override
+    public ProductModel ensureProductHierarchy(String productName, String material, String modelName, String unit) {
+        productName = StringUtils.trim(productName);
+        material = StringUtils.trim(material);
+        modelName = StringUtils.trim(modelName);
+        if (StringUtils.isEmpty(productName) || StringUtils.isEmpty(material) || StringUtils.isEmpty(modelName)) {
+            return null;
+        }
+
+        // 鍝佸悕锛氫竴绾уぇ绫伙紙parentId 涓虹┖锛�
+        LambdaQueryWrapper<Product> categoryWrapper = new LambdaQueryWrapper<>();
+        categoryWrapper.isNull(Product::getParentId)
+                .eq(Product::getProductName, productName)
+                .last("limit 1");
+        Product category = productMapper.selectOne(categoryWrapper);
+        if (category == null) {
+            category = new Product();
+            category.setProductName(productName);
+            productMapper.insert(category);
+        }
+
+        // 鏉愯川锛氬ぇ绫讳笅鐨勫瓙绫�
+        LambdaQueryWrapper<Product> materialWrapper = new LambdaQueryWrapper<>();
+        materialWrapper.eq(Product::getParentId, category.getId())
+                .eq(Product::getProductName, material)
+                .last("limit 1");
+        Product materialProduct = productMapper.selectOne(materialWrapper);
+        if (materialProduct == null) {
+            materialProduct = new Product();
+            materialProduct.setParentId(category.getId());
+            materialProduct.setProductName(material);
+            productMapper.insert(materialProduct);
+        }
+
+        // 瑙勬牸鍨嬪彿锛氬瓙绫讳笅鐨勮鏍�
+        LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>();
+        modelWrapper.eq(ProductModel::getProductId, materialProduct.getId())
+                .eq(ProductModel::getModel, modelName)
+                .last("limit 1");
+        ProductModel productModel = productModelMapper.selectOne(modelWrapper);
+        if (productModel == null) {
+            productModel = new ProductModel();
+            productModel.setProductId(materialProduct.getId());
+            productModel.setModel(modelName);
+            productModel.setUnit(unit);
+            productModelMapper.insert(productModel);
+        }
+        return productModel;
+    }
+
 }

--
Gitblit v1.9.3