From 2ad2948d0e1c4e0d9d5000c3eb02605c3aceed94 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 09 四月 2026 18:04:00 +0800
Subject: [PATCH] fix: 产品被绑定不允许删除
---
src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java | 174 +++++++++++++++++++++++++++++++++++++++++----------------
1 files changed, 125 insertions(+), 49 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 49829f7..3ee1d34 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
@@ -11,8 +11,24 @@
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.dto.ProductMaterialSkuImportDto;
import com.ruoyi.production.service.ProductMaterialSkuService;
+import com.ruoyi.production.mapper.ProductOrderMapper;
+import com.ruoyi.production.mapper.ProductStructureMapper;
+import com.ruoyi.production.mapper.ProductionOrderRouteItemMapper;
+import com.ruoyi.production.mapper.ProductionOrderRouteMapper;
+import com.ruoyi.production.mapper.ProductionOrderStructureMapper;
+import com.ruoyi.production.mapper.ProductionProductInputMapper;
+import com.ruoyi.production.mapper.ProductionProductOutputMapper;
+import com.ruoyi.production.pojo.ProductOrder;
+import com.ruoyi.production.pojo.ProductStructure;
+import com.ruoyi.production.pojo.ProductionOrderRoute;
+import com.ruoyi.production.pojo.ProductionOrderRouteItem;
+import com.ruoyi.production.pojo.ProductionOrderStructure;
+import com.ruoyi.production.pojo.ProductionProductInput;
+import com.ruoyi.production.pojo.ProductionProductOutput;
+import com.ruoyi.productionPlan.mapper.ProductionPlanMapper;
+import com.ruoyi.productionPlan.pojo.ProductionPlan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -20,12 +36,7 @@
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.*;
import java.util.stream.Collectors;
/**
@@ -44,6 +55,30 @@
@Autowired
private ProductMaterialMapper productMaterialMapper;
+ @Autowired
+ private ProductionPlanMapper productionPlanMapper;
+
+ @Autowired
+ private ProductOrderMapper productOrderMapper;
+
+ @Autowired
+ private ProductStructureMapper productStructureMapper;
+
+ @Autowired
+ private ProductionOrderRouteItemMapper productionOrderRouteItemMapper;
+
+ @Autowired
+ private ProductionOrderRouteMapper productionOrderRouteMapper;
+
+ @Autowired
+ private ProductionOrderStructureMapper productionOrderStructureMapper;
+
+ @Autowired
+ private ProductionProductOutputMapper productionProductOutputMapper;
+
+ @Autowired
+ private ProductionProductInputMapper productionProductInputMapper;
+
/**
* 鏌ヨ鐗╂枡瑙勬牸鍒楄〃
*/
@@ -59,12 +94,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();
@@ -76,7 +111,7 @@
if (!this.save(sku)) {
throw new ServiceException("鏂板鐗╂枡瑙勬牸澶辫触");
}
- log.info("鏂板鐗╂枡瑙勬牸鎴愬姛 materialId={}, specification={}", sku.getMaterialId(), sku.getSpecification());
+ log.info("鏂板鐗╂枡瑙勬牸鎴愬姛 materialId={}, specification={}", sku.getProductId(), sku.getModel());
}
/**
@@ -86,7 +121,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());
@@ -104,6 +139,32 @@
if (ids == null || ids.isEmpty()) {
throw new ServiceException("璇烽�夋嫨鑷冲皯涓�鏉℃暟鎹�");
}
+
+ if (productionPlanMapper.selectCount(new LambdaQueryWrapper<ProductionPlan>().in(ProductionPlan::getProductMaterialSkuId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚攢鍞敓浜ц鍒掑紩鐢紝鏃犳硶鍒犻櫎");
+ }
+ if (productOrderMapper.selectCount(new LambdaQueryWrapper<ProductOrder>().in(ProductOrder::getProductMaterialSkuId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚敓浜ц鍗曞紩鐢紝鏃犳硶鍒犻櫎");
+ }
+ if (productStructureMapper.selectCount(new LambdaQueryWrapper<ProductStructure>().in(ProductStructure::getProductModelId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚獴OM瀛愯〃寮曠敤锛屾棤娉曞垹闄�");
+ }
+ if (productionOrderRouteItemMapper.selectCount(new LambdaQueryWrapper<ProductionOrderRouteItem>().in(ProductionOrderRouteItem::getProductModelId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚敓浜ц鍗曠粦瀹氱殑宸ヨ壓璺嚎--宸ュ簭琛ㄥ紩鐢紝鏃犳硶鍒犻櫎");
+ }
+ if (productionOrderRouteMapper.selectCount(new LambdaQueryWrapper<ProductionOrderRoute>().in(ProductionOrderRoute::getProductModelId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚敓浜ц鍗曠粦瀹氱殑宸ヨ壓璺嚎琛ㄥ紩鐢紝鏃犳硶鍒犻櫎");
+ }
+ if (productionOrderStructureMapper.selectCount(new LambdaQueryWrapper<ProductionOrderStructure>().in(ProductionOrderStructure::getProductModelId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚敓浜ц鍗曠粦瀹氱殑BOM瀛愯〃寮曠敤锛屾棤娉曞垹闄�");
+ }
+ if (productionProductOutputMapper.selectCount(new LambdaQueryWrapper<ProductionProductOutput>().in(ProductionProductOutput::getProductModelId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚敓浜ф姤宸ヤ骇鍑烘槑缁嗚〃寮曠敤锛屾棤娉曞垹闄�");
+ }
+ if (productionProductInputMapper.selectCount(new LambdaQueryWrapper<ProductionProductInput>().in(ProductionProductInput::getProductId, ids)) > 0) {
+ throw new ServiceException("璇ョ墿鏂欏凡琚敓浜ф姤宸ョ墿鏂欐姇鍏ヨ〃寮曠敤锛屾棤娉曞垹闄�");
+ }
+
if (!this.removeByIds(ids)) {
throw new ServiceException("鍒犻櫎鐗╂枡瑙勬牸澶辫触");
}
@@ -120,10 +181,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("瑙勬牸涓嶈兘涓虹┖");
}
}
@@ -133,8 +194,8 @@
*/
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);
}
@@ -154,7 +215,7 @@
ProductMaterial material = productMaterialMapper.selectById(materialId);
if (material == null) {
- throw new ServiceException("鐗╂枡涓嶅瓨鍦�");
+ throw new ServiceException("涓荤墿鏂欎俊鎭笉瀛樺湪");
}
ExcelUtil<ProductMaterialSkuImportDto> excelUtil = new ExcelUtil<>(ProductMaterialSkuImportDto.class);
@@ -165,70 +226,85 @@
log.error("瀵煎叆鐗╂枡瑙勬牸Excel瑙f瀽澶辫触", e);
throw new ServiceException("Excel瑙f瀽澶辫触");
}
-
if (importList == null || importList.isEmpty()) {
- throw new ServiceException("Excel娌℃湁鏁版嵁");
+ throw new ServiceException("Excel涓湭妫�娴嬪埌鏈夋晥鏁版嵁");
}
+ List<Integer> emptyCodeRows = new ArrayList<>();
+ int currentRow = 1;
Map<String, ProductMaterialSkuImportDto> specMap = new LinkedHashMap<>();
for (ProductMaterialSkuImportDto dto : importList) {
- if (dto == null || StringUtils.isEmpty(dto.getSpecification())) {
+ currentRow++;
+ if (dto == null) continue;
+ // 鐗╂枡缂栫爜涓嶈兘涓虹┖
+ if (StringUtils.isEmpty(dto.getMaterialCode())) {
+ emptyCodeRows.add(currentRow);
continue;
}
- String specification = dto.getSpecification().trim();
- if (specification.isEmpty()) {
- continue;
+ // 浜у搧瑙勬牸涓嶈兘涓虹┖
+ if (StringUtils.isEmpty(dto.getModel())) {
+ throw new ServiceException("瀵煎叆澶辫触,浜у搧瑙勬牸涓嶈兘涓虹┖");
}
- specMap.putIfAbsent(specification, dto);
+ if (StringUtils.isEmpty(dto.getSupplyType())) {
+ throw new ServiceException("瀵煎叆澶辫触,渚涘簲鏂瑰紡涓嶈兘涓虹┖");
+ }
+
+ String modelKey = dto.getModel().trim();
+ specMap.putIfAbsent(modelKey, dto);
}
+ if (!emptyCodeRows.isEmpty()) {
+ throw new ServiceException("瀵煎叆澶辫触,浠ヤ笅琛屽彿鐨勩�愮墿鏂欑紪鐮併�戜负绌猴細" + emptyCodeRows.toString());
+ }
if (specMap.isEmpty()) {
- throw new ServiceException("Excel娌℃湁鏈夋晥鐨勮鏍兼暟鎹�");
+ throw new ServiceException("Excel娌℃湁鏈夋晥鐨勮鏍煎瀷鍙锋暟鎹�");
}
- Set<String> specifications = specMap.keySet();
-
+ Set<String> modelSet = 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));
+ .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 specification = entry.getKey();
+ String model = 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);
+ 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 (supplyType != null && !supplyType.equals(exist.getSupplyType())) {
- exist.setSupplyType(supplyType);
+ if (!Objects.equals(excelMaterialCode, existSku.getMaterialCode())) {
+ existSku.setMaterialCode(excelMaterialCode);
+ needUpdate = true;
+ }
+ if (!Objects.equals(excelSupplyType, existSku.getSupplyType())) {
+ existSku.setSupplyType(excelSupplyType);
needUpdate = true;
}
if (needUpdate) {
- exist.setUpdateTime(now);
- updateList.add(exist);
+ existSku.setUpdateTime(now);
+ updateList.add(existSku);
}
}
}
if (saveList.isEmpty() && updateList.isEmpty()) {
- throw new ServiceException("Excel涓庣幇鏈夋暟鎹竴鑷达紝鏃犻渶瀵煎叆");
+ throw new ServiceException("Excel鍐呭涓庣幇鏈夋暟鎹畬鍏ㄤ竴鑷�");
}
-
if (!saveList.isEmpty()) {
this.saveBatch(saveList);
}
@@ -236,6 +312,6 @@
this.updateBatchById(updateList);
}
- log.info("鐗╂枡瑙勬牸瀵煎叆瀹屾垚 materialId={}, 鏂板{}鏉★紝鏇存柊{}鏉�", materialId, saveList.size(), updateList.size());
+ log.info("鐗╂枡瑙勬牸瀵煎叆瀹屾垚! materialId={}, 鏂板{}鏉★紝鏇存柊{}鏉�", materialId, saveList.size(), updateList.size());
}
}
--
Gitblit v1.9.3