From 0a1f000d5031c090059d5ae960019825196d4ba9 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 10 四月 2026 17:46:19 +0800
Subject: [PATCH] fix: BOM导入验证产品、工序是否存在
---
src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java | 360 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 333 insertions(+), 27 deletions(-)
diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
index 06d4d09..1315d5c 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -1,23 +1,34 @@
package com.ruoyi.production.service.impl;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.basic.pojo.ProductModel;
-import com.ruoyi.basic.service.IProductModelService;
+import com.ruoyi.production.pojo.ProductMaterial;
+import com.ruoyi.production.pojo.ProductMaterialSku;
import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
+import com.ruoyi.production.dto.BomImportDto;
import com.ruoyi.production.dto.ProductBomDto;
+import com.ruoyi.production.dto.ProductStructureDto;
import com.ruoyi.production.mapper.ProductBomMapper;
-import com.ruoyi.production.mapper.ProductStructureMapper;
import com.ruoyi.production.pojo.ProductBom;
+import com.ruoyi.production.pojo.ProductProcess;
import com.ruoyi.production.pojo.ProductStructure;
-import com.ruoyi.production.service.ProductBomService;
+import com.ruoyi.production.service.*;
+import com.ruoyi.project.system.domain.SysDictData;
+import com.ruoyi.project.system.mapper.SysDictDataMapper;
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 javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* <p>
@@ -34,47 +45,342 @@
private ProductBomMapper productBomMapper;
@Autowired
- private IProductModelService productModelService;
+ private ProductStructureService productStructureService;
@Autowired
- private ProductStructureMapper productStructureMapper;
+ private ProductProcessService productProcessService;
+
+ @Autowired
+ private ProductMaterialService productMaterialService;
+
+ @Autowired
+ private ProductMaterialSkuService productMaterialSkuService;
+
+ @Autowired
+ private SysDictDataMapper sysDictDataMapper;
@Override
- public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) {
+ public IPage<ProductBomDto> listPage(Page<ProductBom> page, ProductBomDto productBomDto) {
return productBomMapper.listPage(page, productBomDto);
}
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult add(ProductBom productBom) {
+ if (productBom == null || productBom.getDictCode() == null) {
+ throw new ServiceException("鏂板BOM澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
+ }
+
+ SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode());
+ if (sysDictData == null) {
+ throw new ServiceException("鏂板BOM澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
+ }
+
boolean save = productBomMapper.insert(productBom) > 0;
if (save) {
- // 鏍规嵁id鐢熸垚no瀛楁锛欸X + 8浣嶆暟瀛楋紙涓嶈冻8浣嶅墠闈㈣ˉ0锛�
String no = "BM." + String.format("%05d", productBom.getId());
productBom.setBomNo(no);
productBomMapper.updateById(productBom);
- // 鏌ヨ鍑轰骇鍝佹ā鍨嬩俊鎭�
- if (productBom.getProductModelId() == null) {
- throw new ServiceException("璇烽�夋嫨浜у搧妯″瀷");
- }
-
- ProductModel productModel = productModelService.getById(productBom.getProductModelId());
- if (productModel == null) {
- throw new ServiceException("閫夋嫨鐨勪骇鍝佹ā鍨嬩笉瀛樺湪");
- }
-
- // 娣诲姞鍒濆鐨勪骇鍝佺粨鏋�
- ProductStructure productStructure = new ProductStructure();
- productStructure.setProductModelId(productBom.getProductModelId());
- productStructure.setUnit(productModel.getUnit());
- productStructure.setUnitQuantity(BigDecimal.valueOf(1));
- productStructure.setBomId(Long.valueOf(productBom.getId()));
-
- productStructureMapper.insert(productStructure);
-
return AjaxResult.success();
}
return AjaxResult.error();
}
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public AjaxResult uploadBom(MultipartFile file, Long dictCode) {
+ if (dictCode == null) {
+ return AjaxResult.error("瀵煎叆澶辫触,浜у搧绫诲瀷涓嶈兘涓虹┖");
+ }
+ SysDictData sysDictData = sysDictDataMapper.selectDictDataById(dictCode);
+ if (sysDictData == null) {
+ return AjaxResult.error("瀵煎叆澶辫触,浜у搧绫诲瀷涓嶅瓨鍦�");
+ }
+ ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
+ List<BomImportDto> list;
+ try {
+ list = util.importExcel(file.getInputStream());
+ } catch (Exception e) {
+ return AjaxResult.error("Excel瑙f瀽澶辫触");
+ }
+
+ if (list == null || list.isEmpty()) {
+ return AjaxResult.error("鏁版嵁涓虹┖");
+ }
+
+ list.forEach(dto -> {
+ dto.setParentName(clean(dto.getParentName()));
+ dto.setParentSpec(clean(dto.getParentSpec()));
+ dto.setChildName(clean(dto.getChildName()));
+ dto.setChildSpec(clean(dto.getChildSpec()));
+ dto.setProcess(clean(dto.getProcess()));
+ });
+
+ Set<String> processNames = list.stream()
+ .map(BomImportDto::getProcess)
+ .filter(StringUtils::isNotBlank)
+ .collect(Collectors.toSet());
+
+ Map<String, Long> processMap = new HashMap<>();
+ if (!processNames.isEmpty()) {
+ List<ProductProcess> exists = productProcessService.list(
+ new LambdaQueryWrapper<ProductProcess>().in(ProductProcess::getName, processNames)
+ );
+ processMap = exists.stream().collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1));
+
+ Map<String, Long> finalProcessMap = processMap;
+ List<String> missingProcesses = processNames.stream()
+ .filter(n -> !finalProcessMap.containsKey(n))
+ .collect(Collectors.toList());
+
+ if (!missingProcesses.isEmpty()) {
+ throw new ServiceException("瀵煎叆澶辫触锛屼互涓嬪伐搴忎笉瀛樺湪锛�" + String.join(", ", missingProcesses));
+ }
+ }
+
+ Set<String> materialKeys = new HashSet<>();
+ for (BomImportDto dto : list) {
+ if (StringUtils.isNotBlank(dto.getParentName())) {
+ materialKeys.add(dto.getParentName() + "|" + (dto.getParentSpec() == null ? "" : dto.getParentSpec()));
+ }
+ if (StringUtils.isNotBlank(dto.getChildName())) {
+ materialKeys.add(dto.getChildName() + "|" + (dto.getChildSpec() == null ? "" : dto.getChildSpec()));
+ }
+ }
+
+ Map<String, ProductMaterialSku> skuMap = validateAndGetSkuMap(materialKeys);
+
+ ProductBom bom = new ProductBom();
+ bom.setDictCode(dictCode);
+ bom.setVersion("1.0");
+ productBomMapper.insert(bom);
+ bom.setBomNo("BM." + String.format("%05d", bom.getId()));
+ productBomMapper.updateById(bom);
+
+ // 璁板綍宸茬粡鎻掑叆缁撴瀯鐨勮妭鐐癸細Key = "鍚嶇О+瑙勬牸", Value = structure_id
+ Map<String, Long> treePathMap = new HashMap<>();
+
+ for (BomImportDto dto : list) {
+ String parentName = dto.getParentName();
+ String parentSpec = dto.getParentSpec();
+ String childName = dto.getChildName();
+ String childSpec = dto.getChildSpec();
+
+ String parentKey = parentName + "|" + (parentSpec == null ? "" : parentSpec);
+ String childKey = childName + "|" + (childSpec == null ? "" : childSpec);
+
+ // 澶勭悊椤剁骇鑺傜偣 (瀛愰」鍙婂叾瑙勬牸鍧囦负绌哄綋浣滈《绾ц妭鐐瑰畾涔�)
+ if (StringUtils.isBlank(childName) && StringUtils.isBlank(childSpec)) {
+ if (!treePathMap.containsKey(parentKey)) {
+ ProductMaterialSku model = skuMap.get(parentKey);
+ ProductStructure node = saveStructure(bom.getId(), null, model, dto.getUnitQty(), null);
+ treePathMap.put(parentKey, node.getId());
+ } else {
+ // 濡傛灉宸茬粡瀛樺湪锛屼粎鍦ㄥ畠鏄《绾ц妭鐐规椂鏇存柊
+ Long structureId = treePathMap.get(parentKey);
+ ProductStructure node = productStructureService.getById(structureId);
+ if (node != null && node.getParentId() == null && dto.getUnitQty() != null) {
+ node.setUnitQuantity(dto.getUnitQty());
+ productStructureService.updateById(node);
+ }
+ }
+ continue;
+ }
+
+ // 澶勭悊鐖跺瓙灞傜骇鍏崇郴
+ Long parentStructureId = treePathMap.get(parentKey);
+ if (parentStructureId == null) {
+ // 濡傛灉 Map 閲屾壘涓嶅埌鐖惰妭鐐癸紝瑙嗕负椤剁骇鐖惰妭鐐�
+ ProductMaterialSku parentModel = skuMap.get(parentKey);
+ ProductStructure parentNode = saveStructure(bom.getId(), null, parentModel, BigDecimal.ONE, null);
+ parentStructureId = parentNode.getId();
+ treePathMap.put(parentKey, parentStructureId);
+ }
+
+ // 鑾峰彇瀛愰」妯″瀷淇℃伅
+ ProductMaterialSku childModel = skuMap.get(childKey);
+ Long processId = null;
+ if (StringUtils.isNotBlank(dto.getProcess())) {
+ processId = processMap.get(dto.getProcess());
+ }
+ // 鎻掑叆瀛愯妭鐐圭粨鏋勮〃
+ ProductStructure childNode = saveStructure(bom.getId(), parentStructureId, childModel, dto.getUnitQty(), processId);
+ // 鎶婂綋鍓嶅瓙椤硅褰曞埌 Map,浣滀负浠ュ悗鏇存繁灞傜骇鐨勭埗椤规煡鎵句緷鎹�
+ treePathMap.put(childKey, childNode.getId());
+ }
+
+ return AjaxResult.success("BOM瀵煎叆鎴愬姛");
+ }
+
+ /**
+ * 淇濆瓨缁撴瀯鑺傜偣
+ */
+ private ProductStructure saveStructure(Integer bomId, Long parentId, ProductMaterialSku model, BigDecimal qty, Long processId) {
+ ProductStructure node = new ProductStructure();
+ node.setBomId(bomId);
+ node.setParentId(parentId);
+ node.setProductModelId(model.getId());
+ node.setUnitQuantity(qty != null ? qty : BigDecimal.ONE);
+ ProductMaterial material = productMaterialService.getById(model.getProductId());
+ node.setUnit(material != null ? material.getUnit() : null);
+ node.setProcessId(processId);
+ productStructureService.save(node);
+ return node;
+ }
+
+
+ @Override
+ public void exportBom(HttpServletResponse response, Integer bomId) {
+ if (bomId == null) {
+ return;
+ }
+
+ List<ProductStructureDto> treeData = productStructureService.listByBomId(bomId);
+ if (treeData == null || treeData.isEmpty()) {
+ return;
+ }
+
+ // 灏嗘爲褰㈢粨鏋勬墎骞冲寲 浣跨敤 BFS绠楁硶 瀵煎嚭,鎸夊眰绾ч『搴�
+ List<BomImportDto> exportList = new ArrayList<>();
+
+ // Map<ID, Node> idMap 鐢ㄤ簬鏌ユ壘鐖惰妭鐐�
+ Map<Long, ProductStructureDto> idMap = new HashMap<>();
+ populateMap(treeData, idMap);
+
+ // treeData 鐨勭涓�涓槸鏍硅妭鐐�
+ for (ProductStructureDto root : treeData) {
+ // 娣诲姞鏍硅妭鐐�
+ BomImportDto rootRow = new BomImportDto();
+ rootRow.setParentName(root.getProductName());
+ rootRow.setParentSpec(root.getModel());
+ rootRow.setUnitQty(root.getUnitQuantity());
+ rootRow.setRemark("");
+ exportList.add(rootRow);
+
+ // BFS 閬嶅巻-闃熷垪
+ Queue<ProductStructureDto> queue = new LinkedList<>();
+ if (root.getChildren() != null) {
+ queue.addAll(root.getChildren());
+ }
+
+ while (!queue.isEmpty()) {
+ ProductStructureDto child = queue.poll();
+
+ // 鏌ユ壘鐖惰妭鐐�
+ ProductStructureDto parent = idMap.get(child.getParentId());
+ if (parent == null) {
+ // 闄や簡鏈�澶栧眰鑺傜偣,鍏朵粬鑺傜偣鐨勭埗绫昏偗瀹氭槸涓嶄細涓虹┖鐨�
+ continue;
+ }
+
+ BomImportDto row = new BomImportDto();
+ // 鐖剁被淇℃伅
+ row.setParentName(parent.getProductName());
+ row.setParentSpec(parent.getModel());
+ // 瀛愮被淇℃伅
+ row.setChildName(child.getProductName());
+ row.setChildSpec(child.getModel());
+ row.setUnitQty(child.getUnitQuantity());
+ row.setProcess(child.getProcessName());
+
+ exportList.add(row);
+
+ // 灏嗗瓙鑺傜偣鐨勫瓙鑺傜偣鍔犲叆闃熷垪-涓嬩竴灞�
+ if (child.getChildren() != null && !child.getChildren().isEmpty()) {
+ queue.addAll(child.getChildren());
+ }
+ }
+ }
+
+ ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
+ util.exportExcel(response, exportList, "BOM缁撴瀯瀵煎嚭");
+ }
+
+ @Override
+ public String strengthById(Long bomId) {
+ if (bomId == null) {
+ return null;
+ }
+ return baseMapper.selectStrengthById(bomId);
+ }
+
+ private void populateMap(List<ProductStructureDto> nodes, Map<Long, ProductStructureDto> map) {
+ if (nodes == null || nodes.isEmpty()) {
+ return;
+ }
+ for (ProductStructureDto node : nodes) {
+ map.put(node.getId(), node);
+ populateMap(node.getChildren(), map);
+ }
+ }
+
+
+ /**
+ * 鎵归噺楠岃瘉骞惰幏鍙栦骇鍝丼KU淇℃伅
+ */
+ private Map<String, ProductMaterialSku> validateAndGetSkuMap(Set<String> materialKeys) {
+ if (materialKeys == null || materialKeys.isEmpty()) {
+ return Collections.emptyMap();
+ }
+
+ // 1. 鑾峰彇鎵�鏈変骇鍝佸悕绉板苟鏌ヨ
+ Set<String> productNames = materialKeys.stream()
+ .map(k -> k.split("\\|")[0])
+ .collect(Collectors.toSet());
+
+ List<ProductMaterial> products = productMaterialService.list(
+ new LambdaQueryWrapper<ProductMaterial>().in(ProductMaterial::getProductName, productNames)
+ );
+ Map<String, ProductMaterial> productMap = products.stream()
+ .collect(Collectors.toMap(ProductMaterial::getProductName, p -> p, (k1, k2) -> k1));
+
+ // 妫�鏌ョ己澶辩殑浜у搧
+ List<String> missingProducts = productNames.stream()
+ .filter(n -> !productMap.containsKey(n))
+ .collect(Collectors.toList());
+ if (!missingProducts.isEmpty()) {
+ throw new ServiceException("瀵煎叆澶辫触锛屼互涓嬩骇鍝佹湭缁存姢锛�" + String.join(", ", missingProducts));
+ }
+
+ // 2. 鏌ヨ鎵�鏈夌浉鍏崇殑 SKU
+ // 鏋勯�犳煡璇細(productId = ? AND model = ?) OR (productId = ? AND model = ?) ...
+ // 鐢变簬 MyBatis Plus 鐨� Or 宓屽鍙兘杈冩參涓旀澶勬暟閲忛�氬父鍙帶锛屾垜浠彲浠ュ厛鏌ュ嚭鎵�鏈夎繖浜� Product 鐨勬墍鏈� SKU
+ List<Long> productIds = products.stream().map(ProductMaterial::getId).collect(Collectors.toList());
+ List<ProductMaterialSku> skus = productMaterialSkuService.list(
+ new LambdaQueryWrapper<ProductMaterialSku>().in(ProductMaterialSku::getProductId, productIds)
+ );
+
+ // 鏋勫缓 Map: "浜у搧鍚嶇О|瑙勬牸" -> Sku
+ Map<String, ProductMaterialSku> skuMap = new HashMap<>();
+ for (ProductMaterialSku sku : skus) {
+ ProductMaterial p = productMap.values().stream()
+ .filter(pm -> pm.getId().equals(sku.getProductId()))
+ .findFirst().orElse(null);
+ if (p != null) {
+ String key = p.getProductName() + "|" + (sku.getModel() == null ? "" : sku.getModel());
+ skuMap.put(key, sku);
+ }
+ }
+
+ // 妫�鏌ョ己澶辩殑瑙勬牸
+ List<String> missingSkus = materialKeys.stream()
+ .filter(k -> !skuMap.containsKey(k))
+ .map(k -> {
+ String[] parts = k.split("\\|");
+ return parts[0] + "[" + (parts.length > 1 ? parts[1] : "") + "]";
+ })
+ .collect(Collectors.toList());
+
+ if (!missingSkus.isEmpty()) {
+ throw new ServiceException("瀵煎叆澶辫触锛屼互涓嬩骇鍝佽鏍兼湭缁存姢锛�" + String.join(", ", missingSkus));
+ }
+
+ return skuMap;
+ }
+
+ private String clean(String s) {
+ if (s == null) return null;
+ return s.replaceAll("[\\u00A0\\u3000]", "").trim();
+ }
}
--
Gitblit v1.9.3