@Override
public Res<Object> centerTree(String keyword) {
List<SupportCenterTreeVo> treeList = supportCenterMapper.tree();
if(treeList.isEmpty()) return Res.createBySuccess(SysConstant.SUPPORT_CENTER_TYPE_SUCCESS, treeList);
List<SupportCenterTreeVo> treeList1 = getSupportCenterTreesVo2(treeList, keyword);
return Res.createBySuccess(SysConstant.SUPPORT_CENTER_TYPE_SUCCESS,treeList1);
}
private List<SupportCenterTreeVo> getSupportCenterTreesVo2(List<SupportCenterTreeVo> treeList, String keyword){
//包含关键字的文件夹
List<SupportCenterTreeVo> filterResourceList =
treeList.stream().filter(supportCenterTreeVo -> supportCenterTreeVo.getName().contains(keyword))
.collect(Collectors.toList());
if(org.springframework.util.CollectionUtils.isEmpty(filterResourceList)){
return Collections.emptyList();
}
//查询包含关键字的最上层文件夹,及其子文件夹
List<String> collect1 = filterResourceList.stream().map(SupportCenterTreeVo::getId).collect(Collectors.toList());
List<SupportCenterTreeVo> supportCenterTreesVos = new ArrayList<>();
for (SupportCenterTreeVo s : filterResourceList) {
if(!collect1.contains(s.getPid())){
SupportCenterTreeVo supportCenterTreeVo = treeList.stream().filter(a -> s.getId().equals(a.getId())).collect(Collectors.toList()).get(0);
SupportCenterTreeVo hh= getSupportCenterTreesVo3(treeList,supportCenterTreeVo);
supportCenterTreesVos.add(hh);
}
}
return supportCenterTreesVos
}
private SupportCenterTreeVo getSupportCenterTreesVo3(List<SupportCenterTreeVo> treeList,SupportCenterTreeVo supportCenterTreeVo){
List<SupportCenterTreeVo> collect = treeList.stream().filter(a -> supportCenterTreeVo.getId().equals(a.getPid())).collect(Collectors.toList());
supportCenterTreeVo.setChildren(collect);
if(CollectionUtils.isEmpty(collect)) return supportCenterTreeVo;
for (SupportCenterTreeVo centerTreeVo : collect) {
getSupportCenterTreesVo3(treeList,centerTreeVo);
}
return supportCenterTreeVo;
}
附带石姓朋友提供的树形结构查询工具类(可满足树结构数据普通查询)
public class TreeUtils {
public static <T> List<T> buidTree(List<T> list, Class<T> clazz, String id, String parentId, String children) throws Exception{
Field idField;
try {
idField = clazz.getDeclaredField(id);
} catch (NoSuchFieldException e) {
idField = clazz.getSuperclass().getDeclaredField(id);
}
Field parentField;
try {
parentField = clazz.getDeclaredField(parentId);
} catch (NoSuchFieldException e) {
parentField = clazz.getSuperclass().getDeclaredField(parentId);
}
Field childrenField;
try {
childrenField = clazz.getDeclaredField(children);
} catch (NoSuchFieldException e) {
childrenField = clazz.getSuperclass().getDeclaredField(children);
}
idField.setAccessible(true);
parentField.setAccessible(true);
childrenField.setAccessible(true);
List<T> tree = new ArrayList<>();
for (T t : list) {
Object o = parentField.get(t);
if (o == null) {
tree.add(findChild(t, list, idField, parentField, childrenField));
} else {
boolean flag = true;
for (T t1 : list) {
Object o1 = idField.get(t1);
if (o.equals(o1)) {
flag = false;
break;
}
}
if (flag) {
tree.add(findChild(t, list, idField, parentField, childrenField));
}
}
}
return tree;
}
static <T> T findChild( T node, List<T> list, Field id, Field parent, Field children ) throws IllegalAccessException{
for (T n : list) {
if (parent.get(n).equals(id.get(node))) {
if (children.get(node) == null) {
children.set(node, new ArrayList<T>());
}
List list1 = (List) children.get(node);
list1.add(findChild(n, list, id, parent, children));
}
}
return node;
}
}