记个笔记,方便以后查阅
逻辑代码:
//定义最终返回值
List<UserDeptDto> respList = new ArrayList<>();
//查询 每个部门信息 以及 旗下的人员集合
List<UserDeptDto> allUserWithOutAdmin = userMapper.getAllUserWithOutAdmin();
if (null == allUserWithOutAdmin || allUserWithOutAdmin.isEmpty()) {
return respList;
}
Map<Long, UserDeptDto> tempMap = allUserWithOutAdmin.stream()
.collect(Collectors.toMap(UserDeptDto::getDeptId, v -> v, (oldKey, newKey) -> newKey));
allUserWithOutAdmin.stream().forEach(item->{
if (item.getParentId() == 0) {
respList.add(item);//如果父节点为0,表命一级部门,直接录入
}
//获取父节点
UserDeptDto userDeptDto = tempMap.get(item.getParentId());
if (null != userDeptDto) {
if (null == userDeptDto.getChildDeptList()) {
userDeptDto.setChildDeptList(new ArrayList<>());
}
userDeptDto.getChildDeptList().add(item);
}
});
return respList;
封装到的实体类 :
public class UserDeptDto {
@Schema(description = "部门编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Long deptId;
@Schema(description = "部门名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "研发部")
private String deptName;
@Schema(description = "部门父级id", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
private Long parentId;
@Schema(description = "该部门的子部门集合")
private List<UserDeptDto> childDeptList;
@Schema(description = "每个部门下的人员信息集合")
private List<User> userVoList;
}
2024-3-23 补充封装后的递归方法。前端要求人员List和 部门List 都放到childList里面,不要分开,所以在原来方法上进行一个递归封装。
//上方代码下插入
respList.forEach(item -> {
UserDeptTreeRespVo respVo = new UserDeptTreeRespVo();
foramtData(respVo, item);
treeRespVoList.add(respVo);
});
//formatData递归封装方法
/**
* 递归重新封装数据
*
* @param dto
*/
private void foramtData(UserDeptTreeRespVo respVo, UserDeptDto dto) {
respVo.setName(dto.getDeptName());
respVo.setParentId("###" + dto.getParentId());
respVo.setId("###" + dto.getDeptId());
if (null == respVo.getChildList()) {
respVo.setChildList(new ArrayList<>());
}
//把用户集合塞进child
if (null != dto.getUserVoList()) {
dto.getUserVoList().forEach(item->{
UserDeptTreeRespVo userRespVo = new UserDeptTreeRespVo();
userRespVo.setId(item.getId()+"");
userRespVo.setName(item.getUsername());
respVo.getChildList().add(userRespVo);
});
}
//把部门集合塞进child
List<UserDeptDto> childDeptList = dto.getChildDeptList();
if (null != childDeptList) {
for (UserDeptDto userDeptDto : childDeptList) {
UserDeptTreeRespVo userRespVo = new UserDeptTreeRespVo();
if (null != userDeptDto.getChildDeptList()) {
//若部门后还有部门
foramtData(userRespVo, userDeptDto);
} else {
//把该部门塞入child
userRespVo.setName(userDeptDto.getDeptName());
userRespVo.setParentId("###" + userDeptDto.getParentId());
userRespVo.setId("###" + userDeptDto.getDeptId());
userRespVo.setChildList(new ArrayList<>());
//把该部门人员塞入child
if (null != userDeptDto.getUserVoList()) {
userDeptDto.getUserVoList().forEach(item->{
UserDeptTreeRespVo temp = new UserDeptTreeRespVo();
temp.setId(item.getId()+"");
temp.setName(item.getUsername());
userRespVo.getChildList().add(temp);
});
}
}
respVo.getChildList().add(userRespVo);
}
}
}
原来返回Json
@Schema(description = "部门编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Long deptId;
@Schema(description = "部门名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "研发部")
private String deptName;
@Schema(description = "部门父级id", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
private Long parentId;
@Schema(description = "该部门的子部门集合")
private List<UserDeptDto> childDeptList;
@Schema(description = "每个部门下的人员信息集合")
private List<User> userVoList;
{
"deptId": "###100",
"deptName": "河北小度",
"parentId": 0,
"childDeptList": [
{
"deptId": "###101",
"deptName": "保定总公司",
"parentId": 100,
"childDeptList": [
{
"deptId": "###106",
"deptName": "财务部门",
"parentId": 101,
"childDeptList": null,
"userVoList": [
{
"id": 103,
"username": "源码"
}
]
},
{
"deptId": "###107",
"deptName": "运维部门",
"parentId": 101,
"childDeptList": null,
"userVoList": [
{
"id": 104,
"username": "测试号"
}
]
},
{
"deptId": "###104",
"deptName": "市场部门",
"parentId": 101,
"childDeptList": null,
"userVoList": [
{
"id": 20,
"username": "sc"
}
]
},
{
"deptId": "###103",
"deptName": "研发部门",
"parentId": 101,
"childDeptList": null,
"userVoList": []
},
{
"deptId": "###105",
"deptName": "测试部门",
"parentId": 101,
"childDeptList": null,
"userVoList": []
}
],
"userVoList": []
},
{
"deptId": "###102",
"deptName": "石家庄分公司",
"parentId": 100,
"childDeptList": [
{
"deptId": "###109",
"deptName": "财务部门",
"parentId": 102,
"childDeptList": null,
"userVoList": []
},
{
"deptId": "###108",
"deptName": "市场部门",
"parentId": 102,
"childDeptList": null,
"userVoList": []
}
],
"userVoList": []
}
],
"userVoList": [
{
"id": 115,
"username": "1"
},
{
"id": 23,
"username": "测试"
},
{
"id": 1,
"username": "河北小度"
}
]
},
现在返回Json数据
@Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private String id;
@Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private String name;
@Schema(description = "父级id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private String parentId;
@Schema(description = "子集合", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private List<UserDeptTreeRespVo> childList;
{
"id": "###100",
"name": "河北小度",
"parentId": "###0",
"childList": [
{
"id": "115",
"name": "1",
"parentId": null,
"childList": null
},
{
"id": "23",
"name": "测试",
"parentId": null,
"childList": null
},
{
"id": "1",
"name": "河北小度",
"parentId": null,
"childList": null
},
{
"id": "###101",
"name": "保定总公司",
"parentId": "###100",
"childList": [
{
"id": "###106",
"name": "财务部门",
"parentId": "###101",
"childList": [
{
"id": "103",
"name": "源码",
"parentId": null,
"childList": null
}
]
},
{
"id": "###107",
"name": "运维部门",
"parentId": "###101",
"childList": [
{
"id": "104",
"name": "测试号",
"parentId": null,
"childList": null
}
]
},
{
"id": "###104",
"name": "市场部门",
"parentId": "###101",
"childList": [
{
"id": "20",
"name": "sc",
"parentId": null,
"childList": null
}
]
},
{
"id": "###103",
"name": "研发部门",
"parentId": "###101",
"childList": []
},
{
"id": "###105",
"name": "测试部门",
"parentId": "###101",
"childList": []
}
]
},
{
"id": "###102",
"name": "石家庄分公司",
"parentId": "###100",
"childList": [
{
"id": "###109",
"name": "财务部门",
"parentId": "###102",
"childList": []
},
{
"id": "###108",
"name": "市场部门",
"parentId": "###102",
"childList": []
}
]
}
]
},