|
@@ -1,9 +1,15 @@
|
|
package api.common.util;
|
|
package api.common.util;
|
|
|
|
|
|
|
|
+import api.common.pojo.annotation.WChildren;
|
|
|
|
+import api.common.pojo.annotation.WId;
|
|
|
|
+import api.common.pojo.annotation.WParentId;
|
|
import api.common.pojo.vo.system.DictVO;
|
|
import api.common.pojo.vo.system.DictVO;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
|
+import java.lang.reflect.Method;
|
|
import java.security.PublicKey;
|
|
import java.security.PublicKey;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.BiConsumer;
|
|
@@ -146,4 +152,74 @@ public class ObjectUtil {
|
|
}
|
|
}
|
|
return parent;
|
|
return parent;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * list转tree结构,<T>类型需要在属性上添加注解,标识树形结构:@WId 、@WParentId、 @WChildren;
|
|
|
|
+ * @param list
|
|
|
|
+ * @param <T>
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static <T> List<T> listToTree(List<T> list){
|
|
|
|
+ return listToTree(list,"0");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <T> List<T> listToTree(List<T> list,String root) {
|
|
|
|
+ try {
|
|
|
|
+ List<T> treeList = new LinkedList<T>();
|
|
|
|
+ Class<?> tClass = null;
|
|
|
|
+ for (T t : list) {
|
|
|
|
+ tClass = t.getClass();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ //反射解析Getter和Setter
|
|
|
|
+ Field[] fields = tClass.getDeclaredFields();
|
|
|
|
+ Method getId = null;
|
|
|
|
+ Method getParentId = null;
|
|
|
|
+ Method getChildren = null;
|
|
|
|
+ Method setChildren = null;
|
|
|
|
+ for (Field field:fields) {
|
|
|
|
+ String fieldName= field.getName();
|
|
|
|
+ String firstChar = fieldName.substring(0,1);
|
|
|
|
+ if(field.isAnnotationPresent(WId.class)){
|
|
|
|
+ getId = tClass.getMethod("get" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()));
|
|
|
|
+ };
|
|
|
|
+ if(field.isAnnotationPresent(WParentId.class)){
|
|
|
|
+ getParentId = tClass.getMethod("get" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()));
|
|
|
|
+ };
|
|
|
|
+ if(field.isAnnotationPresent(WChildren.class)){
|
|
|
|
+ getChildren = tClass.getMethod("get" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()));
|
|
|
|
+ setChildren = tClass.getMethod("set" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()),List.class);
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+ //解析列表
|
|
|
|
+ for (T node : list) {
|
|
|
|
+ if (root.equals((String)getParentId.invoke(node))) {
|
|
|
|
+ treeList.add(findChildren(node, list,getId,getParentId,getChildren,setChildren));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return treeList;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * list转tree递归调用
|
|
|
|
+ */
|
|
|
|
+ private static <T> T findChildren(T parent, List<T> list, Method getId, Method getParentId, Method getChildren , Method setChildren) throws InvocationTargetException, IllegalAccessException {
|
|
|
|
+ for (T node : list){
|
|
|
|
+ String id = (String)getId.invoke(parent);
|
|
|
|
+ String parentId = (String)getParentId.invoke(node);
|
|
|
|
+ if(id.equals(parentId)){
|
|
|
|
+ List<T> children = (List<T>) getChildren.invoke(parent);
|
|
|
|
+ if(ObjectUtil.isNull(children)){
|
|
|
|
+ children = new ArrayList<T>();
|
|
|
|
+ setChildren.invoke(parent,children);
|
|
|
|
+ }
|
|
|
|
+ children.add(findChildren(node, list,getId,getParentId,getChildren,setChildren));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return parent;
|
|
|
|
+ }
|
|
}
|
|
}
|