欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

取一个公司的部门和人迭代树

程序员文章站 2024-03-23 22:19:04
...

取一个公司的部门和人迭代树

逻辑思路:部门下面的部门和人
用一个儿子还是两个儿子取决于前端工程师。

第一种:部门下面的部门和人,人和部门是一个儿子
public List departmentList;
public List userList;

@ApiOperation(value = "getUserAndDeptListTree", notes = "get department list")
@ApiImplicitParams({
        @ApiImplicitParam(name = "orgCode", value = "orgCode", dataType = "String", paramType = "query", example = "")
})
@ResponseBody
@RequestMapping(value = "/getUserAndDeptListTree", produces = {"application/json;charset=UTF-8"})
public BaseResultData getUserAndDeptListTree(
        @RequestParam(name = "orgCode", required = true) String orgCode) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("KEYCODE", "true");
    map.put("PARENT_CODE", "true");
    map.put("NAME", "true");
    map.put("orgCode", orgCode);
    List<Department> departmentList = this.departmentService.getList(map).getList();

    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("ORG_CODE", "true");
    map1.put("DEPT_CODE", "true");
    map1.put("NAME", "true");
    map1.put("LOGO", "true");
    map.put("orgCode", orgCode);
    List<User> userList = this.userService.getList(map1).getList();

    ArrayNode dataArray = JsonUtil.getObjectMapper().createArrayNode();
    this.departmentList = departmentList;
    this.userList = userList;
    for(Department department : departmentList){
        ObjectNode departmentData = JsonUtil.getObjectMapper().createObjectNode();
        if(department.getParentCode() == null || department.getParentCode().equals("")){
            departmentData.put("id", department.getKeycode());
            departmentData.put("label", department.getName());
            departmentData.put("pid", department.getParentCode());
            departmentData.put("disabled", true);
            departmentData.set("children", departmentChild(department.getKeycode()));
            dataArray.add(departmentData);
        }
    }
    for(User user : userList){
        ObjectNode userData = JsonUtil.getObjectMapper().createObjectNode();
        if(user.getDeptCode() == null || user.getDeptCode().equals("")){
            userData.put("id", user.getKeycode());
            userData.put("label", user.getName());
            userData.put("pid", user.getDeptCode());
            dataArray.add(userData);
        }
    }
    return BaseResultData.success(dataArray);
}

public ArrayNode departmentChild(String departmentCode){
    ArrayNode deptChildArray = JsonUtil.getObjectMapper().createArrayNode();
    for(Department department : departmentList){
        ObjectNode deptChild = JsonUtil.getObjectMapper().createObjectNode();
        if(department.getParentCode().equals(departmentCode)){
            deptChild.put("id", department.getKeycode());
            deptChild.put("label", department.getName());
            deptChild.put("pid", department.getParentCode());
            deptChild.put("disabled", true);
            deptChild.set("children", departmentChild(department.getKeycode()));
            deptChildArray.add(deptChild);
        }
    }
    for(User user : userList){
        ObjectNode userChild = JsonUtil.getObjectMapper().createObjectNode();
        if(user.getDeptCode().equals(departmentCode)){
            userChild.put("id", user.getKeycode());
            userChild.put("label", user.getName());
            userChild.put("pid", user.getDeptCode());
            deptChildArray.add(userChild);
        }
    }
    return deptChildArray;
}

第二种:部门下面的部门和人,人和部门是两个儿子
public List departmentList;
public List userList;

@ApiOperation(value = "getUserAndDeptListTree", notes = "get department list")
@ApiImplicitParams({
        @ApiImplicitParam(name = "orgCode", value = "orgCode", dataType = "String", paramType = "query", example = "")
})
@ResponseBody
@RequestMapping(value = "/getUserAndDeptListTree", produces = {"application/json;charset=UTF-8"})
public BaseResultData getUserAndDeptListTree(
        @RequestParam(name = "orgCode", required = true) String orgCode) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("KEYCODE", "true");
    map.put("PARENT_CODE", "true");
    map.put("NAME", "true");
    map.put("orgCode", orgCode);
    List<Department> departmentList = this.departmentService.getList(map).getList();

    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("ORG_CODE", "true");
    map1.put("DEPT_CODE", "true");
    map1.put("NAME", "true");
    map1.put("LOGO", "true");
    map.put("orgCode", orgCode);
    List<User> userList = this.userService.getList(map1).getList();

    ObjectNode data = JsonUtil.getObjectMapper().createObjectNode();
    ArrayNode departmentArray = JsonUtil.getObjectMapper().createArrayNode();
    ArrayNode userArray = JsonUtil.getObjectMapper().createArrayNode();
    this.departmentList = departmentList;
    this.userList = userList;
    for(Department department : departmentList){
        ObjectNode departmentData = JsonUtil.getObjectMapper().createObjectNode();
        if(department.getParentCode() == null || department.getParentCode().equals("")){
            departmentData.put("id", department.getKeycode());
            departmentData.put("name", department.getName());
            departmentData.put("pid", department.getParentCode());
            departmentData.set("deptChildList", departmentChild(department.getKeycode()));
            departmentData.set("userChildList", userChild(department.getKeycode()));
            departmentArray.add(departmentData);
        }
    }
    for(User user : userList){
        ObjectNode userData = JsonUtil.getObjectMapper().createObjectNode();
        if(user.getDeptCode() == null || user.getDeptCode().equals("")){
            userData.put("id", user.getKeycode());
            userData.put("name", user.getName());
            userData.put("pid", user.getDeptCode());
            userArray.add(userData);
        }
    }
    data.set("departmentArray",departmentArray);
    data.set("userArray",userArray);
    return BaseResultData.success(data);
}

public ArrayNode departmentChild(String departmentCode){
    ArrayNode deptChildArray = JsonUtil.getObjectMapper().createArrayNode();
    for(Department department : departmentList){
        ObjectNode deptChild = JsonUtil.getObjectMapper().createObjectNode();
        if(department.getParentCode().equals(departmentCode)){
            deptChild.put("id", department.getKeycode());
            deptChild.put("name", department.getName());
            deptChild.put("pid", department.getParentCode());
            deptChild.set("deptChildList", departmentChild(department.getKeycode()));
            deptChild.set("userChildList", userChild(department.getKeycode()));
            deptChildArray.add(deptChild);
        }
    }
    return deptChildArray;
}

public ArrayNode userChild(String departmentCode){
    ArrayNode userChildArray = JsonUtil.getObjectMapper().createArrayNode();
    for(User user : userList){
        ObjectNode userChild = JsonUtil.getObjectMapper().createObjectNode();
        if(user.getDeptCode().equals(departmentCode)){
            userChild.put("id", user.getKeycode());
            userChild.put("name", user.getName());
            userChild.put("pid", user.getDeptCode());
            userChildArray.add(userChild);
        }
    }
    return userChildArray;
}
相关标签: JAVA 迭代树