Commit 068c3e6f0deb4a179a2535119df5f6fc0a6ad226
1 parent
dd664008
优化递归子节点
Showing
2 changed files
with
12 additions
and
19 deletions
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
| @@ -267,13 +267,7 @@ public class SysDeptServiceImpl implements ISysDeptService | @@ -267,13 +267,7 @@ public class SysDeptServiceImpl implements ISysDeptService | ||
| 267 | { | 267 | { |
| 268 | if (hasChild(list, tChild)) | 268 | if (hasChild(list, tChild)) |
| 269 | { | 269 | { |
| 270 | - // 判断是否有子节点 | ||
| 271 | - Iterator<SysDept> it = childList.iterator(); | ||
| 272 | - while (it.hasNext()) | ||
| 273 | - { | ||
| 274 | - SysDept n = (SysDept) it.next(); | ||
| 275 | - recursionFn(list, n); | ||
| 276 | - } | 270 | + recursionFn(list, tChild); |
| 277 | } | 271 | } |
| 278 | } | 272 | } |
| 279 | } | 273 | } |
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java
| @@ -178,14 +178,19 @@ public class SysMenuServiceImpl implements ISysMenuService | @@ -178,14 +178,19 @@ public class SysMenuServiceImpl implements ISysMenuService | ||
| 178 | public List<SysMenu> buildMenuTree(List<SysMenu> menus) | 178 | public List<SysMenu> buildMenuTree(List<SysMenu> menus) |
| 179 | { | 179 | { |
| 180 | List<SysMenu> returnList = new ArrayList<SysMenu>(); | 180 | List<SysMenu> returnList = new ArrayList<SysMenu>(); |
| 181 | + List<Long> tempList = new ArrayList<Long>(); | ||
| 182 | + for (SysMenu dept : menus) | ||
| 183 | + { | ||
| 184 | + tempList.add(dept.getMenuId()); | ||
| 185 | + } | ||
| 181 | for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();) | 186 | for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();) |
| 182 | { | 187 | { |
| 183 | - SysMenu t = (SysMenu) iterator.next(); | ||
| 184 | - // 根据传入的某个父节点ID,遍历该父节点的所有子节点 | ||
| 185 | - if (t.getParentId() == 0) | 188 | + SysMenu menu = (SysMenu) iterator.next(); |
| 189 | + // 如果是顶级节点, 遍历该父节点的所有子节点 | ||
| 190 | + if (!tempList.contains(menu.getParentId())) | ||
| 186 | { | 191 | { |
| 187 | - recursionFn(menus, t); | ||
| 188 | - returnList.add(t); | 192 | + recursionFn(menus, menu); |
| 193 | + returnList.add(menu); | ||
| 189 | } | 194 | } |
| 190 | } | 195 | } |
| 191 | if (returnList.isEmpty()) | 196 | if (returnList.isEmpty()) |
| @@ -406,13 +411,7 @@ public class SysMenuServiceImpl implements ISysMenuService | @@ -406,13 +411,7 @@ public class SysMenuServiceImpl implements ISysMenuService | ||
| 406 | { | 411 | { |
| 407 | if (hasChild(list, tChild)) | 412 | if (hasChild(list, tChild)) |
| 408 | { | 413 | { |
| 409 | - // 判断是否有子节点 | ||
| 410 | - Iterator<SysMenu> it = childList.iterator(); | ||
| 411 | - while (it.hasNext()) | ||
| 412 | - { | ||
| 413 | - SysMenu n = (SysMenu) it.next(); | ||
| 414 | - recursionFn(list, n); | ||
| 415 | - } | 414 | + recursionFn(list, tChild); |
| 416 | } | 415 | } |
| 417 | } | 416 | } |
| 418 | } | 417 | } |