From fca777674ee1fe99a519b38db4b2a459c24966d8 Mon Sep 17 00:00:00 2001 From: 王彪总 Date: Mon, 12 Jan 2026 00:41:07 +0800 Subject: [PATCH] refactor(dept): 统一部门接口返回格式为标准Map结构 --- urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/service/user/AdminUserServiceImpl.java | 89 +++++++++++++++++++++++++++++++++++++---------------------------------------------------- 1 file changed, 37 insertions(+), 52 deletions(-) diff --git a/urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/service/user/AdminUserServiceImpl.java b/urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/service/user/AdminUserServiceImpl.java index 227ae8c..7f14cdc 100644 --- a/urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/service/user/AdminUserServiceImpl.java +++ b/urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/service/user/AdminUserServiceImpl.java @@ -668,72 +668,57 @@ public class AdminUserServiceImpl implements AdminUserService { } private Long getParentDeptId(Long deptId) { + if (deptId == null) { + return null; + } + DeptDO dept = deptService.getDept(deptId); if (dept == null) { return null; } - //顶级部门ID - if(dept.getParentId().equals(0L)) { + + // 顶级部门ID (parentId=0) + if (dept.getParentId().equals(0L)) { return dept.getId(); } - //二级部门 - if(dept.getParentId().equals(100L)) { + + // 直接是二级部门 (parentId=100) + if (dept.getParentId().equals(100L)) { return dept.getId(); } - //所有 - List allDeptList = deptService.getDeptAllList(); - if (CollectionUtil.isEmpty(allDeptList)) { - return null; - } - // 构建部门ID → 部门对象的Map(核心:快速根据ID查部门) - Map deptMap = allDeptList.stream() - // 第一个参数:Map 的 Key(取 deptId) - // 第二个参数:Map 的 Value(取 Dept 对象本身) - .collect(Collectors.toMap(DeptDO::getId, DeptDO -> DeptDO)); - - DeptDO findDept = findDeptByTargetParentIdLoop(dept.getParentId(), 100L, deptMap); - return findDept == null ? null : findDept.getId(); + + // 递归查找父部门链中parentId=100的部门 + return findParentDeptWithParentId(dept.getParentId(), 100L); } - + /** - * 循环向上追溯,查找父部门ID等于目标值的部门对象(避免栈溢出) - * @param startDeptId 起始部门ID(指定deptId) - * @param targetParentId 目标parentId(要匹配的父部门ID) - * @param deptIdMap 部门ID映射Map - * @return 匹配的部门对象(无则返回null) + * 递归查找父部门链中parentId等于目标值的部门ID + * @param currentDeptId 当前部门ID + * @param targetParentId 目标parentId + * @return 匹配的部门ID,无则返回null */ - public static DeptDO findDeptByTargetParentIdLoop(Long startDeptId, Long targetParentId, Map deptIdMap) { - // 1. 入参校验 - if (startDeptId == null || targetParentId == null || deptIdMap == null || targetParentId == 0) { + private Long findParentDeptWithParentId(Long currentDeptId, Long targetParentId) { + if (currentDeptId == null) { return null; } - - Long currentDeptId = startDeptId; - while (true) { - // 2. 获取当前部门对象 - DeptDO currentDept = deptIdMap.get(currentDeptId); - if (currentDept == null) { - break; - } - - // 3. 获取当前部门的父ID(父部门的deptId) - Long parentDeptId = currentDept.getParentId(); - // 终止条件:追溯到顶级部门(parentId=0) - if (parentDeptId == 0L) { - break; - } - - // 4. 校验是否匹配目标parentId - if (parentDeptId.equals(targetParentId)) { - return currentDept; // 匹配,返回对象 - } - - // 5. 未匹配,继续追溯父部门 - currentDeptId = currentDept.getParentId(); + + DeptDO dept = deptService.getDept(currentDeptId); + if (dept == null) { + return null; } - - // 遍历结束未匹配 - return null; + + // 找到目标父部门 + if (dept.getParentId().equals(targetParentId)) { + return dept.getId(); + } + + // 达到顶级部门,停止递归 + if (dept.getParentId().equals(0L)) { + return null; + } + + // 继续向上查找 + return findParentDeptWithParentId(dept.getParentId(), targetParentId); } } -- libgit2 0.21.4