From 86bceee8a4a9114527ef6eac70d5c7dd39c87490 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 10 九月 2026 10:38:36 +0800
Subject: [PATCH] fix: 产品导入类

---
 src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java |   86 +++++++++++++++++++++++++++++++++---------
 1 files changed, 67 insertions(+), 19 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 fdf2dd3..b8f2f44 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductServiceImpl.java
@@ -2,6 +2,7 @@
 
 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;
@@ -11,19 +12,26 @@
 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 com.ruoyi.common.utils.poi.ExcelUtil;
-import com.ruoyi.framework.web.domain.AjaxResult;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
 
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 @Service
 @AllArgsConstructor
+@Slf4j
 public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
 
     private ProductMapper productMapper;
@@ -55,7 +63,7 @@
     }
 
     @Override
-    public IPage<ProductModel> listPageProductModel(Page<ProductModel> page, ProductModel productModel) {
+    public IPage<ProductModelVo> listPageProductModel(Page<ProductModelVo> page, ProductModel productModel) {
         return productModelMapper.listPageProductModel(page, productModel);
     }
 
@@ -89,17 +97,21 @@
 
     @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("浜у搧鍚嶇О涓嶈兘涓虹┖");
+        }
+        productDto.setProductName(productName);
+        checkProductNameUnique(productDto.getParentId(), productName, productDto.getId());
         if (productDto.getId() == null) {
             // 鏂板浜у搧閫昏緫
-            if (productDto.getParentId() == null) {
-                // 鑻ユ湭鎸囧畾鐖惰妭鐐癸紝榛樿涓烘牴鑺傜偣锛坧arentId 璁句负 null锛�
-                productDto.setParentId(null);
-            } else {
-                // 妫�鏌ョ埗鑺傜偣鏄惁瀛樺湪锛堝彲閫夛紝鏍规嵁涓氬姟闇�姹傦級
-                Product parent = productMapper.selectById(productDto.getParentId());
-                if (parent == null) {
-                    throw new IllegalArgumentException("鐖惰妭鐐逛笉瀛樺湪锛屾棤娉曟坊鍔犲瓙浜у搧");
-                }
+            // 妫�鏌ョ埗鑺傜偣鏄惁瀛樺湪锛堝彲閫夛紝鏍规嵁涓氬姟闇�姹傦級
+            Product parent = productMapper.selectById(productDto.getParentId());
+            if (parent == null) {
+                throw new IllegalArgumentException("鐖惰妭鐐逛笉瀛樺湪锛屾棤娉曟坊鍔犲瓙浜у搧");
             }
             return productMapper.insert(productDto);
         } else {
@@ -113,17 +125,53 @@
         }
     }
 
+    private void checkProductNameUnique(Long parentId, String productName, Long currentId) {
+        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(Product::getParentId, parentId)
+                .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 void exportImportTemplate(HttpServletResponse response) {
+        String templatePath = "static/浜у搧瀵煎叆妯℃澘.xlsx";
+        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath)) {
+            if (inputStream == null) {
+                throw new FileNotFoundException("妯℃澘鏂囦欢涓嶅瓨鍦細" + templatePath);
+            }
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            String fileName = URLEncoder.encode("浜у搧瀵煎叆妯℃澘.xlsx", "utf-8").replaceAll("\\+", "%20");
+            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
+            try (OutputStream outputStream = response.getOutputStream()) {
+                byte[] buffer = new byte[1024];
+                int len;
+                while ((len = inputStream.read(buffer)) > 0) {
+                    outputStream.write(buffer, 0, len);
+                }
+                outputStream.flush();
+            }
+        } catch (IOException e) {
+            log.error("瀵煎嚭浜у搧瀵煎叆妯℃澘澶辫触", e);
+            try {
+                response.getWriter().write("妯℃澘瀵煎嚭澶辫触锛�" + e.getMessage());
+            } catch (IOException ex) {
+                log.error("鍝嶅簲杈撳嚭閿欒", ex);
+            }
+        }
+    }
+
     @Override
     public int delProductByIds(Long[] ids) {
-        // 1. 鍒犻櫎瀛愯〃 product_model 涓叧鑱旂殑鏁版嵁
         LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.in(ProductModel::getProductId, ids);
         productModelMapper.delete(queryWrapper);
 
-        // 2. 鍒犻櫎涓昏〃 product 鏁版嵁
-        int deleteCount = productMapper.deleteBatchIds(Arrays.asList(ids));
-
-        return deleteCount;
+        return productMapper.deleteBatchIds(Arrays.asList(ids));
     }
-
 }

--
Gitblit v1.9.3