From d5b6dcdeb9980b7b4cdafdc6324efc7b791c3357 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 20 三月 2026 09:33:22 +0800
Subject: [PATCH] fix:耗材库存报表查询字段优化
---
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java | 62 +++++++++++++++++++++++-------
1 files changed, 47 insertions(+), 15 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 16a45b1..b72cf61 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -3,6 +3,7 @@
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;
@@ -12,6 +13,7 @@
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;
@@ -19,13 +21,12 @@
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
+import org.springframework.dao.DuplicateKeyException;
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 java.util.*;
import java.util.stream.Collectors;
/**
@@ -44,16 +45,23 @@
@Override
public int addOrEditProductModel(ProductModelDto productModelDto) {
-
- if (productModelDto.getId() == null) {
- ProductModel productModel = new ProductModel();
- BeanUtils.copyProperties(productModelDto, productModel);
- return productModelMapper.insert(productModel);
- } else {
- return productModelMapper.updateById(productModelDto);
+ ProductModel productModel = new ProductModel();
+ BeanUtils.copyProperties(productModelDto, productModel);
+ try {
+ if (productModelDto.getId() == null) {
+ int result = productModelMapper.insert(productModel);
+ productModelDto.setId(productModel.getId());
+ return result;
+ } else {
+ int result = productModelMapper.updateById(productModel);
+ productModelDto.setId(productModel.getId());
+ return result;
+ }
+ } catch (DuplicateKeyException e) {
+ // 鎹曡幏鏁版嵁搴撳敮涓�绾︽潫寮傚父
+ throw new RuntimeException("璇ヤ骇鍝佸凡瀛樺湪鐩稿悓鐨勮鏍煎瀷鍙峰拰鍗曚綅", e);
}
}
-
@Override
public int delProductModel(Long[] ids) {
@@ -103,9 +111,16 @@
ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class);
List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
- if (productModelList == null || productModelList.isEmpty()) {
+ 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);
@@ -117,14 +132,31 @@
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());
}
- saveOrUpdateBatch(productModelList);
- return AjaxResult.success("鎴愬姛瀵煎叆 " + productModelList.size() + " 鏉℃暟鎹�");
+ 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);
- return AjaxResult.error("瀵煎叆澶辫触");
+ throw new ServiceException("瀵煎叆澶辫触");
}
}
}
--
Gitblit v1.9.3