TreeUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.qryord.common.utils;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.qryord.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 parentId 传入的父节点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 t
  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. recursionFn(list, tChild);
  51. }
  52. }
  53. }
  54. /**
  55. * 得到子节点列表
  56. */
  57. private static List<Menu> getChildList(List<Menu> list, Menu t)
  58. {
  59. List<Menu> tlist = new ArrayList<Menu>();
  60. Iterator<Menu> it = list.iterator();
  61. while (it.hasNext())
  62. {
  63. Menu n = (Menu) it.next();
  64. if (n.getParentId().longValue() == t.getMenuId().longValue())
  65. {
  66. tlist.add(n);
  67. }
  68. }
  69. return tlist;
  70. }
  71. List<Menu> returnList = new ArrayList<Menu>();
  72. /**
  73. * 根据父节点的ID获取所有子节点
  74. *
  75. * @param list 分类表
  76. * @param typeId 传入的父节点ID
  77. * @param prefix 子节点前缀
  78. */
  79. public List<Menu> getChildPerms(List<Menu> list, int typeId, String prefix)
  80. {
  81. if (list == null)
  82. {
  83. return null;
  84. }
  85. for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
  86. {
  87. Menu node = (Menu) iterator.next();
  88. // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
  89. if (node.getParentId() == typeId)
  90. {
  91. recursionFn(list, node, prefix);
  92. }
  93. // 二、遍历所有的父节点下的所有子节点
  94. /*
  95. * if (node.getParentId()==0) { recursionFn(list, node); }
  96. */
  97. }
  98. return returnList;
  99. }
  100. private void recursionFn(List<Menu> list, Menu node, String p)
  101. {
  102. // 得到子节点列表
  103. List<Menu> childList = getChildList(list, node);
  104. if (hasChild(list, node))
  105. {
  106. // 判断是否有子节点
  107. returnList.add(node);
  108. Iterator<Menu> it = childList.iterator();
  109. while (it.hasNext())
  110. {
  111. Menu n = (Menu) it.next();
  112. n.setMenuName(p + n.getMenuName());
  113. recursionFn(list, n, p + p);
  114. }
  115. }
  116. else
  117. {
  118. returnList.add(node);
  119. }
  120. }
  121. /**
  122. * 判断是否有子节点
  123. */
  124. private static boolean hasChild(List<Menu> list, Menu t)
  125. {
  126. return getChildList(list, t).size() > 0;
  127. }
  128. }