From d3a5c4420a7daf89bc9832f22ca8ed9d7e1adaf9 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期五, 27 三月 2026 17:34:32 +0800
Subject: [PATCH] yys 1.完善销售下单,生成生产订单
---
src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java | 254 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 249 insertions(+), 5 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..982b0c2 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,37 @@
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.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.pojo.Product;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.basic.service.IProductModelService;
+import com.ruoyi.basic.service.IProductService;
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.ProductProcessService;
+import com.ruoyi.production.service.ProductStructureService;
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>
@@ -31,13 +45,20 @@
public class ProductBomServiceImpl extends ServiceImpl<ProductBomMapper, ProductBom> implements ProductBomService {
@Autowired
+ private IProductService productService;
+
+ @Autowired
private ProductBomMapper productBomMapper;
+
@Autowired
private IProductModelService productModelService;
@Autowired
- private ProductStructureMapper productStructureMapper;
+ private ProductStructureService productStructureService;
+
+ @Autowired
+ private ProductProcessService productProcessService;
@Override
public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) {
@@ -49,7 +70,6 @@
public AjaxResult add(ProductBom productBom) {
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);
@@ -69,12 +89,236 @@
productStructure.setProductModelId(productBom.getProductModelId());
productStructure.setUnit(productModel.getUnit());
productStructure.setUnitQuantity(BigDecimal.valueOf(1));
- productStructure.setBomId(Long.valueOf(productBom.getId()));
+ productStructure.setBomId(productBom.getId());
- productStructureMapper.insert(productStructure);
+ productStructureService.save(productStructure);
return AjaxResult.success();
}
return AjaxResult.error();
}
+
+ /**
+ * 閫掑綊淇濆瓨 BOM锛氬厛淇濆瓨鐖堕」 鈫� 淇濆瓨瀛愰」 鈫� 寤虹珛鍏崇郴 鈫� 閫掑綊瀛愰」鐨勫瓙椤�
+ * @param children 褰撳墠鐖堕」鐨勫瓙椤瑰垪琛�
+ * @return 淇濆瓨鍚庣殑鐖堕」浜у搧ID
+ */
+ private void saveBomRecursive(List<BomImportDto> children,ProductBom bom,ProductModel rootModel,Map<String, Long> processMap) {
+ // 1. 鑾峰彇children涓瓙椤逛骇鍝佺紪鍙蜂负绌虹殑鏁版嵁
+ List<BomImportDto> parentChildren = children
+ .stream()
+ .filter(child -> StringUtils.isEmpty(child.getChildCode()))
+ .collect(Collectors.toList());
+ if(CollectionUtils.isEmpty(parentChildren)){
+ throw new ServiceException("璇烽�夋嫨鐖堕」浜у搧缂栧彿");
+ }
+ BomImportDto parentId = parentChildren.get(0); // 鐖剁骇鏁版嵁
+ ProductStructure rootNode = new ProductStructure();
+ rootNode.setBomId(bom.getId());
+ rootNode.setParentId(null); // 椤跺眰娌℃湁鐖惰妭鐐�
+ rootNode.setProductModelId(rootModel.getId());
+ rootNode.setUnitQuantity(BigDecimal.ONE);
+ rootNode.setUnit(rootModel.getUnit());
+ productStructureService.save(rootNode);
+
+ // 2. 閬嶅巻瀛愰」锛岄�愪釜澶勭悊
+ for (BomImportDto child : children) {
+ // 璺宠繃娌℃湁瀛愰」鐨勭埗椤�
+ if(StringUtils.isEmpty(child.getChildCode())){
+ continue;
+ }
+ // 鑾峰彇瀛愰」妯″瀷淇℃伅
+ ProductModel childModel = findModel(child.getChildName(), child.getChildSpec());
+
+ // 鎻掑叆缁撴瀯琛�
+ ProductStructure node = new ProductStructure();
+ node.setBomId(bom.getId());
+ node.setParentId(rootNode.getId()); // 鐖惰妭鐐笽D
+ node.setProductModelId(childModel.getId());
+ node.setUnitQuantity(child.getUnitQty());
+ node.setUnit(childModel.getUnit());
+ if (processMap.containsKey(child.getProcess())) {
+ node.setProcessId(processMap.get(child.getProcess()));
+ }
+ productStructureService.save(node);
+ }
+ }
+
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public AjaxResult uploadBom(MultipartFile file) {
+ 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()));
+ });
+ handleProcess(list);
+ Map<String, Long> processMap = productProcessService.list().stream()
+ .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1));
+ // 1. 鎸夌埗椤圭紪鐮佸垎缁�
+ Map<String, List<BomImportDto>> parentMap = list.stream()
+ .filter(bom -> StringUtils.isNotBlank(bom.getParentCode()))
+ .collect(Collectors.groupingBy(BomImportDto::getParentCode));
+ // 2. 閬嶅巻鎵�鏈夌埗椤癸紝閫掑綊淇濆瓨
+ for (Map.Entry<String, List<BomImportDto>> entry : parentMap.entrySet()) {
+ // 鍒涘缓 BOM 鏁版嵁
+ BomImportDto first = entry.getValue().get(0);
+ ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec());
+ ProductBom bom = new ProductBom();
+ bom.setProductModelId(rootModel.getId());
+ bom.setVersion("1.0");
+ productBomMapper.insert(bom);
+ bom.setBomNo("BM." + String.format("%05d", bom.getId()));
+ productBomMapper.updateById(bom);
+ // 澶勭悊bom瀛愯〃鏁版嵁
+ List<BomImportDto> children = entry.getValue();
+ saveBomRecursive(children,bom,rootModel,processMap);
+ }
+ return AjaxResult.success("BOM瀵煎叆鎴愬姛");
+ }
+
+
+ @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缁撴瀯瀵煎嚭");
+ }
+
+ 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);
+ }
+ }
+
+ private ProductModel findModel(String name, String spec) {
+ Product product = productService.getOne(new LambdaQueryWrapper<Product>()
+ .eq(Product::getProductName, name).last("limit 1"));
+ if (product == null) throw new ServiceException("浜у搧鏈淮鎶わ細" + name);
+
+ ProductModel model = productModelService.getOne(new LambdaQueryWrapper<ProductModel>()
+ .eq(ProductModel::getProductId, product.getId())
+ .eq(ProductModel::getModel, spec).last("limit 1"));
+ if (model == null) throw new ServiceException("瑙勬牸鏈淮鎶わ細" + name + "[" + spec + "]");
+ return model;
+ }
+
+ private void handleProcess(List<BomImportDto> list) {
+
+ Set<String> processNames = list.stream()
+ .map(BomImportDto::getProcess)
+ .filter(StringUtils::isNotBlank)
+ .collect(Collectors.toSet());
+
+ if (processNames.isEmpty()) {
+ return;
+ }
+
+ List<ProductProcess> exists = productProcessService.list(
+ new LambdaQueryWrapper<ProductProcess>().in(ProductProcess::getName, processNames)
+ );
+
+ Set<String> existNames = exists.stream()
+ .map(ProductProcess::getName)
+ .collect(Collectors.toSet());
+
+ List<ProductProcess> needSave = processNames.stream()
+ .filter(n -> !existNames.contains(n))
+ .map(n -> {
+ ProductProcess p = new ProductProcess();
+ p.setName(n);
+ return p;
+ })
+ .collect(Collectors.toList());
+
+ if (!needSave.isEmpty()) {
+ productProcessService.saveBatch(needSave);
+ needSave.forEach(p -> p.setNo("GX" + String.format("%08d", p.getId())));
+ productProcessService.updateBatchById(needSave);
+ }
+ }
+
+ private String clean(String s) {
+ if (s == null) return null;
+ return s.replaceAll("[\\u00A0\\u3000]", "").trim();
+ }
}
+
+
--
Gitblit v1.9.3