查询全部数据返回树形结构
public List<DeviceClass> listWithTree(DeviceClass deviceClass) {
List<DeviceClass> entities = deviceClassService.selectDeptList(deviceClass);
List<DeviceClass> subjectVOS = entities.stream().map(subject -> {
DeviceClass subjectVO = new DeviceClass();
BeanUtils.copyProperties(subject, subjectVO);
return subjectVO;
}).collect(Collectors.toList());
List<DeviceClass> list = subjectVOS.stream().filter(subjectVO ->
subjectVO.getParentId().equals("0")
).map((menu)->{
menu.setChildren(getChildrenData(menu,subjectVOS));
return menu;
}).collect(Collectors.toList());
return list;
}
//获取孩子(下级目录)的方法,递归实现
private List<DeviceClass> getChildrenData(DeviceClass root, List<DeviceClass> all) {
List<DeviceClass> children = all.stream().filter(subjectVO ->
subjectVO.getParentId().equals(root.getClassId())
).map(subjectVO -> {
subjectVO.setChildren(getChildrenData(subjectVO,all));
return subjectVO;
}).collect(Collectors.toList());
return children;
}
查询某一节点下全部数据(非树形结构)
public List<DeviceClass> queryAllDepartmentId(List<String> ids){
//根据父ID查询部门
List<DeviceClass> departments = new ArrayList<>();
for (String id: ids) {
departments.addAll(deviceClassService.hasChildById(id));
}
if (CollectionUtils.isNotEmpty(departments)) {
//拿到当前所有部门ID
List<String> parentIds = departments.stream().map(item -> item.getClassId()).collect(Collectors.toList());
//拼接子部门查询结果
departments.addAll(queryAllDepartmentId(parentIds));
return departments;
} else {
//如果没有下级部门那么我们就返回空集合,结束递归。
return Lists.newArrayList();
}
}