@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;
}
//树结构
public class Tree<T> {
private String id;//节点ID
private String text;//显示节点文本
private Map<String, Object> state;//节点状态,open closed
private boolean checked = false;//节点是否被选中 true false
private Map<String, Object> attributes;//节点属性
private List<Tree<T>> children = new ArrayList<Tree<T>>();//节点的子节点
private String parentId;//父ID
private boolean hasParent = false;//是否有父节点
private boolean hasChildren = false;//是否有子节点
//省略getter setter方法
}
//第2种实现
@Override
public List<Tree> getTree() {
List<Tree> trees = new ArrayList();
List<DeptDO> list = deptDao.list(new HashMap<>());
for (DeptDO d : list) {
if (d.getParentid() == null) {
Tree tree = new Tree();
tree.setId(d.getDeptid().toString());
tree.setText(d.getDeptname());
Map state = new HashMap();
state.put("opened", true);
tree.setState(state);
List<Tree> childTree = getChild(d.getDeptid());
if (childTree.size() > 0) {
tree.setChildren(childTree);
}
trees.add(tree);
}
}
return trees;
}
private List<Tree> getChild(Long parentid) {
List<Tree> trees = new ArrayList();
Map map = new HashMap();
map.put("parentid", parentid);
List<DeptDO> childList = deptDao.list(map);
for (DeptDO d : childList) {
Tree tree = new Tree();
tree.setId(d.getDeptid().toString());
tree.setText(d.getDeptname());
List<Tree> childTree = getChild(d.getDeptid());
if (childTree.size() > 0) {
tree.setChildren(childTree);
}
trees.add(tree);
}
return trees;
}
//jstree json
public class Tree {
private String id;
private String text;
private String icon;
private Map state;
private List<Tree> children;
//省略getter setter方法
}