废话不错说,上代码
Entity
public class TreeSelect implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 节点ID
*/
private Long id;
/**
* 节点ID值
*/
private Long value;
/**
* 节点名称
*/
private String label;
/**
* 子节点
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<TreeSelect> children;
}
Controller
/**
* 用户树
*/
@Log(title = "用户树" , businessType = BusinessType.UPDATE)
@GetMapping("/userTree")
public AjaxResult userTree() {
return AjaxResult.success(userService.userTree());
}
ServiceImpl
@Override
public List<TreeSelect> userTree() {
List<SysUser> userList = userMapper.selectUsertLists();
//一级菜单
List<TreeSelect> rootTree = new ArrayList<>();
TreeSelect treeSelect = new TreeSelect();
treeSelect.setId(1L);
treeSelect.setValue(1L);
treeSelect.setLabel("用户树");
treeSelect.setChildren(new ArrayList<>());
rootTree.add(treeSelect);
List<TreeSelect> roots = bulidTree(rootTree, userList);
return roots;
}
public List<TreeSelect> bulidTree(List<TreeSelect> rootTree, List<SysUser> userList) {
for (TreeSelect deptTreeSelect : rootTree) {
for (SysUser user : userList) {
if (user.getDeptId().intValue() == deptTreeSelect.getId().intValue()) {
TreeSelect treeSelect = new TreeSelect();
treeSelect.setId(user.getUserId());
treeSelect.setValue(user.getUserId());
treeSelect.setLabel(user.getNickName());
if (null != deptTreeSelect.getChildren()) {
deptTreeSelect.getChildren().add(treeSelect);
}
}
}
if (null != deptTreeSelect.getChildren() && deptTreeSelect.getChildren().size() > 0) {
bulidTree(deptTreeSelect.getChildren(), userList);
}
}
return rootTree;
}