From 78e8144e8713af9e6b3aa14ff1f8b4091d698616 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 24 四月 2026 14:25:13 +0800
Subject: [PATCH] feat: 产品维护导出功能

---
 src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java |  146 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 128 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
index dc6d3e9..41daac9 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -3,26 +3,31 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 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.ProductModelDataExportDto;
 import com.ruoyi.basic.dto.ProductModelDto;
 import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
 import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.service.IProductModelService;
+import com.ruoyi.common.exception.ServiceException;
+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 com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import javax.servlet.http.HttpServletResponse;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -44,12 +49,13 @@
 
         if (productModelDto.getId() == null) {
             ProductModel productModel = new ProductModel();
-            BeanUtils.copyProperties(productModelDto,productModel);
+            BeanUtils.copyProperties(productModelDto, productModel);
             return productModelMapper.insert(productModel);
         } else {
             return productModelMapper.updateById(productModelDto);
         }
     }
+
 
     @Override
     public int delProductModel(Long[] ids) {
@@ -71,6 +77,7 @@
 
     /**
      * 鏍规嵁id鏌ヨ浜у搧瑙勬牸鍒嗛〉鏌ヨ
+     *
      * @param page
      * @param productDto
      * @return
@@ -83,24 +90,127 @@
     }
 
     @Override
-    public Boolean importProduct(MultipartFile file) {
+    @Transactional(rollbackFor = Exception.class)
+    public AjaxResult importProductModel(MultipartFile file, Integer productId) {
+        if (productId == null) {
+            return AjaxResult.error("璇峰厛閫夋嫨浜у搧鍐嶅鍏ヨ鏍煎瀷鍙�");
+        }
+
+        Product product = productMapper.selectById(productId);
+        if (product == null) {
+            return AjaxResult.error("閫夋嫨鐨勪骇鍝佷笉瀛樺湪");
+        }
+
         try {
             ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class);
             List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
-            Map<String, List<ProductModel>> collect = productModelList.stream().collect(Collectors.groupingBy(ProductModel::getProductName));
-            collect.forEach((k,v)->{
-                Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>().eq(Product::getProductName, k).last("LIMIT 1"));
-                if (product != null) {
-                    v.forEach(productModel -> {
-                        productModel.setProductId(product.getId());
-                    });
-                    this.saveOrUpdateBatch(v);
+
+            if (CollectionUtils.isEmpty(productModelList)) {
+                return AjaxResult.error("瀵煎叆鏁版嵁涓嶈兘涓虹┖");
+            }
+
+            //  鑾峰彇褰撳墠浜у搧涓嬫墍鏈夌殑瑙勬牸鍨嬪彿鍚�
+            List<ProductModel> existingModels = list(new LambdaQueryWrapper<ProductModel>().eq(ProductModel::getProductId, productId));
+            Set<String> existingModelNames = existingModels.stream().map(ProductModel::getModel).collect(Collectors.toSet());
+
+            List<ProductModel> waitToSaveList = new ArrayList<>();
+            int skipCount = 0;
+
+            for (int i = 0; i < productModelList.size(); i++) {
+                ProductModel item = productModelList.get(i);
+                int rowNum = i + 2;
+
+                if (StringUtils.isEmpty(item.getModel())) {
+                    return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [瑙勬牸鍨嬪彿] 涓嶈兘涓虹┖");
                 }
-            });
-            return true;
-        }catch (Exception e) {
-            e.printStackTrace();
+                if (StringUtils.isEmpty(item.getUnit())) {
+                    return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [鍗曚綅] 涓嶈兘涓虹┖");
+                }
+
+                //  鍘婚噸,濡傛灉宸插寘鍚鍨嬪彿,鍒欒烦杩�
+                if (existingModelNames.contains(item.getModel())) {
+                    skipCount++;
+                    continue;
+                }
+
+                item.setProductId(product.getId());
+                waitToSaveList.add(item);
+
+                existingModelNames.add(item.getModel());
+            }
+
+            if (!waitToSaveList.isEmpty()) {
+                saveBatch(waitToSaveList);
+            }
+
+            if (skipCount == 0) {
+                return AjaxResult.success(String.format("鎴愬姛瀵煎叆 %d 鏉℃暟鎹�", waitToSaveList.size()));
+            } else {
+                return AjaxResult.success(String.format("鎴愬姛瀵煎叆 %d 鏉★紝璺宠繃宸插瓨鍦ㄦ暟鎹� %d 鏉�", waitToSaveList.size(), skipCount));
+            }
+        } catch (Exception e) {
+            log.error("瀵煎叆浜у搧瑙勬牸寮傚父", e);
+            throw new ServiceException("瀵煎叆澶辫触");
         }
-        return false;
+    }
+
+    @Override
+    public void exportData(HttpServletResponse response, Long productId) {
+        List<Product> allProducts = productMapper.selectList(null);
+        Map<Long, Product> productMap = allProducts.stream()
+                .filter(product -> product.getId() != null)
+                .collect(Collectors.toMap(Product::getId, product -> product));
+        Map<Long, List<Long>> parentChildrenMap = new HashMap<>();
+        for (Product product : allProducts) {
+            if (product.getParentId() == null || product.getId() == null) {
+                continue;
+            }
+            parentChildrenMap.computeIfAbsent(product.getParentId(), key -> new ArrayList<>()).add(product.getId());
+        }
+
+        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
+        if (productId != null) {
+            Set<Long> targetProductIds = new HashSet<>();
+            ArrayDeque<Long> queue = new ArrayDeque<>();
+            queue.offer(productId);
+            while (!queue.isEmpty()) {
+                Long currentId = queue.poll();
+                if (currentId == null || !targetProductIds.add(currentId)) {
+                    continue;
+                }
+                List<Long> children = parentChildrenMap.get(currentId);
+                if (children != null && !children.isEmpty()) {
+                    queue.addAll(children);
+                }
+            }
+            queryWrapper.in(ProductModel::getProductId, targetProductIds);
+        }
+        List<ProductModel> productModelList = list(queryWrapper);
+        List<ProductModelDataExportDto> exportList = productModelList.stream().map(item -> {
+            ProductModelDataExportDto dto = new ProductModelDataExportDto();
+            Product currentProduct = productMap.get(item.getProductId());
+            dto.setProductName(currentProduct == null ? "" : currentProduct.getProductName());
+            dto.setProductCategoryName(resolveRootCategoryName(currentProduct, productMap));
+            dto.setModel(item.getModel());
+            dto.setUnit(item.getUnit());
+            return dto;
+        }).collect(Collectors.toList());
+        ExcelUtil<ProductModelDataExportDto> excelUtil = new ExcelUtil<>(ProductModelDataExportDto.class);
+        excelUtil.exportExcel(response, exportList, "浜у搧瑙勬牸鏁版嵁");
+    }
+
+    private String resolveRootCategoryName(Product currentProduct, Map<Long, Product> productMap) {
+        if (currentProduct == null) {
+            return "";
+        }
+        Product node = currentProduct;
+        while (node.getParentId() != null) {
+            Product parent = productMap.get(node.getParentId());
+            if (parent == null) {
+                break;
+            }
+            node = parent;
+        }
+        return node.getProductName();
     }
 }

--
Gitblit v1.9.3