本文主要是介绍若依 ruoyi-vue 维护Ancestors字段 树转换成List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
迁移部门表,没有ancestors字段,若依部门权限没办法做,写了一段代码维护ancestors字段。
调用的若依框架组成树的方法,拼接ancestors
public AjaxResult tree() {List<SysDept> depts = deptService.selectDeptTree();for (SysDept dept : depts) {//是顶级节点则设置为if (dept.getParentId().equals(0L)) {dept.setAncestors("0");}if (CollectionUtil.isNotEmpty(dept.getChildren())) {setAncestors(dept.getAncestors(), dept.getChildren());}}List<SysDept> sysDepts = treeToList(depts);for (SysDept sysDept : sysDepts) {deptService.updateDept(sysDept);}return success(depts);}
public void setAncestors(String parentAncestors, List<SysDept> children) {for (SysDept child : children) {// 将父节点的祖先节点字符串和子部门的父节点ID连接起来,以逗号分隔String ancestors = parentAncestors + "," + child.getParentId();// 设置当前子部门的祖先节点字符串child.setAncestors(ancestors);// 如果当前子部门有子部门,则递归调用该方法if (CollectionUtil.isNotEmpty(child.getChildren())) {setAncestors(ancestors, child.getChildren());}}}
public List<SysDept> treeToList(List<SysDept> depts) {List<SysDept> deptList = new ArrayList<>();if (CollectionUtil.isNotEmpty(depts)) {for (SysDept dept : depts) {// 将当前节点添加到列表中deptList.add(dept);// 如果当前节点有子节点,则递归将子节点添加到列表中if (CollectionUtil.isNotEmpty(dept.getChildren())) {deptList.addAll(treeToList(dept.getChildren()));}}}return deptList;}
这篇关于若依 ruoyi-vue 维护Ancestors字段 树转换成List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!