• java递归查询部门


    @Data
    public class SysDeptModel {
    private Long id;
    private String name;
    private Long pid;
    private List<SysDeptModel> childrens;
    }


    //----------------------------------------------------------
    List<SysDept> sysDepts = sysDeptService.getDeptList(null);
    List<SysDeptModel> list = new ArrayList<>();
    SysDeptModel jsonObject = new SysDeptModel();
    //添加顶级部门
    for (SysDept sysDept : sysDepts) {
    if (sysDept.getParentId().equals(0L)) {
    jsonObject.setId(sysDept.getDeptId());
    jsonObject.setName(sysDept.getDeptName());
    jsonObject.setPid(null);
    list.add(jsonObject);
    break;
    }
    }
    List<SysDeptModel> jsonObjectList;

    for (SysDeptModel object : list) {
    jsonObjectList = getChildren(object.getId(),sysDepts);
    object.setChildrens(jsonObjectList);
    }
    JSONObject json = new JSONObject();
    json.put("data",list);
    //----------------------------------------------------------

    public List<SysDeptModel> getChildren(Long id,List<SysDept> deptList){
    List<SysDeptModel> childrens = new ArrayList<>();
    SysDeptModel obj;
    for (SysDept sysDept : deptList) {
    if (sysDept.getParentId().equals(id)) {
    obj = new SysDeptModel();
    obj.setId(sysDept.getDeptId());
    obj.setName(sysDept.getDeptName());
    obj.setPid(id);
    childrens.add(obj);
    }
    }
    List<SysDeptModel> jsonList = new ArrayList<>();
    jsonList.add(new SysDeptModel());

    for (SysDeptModel children : childrens) {
    if (children.getChildrens() == null) {
    children.setChildrens(jsonList);
    }
    children.setChildrens(getChildren(children.getId(), deptList));
    }
    if (childrens.size() == 0){
    return null;
    }
    return childrens;
    }

    技术有限,只能通过这个方法实现了
    返回的数据结构
    {
    "data": [{
    "id": 1,
    "name": "企业主体信用得分",
    "pid": null,
    "childrens": [{
    "id": 2,
    "name": "企业素质",
    "pid": 1,
    "childrens": [{
    "id": 5,
    "name": "基本信息",
    "pid": 2,
    "childrens": [{
    "id": 10,
    "name": "企业主体信息识别",
    "pid": 5,
    "childrens": []
    },
    {
    "id": 11,
    "name": "企业持续注册时间",
    "pid": 5,
    "childrens": []
    },
    {
    "id": 12,
    "name": "注册资本",
    "pid": 5,
    "childrens": []
    }
    ]
    },
    {
    "id": 6,
    "name": "管理认证",
    "pid": 2,
    "childrens": [{
    "id": 13,
    "name": "国际性管理认证",
    "pid": 6,
    "childrens": []
    }]
    }
    ]
    },
    {
    "id": 3,
    "name": "履约记录",
    "pid": 1,
    "childrens": [{
    "id": 7,
    "name": "税务执行情况",
    "pid": 3,
    "childrens": [{
    "id": 14,
    "name": "是否按时缴纳税款",
    "pid": 7,
    "childrens": []
    }]
    },
    {
    "id": 8,
    "name": "网贷情况",
    "pid": 3,
    "childrens": [{
    "id": 15,
    "name": "网贷逾期",
    "pid": 8,
    "childrens": []
    }]
    }
    ]
    },
    {
    "id": 4,
    "name": "公共监督",
    "pid": 1,
    "childrens": [{
    "id": 9,
    "name": "行政处罚",
    "pid": 4,
    "childrens": [{
    "id": 16,
    "name": "处罚信息",
    "pid": 9,
    "childrens": []
    }]
    }]
    }
    ]
    }]
    }
  • 相关阅读:
    1074 食物链 (并查集)
    2832 6个朋友
    病毒 (拓扑)
    4735 烦人的幻灯片 (拓扑)
    JavaScript中变量的LHS引述和RHS引用
    td自动换行
    SQL Server 中的 NOLOCK 到底是什么意思?
    jQuery中遇到的坑
    jQuery中attr()函数 VS prop()函数
    Javascript刷新页面的几种方法
  • 原文地址:https://www.cnblogs.com/michaelcnblogs/p/14951691.html
Copyright © 2020-2023  润新知