1.数组列表
{
"data": [
{
"deptName": "资源概览",
"contain": "0",
"deptLevel": "1",
"deptId": "5862",
"superCode": null,
"parentDeptName": null,
"deptCode": "KS419"
},
{
"deptName": "资源目录",
"contain": "0",
"deptLevel": "2",
"deptId": "5861",
"superCode": "KS419",
"parentDeptName": "资源概览",
"deptCode": "KS418"
},
{
"deptName": "资源大屏",
"contain": "0",
"deptLevel": "2",
"deptId": "5862",
"superCode": "KS419",
"parentDeptName": "资源概览",
"deptCode": "KS417"
},
{
"deptName": "监控大屏",
"contain": "0",
"deptLevel": "1",
"deptId": "5864",
"superCode": null,
"parentDeptName": null,
"deptCode": "KS416"
}
]
}
2.根据父编码superCode递归转树形结构
public List<DeptInfoVo> getRoleMenuTree(List<DeptInfoVo> deptList) {
List<DeptInfoVo> resultList = new ArrayList<>();
//获取顶层元素集合
for (DeptInfoVo permission : deptList) {
String parentId = permission.getSuperCode();
//顶层元素的id为null或者为0
if (StringUtils.isBlank(parentId)) {
DeptInfoVo superDept=new DeptInfoVo();
superDept.setDeptId(permission.getDeptId());
superDept.setDeptCode(permission.getDeptCode());
superDept.setDeptName(permission.getDeptName());
superDept.setDeptLevel(permission.getDeptLevel());
superDept.setSuperCode(parentId);
resultList.add(superDept);
}
}
//获取每个顶层元素的子数据集合
for (DeptInfoVo entity : resultList) {
entity.setChild(getPowerSubList(entity.getDeptCode(), deptList));
}
return resultList;
}
/**
* 获取子数据集合
*
* @param code
* @param deptList
* @return
*/
private List<DeptInfoVo> getPowerSubList(String code, List<DeptInfoVo> deptList) {
List<DeptInfoVo> childList = new ArrayList<>();
//子集的直接子对象
for (DeptInfoVo entity : deptList) {
String parentId = entity.getSuperCode();
if (code.equals(parentId)) {
DeptInfoVo vo = new DeptInfoVo();
vo.setDeptId(entity.getDeptId());
vo.setDeptCode(entity.getDeptCode());
vo.setDeptName(entity.getDeptName());
vo.setDeptLevel(entity.getDeptLevel());
vo.setSuperCode(parentId);
}
}
//子集的间接子对象
for (DeptInfoVo entity : childList) {
entity.setChild(getPowerSubList(entity.getDeptCode(), deptList));
}
//递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
3.返回结果
{
"data": [{
"deptName": "资源概览",
"contain": "0",
"deptLevel": "1",
"deptId": "5862",
"superCode": null,
"parentDeptName": null,
"deptCode": "KS419",
"child": [{
"deptName": "资源目录",
"contain": "0",
"deptLevel": "2",
"deptId": "5861",
"superCode": "KS419",
"parentDeptName": "资源概览",
"deptCode": "KS418"
},
{
"deptName": "资源大屏",
"contain": "0",
"deptLevel": "2",
"deptId": "5862",
"superCode": "KS419",
"parentDeptName": "资源概览",
"deptCode": "KS417"
}
]
}, {
"deptName": "患者视图",
"contain": "0",
"deptLevel": "1",
"deptId": "5864",
"superCode": null,
"parentDeptName": null,
"deptCode": "KS416"
}]
}