一次性搞定权限树遍历(通用解决方案)
恋习Python
共 6075字,需浏览 13分钟
·
2022-06-20 19:13
data:数据库表的任意一行权限 childrenNode:data节点下所有的权限集合
public class TreeNode<T> {
//数据内容
private T data;
//这个节点下的子节点
private List<TreeNode<T>> childrenNode = new ArrayList<>();
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public List<TreeNode<T>> getChildrenNode() {
return childrenNode;
}
public void setChildrenNode(List<TreeNode<T>> childrenNode) {
this.childrenNode = childrenNode;
}
public TreeNode(T data, List<TreeNode<T>> childrenNode) {
this.data = data;
this.childrenNode = childrenNode;
}
public TreeNode() {
}
}
public List<TreeNode<T>> childrenNode(List<T> datas, String idName, String fidName) {
ConvertTree<T> convertTree = new ConvertTree<>();
String idValue = convertTree.getFieldValue(data, idName);
List<T> collect = datas.stream()
.filter(date -> idValue.equals(convertTree.getFieldValue(date, fidName)))
.collect(Collectors.toList());
datas.removeAll(collect);
for (T node : collect) {
TreeNode<T> treeNode = new TreeNode<>();
treeNode.setData(node);
childrenNode.add(treeNode);
}
return childrenNode;
}
public String getFieldValue(T o, String fieldName) {
//得到class
Class cls = o.getClass();
//得到所有属性
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
try {
//打开私有访问
field.setAccessible(true);
//获取属性
if (field.getName().equals(fieldName)) {
Object result = field.get(o);
if (result == null) {
return null;
}
return result.toString();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
throw new RuntimeException("获取属性值失败");
}
private TreeNode<T> getRootNode(List<T> datas, String idName, String fidName) {
if (datas.isEmpty()) {
return null;
}
T node = datas.get(0);
T rootNode = getRootNode(datas, idName, fidName, node);
TreeNode<T> rootTreeNode = new TreeNode<>();
datas.remove(rootNode);
rootTreeNode.setData(rootNode);
rootTreeNode.childrenNode(datas, idName, fidName);
return rootTreeNode;
}
private T getRootNode(List<T> datas, String idName, String fidName, T node) {
T fNode = null;
String fieldValue = getFieldValue(node, fidName);
for (T data : datas) {
if (getFieldValue(data, idName).equals(fieldValue)) {
fNode = data;
break;
}
}
if (fNode == null) {
return node;
} else {
return getRootNode(datas, idName, fidName, fNode);
}
}
private TreeNode<T> getTree(List<T> datas, String idName, String fidName) {
//获取树根
TreeNode<T> rootNode = getRootNode(datas, idName, fidName);
//遍历树根的子集
List<TreeNode<T>> childrenNode = rootNode.getChildrenNode();
forChildren(datas, idName, fidName, childrenNode);
//此时树已经形成
return rootNode;
}
private void forChildren(List<T> datas, String idName, String fidName, List<TreeNode<T>> childrenNode) {
//需要遍历的集合
List<TreeNode<T>> needForList = new ArrayList<>();
for (TreeNode<T> tTreeNode : childrenNode) {
List<TreeNode<T>> treeNodes = tTreeNode.childrenNode(datas, idName, fidName);
needForList.addAll(treeNodes);
}
if (!needForList.isEmpty()) {
forChildren(datas, idName, fidName, needForList);
}
}
public List<TreeNode<T>> getForest(List<T> datas, String idName, String fidName) {
List<TreeNode<T>> forest = new ArrayList<>();
while (!datas.isEmpty()) {
TreeNode<T> tree = getTree(datas, idName, fidName);
forest.add(tree);
}
return forest;
}
public List<TreeNode<AuthResource>> toTree() {
//获取数据集合
List<AuthResource> list = authResourceRepository.findAll();
//创建工具类
ConvertTree<AuthResource> convertTree = new ConvertTree<>();
//生成森林
List<TreeNode<AuthResource>> forest = convertTree.getForest(list, "id", "parentId");
return convertTree.getForest(list);
}
评论