提交 1e80b668 编写于 作者: Tyl666's avatar Tyl666

去重角色父子级关系

上级 d731de26
package cn.ibizlab.core.uaa.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.core.uaa.domain.SysRole;
......@@ -30,13 +20,11 @@ import cn.ibizlab.core.uaa.filter.SysRoleSearchContext;
import cn.ibizlab.core.uaa.service.ISysRoleService;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.helper.DEFieldCacheMap;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ibizlab.core.uaa.mapper.SysRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
......@@ -57,6 +45,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Lazy
protected cn.ibizlab.core.uaa.service.ISysUserRoleService sysuserroleService;
List<SysRole> result = new ArrayList<>();
protected int batchSize = 500;
@Override
......@@ -194,9 +183,78 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
*/
@Override
public Page<SysRole> searchNoRepeat(SysRoleSearchContext context) {
List<SysRole> newResult = new CopyOnWriteArrayList<>();
String sysRoleid = (String) context.getParams().get("sys_roleid");
com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRole> pages=baseMapper.searchNoRepeat(context.getPages(),context,context.getSelectCond());
return new PageImpl<SysRole>(pages.getRecords(), context.getPageable(), pages.getTotal());
List<SysRole> sysRoles = new ArrayList<>();
SysRole currentSysRole = baseMapper.selectById(sysRoleid);
if(!ObjectUtils.isEmpty(currentSysRole)){
sysRoles.add(currentSysRole);
selectChild(sysRoles);
// 加入父子级关联对象
newResult.addAll(findPRelation(sysRoleid,new ArrayList<SysRole>()));
newResult.addAll(result);
result.clear();
}
for (int i=0;i<newResult.size();i++){
for (int j=0;j<pages.getRecords().size();j++){
if (pages.getRecords().get(j).getRoleid().equals(newResult.get(i).getRoleid())){
pages.getRecords().remove(j);
}
}
}
return new PageImpl<SysRole>(pages.getRecords(), context.getPageable(), pages.getTotal()-newResult.size());
}
/**
*
* @param contextId
* @return
*/
private List<SysRole> findPRelation(String contextId,List<SysRole> pList){
SysRole currentSysRole = baseMapper.selectById(contextId);
// 查出当前id的所有父级别id
if (!ObjectUtils.isEmpty(currentSysRole)){
String proleId = currentSysRole.getProleid();
if(!StringUtils.isEmpty(proleId)){
pList.add(currentSysRole);
return findPRelation(proleId,pList);
}else {
return pList;
}
}
return null;
}
/**
* 获取某个父节点下面的所有子节点
* @return
*/
public void selectChild(List<SysRole> ids){
//用来存取调用自身递归时的参数
List<SysRole> temp= new ArrayList<SysRole>();
//查询数据库中对应id的实体类
List<SysRole> sysEnterpriseOrgList = new ArrayList<SysRole>();
//遍历传递过来的参数ids
for (SysRole sRole :ids) {
//查询子级架构
sysEnterpriseOrgList= selectByProleid(sRole.getRoleid());
//遍历list获取符合条件的对象的id值,一份存到temp中用作递归的参数,并存到全局变量中用来获取所有符合条件的id
for (SysRole s:sysEnterpriseOrgList) {
temp.add(s);
result.add(s);
}
}
if(temp.size()!=0&&temp!=null){
selectChild(temp);
}
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册