From ad9694f7c53d81dec76292b8d329e4dc06e90bc6 Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期四, 23 四月 2026 14:45:22 +0800
Subject: [PATCH] feat(production): 新增BOM复制功能

---
 src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java |  111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 111 insertions(+), 0 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 b7b67bb..b91ba31 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -2,6 +2,7 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.pojo.Product;
@@ -20,6 +21,7 @@
 import com.ruoyi.production.service.ProductBomService;
 import com.ruoyi.production.service.ProductProcessService;
 import com.ruoyi.production.service.ProductStructureService;
+import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -188,6 +190,115 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
+    public AjaxResult copy(ProductBomDto productBom) {
+        Long copyId = productBom.getCopyId();
+        if (copyId == null) {
+            throw new ServiceException("澶嶅埗婧怋OM ID涓嶈兘涓虹┖");
+        }
+        ProductBom sourceBom = productBomMapper.selectById(copyId);
+        if (sourceBom == null) {
+            throw new ServiceException("澶嶅埗婧怋OM涓嶅瓨鍦�");
+        }
+
+
+        ProductBom newBom = getProductBom(productBom, sourceBom);
+        productBomMapper.insert(newBom);
+        newBom.setBomNo("BM." + String.format("%05d", newBom.getId()));
+        productBomMapper.updateById(newBom);
+
+
+
+        ProductModel productModel = productModelService.getById(newBom.getProductModelId());
+        if (productModel == null) {
+            throw new ServiceException("閫夋嫨鐨勪骇鍝佹ā鍨嬩笉瀛樺湪");
+        }
+
+        ProductStructure newRoot = getProductStructure(newBom, productModel);
+        productStructureService.save(newRoot);
+        List<ProductStructure> sourceStructures = productStructureMapper.selectList(
+                        Wrappers.<ProductStructure>lambdaQuery()
+                                .eq(ProductStructure::getBomId, copyId.intValue()));
+        if (sourceStructures == null || sourceStructures.isEmpty()) {
+            return AjaxResult.success();
+        }
+
+        ProductStructure oldRoot = sourceStructures.stream()
+                .filter(s -> s.getParentId() == null)
+                .findFirst().orElse(new ProductStructure());
+
+        List<ProductStructure> children = sourceStructures
+                .stream()
+                .filter(s -> !s.getId().equals(oldRoot.getId()))
+                .collect(Collectors.toList());
+
+        Map<Long, Long> oldNewIdMap = new HashMap<>();
+
+        oldNewIdMap.put(oldRoot.getId(), newRoot.getId());
+
+        List<ProductStructure> insertList = children
+                .stream()
+                .map(item -> getProductStructures(item, newBom))
+                .collect(Collectors.toList());
+
+        productStructureService.saveBatch(insertList);
+
+        for (int i = 0; i < children.size(); i++) {
+            oldNewIdMap.put(
+                    children.get(i).getId(),
+                    insertList.get(i).getId()
+            );
+        }
+
+        List<ProductStructure> updateList = new ArrayList<>();
+        for (int i = 0; i < children.size(); i++) {
+            ProductStructure source = children.get(i);
+            ProductStructure inserted = insertList.get(i);
+            Long newParentId = oldNewIdMap.get(source.getParentId());
+            if (newParentId != null) {
+                inserted.setParentId(newParentId);
+                updateList.add(inserted);
+            }
+        }
+        if (!updateList.isEmpty()) {
+            productStructureService.updateBatchById(updateList);
+        }
+
+        return AjaxResult.success();
+    }
+
+    @NotNull
+    private static ProductStructure getProductStructures(ProductStructure item, ProductBom newBom) {
+        ProductStructure copy = new ProductStructure();
+        copy.setProductModelId(item.getProductModelId());
+        copy.setProcessId(item.getProcessId());
+        copy.setUnitQuantity(item.getUnitQuantity());
+        copy.setDemandedQuantity(item.getDemandedQuantity());
+        copy.setUnit(item.getUnit());
+        copy.setBomId(newBom.getId());
+        return copy;
+    }
+
+    @NotNull
+    private static ProductStructure getProductStructure(ProductBom newBom, ProductModel productModel) {
+        ProductStructure newRoot = new ProductStructure();
+        newRoot.setProductModelId(newBom.getProductModelId());
+        newRoot.setUnitQuantity(BigDecimal.valueOf(1));
+        newRoot.setUnit(productModel.getUnit());
+        newRoot.setBomId(newBom.getId());
+        return newRoot;
+    }
+
+    @NotNull
+    private static ProductBom getProductBom(ProductBomDto productBom, ProductBom sourceBom) {
+        ProductBom newBom = new ProductBom();
+        newBom.setProductModelId(productBom.getProductModelId() != null ? productBom.getProductModelId() : sourceBom.getProductModelId());
+        newBom.setRemark(productBom.getRemark());
+        newBom.setVersion(productBom.getVersion() != null ? productBom.getVersion() : sourceBom.getVersion());
+        return newBom;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
     public AjaxResult uploadBom(MultipartFile file) {
         ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
         List<BomImportDto> list;

--
Gitblit v1.9.3