迁移部门表,没有ancestors字段,若依部门权限没办法做,写了一段代码维护ancestors字段。
调用的若依框架组成树的方法,拼接ancestors
java
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);
}
java
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());
}
}
}
java
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;
}