From 8c6aad555647fcecaddfaed99516d8b93db837c8 Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期三, 01 四月 2026 11:18:33 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_宁夏_中盛建材' into dev_宁夏_中盛建材

---
 src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java |   81 ++++++++++++++++++++++------------------
 1 files changed, 44 insertions(+), 37 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
index 770944b..35a2034 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
@@ -20,11 +20,7 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -153,7 +149,7 @@
 
         ProductMaterial material = productMaterialMapper.selectById(materialId);
         if (material == null) {
-            throw new ServiceException("鐗╂枡涓嶅瓨鍦�");
+            throw new ServiceException("涓荤墿鏂欎俊鎭笉瀛樺湪");
         }
 
         ExcelUtil<ProductMaterialSkuImportDto> excelUtil = new ExcelUtil<>(ProductMaterialSkuImportDto.class);
@@ -164,70 +160,81 @@
             log.error("瀵煎叆鐗╂枡瑙勬牸Excel瑙f瀽澶辫触", e);
             throw new ServiceException("Excel瑙f瀽澶辫触");
         }
-
         if (importList == null || importList.isEmpty()) {
-            throw new ServiceException("Excel娌℃湁鏁版嵁");
+            throw new ServiceException("Excel涓湭妫�娴嬪埌鏈夋晥鏁版嵁");
         }
+        List<Integer> emptyCodeRows = new ArrayList<>();
+        int currentRow = 1;
 
         Map<String, ProductMaterialSkuImportDto> specMap = new LinkedHashMap<>();
         for (ProductMaterialSkuImportDto dto : importList) {
-            if (dto == null || StringUtils.isEmpty(dto.getModel())) {
+            currentRow++;
+            if (dto == null) continue;
+            //  鐗╂枡缂栫爜涓嶈兘涓虹┖
+            if (StringUtils.isEmpty(dto.getMaterialCode())) {
+                emptyCodeRows.add(currentRow);
                 continue;
             }
-            String specification = dto.getModel().trim();
-            if (specification.isEmpty()) {
+            if (StringUtils.isEmpty(dto.getModel())) {
                 continue;
             }
-            specMap.putIfAbsent(specification, dto);
+
+            String modelKey = dto.getModel().trim();
+            specMap.putIfAbsent(modelKey, dto);
         }
 
+        if (!emptyCodeRows.isEmpty()) {
+            throw new ServiceException("瀵煎叆澶辫触,浠ヤ笅琛屽彿鐨勩�愮墿鏂欑紪鐮併�戜负绌猴細" + emptyCodeRows.toString());
+        }
         if (specMap.isEmpty()) {
-            throw new ServiceException("Excel娌℃湁鏈夋晥鐨勮鏍兼暟鎹�");
+            throw new ServiceException("Excel娌℃湁鏈夋晥鐨勮鏍煎瀷鍙锋暟鎹�");
         }
 
-        Set<String> specifications = specMap.keySet();
-
+        Set<String> modelSet = specMap.keySet();
         List<ProductMaterialSku> existList = this.list(new LambdaQueryWrapper<ProductMaterialSku>()
                 .eq(ProductMaterialSku::getProductId, materialId)
-                .in(ProductMaterialSku::getModel, specifications));
-        Map<String, ProductMaterialSku> existMap = existList.stream()
-                .collect(Collectors.toMap(ProductMaterialSku::getModel, sku -> sku, (a, b) -> a));
+                .in(ProductMaterialSku::getModel, modelSet));
 
+        Map<String, ProductMaterialSku> existMap = existList.stream().collect(Collectors.toMap(ProductMaterialSku::getModel, sku -> sku, (a, b) -> a));
         LocalDateTime now = LocalDateTime.now();
         List<ProductMaterialSku> saveList = new ArrayList<>();
         List<ProductMaterialSku> updateList = new ArrayList<>();
 
         for (Map.Entry<String, ProductMaterialSkuImportDto> entry : specMap.entrySet()) {
-            String specification = entry.getKey();
+            String model = entry.getKey();
             ProductMaterialSkuImportDto dto = entry.getValue();
-            String supplyType = StringUtils.isNotEmpty(dto.getSupplyType()) ? dto.getSupplyType().trim() : null;
-
-            ProductMaterialSku exist = existMap.get(specification);
-            if (exist == null) {
-                ProductMaterialSku sku = new ProductMaterialSku();
-                sku.setProductId(materialId);
-                sku.setModel(specification);
-                sku.setSupplyType(supplyType);
-                sku.setCreateTime(now);
-                sku.setUpdateTime(now);
-                saveList.add(sku);
+            String excelMaterialCode = dto.getMaterialCode().trim();
+            String excelSupplyType = StringUtils.isNotEmpty(dto.getSupplyType()) ? dto.getSupplyType().trim() : null;
+            ProductMaterialSku existSku = existMap.get(model);
+            if (existSku == null) {
+                ProductMaterialSku newSku = new ProductMaterialSku();
+                newSku.setProductId(materialId);
+                newSku.setModel(model);
+                newSku.setMaterialCode(excelMaterialCode);
+                newSku.setSupplyType(excelSupplyType);
+                newSku.setCreateTime(now);
+                newSku.setUpdateTime(now);
+                saveList.add(newSku);
             } else {
                 boolean needUpdate = false;
-                if (supplyType != null && !supplyType.equals(exist.getSupplyType())) {
-                    exist.setSupplyType(supplyType);
+                if (!Objects.equals(excelMaterialCode, existSku.getMaterialCode())) {
+                    existSku.setMaterialCode(excelMaterialCode);
+                    needUpdate = true;
+                }
+                if (!Objects.equals(excelSupplyType, existSku.getSupplyType())) {
+                    existSku.setSupplyType(excelSupplyType);
                     needUpdate = true;
                 }
                 if (needUpdate) {
-                    exist.setUpdateTime(now);
-                    updateList.add(exist);
+                    existSku.setUpdateTime(now);
+                    updateList.add(existSku);
                 }
             }
         }
 
         if (saveList.isEmpty() && updateList.isEmpty()) {
-            throw new ServiceException("Excel涓庣幇鏈夋暟鎹竴鑷达紝鏃犻渶瀵煎叆");
+            throw new ServiceException("Excel鍐呭涓庣幇鏈夋暟鎹畬鍏ㄤ竴鑷�");
         }
-
         if (!saveList.isEmpty()) {
             this.saveBatch(saveList);
         }
@@ -235,6 +242,6 @@
             this.updateBatchById(updateList);
         }
 
-        log.info("鐗╂枡瑙勬牸瀵煎叆瀹屾垚 materialId={}, 鏂板{}鏉★紝鏇存柊{}鏉�", materialId, saveList.size(), updateList.size());
+        log.info("鐗╂枡瑙勬牸瀵煎叆瀹屾垚! materialId={}, 鏂板{}鏉★紝鏇存柊{}鏉�", materialId, saveList.size(), updateList.size());
     }
 }

--
Gitblit v1.9.3