From 69e1d95373227f364b2fa6d3852be24bafd1a9dc Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期一, 16 三月 2026 17:51:59 +0800
Subject: [PATCH] yys 产品基础数据修改,分批发货开发

---
 src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java |  131 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 110 insertions(+), 21 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 bc3840f..632edec 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -5,20 +5,23 @@
 import com.baomidou.mybatisplus.core.metadata.IPage;
 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.ProductModelDto;
+import com.ruoyi.basic.dto.*;
 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.utils.OrderUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.common.utils.uuid.IdUtils;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import lombok.AllArgsConstructor;
+import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.time.LocalDate;
@@ -44,19 +47,27 @@
 
     @Override
     public int addOrEditProductModel(ProductModelDto productModelDto) {
-
+        if(StringUtils.isEmpty(productModelDto.getProductName())){
+            throw new RuntimeException("浜у搧鍚嶇О涓嶈兘涓虹┖");
+        }
+        Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
+                .eq(Product::getProductName, productModelDto.getProductName()));
         if (productModelDto.getId() == null) {
+            if(product == null){
+                product = new Product();
+                product.setProductName(productModelDto.getProductName());
+                productMapper.insert(product);
+            }
             ProductModel productModel = new ProductModel();
             BeanUtils.copyProperties(productModelDto,productModel);
-            productModelMapper.insert(productModel);
-
-            String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
-            String idStr = String.format("%06d", productModel.getId()); // 鍏綅琛�0
-            String newProductCode = "BM" + dateStr + idStr;
-            // 鏇存柊鏁版嵁搴撲腑鐨刾roductCode
-            productModel.setProductCode(newProductCode);
-            return productModelMapper.updateById(productModel);
+            productModel.setProductId(product.getId());
+            return productModelMapper.insert(productModel);
         } else {
+            if(product != null){
+                product.setProductName(productModelDto.getProductName());
+                productMapper.updateById(product);
+            }
+
             return productModelMapper.updateById(productModelDto);
         }
     }
@@ -95,24 +106,102 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Boolean importProduct(MultipartFile file) {
         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());
+            ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class);
+            List<ProductModelExcelCopyDto> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
+            List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>()
+                    .isNull(Product::getParentId));
+            if(CollectionUtils.isEmpty(productList)) {
+                throw new RuntimeException("璇峰厛娣诲姞鐖剁骇浜у搧");
+            }
+            if(CollectionUtils.isNotEmpty(productModelList)){
+                // 2. 鎸変骇鍝佸悕绉板垎缁�
+                Map<String, List<ProductModelExcelCopyDto>> groupedByProductName = productModelList.stream()
+                        .collect(Collectors.groupingBy(ProductModelExcelCopyDto::getProductName));
+                for (Map.Entry<String, List<ProductModelExcelCopyDto>> entry : groupedByProductName.entrySet()) {
+                    // 鏍规嵁鍚嶇О鏌ヨ鏄惁瀛樺湪浜у搧
+                    Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
+                            .eq(Product::getProductName, entry.getKey())
+                            .last("limit 1"));
+                    if(product == null){
+                        product = new Product();
+                        product.setProductName(entry.getKey());
+                        product.setParentId(null); // 鐖惰妭鐐笽D
+                        // 2. 鎻掑叆褰撳墠鑺傜偣锛孧yBatis浼氳嚜鍔ㄥ洖濉玴roduct鐨刬d灞炴��
+                        productMapper.insert(product);
+                    }
+                    Product finalProduct = product;
+                    entry.getValue().forEach(productModelExcelDto -> {
+                        // 鏍规嵁鍥剧焊缂栧彿鏌ヨ
+                        ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>()
+                                .eq(ProductModel::getModel, productModelExcelDto.getModel())
+                                .last("limit 1"));
+                        if(productModel == null){
+                            productModel = new ProductModel();
+                            BeanUtils.copyProperties(productModelExcelDto,productModel);
+                            if(productModelExcelDto.getProductType() == null) {
+                                productModel.setProductType(1);
+                            }
+                            productModel.setProductId(finalProduct.getId());
+                            productModelMapper.insert(productModel);
+                        }else{
+                            BeanUtils.copyProperties(productModelExcelDto,productModel);
+                            productModel.setProductId(finalProduct.getId());
+                            productModelMapper.updateById(productModel);
+                        }
                     });
-                    this.saveOrUpdateBatch(v);
                 }
-            });
+            }
+//            List<ProductModelExcelDto> productModelExcelDtos = OrderUtils.buildTree(productModelList);
+//            if(CollectionUtils.isNotEmpty(productModelExcelDtos)){
+//                recursiveSaveProduct(productModelExcelDtos.get(0),null);
+//            }
             return true;
         }catch (Exception e) {
             e.printStackTrace();
         }
         return false;
     }
+
+    /**
+     * 閫掑綊瀵煎叆鏍戝舰浜у搧鏁版嵁
+     * @param excelDto Excel瑙f瀽鍚庣殑鏍戝舰鑺傜偣
+     * @param parentId 鐖惰妭鐐笽D锛堥《绾ц妭鐐逛紶null/0锛�
+     */
+    @Transactional(rollbackFor = Exception.class) // 浜嬪姟淇濊瘉锛屽け璐ュ垯鍥炴粴
+    public void recursiveSaveProduct(ProductModelExcelDto excelDto, Long parentId) {
+        // 1. 鏋勫缓褰撳墠鑺傜偣鐨凱roduct瀹炰綋
+        Product product = new Product();
+        product.setProductName(excelDto.getProductName());
+        product.setParentId(parentId); // 鐖惰妭鐐笽D
+
+        // 2. 鎻掑叆褰撳墠鑺傜偣锛孧yBatis浼氳嚜鍔ㄥ洖濉玴roduct鐨刬d灞炴��
+        productMapper.insert(product);
+        Long currentId = product.getId(); // 鑾峰彇褰撳墠鑺傜偣涓婚敭
+
+        // 3. 閫掑綊澶勭悊瀛愯妭鐐�
+        if (excelDto.getChildren() != null && !excelDto.getChildren().isEmpty()) {
+            for (ProductModelExcelDto childDto : excelDto.getChildren()) {
+                recursiveSaveProduct(childDto, currentId); // 鐖禝D涓哄綋鍓嶈妭鐐笽D
+            }
+        }
+
+        // 4. 锛堝彲閫夛級澶勭悊items鏁版嵁锛堝鏋滈渶瑕佹彃鍏ュ埌鍏宠仈琛級
+         if (excelDto.getItems() != null && !excelDto.getItems().isEmpty()) {
+             for (ProductModelExcelItemDto item : excelDto.getItems()) {
+                 // 鎻掑叆item鍏宠仈鏁版嵁锛屽叧鑱攃urrentId
+                 // 鏋勫缓ProductModel瀹炰綋
+                 ProductModel productModel = new ProductModel();
+                 productModel.setProductId(currentId); // 鍏宠仈褰撳墠浜у搧鑺傜偣ID
+                 productModel.setModel(item.getModel()); // 浠嶪temDTO鑾峰彇鍨嬪彿
+                 productModel.setUnit(item.getUnit());   // 浠嶪temDTO鑾峰彇鍗曚綅
+                 productModel.setDrawingNumber(item.getDrawingNumber()); // 鍥剧焊缂栧彿
+                 productModel.setProductType(item.getProductType());
+                 // 鎻掑叆product_model琛�
+                 productModelMapper.insert(productModel);
+             }
+         }
+    }
 }

--
Gitblit v1.9.3