| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package com.zhongshu.card.client.utils;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.function.Function;
- /**
- *
- */
- public class TreeUtil {
- 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',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
- public static <T extends ITree> List<T> buildTree(List<T> rows) {
- return doBuildTree(rows, null);
- }
- public static <T extends ITree> List<T> buildTree(List<T> list, String parentId) {
- if (ObjectUtils.isEmpty(list)) {
- return new ArrayList<>(0);
- }
- if (StringUtils.isNotBlank(parentId)) {
- //如果不是从根节点开始构建树,则需要把构建的父节点加入到树中
- T parent = null;
- for (T tree : list) {
- if (tree.getId().equals(parentId)) {
- parent = tree;
- break;
- }
- }
- if (parent != null) {
- List<T> children = doBuildTree(list, parentId);
- parent.setChildren(children);
- List<T> ret = new ArrayList<>();
- ret.add(parent);
- return ret;
- } else {
- return doBuildTree(list, parentId);
- }
- } else {
- return doBuildTree(list, null);
- }
- }
- private static <T extends ITree> List<T> doBuildTree(List<T> list, String parentId) {
- List<T> ret = null;
- if (StringUtils.isBlank(parentId)) {
- parentId = "";
- }
- for (T m : list) {
- if (parentId.equals(m.getParentId())) {
- if (ret == null) {
- ret = new ArrayList<>();
- }
- List tempList = doBuildTree(list, m.getId());
- if (tempList == null || tempList.size() <= 0) {
- tempList = new ArrayList();
- }
- m.setChildren(tempList);
- ret.add(m);
- }
- }
- if (ObjectUtils.isEmpty(ret)) {
- return new ArrayList<>(0);
- }
- return ret;
- }
- public static String createSubCode() {
- StringBuilder sb = new StringBuilder();
- Random random = new Random();
- random.nextInt(constCode.length - 1);
- sb.append(constCode[random.nextInt(constCode.length)]).append(constCode[random.nextInt(constCode.length)]);
- return sb.toString();
- }
- /**
- * 分离部门代码
- *
- * @param code
- * @return
- */
- public static List<String> splitCode(String code) {
- List<String> ret = new ArrayList<>();
- ret.add(code);
- while (code != null && code.length() > 2) {
- code = code.substring(0, code.length() - 2);
- ret.add(code);
- }
- return ret;
- }
- // public static void main(String[] args) {
- // /*List<String> list = splitCode("aabbccdd");
- // for(String str : list){
- // System.out.println(str);
- // }*/
- // System.out.println(createSubCode());
- // }
- public static void main(String[] args) {
- // String accountId = "62e253756c21d410c0e0a0f5";
- //// String[] path = new String[]{"BIM模型","其他","场布模型"};
- // String[] parent = new String[]{"BIM模型","其他","场布模型","场地模型及相关资料"};
- // final String[] modelPath = new String[parent.length - 1];
- // System.arraycopy(parent, 0, modelPath, 0, modelPath.length);
- // System.out.println(DigestUtils.md5Hex(String.format("%s_%s",accountId ,String.join("/", modelPath))));
- List<String> times = List.of("11:22", "次日11:22");
- times.forEach(time -> {
- try {
- if (StringUtils.contains(time, "次日")) {
- time = StringUtils.substring(time, 2);
- System.out.println("时间=" + time + " " + (new SimpleDateFormat("HH:mm").parse(time).getTime() + 24 * 60 * 60 * 1000L));
- } else {
- System.out.println("时间=" + time + " " + new SimpleDateFormat("HH:mm").parse(time).getTime());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- }
- public static List<? extends ITree> getAllChildren(String id, Function<String, List<? extends ITree>> getByParentId) {
- List<? extends ITree> children = getByParentId.apply(id);
- List<ITree> allChildren = new ArrayList<>();
- children.forEach((child) -> {
- allChildren.addAll(getAllChildren(child.getId(), getByParentId));
- });
- allChildren.addAll(children);
- return allChildren;
- }
- }
|