From 5b2c9ae479a1b7a0c4e3b15910b090f8b20cf4fe Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 08 四月 2026 17:35:30 +0800
Subject: [PATCH] yys 1.产品表,规格表变成一对一
---
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java | 179 ++++++++++++++++++++++++++++++-----------------------------
1 files changed, 92 insertions(+), 87 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 51437ec..dd16598 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -11,6 +11,8 @@
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.basic.service.IProductModelService;
+import com.ruoyi.common.enums.FileNameType;
+import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
@@ -18,23 +20,24 @@
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.other.mapper.TempFileMapper;
import com.ruoyi.other.service.impl.TempFileServiceImpl;
+import com.ruoyi.production.mapper.ProductProcessMapper;
+import com.ruoyi.production.pojo.ProductProcess;
import com.ruoyi.sales.mapper.CommonFileMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
-import java.util.AbstractMap;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -51,6 +54,7 @@
private final SalesLedgerProductMapper salesLedgerProductMapper;
private ProductModelMapper productModelMapper;
private final CommonFileServiceImpl commonFileService;
+ private final ProductProcessMapper productProcessMapper;
private final TempFileServiceImpl tempFileService;
@@ -59,29 +63,27 @@
if(StringUtils.isEmpty(productModelDto.getProductName())){
throw new RuntimeException("浜у搧鍚嶇О涓嶈兘涓虹┖");
}
- Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
-// .eq(Product::getModel, productModelDto.getModel())
- .eq(Product::getProductName, productModelDto.getProductName()));
+ // 浜у搧琛� - 瑙勬牸琛ㄥ彉鎴愪竴瀵逛竴
+
if (productModelDto.getId() == null) {
- if(product == null){
- product = new Product();
- product.setProductName(productModelDto.getProductName());
- productMapper.insert(product);
- }
+ Product product1 = new Product();
+ product1.setProductName(productModelDto.getProductName());
+ productMapper.insert(product1);
ProductModel productModel = new ProductModel();
BeanUtils.copyProperties(productModelDto,productModel);
- productModel.setProductId(product.getId());
+ productModel.setProductId(product1.getId());
productModelMapper.insert(productModel);
if(CollectionUtils.isNotEmpty(productModelDto.getTempFileIds())){
commonFileService.migrateTempFilesToFormal(productModel.getId(), productModelDto.getTempFileIds());
}
return 1;
} else {
- Product product1 = productMapper.selectById(productModelDto.getProductId());
- if(product1 != null){
- product1.setProductName(productModelDto.getProductName());
- productMapper.updateById(product1);
+ Product product = productMapper.selectById(productModelDto.getProductId());
+ if(product != null){
+ product.setProductName(productModelDto.getProductName());
+ productMapper.updateById(product);
}
+ commonFileService.deleteByBusinessIds(Collections.singletonList(productModelDto.getId()), FileNameType.PRODUCT_MODEL.getValue());
if(CollectionUtils.isNotEmpty(productModelDto.getTempFileIds())){
commonFileService.migrateTempFilesToFormal(productModelDto.getId(), productModelDto.getTempFileIds());
}
@@ -127,93 +129,96 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public Boolean importProduct(MultipartFile file) {
+ public void importProduct(MultipartFile file, HttpServletResponse response) {
+ ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class);
+ List<ProductModelExcelCopyDto> productModelList = null;
try {
- ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class);
- List<ProductModelExcelCopyDto> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
- if(CollectionUtils.isNotEmpty(productModelList)){
- // 2. 鎸変骇鍝佸悕绉�,鍥剧焊缂栧彿鍒嗙粍
- Map<Map.Entry<String, String>, List<ProductModelExcelCopyDto>> groupedByProductNameAndDrawingNumber =
- productModelList.stream()
- .collect(Collectors.groupingBy(
- dto -> new AbstractMap.SimpleEntry<>(
- dto.getProductName(),
- dto.getModel()
- )
- ));
- // 2. 閬嶅巻鍒嗙粍缁撴灉澶勭悊鏁版嵁
- for (Map.Entry<Map.Entry<String, String>, List<ProductModelExcelCopyDto>> entry : groupedByProductNameAndDrawingNumber.entrySet()) {
- Map.Entry<String, String> groupKey = entry.getKey();
- String productName = groupKey.getKey(); // 浜у搧鍚嶇О
- String drawingNumber = groupKey.getValue(); // 鍥剧焊缂栧彿
- List<ProductModelExcelCopyDto> dtoList = entry.getValue();
-
- // 绌哄垪琛ㄨ烦杩囷紝閬垮厤鍚庣画NPE
- if (CollectionUtils.isEmpty(dtoList)) {
- continue;
- }
- ProductModelExcelCopyDto firstDto = dtoList.get(0);
- String model = firstDto.getModel();
-
- // 3. 鏌ヨ/鏂板浜у搧锛堟寜浜у搧鍚嶇О+鍥剧焊缂栧彿锛屾洿绮惧噯锛�
- Product product = getOrCreateProduct(productName, drawingNumber);
-
- // 4. 鎵归噺澶勭悊浜у搧鍨嬪彿锛堟寜鍥剧焊缂栧彿+鍨嬪彿锛�
- processProductModel(dtoList, product.getId(), model, drawingNumber);
- }
- }
- return true;
+ productModelList = productModelExcelUtil.importExcel(file.getInputStream());
}catch (Exception e) {
- e.printStackTrace();
+ throw new ServiceException("鏂囦欢瑙f瀽澶辫触");
}
- return false;
+ if(CollectionUtils.isNotEmpty(productModelList)){
+ // 2. 鎸変骇鍝佸悕绉�,鍥剧焊缂栧彿鍒嗙粍
+// Map<Map.Entry<String, String>, List<ProductModelExcelCopyDto>> groupedByProductNameAndDrawingNumber =
+// productModelList.stream()
+// .collect(Collectors.groupingBy(
+// dto -> new AbstractMap.SimpleEntry<>(
+// dto.getProductName(),
+// dto.getModel()
+// )
+// ));
+ List<ProductModelExcelCopyErrorDto> errorList = new ArrayList<>();
+ // 2. 閬嶅巻鍒嗙粍缁撴灉澶勭悊鏁版嵁
+ for (ProductModelExcelCopyDto entry : productModelList) {
+ String productName = entry.getProductName(); // 浜у搧鍚嶇О
+ String drawingNumber = entry.getModel(); // 鍥剧焊缂栧彿
+ String model = entry.getModel();
+ // 3. 鏌ヨ/鏂板浜у搧锛堟寜浜у搧鍚嶇О+鍥剧焊缂栧彿锛屾洿绮惧噯锛�
+ Product product = getOrCreateProduct(productName, drawingNumber);
+
+ // 4. 鎵归噺澶勭悊浜у搧鍨嬪彿锛堟寜鍥剧焊缂栧彿+鍨嬪彿锛�
+ processProductModel(entry, product.getId(), model, drawingNumber,errorList);
+ }
+ if(CollectionUtils.isNotEmpty(errorList)){
+ // 5. 鎵归噺澶勭悊閿欒鏁版嵁
+ ExcelUtil<ProductModelExcelCopyErrorDto> errorExcelUtil = new ExcelUtil<>(ProductModelExcelCopyErrorDto.class);
+ errorExcelUtil.exportExcel(response,errorList, "閿欒鏁版嵁");
+ }
+ }
+
}
/**
* 鎶藉彇閫氱敤鏂规硶锛氭煡璇㈡垨鏂板浜у搧
*/
private Product getOrCreateProduct(String productName, String model) {
- // 绮惧噯鏌ヨ锛氫骇鍝佸悕绉�+鍨嬪彿锛堥伩鍏嶅悓鍚嶄笉鍚屽瀷鍙风殑闂锛�
- Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
- .eq(Product::getProductName, productName)
-// .eq(Product::getModel, model)
- .last("limit 1"));
-
- if (product == null) {
- product = new Product();
- product.setProductName(productName);
+ Product product = product = new Product();
+ product.setProductName(productName);
// product.setModel(model); // 琛ュ厖鍨嬪彿锛屽瓧娈垫洿瀹屾暣
- product.setParentId(null);
- productMapper.insert(product);
- }
+ product.setParentId(null);
+ productMapper.insert(product);
return product;
}
/**
* 鎶藉彇閫氱敤鏂规硶锛氬鐞嗕骇鍝佸瀷鍙凤紙鏂板/鏇存柊锛�
*/
- private void processProductModel(List<ProductModelExcelCopyDto> dtoList,
- Long productId, String model, String drawingNumber) {
- for (ProductModelExcelCopyDto dto : dtoList) {
- // 鏌ヨ鏉′欢锛氬瀷鍙�+鍥剧焊缂栧彿锛堟洿绮惧噯锛岀鍚堝垎缁勯�昏緫锛�
- ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>()
- .eq(ProductModel::getModel, model)
- .last("limit 1"));
-
- if (productModel == null) {
- productModel = new ProductModel();
- BeanUtils.copyProperties(dto, productModel);
- // 鍏滃簳榛樿鍊硷紝閬垮厤绌哄��
- if (productModel.getProductType() == null) {
- productModel.setProductType(1);
- }
- productModel.setProductId(productId);
- productModelMapper.insert(productModel);
- } else {
- BeanUtils.copyProperties(dto, productModel);
- productModel.setProductId(productId);
- productModelMapper.updateById(productModel);
+ private void processProductModel(ProductModelExcelCopyDto dto,
+ Long productId,
+ String model,
+ String drawingNumber,
+ List<ProductModelExcelCopyErrorDto> errorList) {
+ // 鏌ヨ鎵�鏈夊伐鑹鸿矾绾�
+ List<ProductProcess> productRoutes = productProcessMapper.selectList(new QueryWrapper<ProductProcess>());
+ if(CollectionUtils.isEmpty(productRoutes)){
+ productRoutes = new ArrayList<>();
+ }
+ // 鏌ヨ鏉′欢锛氬瀷鍙�+鍥剧焊缂栧彿锛堟洿绮惧噯锛岀鍚堝垎缁勯�昏緫锛�
+ ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>()
+ .eq(ProductModel::getModel, model)
+ .last("limit 1"));
+ // 閫氳繃宸ヨ壓璺嚎鍚嶇О鍖归厤鏈�鏂颁竴鏉″伐鑹鸿矾绾�
+ ProductProcess productRoute = productRoutes.stream()
+ .filter(route -> route.getName().equals(dto.getProcessRoute()))
+ .max(Comparator.comparing(ProductProcess::getCreateTime))
+ .orElse(null);
+ if (productModel == null) {
+ productModel = new ProductModel();
+ BeanUtils.copyProperties(dto, productModel);
+ if (productRoute != null) {
+ productModel.setRouteId(productRoute.getId());
}
+ // 鍏滃簳榛樿鍊硷紝閬垮厤绌哄��
+ if (productModel.getProductType() == null) {
+ productModel.setProductType(1);
+ }
+ productModel.setProductId(productId);
+ productModelMapper.insert(productModel);
+ } else {
+ productModel.setRouteId(productRoute != null ? productRoute.getId() : null);
+ BeanUtils.copyProperties(dto, productModel);
+ productModel.setProductId(productId);
+ productModelMapper.updateById(productModel);
}
}
--
Gitblit v1.9.3