Stream流处理
@Override
public List<CategoryEntity> listWithTree() {
// 1、查出所有分类
List<CategoryEntity> entities = baseMapper.selectList(null);
// 组装成父子的树形结构
return entities.stream().filter(m1 ->
m1.getParentCid() == 0
).peek(menu -> {
menu.setChildren(getChildren(menu, entities));
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
}
private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
return all.stream().filter(m1 -> {
return m1.getParentCid().equals(root.getCatId());
}).peek(m1 -> {
m1.setChildren(getChildren(m1, all));
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
}
@GetMapping("/getorganddep")
@ApiOperation(value = "获取机构与部门列表")
public ResultData<GetDepartmentVO> getOrgAndDep(UserContext user) {
GetDepartmentVO vo = new GetDepartmentVO();
Organization organization = orgRemoteService.get(user.getOrgId());
BeanUtils.copyProperties(organization, vo);
List<QueryCriteria> collection = new ArrayList<>();
collection.add(new QueryCriteria("ancestors", QueryCriteria.LIKE, user.getOrgId()));
List<Department> departments = departmentService.list(collection);
vo.setDepartments(departments.stream().filter(v -> v.getParentId().equals(user.getOrgId())).peek(t -> {
t.setChilds(getChildren(departments, t.getId()));
}).collect(Collectors.toList()));
return ResultData.OK(vo);
}
private List<Department> getChildren(List<Department> departments, String parentId) {
return departments.stream().filter(v -> {
return v.getParentId().equals(parentId);
}).peek(t -> {
t.setChilds(getChildren(departments, t.getId()));
}).collect(Collectors.toList());
}