From bb80ead86615da40c73ed60a04944461e52929e7 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 20 三月 2026 16:19:25 +0800
Subject: [PATCH] fix: 工艺路线查询未携带状态

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderStructureServiceImpl.java |  145 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 145 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderStructureServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderStructureServiceImpl.java
new file mode 100644
index 0000000..a063566
--- /dev/null
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderStructureServiceImpl.java
@@ -0,0 +1,145 @@
+package com.ruoyi.production.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.production.dto.ProductionOrderStructureDto;
+import com.ruoyi.production.mapper.ProductionOrderStructureMapper;
+import com.ruoyi.production.pojo.ProductionOrderStructure;
+import com.ruoyi.production.service.IProductionOrderStructureService;
+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;
+
+/**
+ * <p>
+ * 鐢熶骇璁㈠崟缁戝畾鐨凚OM瀛愯〃 鏈嶅姟瀹炵幇绫�
+ * </p>
+ *
+ * @author deslrey
+ * @since 2026-03-20
+ */
+@Slf4j
+@Service
+public class ProductionOrderStructureServiceImpl extends ServiceImpl<ProductionOrderStructureMapper, ProductionOrderStructure> implements IProductionOrderStructureService {
+
+    @Override
+    public List<ProductionOrderStructureDto> listByOrderId(Long orderId) {
+        List<ProductionOrderStructureDto> dtoList = baseMapper.listByOrderId(orderId);
+
+        Map<Long, ProductionOrderStructureDto> map = new HashMap<>();
+        for (ProductionOrderStructureDto node : dtoList) {
+            map.put(node.getId(), node);
+        }
+
+        List<ProductionOrderStructureDto> tree = new ArrayList<>();
+        for (ProductionOrderStructureDto 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(Long orderId, List<ProductionOrderStructureDto> list) {
+        // 鎵佸钩鍖栧墠绔紶鍏ョ殑鏍�
+        List<ProductionOrderStructureDto> flatList = new ArrayList<>();
+        for (ProductionOrderStructureDto root : list) {
+            flatList.add(root);
+            flattenTree(root.getChildren(), flatList);
+        }
+
+        // 鏌ヨ鏁版嵁搴撳凡鏈夋暟鎹�
+        List<ProductionOrderStructure> dbList = list(new LambdaQueryWrapper<ProductionOrderStructure>()
+                .eq(ProductionOrderStructure::getOrderId, orderId));
+
+        // 鍓嶇宸叉湁id闆嗗悎
+        Set<Long> frontendIds = flatList.stream()
+                .map(ProductionOrderStructureDto::getId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        // 闇�瑕佸垹闄ょ殑鑺傜偣
+        Set<Long> deleteIds = dbList.stream()
+                .map(ProductionOrderStructure::getId)
+                .filter(id -> !frontendIds.contains(id))
+                .collect(Collectors.toSet());
+        if (!deleteIds.isEmpty()) {
+            removeByIds(deleteIds);
+        }
+
+        List<ProductionOrderStructure> insertList = new ArrayList<>();
+        List<ProductionOrderStructure> updateList = new ArrayList<>();
+        Map<String, ProductionOrderStructure> tempEntityMap = new HashMap<>();
+
+        for (ProductionOrderStructureDto dto : flatList) {
+            ProductionOrderStructure entity = new ProductionOrderStructure();
+            org.springframework.beans.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<ProductionOrderStructure> parentFixList = new ArrayList<>();
+        Long realParentId;
+        for (ProductionOrderStructureDto dto : flatList) {
+            if (dto.getId() != null) continue;
+            ProductionOrderStructure child = tempEntityMap.get(dto.getTempId());
+            if (child == null) continue;
+            String parentTempId = dto.getParentTempId();
+            if (parentTempId != null && !parentTempId.isEmpty()) {
+                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<ProductionOrderStructureDto> source, List<ProductionOrderStructureDto> result) {
+        if (source == null) return;
+        for (ProductionOrderStructureDto node : source) {
+            result.add(node);
+            flattenTree(node.getChildren(), result);
+        }
+    }
+}

--
Gitblit v1.9.3