TreeUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.zhongshu.card.client.utils;
  2. import org.apache.commons.lang3.ObjectUtils;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.function.Function;
  9. /**
  10. *
  11. */
  12. public class TreeUtil {
  13. private static final char[] constCode = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  14. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  15. public static <T extends ITree> List<T> buildTree(List<T> rows) {
  16. return doBuildTree(rows, null);
  17. }
  18. public static <T extends ITree> List<T> buildTree(List<T> list, String parentId) {
  19. if (ObjectUtils.isEmpty(list)) {
  20. return new ArrayList<>(0);
  21. }
  22. if (StringUtils.isNotBlank(parentId)) {
  23. //如果不是从根节点开始构建树,则需要把构建的父节点加入到树中
  24. T parent = null;
  25. for (T tree : list) {
  26. if (tree.getId().equals(parentId)) {
  27. parent = tree;
  28. break;
  29. }
  30. }
  31. if (parent != null) {
  32. List<T> children = doBuildTree(list, parentId);
  33. parent.setChildren(children);
  34. List<T> ret = new ArrayList<>();
  35. ret.add(parent);
  36. return ret;
  37. } else {
  38. return doBuildTree(list, parentId);
  39. }
  40. } else {
  41. return doBuildTree(list, null);
  42. }
  43. }
  44. private static <T extends ITree> List<T> doBuildTree(List<T> list, String parentId) {
  45. List<T> ret = null;
  46. if (StringUtils.isBlank(parentId)) {
  47. parentId = "";
  48. }
  49. for (T m : list) {
  50. if (parentId.equals(m.getParentId())) {
  51. if (ret == null) {
  52. ret = new ArrayList<>();
  53. }
  54. List tempList = doBuildTree(list, m.getId());
  55. if (tempList == null || tempList.size() <= 0) {
  56. tempList = new ArrayList();
  57. }
  58. m.setChildren(tempList);
  59. ret.add(m);
  60. }
  61. }
  62. if (ObjectUtils.isEmpty(ret)) {
  63. return new ArrayList<>(0);
  64. }
  65. return ret;
  66. }
  67. public static String createSubCode() {
  68. StringBuilder sb = new StringBuilder();
  69. Random random = new Random();
  70. random.nextInt(constCode.length - 1);
  71. sb.append(constCode[random.nextInt(constCode.length)]).append(constCode[random.nextInt(constCode.length)]);
  72. return sb.toString();
  73. }
  74. /**
  75. * 分离部门代码
  76. *
  77. * @param code
  78. * @return
  79. */
  80. public static List<String> splitCode(String code) {
  81. List<String> ret = new ArrayList<>();
  82. ret.add(code);
  83. while (code != null && code.length() > 2) {
  84. code = code.substring(0, code.length() - 2);
  85. ret.add(code);
  86. }
  87. return ret;
  88. }
  89. // public static void main(String[] args) {
  90. // /*List<String> list = splitCode("aabbccdd");
  91. // for(String str : list){
  92. // System.out.println(str);
  93. // }*/
  94. // System.out.println(createSubCode());
  95. // }
  96. public static void main(String[] args) {
  97. // String accountId = "62e253756c21d410c0e0a0f5";
  98. //// String[] path = new String[]{"BIM模型","其他","场布模型"};
  99. // String[] parent = new String[]{"BIM模型","其他","场布模型","场地模型及相关资料"};
  100. // final String[] modelPath = new String[parent.length - 1];
  101. // System.arraycopy(parent, 0, modelPath, 0, modelPath.length);
  102. // System.out.println(DigestUtils.md5Hex(String.format("%s_%s",accountId ,String.join("/", modelPath))));
  103. List<String> times = List.of("11:22", "次日11:22");
  104. times.forEach(time -> {
  105. try {
  106. if (StringUtils.contains(time, "次日")) {
  107. time = StringUtils.substring(time, 2);
  108. System.out.println("时间=" + time + " " + (new SimpleDateFormat("HH:mm").parse(time).getTime() + 24 * 60 * 60 * 1000L));
  109. } else {
  110. System.out.println("时间=" + time + " " + new SimpleDateFormat("HH:mm").parse(time).getTime());
  111. }
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. });
  116. }
  117. public static List<? extends ITree> getAllChildren(String id, Function<String, List<? extends ITree>> getByParentId) {
  118. List<? extends ITree> children = getByParentId.apply(id);
  119. List<ITree> allChildren = new ArrayList<>();
  120. children.forEach((child) -> {
  121. allChildren.addAll(getAllChildren(child.getId(), getByParentId));
  122. });
  123. allChildren.addAll(children);
  124. return allChildren;
  125. }
  126. }