TreeUtils.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.ruoyi.common.utils;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.ruoyi.project.system.menu.domain.Menu;
  6. /**
  7. * 权限数据处理
  8. *
  9. * @author ruoyi
  10. */
  11. public class TreeUtils
  12. {
  13. /**
  14. * 根据父节点的ID获取所有子节点
  15. *
  16. * @param list 分类表
  17. * @param typeId 传入的父节点ID
  18. * @return String
  19. */
  20. public static List<Menu> getChildPerms(List<Menu> list, int parentId)
  21. {
  22. List<Menu> returnList = new ArrayList<Menu>();
  23. for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
  24. {
  25. Menu t = (Menu) iterator.next();
  26. // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
  27. if (t.getParentId() == parentId)
  28. {
  29. recursionFn(list, t);
  30. returnList.add(t);
  31. }
  32. }
  33. return returnList;
  34. }
  35. /**
  36. * 递归列表
  37. *
  38. * @param list
  39. * @param Menu
  40. */
  41. private static void recursionFn(List<Menu> list, Menu t)
  42. {
  43. // 得到子节点列表
  44. List<Menu> childList = getChildList(list, t);
  45. t.setChildren(childList);
  46. for (Menu tChild : childList)
  47. {
  48. if (hasChild(list, tChild))
  49. {
  50. // 判断是否有子节点
  51. Iterator<Menu> it = childList.iterator();
  52. while (it.hasNext())
  53. {
  54. Menu n = (Menu) it.next();
  55. recursionFn(list, n);
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * 得到子节点列表
  62. */
  63. private static List<Menu> getChildList(List<Menu> list, Menu t)
  64. {
  65. List<Menu> tlist = new ArrayList<Menu>();
  66. Iterator<Menu> it = list.iterator();
  67. while (it.hasNext())
  68. {
  69. Menu n = (Menu) it.next();
  70. if (n.getParentId().longValue() == t.getMenuId().longValue())
  71. {
  72. tlist.add(n);
  73. }
  74. }
  75. return tlist;
  76. }
  77. List<Menu> returnList = new ArrayList<Menu>();
  78. /**
  79. * 根据父节点的ID获取所有子节点
  80. *
  81. * @param list 分类表
  82. * @param typeId 传入的父节点ID
  83. * @param prefix 子节点前缀
  84. */
  85. public List<Menu> getChildPerms(List<Menu> list, int typeId, String prefix)
  86. {
  87. if (list == null)
  88. {
  89. return null;
  90. }
  91. for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
  92. {
  93. Menu node = (Menu) iterator.next();
  94. // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
  95. if (node.getParentId() == typeId)
  96. {
  97. recursionFn(list, node, prefix);
  98. }
  99. // 二、遍历所有的父节点下的所有子节点
  100. /*
  101. * if (node.getParentId()==0) { recursionFn(list, node); }
  102. */
  103. }
  104. return returnList;
  105. }
  106. private void recursionFn(List<Menu> list, Menu node, String p)
  107. {
  108. // 得到子节点列表
  109. List<Menu> childList = getChildList(list, node);
  110. if (hasChild(list, node))
  111. {
  112. // 判断是否有子节点
  113. returnList.add(node);
  114. Iterator<Menu> it = childList.iterator();
  115. while (it.hasNext())
  116. {
  117. Menu n = (Menu) it.next();
  118. n.setMenuName(p + n.getMenuName());
  119. recursionFn(list, n, p + p);
  120. }
  121. }
  122. else
  123. {
  124. returnList.add(node);
  125. }
  126. }
  127. /**
  128. * 判断是否有子节点
  129. */
  130. private static boolean hasChild(List<Menu> list, Menu t)
  131. {
  132. return getChildList(list, t).size() > 0 ? true : false;
  133. }
  134. }