From 23ec64ed997fc206fba9f0d6a84975f7b36c47be Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期日, 11 十月 2026 16:09:26 +0800
Subject: [PATCH] 天津-奇缘恒艺 录数据需求修改
---
src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java | 100 +++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 91 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..5a330d7 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,56 @@
return deleteCount;
}
+ /** 鍙拌处鎵嬪伐褰曞叆鐨勪骇鍝佸ぇ绫�/瑙勬牸鍨嬪彿缁熶竴鎸傚埌杩欎釜澶х被涓� */
+ private static final String TOP_CATEGORY_NAME = "鎴愬搧";
+
+ @Override
+ public ProductModel ensureProductHierarchy(String productName, String modelName, String unit) {
+ productName = StringUtils.trim(productName);
+ modelName = StringUtils.trim(modelName);
+ if (StringUtils.isEmpty(productName) || StringUtils.isEmpty(modelName)) {
+ return null;
+ }
+
+ // 涓�绾уぇ绫伙細鍥哄畾銆屾垚鍝併�嶏紙涓嶅瓨鍦ㄥ垯寤猴級
+ LambdaQueryWrapper<Product> categoryWrapper = new LambdaQueryWrapper<>();
+ categoryWrapper.isNull(Product::getParentId)
+ .eq(Product::getProductName, TOP_CATEGORY_NAME)
+ .last("limit 1");
+ Product category = productMapper.selectOne(categoryWrapper);
+ if (category == null) {
+ category = new Product();
+ category.setProductName(TOP_CATEGORY_NAME);
+ productMapper.insert(category);
+ }
+
+ // 浜岀骇锛氫骇鍝佸ぇ绫伙紙鎴愬搧涓嬬殑瀛愮被锛�
+ LambdaQueryWrapper<Product> nameWrapper = new LambdaQueryWrapper<>();
+ nameWrapper.eq(Product::getParentId, category.getId())
+ .eq(Product::getProductName, productName)
+ .last("limit 1");
+ Product nameProduct = productMapper.selectOne(nameWrapper);
+ if (nameProduct == null) {
+ nameProduct = new Product();
+ nameProduct.setParentId(category.getId());
+ nameProduct.setProductName(productName);
+ productMapper.insert(nameProduct);
+ }
+
+ // 涓夌骇锛氳鏍煎瀷鍙凤紙浜у搧澶х被涓嬬殑瑙勬牸锛�
+ LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>();
+ modelWrapper.eq(ProductModel::getProductId, nameProduct.getId())
+ .eq(ProductModel::getModel, modelName)
+ .last("limit 1");
+ ProductModel productModel = productModelMapper.selectOne(modelWrapper);
+ if (productModel == null) {
+ productModel = new ProductModel();
+ productModel.setProductId(nameProduct.getId());
+ productModel.setModel(modelName);
+ productModel.setUnit(unit);
+ productModelMapper.insert(productModel);
+ }
+ return productModel;
+ }
+
}
--
Gitblit v1.9.3