组织部门实体属性:
private Long deptId;
private String deptName;
private List<SysDept> children = new ArrayList();
实现方法:
/**
* 举例 : 查询营销中心下的所有部门树
*
* @return 结果
*/
@Override
public R<List<TreeSelect>> marketingCenterDeptList() {
SysDept dept = this.getOne(Wrappers
.<SysDept>lambdaQuery().eq(SysDept::getDeptName, "营销中心"));
List<SysDept> deptList = findChildDept(dept, dept.getDeptId());
return R.ok(deptList.stream().map(TreeSelect::new).collect(Collectors.toList()));
}
/**
* 递归所有子部门集合
* @param dept 部门总数据
* @param deptId 部门Id
* @return 结果
*/
public List<SysDept> findChildDept(SysDept dept,Long deptId) {
List<SysDept> organizationList = this.list(Wrappers
.<SysDept>lambdaQuery()
.select(SysDept::getDeptId,SysDept::getParentId,SysDept::getDeptName).eq(SysDept::getParentId, deptId));
dept.setChildren(organizationList);
for (SysDept item : organizationList) {
findChildDept(item,item.getDeptId());
}
List<SysDept> deptList = new ArrayList<>();
deptList.add(dept);
return deptList;
}
部门树结构类:
/**
* Treeselect树结构实体类
*
* @author GuaiKa
*/
@Data
public class TreeSelect implements Serializable {
private static final long serialVersionUID = 1L;
/** 节点ID */
private Long id;
/** 节点名称 */
private String label;
/** 子节点 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<TreeSelect> children;
public TreeSelect(SysDept dept)
{
this.id = dept.getDeptId();
this.label = dept.getDeptName();
this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
}
}