From b572e82dcafea0fd893d908c7bb0e048483a1dd3 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期三, 01 四月 2026 13:38:54 +0800
Subject: [PATCH] fix: 生产计划下发时产品类型ID未保存
---
src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java | 171 ++++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 126 insertions(+), 45 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..35a2034 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,27 @@
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.dto.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.List;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* <br>
@@ -30,9 +34,7 @@
*/
@Slf4j
@Service
-public class ProductMaterialSkuServiceImpl
- extends ServiceImpl<ProductMaterialSkuMapper, ProductMaterialSku>
- implements ProductMaterialSkuService {
+public class ProductMaterialSkuServiceImpl extends ServiceImpl<ProductMaterialSkuMapper, ProductMaterialSku> implements ProductMaterialSkuService {
@Autowired
private ProductMaterialMapper productMaterialMapper;
@@ -41,37 +43,8 @@
* 鏌ヨ鐗╂枡瑙勬牸鍒楄〃
*/
@Override
- public List<ProductMaterialSkuDto> productMaterialSkuList(Long materialId) {
-
- if (materialId == null) {
- return Collections.emptyList();
- }
-
- 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();
- }
- // 鏌ヨ鐗╂枡淇℃伅
- ProductMaterial material = productMaterialMapper.selectById(materialId);
-
- 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);
- }
-
- return result;
+ public Page<ProductMaterialSkuDto> productMaterialSkuList(Page<ProductMaterialSkuDto> page, ProductMaterialSkuDto dto, Integer type) {
+ return baseMapper.selectSkuWithMaterialPage(page, dto, type);
}
/**
@@ -81,12 +54,12 @@
public void addProductMaterialSku(ProductMaterialSku sku) {
validateProductMaterialSku(sku, false);
// 鏍¢獙鐗╂枡鏄惁瀛樺湪
- ProductMaterial material = productMaterialMapper.selectById(sku.getMaterialId());
+ ProductMaterial material = productMaterialMapper.selectById(sku.getProductId());
if (material == null) {
throw new ServiceException("鐗╂枡涓嶅瓨鍦�");
}
// 鏍¢獙瑙勬牸鏄惁閲嶅
- if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), null)) {
+ if (existsSameSpecification(sku.getProductId(), sku.getModel(), null)) {
throw new ServiceException("璇ョ墿鏂欏凡瀛樺湪鐩稿悓瑙勬牸");
}
LocalDateTime now = LocalDateTime.now();
@@ -98,7 +71,7 @@
if (!this.save(sku)) {
throw new ServiceException("鏂板鐗╂枡瑙勬牸澶辫触");
}
- log.info("鏂板鐗╂枡瑙勬牸鎴愬姛 materialId={}, specification={}", sku.getMaterialId(), sku.getSpecification());
+ log.info("鏂板鐗╂枡瑙勬牸鎴愬姛 materialId={}, specification={}", sku.getProductId(), sku.getModel());
}
/**
@@ -108,7 +81,7 @@
public void updateProductMaterialSku(ProductMaterialSku sku) {
validateProductMaterialSku(sku, true);
// 鏍¢獙瑙勬牸鏄惁閲嶅
- if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), sku.getId())) {
+ if (existsSameSpecification(sku.getProductId(), sku.getModel(), sku.getId())) {
throw new ServiceException("璇ョ墿鏂欏凡瀛樺湪鐩稿悓瑙勬牸");
}
sku.setUpdateTime(LocalDateTime.now());
@@ -142,10 +115,10 @@
if (requireId && sku.getId() == null) {
throw new ServiceException("涓婚敭ID涓嶈兘涓虹┖");
}
- if (sku.getMaterialId() == null) {
+ if (sku.getProductId() == null) {
throw new ServiceException("鐗╂枡ID涓嶈兘涓虹┖");
}
- if (StringUtils.isEmpty(sku.getSpecification())) {
+ if (StringUtils.isEmpty(sku.getModel())) {
throw new ServiceException("瑙勬牸涓嶈兘涓虹┖");
}
}
@@ -155,12 +128,120 @@
*/
private boolean existsSameSpecification(Long materialId, String specification, Long excludeId) {
LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId)
- .eq(ProductMaterialSku::getSpecification, specification);
+ queryWrapper.eq(ProductMaterialSku::getProductId, materialId)
+ .eq(ProductMaterialSku::getModel, specification);
if (excludeId != null) {
queryWrapper.ne(ProductMaterialSku::getId, excludeId);
}
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涓湭妫�娴嬪埌鏈夋晥鏁版嵁");
+ }
+ List<Integer> emptyCodeRows = new ArrayList<>();
+ int currentRow = 1;
+
+ Map<String, ProductMaterialSkuImportDto> specMap = new LinkedHashMap<>();
+ for (ProductMaterialSkuImportDto dto : importList) {
+ currentRow++;
+ if (dto == null) continue;
+ // 鐗╂枡缂栫爜涓嶈兘涓虹┖
+ if (StringUtils.isEmpty(dto.getMaterialCode())) {
+ emptyCodeRows.add(currentRow);
+ continue;
+ }
+ if (StringUtils.isEmpty(dto.getModel())) {
+ continue;
+ }
+
+ String modelKey = dto.getModel().trim();
+ specMap.putIfAbsent(modelKey, dto);
+ }
+
+ if (!emptyCodeRows.isEmpty()) {
+ throw new ServiceException("瀵煎叆澶辫触,浠ヤ笅琛屽彿鐨勩�愮墿鏂欑紪鐮併�戜负绌猴細" + emptyCodeRows.toString());
+ }
+ if (specMap.isEmpty()) {
+ throw new ServiceException("Excel娌℃湁鏈夋晥鐨勮鏍煎瀷鍙锋暟鎹�");
+ }
+
+ Set<String> modelSet = specMap.keySet();
+ List<ProductMaterialSku> existList = this.list(new LambdaQueryWrapper<ProductMaterialSku>()
+ .eq(ProductMaterialSku::getProductId, materialId)
+ .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 model = entry.getKey();
+ ProductMaterialSkuImportDto dto = entry.getValue();
+ 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 (!Objects.equals(excelMaterialCode, existSku.getMaterialCode())) {
+ existSku.setMaterialCode(excelMaterialCode);
+ needUpdate = true;
+ }
+ if (!Objects.equals(excelSupplyType, existSku.getSupplyType())) {
+ existSku.setSupplyType(excelSupplyType);
+ needUpdate = true;
+ }
+ if (needUpdate) {
+ existSku.setUpdateTime(now);
+ updateList.add(existSku);
+ }
+ }
+ }
+
+ 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