From 7ea8883ca6b47ec014a32ed57c3bea64544e893e Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 19 三月 2026 17:09:34 +0800
Subject: [PATCH] feat: 生产订单绑定工艺路线、BOM、工艺路线、工序参数新增修改

---
 src/main/java/com/ruoyi/appendix/service/impl/ProductStructureInstanceServiceImpl.java |  131 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/appendix/service/impl/ProductStructureInstanceServiceImpl.java b/src/main/java/com/ruoyi/appendix/service/impl/ProductStructureInstanceServiceImpl.java
index 827e035..5b2f9c2 100644
--- a/src/main/java/com/ruoyi/appendix/service/impl/ProductStructureInstanceServiceImpl.java
+++ b/src/main/java/com/ruoyi/appendix/service/impl/ProductStructureInstanceServiceImpl.java
@@ -1,11 +1,23 @@
 package com.ruoyi.appendix.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.appendix.dto.ProductStructureInstanceDto;
 import com.ruoyi.appendix.mapper.ProductStructureInstanceMapper;
 import com.ruoyi.appendix.pojo.ProductStructureInstance;
 import com.ruoyi.appendix.service.ProductStructureInstanceService;
+import com.ruoyi.common.utils.bean.BeanUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * <br>
@@ -19,4 +31,123 @@
 @Slf4j
 @Service
 public class ProductStructureInstanceServiceImpl extends ServiceImpl<ProductStructureInstanceMapper, ProductStructureInstance> implements ProductStructureInstanceService {
+
+    @Override
+    public List<ProductStructureInstanceDto> listByOrderId(Long orderId) {
+        List<ProductStructureInstance> list = list(new LambdaQueryWrapper<ProductStructureInstance>().eq(ProductStructureInstance::getOrderId, orderId));
+
+        List<ProductStructureInstanceDto> dtoList = list.stream().map(item -> {
+            ProductStructureInstanceDto dto = new ProductStructureInstanceDto();
+            BeanUtils.copyProperties(item, dto);
+            return dto;
+        }).collect(java.util.stream.Collectors.toList());
+
+        Map<Long, ProductStructureInstanceDto> map = new HashMap<>();
+        for (ProductStructureInstanceDto node : dtoList) {
+            map.put(node.getId(), node);
+        }
+
+        List<ProductStructureInstanceDto> tree = new ArrayList<>();
+        for (ProductStructureInstanceDto node : dtoList) {
+            Long parentId = node.getParentId();
+            if (parentId == null || !map.containsKey(parentId)) {
+                tree.add(node);
+            } else {
+                map.get(parentId).getChildren().add(node);
+            }
+        }
+        return tree;
+    }
+
+    @Override
+    public void addOrUpdateBomStructs(ProductStructureInstanceDto instanceDto) {
+        Long orderId = instanceDto.getOrderId();
+
+        // 鎵佸钩鍖栧墠绔紶鍏ョ殑鏍�
+        List<ProductStructureInstanceDto> flatList = new ArrayList<>();
+        flattenTree(instanceDto.getChildren(), flatList);
+
+        // 鏌ヨ鏁版嵁搴撳凡鏈夋暟鎹�
+        List<ProductStructureInstance> dbList = list(new LambdaQueryWrapper<ProductStructureInstance>()
+                .eq(ProductStructureInstance::getOrderId, orderId));
+
+        // 鍓嶇宸叉湁id闆嗗悎
+        Set<Long> frontendIds = flatList.stream()
+                .map(ProductStructureInstanceDto::getId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        // 闇�瑕佸垹闄ょ殑鑺傜偣
+        Set<Long> deleteIds = dbList.stream()
+                .map(ProductStructureInstance::getId)
+                .filter(id -> !frontendIds.contains(id))
+                .collect(Collectors.toSet());
+        if (!deleteIds.isEmpty()) {
+            removeByIds(deleteIds);
+        }
+
+        List<ProductStructureInstance> insertList = new ArrayList<>();
+        List<ProductStructureInstance> updateList = new ArrayList<>();
+        Map<String, ProductStructureInstance> tempEntityMap = new HashMap<>();
+
+        for (ProductStructureInstanceDto dto : flatList) {
+            ProductStructureInstance entity = new ProductStructureInstance();
+            BeanUtils.copyProperties(dto, entity);
+            entity.setOrderId(orderId);
+            if (dto.getId() == null) {
+                entity.setId(null);
+                entity.setParentId(null);
+                insertList.add(entity);
+                if (dto.getTempId() != null) {
+                    tempEntityMap.put(dto.getTempId(), entity);
+                }
+            } else {
+                updateList.add(entity);
+            }
+        }
+
+        if (!insertList.isEmpty()) {
+            saveBatch(insertList);
+        }
+
+        // 鍥炲啓鏂板鑺傜偣鐨� parentId
+        List<ProductStructureInstance> parentFixList = new ArrayList<>();
+        for (ProductStructureInstanceDto dto : flatList) {
+            if (dto.getId() != null) continue;
+            ProductStructureInstance child = tempEntityMap.get(dto.getTempId());
+            if (child == null) continue;
+            String parentTempId = dto.getParentTempId();
+            if (parentTempId != null && !parentTempId.isEmpty()) {
+                Long realParentId;
+                if (tempEntityMap.containsKey(parentTempId)) {
+                    realParentId = tempEntityMap.get(parentTempId).getId();
+                } else {
+                    try {
+                        realParentId = Long.valueOf(parentTempId);
+                    } catch (NumberFormatException e) {
+                        realParentId = 0L;
+                    }
+                }
+                child.setParentId(realParentId);
+            } else {
+                child.setParentId(0L);
+            }
+            parentFixList.add(child);
+        }
+
+        if (!parentFixList.isEmpty()) {
+            updateBatchById(parentFixList);
+        }
+        if (!updateList.isEmpty()) {
+            updateBatchById(updateList);
+        }
+    }
+
+    private void flattenTree(List<ProductStructureInstanceDto> source, List<ProductStructureInstanceDto> result) {
+        if (source == null) return;
+        for (ProductStructureInstanceDto node : source) {
+            result.add(node);
+            flattenTree(node.getChildren(), result);
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3