From 74d740bb065e5806748b1ab8ca95d20984c7eace Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期二, 01 九月 2026 10:05:54 +0800
Subject: [PATCH] 【丽水】1、system/user/getInfo 中的belongDepts和belongWards树形结构只获取子节点

---
 ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml                  |   15 +++++
 ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java            |    9 +++
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java |   14 ++++
 ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java         |   10 +++
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java |   13 +++-
 ruoyi-common/src/main/java/com/ruoyi/common/utils/TreeUtils.java                 |   88 +++++++++++++++++++++++++++++
 6 files changed, 145 insertions(+), 4 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java
index 186c05d..d6aa16b 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java
@@ -64,6 +64,9 @@
     @Qualifier("sysUserDeptSystemMapper")
     private SysUserDeptMapper sysUserDeptMapper;
 
+    @Autowired
+    private ISysDeptService sysDeptService;
+
     @Value("${isAdmin}")
     private List<Long> isAdmin;
 
@@ -147,17 +150,19 @@
         }
         List<SysUserDept> sysUserDeptKSs = null;
         List<SysUserDept> sysUserDeptBQs = null;
+        List<SysDept> sysDeptKSs = null;
+        List<SysDept> sysDeptBQs = null;
         SysUserDept sysUserDept = new SysUserDept();
         sysUserDept.setUserId(userId);
         sysUserDept.setDeptType("1");
 
         sysUserDept.setOrgid(user.getOrgid());
-        sysUserDeptKSs = sysUserDeptMapper.selectSysUserDeptList(sysUserDept);
-        ajax.put("belongDepts", sysUserDeptKSs);
+        sysDeptKSs = sysDeptService.selectLeafSysDeptListBySysUserDept(sysUserDept);
+        ajax.put("belongDepts", sysDeptKSs);
 
         sysUserDept.setDeptType("2");
-        sysUserDeptBQs = sysUserDeptMapper.selectSysUserDeptList(sysUserDept);
-        ajax.put("belongWards", sysUserDeptBQs);
+        sysDeptBQs = sysDeptService.selectLeafSysDeptListBySysUserDept(sysUserDept);
+        ajax.put("belongWards", sysDeptBQs);
 
         return ajax;
     }
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/TreeUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/TreeUtils.java
new file mode 100644
index 0000000..873610d
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/TreeUtils.java
@@ -0,0 +1,88 @@
+package com.ruoyi.common.utils;
+
+import com.ruoyi.common.core.domain.entity.SysDept;
+
+import java.util.*;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * 鏍戠粨鏋勫伐鍏风被锛氭牴鎹� parentId 鏋勯�犳爲銆佷竴娆℃�ц幏鍙栨墍鏈夊彾瀛愯妭鐐广��
+ * 鏍稿績鏂规硶涓烘硾鍨嬪疄鐜帮紝id / parentId / children 閫氳繃鍑芥暟娉ㄥ叆锛屽彲澶嶇敤浜庝换浣曡妭鐐圭被鍨嬨��
+ */
+public class TreeUtils {
+
+    private TreeUtils() {
+    }
+
+    /* ================== 閫氱敤鏂规硶 ================== */
+
+    /**
+     * 鏍规嵁 parentId 鏋勯�犳爲锛孫(n) 涓�娆¢亶鍘嗐��
+     * 鏍硅妭鐐逛负銆岀埗Id涓嶅湪闆嗗悎涓�嶇殑鑺傜偣锛涜嫢涓嶅瓨鍦ㄦ牴鑺傜偣锛堝鍏ㄦ槸鑷紩鐢ㄧ殑鐜級鍒欏師鏍疯繑鍥炪��
+     *
+     * @param nodes          鑺傜偣鍒楄〃
+     * @param idGetter       鍙栬妭鐐筰d
+     * @param parentIdGetter 鍙栫埗鑺傜偣id
+     * @param childrenSetter 璁剧疆瀛愯妭鐐瑰垪琛�
+     */
+    public static <T, K> List<T> buildTree(
+            List<T> nodes,
+            Function<T, K> idGetter,
+            Function<T, K> parentIdGetter,
+            BiConsumer<T, List<T>> childrenSetter) {
+        if (nodes == null || nodes.isEmpty()) {
+            return new ArrayList<>();
+        }
+        // 鎸夌埗Id鍒嗙粍锛屼竴娆¢亶鍘嗘嬁鍒版瘡涓妭鐐圭殑瀛愬垪琛�
+        Map<K, List<T>> byParent = nodes.stream().collect(Collectors.groupingBy(parentIdGetter));
+        for (T node : nodes) {
+            childrenSetter.accept(node, byParent.getOrDefault(idGetter.apply(node), new ArrayList<>()));
+        }
+        Set<K> ids = nodes.stream().map(idGetter).filter(Objects::nonNull).collect(Collectors.toSet());
+        List<T> roots = nodes.stream()
+                .filter(n -> !ids.contains(parentIdGetter.apply(n)))
+                .collect(Collectors.toList());
+        return roots.isEmpty() ? nodes : roots;
+    }
+
+    /**
+     * 涓�娆℃�ц幏鍙栨墍鏈夊彾瀛愯妭鐐癸細鍗炽�宨d 涓嶅嚭鐜板湪浠讳綍鑺傜偣鐨� parentId 涓�嶇殑鑺傜偣锛孫(n) 涓�娆¢亶鍘嗐��
+     *
+     * @param nodes          鑺傜偣鍒楄〃
+     * @param idGetter       鍙栬妭鐐筰d
+     * @param parentIdGetter 鍙栫埗鑺傜偣id
+     */
+    public static <T, K> List<T> getLeafNodes(
+            List<T> nodes,
+            Function<T, K> idGetter,
+            Function<T, K> parentIdGetter) {
+        if (nodes == null || nodes.isEmpty()) {
+            return new ArrayList<>();
+        }
+        Set<K> parentIds = nodes.stream()
+                .map(parentIdGetter)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+        return nodes.stream()
+                .filter(n -> !parentIds.contains(idGetter.apply(n)))
+                .collect(Collectors.toList());
+    }
+
+    /* ================== SysDept 渚挎嵎鏂规硶 ================== */
+
+    /**
+     * 鏍规嵁 SysDept.parentId 鏋勯�犻儴闂ㄦ爲
+     */
+    public static List<SysDept> buildDeptTree(List<SysDept> depts) {
+        return buildTree(depts, SysDept::getDeptId, SysDept::getParentId, SysDept::setChildren);
+    }
+
+    /**
+     * 涓�娆℃�ц幏鍙栨墍鏈夐儴闂ㄥ彾瀛愯妭鐐�
+     */
+    public static List<SysDept> getLeafDepts(List<SysDept> depts) {
+        return getLeafNodes(depts, SysDept::getHisDeptId, SysDept::getHisParentId);
+    }
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java
index f470d0a..583cd91 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java
@@ -2,6 +2,7 @@
 
 import java.util.List;
 
+import com.ruoyi.common.core.domain.entity.SysUserDept;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import com.ruoyi.common.core.domain.entity.SysDept;
@@ -120,4 +121,12 @@
      * @return 缁撴灉
      */
     public int deleteDeptById(Long deptId);
+
+    /**
+     * 鏍规嵁UserID鏌ヨ鎵�鏈夊瓙閮ㄩ棬
+     *
+     * @param sysUserDept 閮ㄩ棬g鍏崇郴
+     * @return 閮ㄩ棬鍒楄〃
+     */
+    public List<SysDept> selectSysDeptListBySysUserDept(SysUserDept sysUserDept);
 }
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java
index 7920340..9b59e1d 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java
@@ -5,6 +5,7 @@
 
 import com.ruoyi.common.core.domain.TreeSelect;
 import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.core.domain.entity.SysUserDept;
 
 /**
  * 閮ㄩ棬绠$悊 鏈嶅姟灞�
@@ -129,4 +130,13 @@
      * @return 缁撴灉
      */
     public int deleteDeptById(Long deptId);
