From 0b4221a457d7899809d3f733c2c64c977fb9553e Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期六, 14 三月 2026 09:46:52 +0800
Subject: [PATCH] 查询物料规格列表接口优化增加如果没有传参则查询所有产品的规格

---
 src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java |  158 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 135 insertions(+), 23 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 289b3ad..b3717ad 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
@@ -1,23 +1,32 @@
 package com.ruoyi.production.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.production.dto.ProductMaterialSkuDto;
 import com.ruoyi.production.mapper.ProductMaterialMapper;
 import com.ruoyi.production.mapper.ProductMaterialSkuMapper;
 import com.ruoyi.production.pojo.ProductMaterial;
 import com.ruoyi.production.pojo.ProductMaterialSku;
+import com.ruoyi.production.pojo.ProductMaterialSkuImportDto;
 import com.ruoyi.production.service.ProductMaterialSkuService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * <br>
@@ -41,37 +50,43 @@
      * 鏌ヨ鐗╂枡瑙勬牸鍒楄〃
      */
     @Override
-    public List<ProductMaterialSkuDto> productMaterialSkuList(Long materialId) {
-
-        if (materialId == null) {
-            return Collections.emptyList();
-        }
-
+    public Page<ProductMaterialSkuDto> productMaterialSkuList(Page<ProductMaterialSku> page, ProductMaterialSkuDto dto) {
         LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId)
-                .orderByAsc(ProductMaterialSku::getId);
-        List<ProductMaterialSku> skuList = this.list(queryWrapper);
-        if (skuList == null || skuList.isEmpty()) {
-            return Collections.emptyList();
+        if (dto != null && dto.getMaterialId() != null) {
+            queryWrapper.eq(ProductMaterialSku::getMaterialId, dto.getMaterialId())
+                    .like(StringUtils.isNotBlank(dto.getSpecification()),
+                            ProductMaterialSku::getSpecification, dto.getSpecification())
+                    .like(StringUtils.isNotBlank(dto.getMaterialCode()),
+                            ProductMaterialSku::getMaterialCode, dto.getMaterialCode())
+                    .orderByAsc(ProductMaterialSku::getId);
         }
-        // 鏌ヨ鐗╂枡淇℃伅
-        ProductMaterial material = productMaterialMapper.selectById(materialId);
+        Page<ProductMaterialSku> skuPage = this.page(page, queryWrapper);
+        List<ProductMaterialSku> skuList = skuPage.getRecords();
+        if (skuList == null || skuList.isEmpty()) {
+            return new Page<>();
+        }
 
+        ProductMaterial material = productMaterialMapper.selectById(dto.getMaterialId());
         String materialName = material != null ? material.getMaterialName() : null;
         String baseUnit = material != null ? material.getBaseUnit() : null;
         List<ProductMaterialSkuDto> result = new ArrayList<>(skuList.size());
         for (ProductMaterialSku sku : skuList) {
-            ProductMaterialSkuDto dto = new ProductMaterialSkuDto();
-            dto.setMaterialId(materialId);
-            dto.setMaterialName(materialName);
-            dto.setBaseUnit(baseUnit);
-            dto.setSkuId(sku.getId());
-            dto.setSpecification(sku.getSpecification());
-            dto.setSupplyType(sku.getSupplyType());
-            result.add(dto);
+            ProductMaterialSkuDto productMaterialSkuDto = new ProductMaterialSkuDto();
+            productMaterialSkuDto.setMaterialId(dto.getMaterialId());
+            productMaterialSkuDto.setMaterialName(materialName);
+            productMaterialSkuDto.setMaterialCode(sku.getMaterialCode());
+            productMaterialSkuDto.setBaseUnit(baseUnit);
+            productMaterialSkuDto.setSkuId(sku.getId());
+            productMaterialSkuDto.setSpecification(sku.getSpecification());
+            productMaterialSkuDto.setSupplyType(sku.getSupplyType());
+            result.add(productMaterialSkuDto);
         }
-
-        return result;
+        Page<ProductMaterialSkuDto> dtoPage = new Page<>();
+        dtoPage.setCurrent(skuPage.getCurrent());
+        dtoPage.setSize(skuPage.getSize());
+        dtoPage.setTotal(skuPage.getTotal());
+        dtoPage.setRecords(result);
+        return dtoPage;
     }
 
     /**
@@ -163,4 +178,101 @@
 
         return this.count(queryWrapper) > 0;
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void importProdData(MultipartFile file, Long materialId) {
+        if (materialId == null) {
+            throw new ServiceException("鐗╂枡ID涓嶈兘涓虹┖");
+        }
+        if (file == null || file.isEmpty()) {
+            throw new ServiceException("瀵煎叆鏂囦欢涓嶈兘涓虹┖");
+        }
+
+        ProductMaterial material = productMaterialMapper.selectById(materialId);
+        if (material == null) {
+            throw new ServiceException("鐗╂枡涓嶅瓨鍦�");
+        }
+
+        ExcelUtil<ProductMaterialSkuImportDto> excelUtil = new ExcelUtil<>(ProductMaterialSkuImportDto.class);
+        List<ProductMaterialSkuImportDto> importList;
+        try {
+            importList = excelUtil.importExcel(file.getInputStream());
+        } catch (Exception e) {
+            log.error("瀵煎叆鐗╂枡瑙勬牸Excel瑙f瀽澶辫触", e);
+            throw new ServiceException("Excel瑙f瀽澶辫触");
+        }
+
+        if (importList == null || importList.isEmpty()) {
+            throw new ServiceException("Excel娌℃湁鏁版嵁");
+        }
+
+        Map<String, ProductMaterialSkuImportDto> specMap = new LinkedHashMap<>();
+        for (ProductMaterialSkuImportDto dto : importList) {
+            if (dto == null || StringUtils.isEmpty(dto.getSpecification())) {
+                continue;
+            }
+            String specification = dto.getSpecification().trim();
+            if (specification.isEmpty()) {
+                continue;
+            }
+            specMap.putIfAbsent(specification, dto);
+        }
+
+        if (specMap.isEmpty()) {
+            throw new ServiceException("Excel娌℃湁鏈夋晥鐨勮鏍兼暟鎹�");
+        }
+
+        Set<String> specifications = specMap.keySet();
+
+        List<ProductMaterialSku> existList = this.list(new LambdaQueryWrapper<ProductMaterialSku>()
+                .eq(ProductMaterialSku::getMaterialId, materialId)
+                .in(ProductMaterialSku::getSpecification, specifications));
+        Map<String, ProductMaterialSku> existMap = existList.stream()
+                .collect(Collectors.toMap(ProductMaterialSku::getSpecification, 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();
+            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.setMaterialId(materialId);
+                sku.setSpecification(specification);
+                sku.setSupplyType(supplyType);
+                sku.setCreateTime(now);
+                sku.setUpdateTime(now);
+                saveList.add(sku);
+            } else {
+                boolean needUpdate = false;
+                if (supplyType != null && !supplyType.equals(exist.getSupplyType())) {
+                    exist.setSupplyType(supplyType);
+                    needUpdate = true;
+                }
+                if (needUpdate) {
+                    exist.setUpdateTime(now);
+                    updateList.add(exist);
+                }
+            }
+        }
+
+        if (saveList.isEmpty() && updateList.isEmpty()) {
+            throw new ServiceException("Excel涓庣幇鏈夋暟鎹竴鑷达紝鏃犻渶瀵煎叆");
+        }
+
+        if (!saveList.isEmpty()) {
+            this.saveBatch(saveList);
+        }
+        if (!updateList.isEmpty()) {
+            this.updateBatchById(updateList);
+        }
+
+        log.info("鐗╂枡瑙勬牸瀵煎叆瀹屾垚 materialId={}, 鏂板{}鏉★紝鏇存柊{}鏉�", materialId, saveList.size(), updateList.size());
+    }
 }

--
Gitblit v1.9.3