实体
public class DeptTreeDto {
/**
* id 主键
*/
private Integer id;
/**
* 父id
*/
private Integer pId;
/**
* 排序
*/
private Integer sort;
/**
* 名称
*/
private String name;
/**
* 子类
*/
@ApiModelProperty(name = "children",value = "子类")
private List<DeptTreeDto> children;
//忽略 getter,setter
。。。。。。。
}
业务逻辑
public List<DeptTreeDto> listDeptUserTree(String name) {
//查询全部数据 ,例如:全部部门数据
List<DeptTreeDto> dtoList = this.orgInfoMapper.listAll();
//树型结构返回 0 顶级 filter 先过滤出顶级数据
return dtoList.stream().filter(tree -> 0.equals(tree.getPId()))
.peek((dept) -> dept.setChildren(getChildrenNode(dept, dtoList))).sorted((cat1,cat2)-> (cat1.getSort()==null?0:cat1.getSort()) - (cat2.getSort()==null?0:cat2.getSort())).collect(Collectors.toList());
}
private List<DeptTreeDto> getChildrenNode(DeptTreeDto dept, List<DeptTreeDto> dtoList) {
return dtoList.stream().filter((tree) -> tree.getPId().equals(dept.getId()))
.peek((tree) -> tree.setChildren(getChildrenNode(tree, dtoList))).sorted((cat1,cat2)-> (cat1.getSort()==null?0:cat1.getSort()) - (cat2.getSort()==null?0:cat2.getSort())).collect(Collectors.toList());
}