Java遞歸獲取部門樹 返回ztree數據
阿新 • • 發佈:2018-10-23
stat nod als array response java equal asp tree
@GetMapping("/getDept")
@ResponseBody
public Tree<DeptDO> getDept(String deptId){
Tree<DeptDO> deptNode = getDeptNode(deptId);
if (deptNode == null){
return null;
}
List<Tree<DeptDO>> childNode = getChildNode(deptId);
for (Tree<DeptDO> child:childNode) {
Tree<DeptDO> node = getDept(child.getId());
deptNode.getChildren().add(node);
deptNode.setChildren(true);
}
return deptNode;
}
@Autowired
private DeptDao sysDeptMapper;
private Tree<DeptDO> getDeptNode(String deptId){
Map<String,Object> query = new HashMap<>();
query.put("deptId",deptId);
List<DeptDO> deptList =sysDeptMapper.list(query);
if (deptList.size() == 1){
DeptDO sysDept = deptList.get(0);
Tree<DeptDO> tree = getDeptTree(sysDept);
return tree;
}else{
return null;
}
}
private List<Tree<DeptDO>> getChildNode(String deptId){
List<Tree<DeptDO>> trees = new ArrayList<>();
Map<String,Object> query = new HashMap<>();
query.put("parentId",deptId);
List<DeptDO> sysDepts = sysDeptMapper.list(query);
for (DeptDO sysDept : sysDepts) {
Tree<DeptDO> tree = getDeptTree(sysDept);
trees.add(tree);
}
return trees;
}
private Tree<DeptDO> getDeptTree(DeptDO sysDept){
Tree<DeptDO> tree = new Tree<>();
tree.setId(sysDept.getDeptId().toString());
tree.setParentId(sysDept.getParentId().toString());
tree.setText(sysDept.getName());
Map<String, Object> state = new HashMap<>(16);
state.put("opened", true);
tree.setState(state);
if (!("0".equals(tree.getParentId()))){
tree.setHasParent(true);
}
return tree;
}
Java遞歸獲取部門樹 返回ztree數據