List<BiUser> userList = thirdPartyApiServiceFactory.getInstance(systemId).getAllUser(systemId, getTenantParam());
List<BiDepartment> departList = thirdPartyApiServiceFactory.getInstance(systemId).getAllDepart(systemId, getTenantParam());
DepartTree departTree = createDepartTree(departList);
List<JSONObject> userJsonList = new ArrayList<>();
List<JSONObject> departJsonList = new ArrayList<>();
Long departId = null;
if(departId == null){
for(DepartNode node:departTree.getRoot().getChildren()){
departJsonList.add(createDepartJson(node.getDepartment()));
}
for(BiUser user:userList){
userJsonList.add(createUserJson(user));
}
}else if(departTree.getDepartNodeMap().containsKey(departId)) {
for(DepartNode node:departTree.getDepartNodeMap().get(departId).getChildren()){
departJsonList.add(createDepartJson(node.getDepartment()));
}
Set<Long> departIdSet = getAllDescendant(departTree,departId,new HashSet<>());
for(BiUser user:userList){
if(departIdSet.contains(user.getDepartId())) {
userJsonList.add(createUserJson(user));
}
}
}
JSONObject rstJson = new JSONObject();
rstJson.put("group",departJsonList);
rstJson.put("user",userJsonList);
class DepartTree{
private DepartNode root = new DepartNode(null);
private Map<Long, DepartNode> departNodeMap = new HashMap<>();
public DepartNode getRoot() {
return root;
}
public Map<Long, DepartNode> getDepartNodeMap() {
return departNodeMap;
}
}
class DepartNode{
private List<DepartNode> children = new ArrayList<>();
private BiDepartment department;
public DepartNode(BiDepartment department) {
this.department = department;
}
public List<DepartNode> getChildren() {
return children;
}
public BiDepartment getDepartment() {
return department;
}
}
private DepartTree createDepartTree(List<BiDepartment> departList){
DepartTree departTree = new DepartTree();
for(BiDepartment depart:departList){
departTree.getDepartNodeMap().put(depart.getId(),new DepartNode(depart));
}
for(BiDepartment depart:departList){
Long parentId = depart.getParentId();
DepartNode parentNode = departTree.getDepartNodeMap().get(parentId);
if(parentNode == null){
parentNode = departTree.getRoot();
}
parentNode.getChildren().add(departTree.getDepartNodeMap().get(depart.getId()));
}
return departTree;
}
private JSONObject createDepartJson(BiDepartment depart){
JSONObject json = new JSONObject();
json.put("id",depart.getId());
json.put("name",depart.getName());
return json;
}
private JSONObject createUserJson(BiUser user){
JSONObject json = new JSONObject();
json.put("userId",user.getId());
json.put("userName",user.getUserName());
return json;
}
private Set<Long> getAllDescendant(DepartTree departTree, Long id, Set<Long> descendant){
if(departTree.getDepartNodeMap().containsKey(id) && !descendant.contains(id)){
descendant.add(id);
for(DepartNode child:departTree.getDepartNodeMap().get(id).getChildren()){
getAllDescendant(departTree,child.getDepartment().getId(),descendant);
}
}
return descendant;
}
部门树型结构