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 |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 112 insertions(+), 5 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 9825b39..ba6b4d6 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
@@ -1,6 +1,9 @@
 package com.ruoyi.basic.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.dto.ProductDto;
 import com.ruoyi.basic.dto.ProductTreeDto;
@@ -9,6 +12,8 @@
 import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.service.IProductService;
+import com.ruoyi.basic.vo.ProductModelVo;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
@@ -49,6 +54,12 @@
         return tree;
     }
 
+    @Override
+    public IPage<ProductModelVo> listPageProductModel(Page<ProductModelVo> page, ProductModel productModel) {
+        return productModelMapper.listPageProductModel(page, productModel);
+    }
+
+
     // 閫掑綊鏋勫缓瀛愯妭鐐�
     private List<ProductTreeDto> buildChildrenNodes(Long parentId) {
         // 鏌ヨ褰撳墠鐖惰妭鐐圭殑瀛愯妭鐐�
@@ -78,13 +89,23 @@
 
     @Override
     public int addOrEditProduct(ProductDto productDto) {
+        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) {
             // 鏂板浜у搧閫昏緫
-            if (productDto.getParentId() == null) {
-                // 鑻ユ湭鎸囧畾鐖惰妭鐐癸紝榛樿涓烘牴鑺傜偣锛坧arentId 璁句负 null锛�
-                productDto.setParentId(null);
-            } else {
-                // 妫�鏌ョ埗鑺傜偣鏄惁瀛樺湪锛堝彲閫夛紝鏍规嵁涓氬姟闇�姹傦級
+            // 妫�鏌ョ埗鑺傜偣鏄惁瀛樺湪锛堜竴绾уぇ绫绘棤鐖惰妭鐐癸級
+            if (productDto.getParentId() != null) {
                 Product parent = productMapper.selectById(productDto.getParentId());
                 if (parent == null) {
                     throw new IllegalArgumentException("鐖惰妭鐐逛笉瀛樺湪锛屾棤娉曟坊鍔犲瓙浜у搧");
@@ -102,8 +123,44 @@
         }
     }
 
+    private void checkProductNameUnique(Long parentId, String productName, Long currentId) {
+        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
+        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);
+        if (duplicateProduct != null) {
+            throw new IllegalArgumentException("瀵瑰簲鐨�" + productName + "宸茬粡瀛樺湪");
+        }
+    }
+
     @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);
@@ -115,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