提交 28d6b2d2 编写于 作者: tangyaologin's avatar tangyaologin

角色查询父子关系代码优化

上级 3587aec3
...@@ -2,6 +2,7 @@ package cn.ibizlab.core.extensions.service; ...@@ -2,6 +2,7 @@ package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.uaa.filter.SysRoleSearchContext; import cn.ibizlab.core.uaa.filter.SysRoleSearchContext;
import cn.ibizlab.core.uaa.service.impl.SysRoleServiceImpl; import cn.ibizlab.core.uaa.service.impl.SysRoleServiceImpl;
import liquibase.pro.packaged.S;
import liquibase.util.StringUtils; import liquibase.util.StringUtils;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -14,6 +15,7 @@ import org.springframework.context.annotation.Primary; ...@@ -14,6 +15,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* 实体[系统角色] 自定义服务对象 * 实体[系统角色] 自定义服务对象
...@@ -22,7 +24,6 @@ import java.util.*; ...@@ -22,7 +24,6 @@ import java.util.*;
@Primary @Primary
@Service("SysRoleExService") @Service("SysRoleExService")
public class SysRoleExService extends SysRoleServiceImpl { public class SysRoleExService extends SysRoleServiceImpl {
List<SysRole> result = new ArrayList<>();
@Override @Override
protected Class currentModelClass() { protected Class currentModelClass() {
...@@ -51,14 +52,20 @@ public class SysRoleExService extends SysRoleServiceImpl { ...@@ -51,14 +52,20 @@ public class SysRoleExService extends SysRoleServiceImpl {
com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRole> pages = baseMapper.searchNoRepeat(context.getPages(),context,context.getSelectCond()); com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRole> pages = baseMapper.searchNoRepeat(context.getPages(),context,context.getSelectCond());
if(!ObjectUtils.isEmpty(sysRoleId)){ if(!ObjectUtils.isEmpty(sysRoleId)){
findSRelation(pages.getRecords(),sysRoleId); List<SysRole> childRole = new ArrayList<>();
List<SysRole> grandSonRole = new ArrayList<>();
findSRelation(childRole,pages.getRecords(),sysRoleId);
grandSonRole.addAll(childRole);
for (SysRole sr : grandSonRole){
findSRelation(childRole,pages.getRecords(),sr.getRoleid());
}
newResult.addAll(childRole);
newResult.addAll(findPRelation(sysRoleId,new ArrayList<>())); newResult.addAll(findPRelation(sysRoleId,new ArrayList<>()));
newResult.addAll(result); newResult = newResult.stream().distinct().collect(Collectors.toList());
result.clear();
} }
for (SysRole sysRole : newResult) { for (int i = 0 ;i < newResult.size();i++){
for (int j = 0; j < pages.getRecords().size(); j++) { for (int j = 0;j < pages.getRecords().size();j++){
if (pages.getRecords().get(j).getRoleid().equals(sysRole.getRoleid())) { if(pages.getRecords().get(j).getRoleid().equals(newResult.get(i).getRoleid())){
pages.getRecords().remove(j); pages.getRecords().remove(j);
} }
} }
...@@ -88,15 +95,16 @@ public class SysRoleExService extends SysRoleServiceImpl { ...@@ -88,15 +95,16 @@ public class SysRoleExService extends SysRoleServiceImpl {
/** /**
* 递归获取某个父节点下的所有子节点 * 递归获取某个父节点下的所有子节点
* @param childRole 返回的结果
* @param roleList 数据库查询出来的所有角色集合 * @param roleList 数据库查询出来的所有角色集合
* @param pId 父id * @param pId 父id
*/ */
private void findSRelation(List<SysRole> roleList, String pId) { private void findSRelation(List<SysRole> childRole,List<SysRole> roleList, String pId) {
for (SysRole role : roleList) { for (SysRole role : roleList) {
if (!StringUtils.isEmpty(role.getProleid())) { if (!StringUtils.isEmpty(role.getProleid())) {
if (role.getProleid().equals(pId)) { if (role.getProleid().equals(pId)) {
findSRelation(roleList, role.getRoleid()); findSRelation(childRole,roleList,role.getRoleid());
result.add(role); childRole.add(role);
} }
} }
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册