From 2737e108612c3ea013e8bcd0ee0f71a4969205bb Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期四, 04 十二月 2025 15:14:01 +0800
Subject: [PATCH] yys

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

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionLineServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionLineServiceImpl.java
new file mode 100644
index 0000000..8ec5e56
--- /dev/null
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionLineServiceImpl.java
@@ -0,0 +1,99 @@
+package com.ruoyi.production.service.impl;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+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.production.mapper.ProductionLineMapper;
+import com.ruoyi.production.pojo.ProductionLine;
+import com.ruoyi.production.service.ProductionLineService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author :yys
+ * @date : 2025/12/4 11:19
+ */
+@Service
+@Slf4j
+public class ProductionLineServiceImpl extends ServiceImpl<ProductionLineMapper, ProductionLine> implements ProductionLineService {
+
+    @Autowired
+    private ProductionLineMapper productionLineMapper;
+
+    /**
+     * 閫掑綊鏌ヨ浜х嚎鏍�
+     * @return
+     */
+    @Override
+    public List<ProductionLine> listTree() {
+        List<ProductionLine> productionLines = productionLineMapper.selectList(Wrappers.lambdaQuery(ProductionLine.class)
+                .eq(ProductionLine::getParentId, 0)
+                .eq(ProductionLine::getType, 1));
+        // 2. 閫掑綊涓烘瘡涓牴鑺傜偣鍔犺浇瀛愯妭鐐癸紙宸ュ簭/瀛愪骇绾匡級
+        for (ProductionLine rootLine : productionLines) {
+            loadChildren(rootLine,1);
+        }
+        return productionLines;
+    }
+
+    /**
+     * 閫掑綊鍔犺浇瀛愯妭鐐癸紙鏍稿績鏂规硶锛�
+     * @param parentNode 鐖惰妭鐐瑰璞�
+     */
+    private void loadChildren(ProductionLine parentNode,Integer type) {
+        // 1. 鏌ヨ褰撳墠鐖惰妭鐐圭殑鎵�鏈夊瓙鑺傜偣
+        List<ProductionLine> children = productionLineMapper.selectList(Wrappers.lambdaQuery(ProductionLine.class)
+                .eq(ProductionLine::getParentId, parentNode.getId())
+                .eq(ProductionLine::getType, type));
+
+        // 2. 鑻ユ湁瀛愯妭鐐癸紝缁х画閫掑綊鍔犺浇瀛愯妭鐐圭殑瀛愯妭鐐�
+        if (!children.isEmpty()) {
+            parentNode.setChildren(children); // 璁剧疆瀛愯妭鐐�
+            for (ProductionLine child : children) {
+                loadChildren(child,type); // 閫掑綊璋冪敤锛屽姞杞藉瓩瀛愯妭鐐�
+            }
+        }
+    }
+
+    /**
+     * 鎸夐渶鎻愪緵锛氭牴鎹埗鑺傜偣ID鏌ヨ鍗曚釜瀛愭爲锛堝鏌ヨ鏌愪釜浜х嚎涓嬬殑鎵�鏈夊伐搴忥級
+     */
+    @Override
+    public IPage<ProductionLine> getSubTreeByParentId(Page page, ProductionLine productionLine) {
+        // 1. 鏌ヨ鐖惰妭鐐规湰韬�
+        ProductionLine parentNode = productionLineMapper.selectById(productionLine.getId());
+        if (parentNode == null) {
+            return null;
+        }
+        // 2. 閫掑綊鍔犺浇瀛愯妭鐐�
+        loadChildren(productionLine,1);
+        List<Long> ids = new ArrayList<>();
+        ids.add(productionLine.getId());
+        if(CollectionUtils.isNotEmpty(productionLine.getChildren())){
+            // 閫掑綊鑾峰彇鎵�鏈夊瓙鑺傜偣鐨刬d
+            getAllChildIds(productionLine,ids);
+        }
+        return productionLineMapper.selectPage(page, Wrappers.lambdaQuery(ProductionLine.class)
+                .in(ProductionLine::getParentId, ids)
+                .eq(ProductionLine::getType, 2));
+    }
+
+    /**
+     * 閫掑綊鑾峰彇鎵�鏈夊瓙鑺傜偣鐨刬d
+     */
+    public List<Long> getAllChildIds(ProductionLine node,List<Long> ids) {
+        if (node.getChildren() != null) {
+            for (ProductionLine child : node.getChildren()) {
+                ids.add(child.getId());
+                ids.addAll(getAllChildIds(child,ids));
+            }
+        }
+        return ids;
+    }
+}

--
Gitblit v1.9.3