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 List buildTree(List rows) { return doBuildTree(rows, null); } public static List buildTree(List 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 children = doBuildTree(list, parentId); parent.setChildren(children); List ret = new ArrayList<>(); ret.add(parent); return ret; } else { return doBuildTree(list, parentId); } } else { return doBuildTree(list, null); } } private static List doBuildTree(List list, String parentId) { List 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 splitCode(String code) { List 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 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 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 getAllChildren(String id, Function> getByParentId) { List children = getByParentId.apply(id); List allChildren = new ArrayList<>(); children.forEach((child) -> { allChildren.addAll(getAllChildren(child.getId(), getByParentId)); }); allChildren.addAll(children); return allChildren; } }