+
+
+    /**
+     * 鑾峰彇鍙跺瓙绉戝
+     *
+     * @param sysUserDept
+     * @return 缁撴灉
+     */
+    public List<SysDept> selectLeafSysDeptListBySysUserDept(SysUserDept sysUserDept);
 }
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
index 1718d66..08677d9 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
@@ -3,6 +3,10 @@
 import java.util.*;
 import java.util.stream.Collectors;
 
+import cn.hutool.core.lang.tree.TreeUtil;
+import com.ruoyi.common.core.domain.entity.SysUserDept;
+import com.ruoyi.common.utils.TreeUtils;
+import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -315,4 +319,14 @@
     private boolean hasChild(List<SysDept> list, SysDept t) {
         return getChildList(list, t).size() > 0;
     }
+
+
+    public List<SysDept> selectLeafSysDeptListBySysUserDept(SysUserDept sysUserDept){
+        List<SysDept> list =  deptMapper.selectSysDeptListBySysUserDept(sysUserDept);
+        if(CollectionUtils.isNotEmpty(list)){
+            List<SysDept> tree = TreeUtils.buildDeptTree(list);
+            list = TreeUtils.getLeafDepts(tree);
+        }
+        return list;
+    }
 }
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml
index b08c908..4350001 100644
--- a/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml
@@ -178,6 +178,8 @@
         where find_in_set(#{deptId}, ancestors)
     </select>
 
+
+
     <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
         select count(*)
         from sys_dept
@@ -280,4 +282,17 @@
         where dept_id = #{deptId}
     </delete>
 
+    <select id="selectSysDeptListBySysUserDept" parameterType="com.ruoyi.common.core.domain.entity.SysUserDept"
+            resultMap="SysDeptResult">
+        <include refid="selectDeptVo"/>
+        inner join sys_user_dept ud
+        on d.dept_code = ud.dept_code
+        where 1=1
+        and d.del_flag = 0
+        and ud.del_flag = 0
+        <if test="orgid != null">and d.orgid = #{orgid} and ud.orgid = #{orgid}</if>
+        <if test="campusid != null">and d.campusid = #{campusid} and ud.campusid = #{campusid}</if>
+        <if test="userId != null">and ud.user_id = #{userId}</if>
+        <if test="deptType != null and deptType != ''">and d.dept_type = #{deptType}</if>
+    </select>
 </mapper>

--
Gitblit v1.9.3