From 1e123568c2f561013f5636e45dc0db30b17a3517 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 10 九月 2026 16:39:26 +0800
Subject: [PATCH] feat(basic): 添加产品条码功能并完善生产统计报表
---
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java | 76 +++++++++++++++++++++++++++++++++-----
1 files changed, 66 insertions(+), 10 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 263a52a..50d8f4a 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -17,7 +17,7 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
-import com.ruoyi.framework.web.domain.R;
+import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.sales.dto.LossProductModelDto;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
@@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@@ -53,9 +54,12 @@
public int addOrEditProductModel(ProductModelDto productModelDto) {
String model = StringUtils.trim(productModelDto.getModel());
String productCode = StringUtils.trim(productModelDto.getProductCode());
+ String barcode = StringUtils.trim(productModelDto.getBarcode());
productModelDto.setModel(model);
productModelDto.setProductCode(productCode);
+ productModelDto.setBarcode(barcode);
checkModelAndProductCodeUnique(model, productCode, productModelDto.getId());
+ checkBarcodeUnique(barcode, productModelDto.getId());
if (productModelDto.getId() == null) {
ProductModel productModel = new ProductModel();
@@ -78,6 +82,19 @@
ProductModel duplicateProductModel = productModelMapper.selectOne(queryWrapper);
if (duplicateProductModel != null) {
throw new ServiceException("瀵瑰簲鐨勫瀷鍙�" + model + "鐨勪骇鍝佺紪鐮�" + productCode + "宸茬粡瀛樺湪");
+ }
+ }
+
+ private void checkBarcodeUnique(String barcode, Long currentId) {
+ if (StringUtils.isEmpty(barcode)) {
+ return;
+ }
+ LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
+ queryWrapper.eq(ProductModel::getBarcode, barcode)
+ .ne(currentId != null, ProductModel::getId, currentId)
+ .last("limit 1");
+ if (productModelMapper.selectOne(queryWrapper) != null) {
+ throw new ServiceException("鏉″舰鐮�" + barcode + "宸茬粡瀛樺湪");
}
}
@@ -124,14 +141,14 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public R<?> importProductModel(MultipartFile file, Integer productId) {
+ public AjaxResult importProductModel(MultipartFile file, Integer productId) {
if (productId == null) {
- return R.fail("璇峰厛閫夋嫨浜у搧鍐嶅鍏ヨ鏍煎瀷鍙�");
+ return AjaxResult.error("璇峰厛閫夋嫨浜у搧鍐嶅鍏ヨ鏍煎瀷鍙�");
}
Product product = productMapper.selectById(productId);
if (product == null) {
- return R.fail("閫夋嫨鐨勪骇鍝佷笉瀛樺湪");
+ return AjaxResult.error("閫夋嫨鐨勪骇鍝佷笉瀛樺湪");
}
try {
@@ -139,12 +156,23 @@
List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
if (CollectionUtils.isEmpty(productModelList)) {
- return R.fail("瀵煎叆鏁版嵁涓嶈兘涓虹┖");
+ 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<String> importBarcodes = productModelList.stream()
+ .map(item -> StringUtils.trim(item.getBarcode()))
+ .filter(StringUtils::isNotEmpty)
+ .distinct()
+ .collect(Collectors.toList());
+ Set<String> dbBarcodeSet = importBarcodes.isEmpty() ? new HashSet<>()
+ : list(new LambdaQueryWrapper<ProductModel>().in(ProductModel::getBarcode, importBarcodes))
+ .stream().map(ProductModel::getBarcode).collect(Collectors.toSet());
+ Set<String> fileBarcodes = new HashSet<>();
List<ProductModel> waitToSaveList = new ArrayList<>();
int skipCount = 0;
@@ -154,13 +182,23 @@
int rowNum = i + 2;
if (StringUtils.isEmpty(item.getProductCode())) {
- return R.fail("绗� " + rowNum + " 琛屽鍏ュけ璐�: [浜у搧缂栫爜] 涓嶈兘涓虹┖");
+ return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [浜у搧缂栫爜] 涓嶈兘涓虹┖");
}
if (StringUtils.isEmpty(item.getModel())) {
- return R.fail("绗� " + rowNum + " 琛屽鍏ュけ璐�: [瑙勬牸鍨嬪彿] 涓嶈兘涓虹┖");
+ return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [瑙勬牸鍨嬪彿] 涓嶈兘涓虹┖");
}
if (StringUtils.isEmpty(item.getUnit())) {
- return R.fail("绗� " + rowNum + " 琛屽鍏ュけ璐�: [鍗曚綅] 涓嶈兘涓虹┖");
+ return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [鍗曚綅] 涓嶈兘涓虹┖");
+ }
+
+ item.setBarcode(StringUtils.trim(item.getBarcode()));
+ if (StringUtils.isNotEmpty(item.getBarcode())) {
+ if (!fileBarcodes.add(item.getBarcode())) {
+ return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: 鏉″舰鐮� " + item.getBarcode() + " 鍦ㄦ枃浠朵腑閲嶅");
+ }
+ if (dbBarcodeSet.contains(item.getBarcode())) {
+ return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: 鏉″舰鐮� " + item.getBarcode() + " 宸插瓨鍦�");
+ }
}
// 鍘婚噸,濡傛灉宸插寘鍚鍨嬪彿,鍒欒烦杩�
@@ -180,13 +218,31 @@
}
if (skipCount == 0) {
- return R.ok(null, String.format("鎴愬姛瀵煎叆 %d 鏉℃暟鎹�", waitToSaveList.size()));
+ return AjaxResult.success(String.format("鎴愬姛瀵煎叆 %d 鏉℃暟鎹�", waitToSaveList.size()));
} else {
- return R.ok(null, String.format("鎴愬姛瀵煎叆 %d 鏉★紝璺宠繃宸插瓨鍦ㄦ暟鎹� %d 鏉�", waitToSaveList.size(), skipCount));
+ return AjaxResult.success(String.format("鎴愬姛瀵煎叆 %d 鏉★紝璺宠繃宸插瓨鍦ㄦ暟鎹� %d 鏉�", waitToSaveList.size(), skipCount));
}
} catch (Exception e) {
log.error("瀵煎叆浜у搧瑙勬牸寮傚父", e);
throw new ServiceException("瀵煎叆澶辫触");
}
}
+
+ @Override
+ public ProductModel selectByBarcode(String barcode) {
+ if (StringUtils.isEmpty(barcode)) {
+ return null;
+ }
+ LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
+ queryWrapper.eq(ProductModel::getBarcode, StringUtils.trim(barcode))
+ .last("limit 1");
+ ProductModel productModel = productModelMapper.selectOne(queryWrapper);
+ if (productModel != null && productModel.getProductId() != null) {
+ Product product = productMapper.selectById(productModel.getProductId());
+ if (product != null) {
+ productModel.setProductName(product.getProductName());
+ }
+ }
+ return productModel;
+ }
}
--
Gitblit v1.9.3