优化 List 去重操作

v1.4.1
hiparker 4 years ago
parent 4b6d6fddc8
commit d7b424d3a7

@ -0,0 +1,91 @@
package org.opsli.common.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
/**
* @BelongsProject: think-bboss-parent
* @BelongsPackage: com.think.bboss.common.utils
* @Author: Parker
* @CreateTime: 2021-01-05 14:26
* @Description: List
*/
@Slf4j
public final class ListDistinctUtil {
/**
*
* String Integer
*
* @param list
* @return List
*/
public static <T> List<T> distinct(List<T> list) {
if(CollUtil.isEmpty(list)){
return ListUtil.empty();
}
List<T> distinctList;
try {
distinctList = list.stream()
.distinct()
.collect(Collectors.toList());
}catch (Exception e){
log.error(e.getMessage(), e);
return list;
}
if(CollUtil.isEmpty(distinctList)){
return ListUtil.empty();
}
return distinctList;
}
/**
*
* Object
*
* @param list
* @param comparator
* @return List
*/
public static <T> List<T> distinct(List<T> list, Comparator<T> comparator) {
if(CollUtil.isEmpty(list)){
return ListUtil.empty();
}
List<T> distinctList;
try {
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
distinctList = list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(comparator)
),
ArrayList::new
)
);
}catch (Exception e){
log.error(e.getMessage(), e);
return list;
}
if(CollUtil.isEmpty(distinctList)){
return ListUtil.empty();
}
return distinctList;
}
// ==========================
private ListDistinctUtil(){}
}

@ -16,7 +16,7 @@ import java.nio.charset.StandardCharsets;
* @BelongsPackage: com.think.bboss.common.utils
* @Author: Parker
* @CreateTime: 2021-01-05 14:26
* @Description:
* @Description: OutputStream
*/
@Slf4j
public final class OutputStreamUtil {

@ -113,8 +113,6 @@
and c.type in ( '1', '3' )
and c.deleted = '0'
and c.hidden = '0'
<!-- 2020-12-11 修复用户多角色菜单重复BUG -->
group by c.id
</select>
<!-- 根据用户id查询菜单列表-->
@ -142,8 +140,6 @@
and a.user_id = #{userId}
and c.deleted = '0'
and c.hidden = '0'
<!-- 2020-12-11 修复用户多角色菜单重复BUG -->
group by c.id
</select>
<update id="updatePassword" parameterType="org.opsli.api.wrapper.system.user.UserPassword">

@ -15,11 +15,14 @@
*/
package org.opsli.modulars.system.user.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
@ -30,8 +33,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
@ -51,15 +52,23 @@ public class UserRoleRefServiceImpl extends ServiceImpl<UserRoleRefMapper, SysUs
@Override
public List<String> getUserIdListByRoleId(String roleId) {
List<String> users = mapper.getUserIdListByRoleId(roleId);
if(CollUtil.isEmpty(users)){
return ListUtil.empty();
}
// 去重
return new ArrayList<>(new LinkedHashSet<>(users));
return ListDistinctUtil.distinct(users);
}
@Override
public List<String> getUserIdListByMenuId(String roleId) {
List<String> users = mapper.getUserIdListByMenuId(roleId);
if(CollUtil.isEmpty(users)){
return ListUtil.empty();
}
// 去重
return new ArrayList<>(new LinkedHashSet<>(users));
return ListDistinctUtil.distinct(users);
}
@Override

@ -16,6 +16,7 @@
package org.opsli.modulars.system.user.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -23,6 +24,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.api.wrapper.system.user.UserAndOrgModel;
@ -32,6 +34,7 @@ import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.HumpUtil;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.msg.CoreMsg;
@ -55,7 +58,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
@ -420,14 +426,14 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
public List<String> getRoleCodeList(String userId) {
List<String> roles = mapper.getRoleCodeList(userId);
// 去重
return new ArrayList<>(new LinkedHashSet<>(roles));
return ListDistinctUtil.distinct(roles);
}
@Override
public List<String> getRoleIdList(String userId) {
List<String> roles = mapper.getRoleIdList(userId);
// 去重
return new ArrayList<>(new LinkedHashSet<>(roles));
return ListDistinctUtil.distinct(roles);
}
@Override
@ -435,7 +441,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
UserModel userModel = this.get(userId);
if(userModel == null){
return new ArrayList<>();
return ListUtil.empty();
}
List<String> perms;
@ -457,7 +463,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
}
// 去重
return new ArrayList<>(new LinkedHashSet<>(perms));
return ListDistinctUtil.distinct(perms);
}
@Override
@ -465,11 +471,10 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
UserModel userModel = this.get(userId);
if(userModel == null){
return new ArrayList<>();
return ListUtil.empty();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
@ -482,7 +487,11 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
menuList = mapper.findMenuListByUserId(userId);
}
return WrapperUtil.transformInstance(menuList, MenuModel.class);
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
List<SysMenu> distinctList = ListDistinctUtil.distinct(
menuList, Comparator.comparing(ApiWrapper::getId));
return WrapperUtil.transformInstance(distinctList, MenuModel.class);
}
@Override
@ -490,11 +499,10 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
UserModel userModel = this.get(userId);
if(userModel == null){
return new ArrayList<>();
return ListUtil.empty();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
@ -506,7 +514,15 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
menuList = mapper.findMenuAllListByUserId(userId);
}
return WrapperUtil.transformInstance(menuList, MenuModel.class);
if(CollUtil.isEmpty(menuList)){
return ListUtil.empty();
}
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
List<SysMenu> distinctList = ListDistinctUtil.distinct(
menuList, Comparator.comparing(ApiWrapper::getId));
return WrapperUtil.transformInstance(distinctList, MenuModel.class);
}
@Override

Loading…
Cancel
Save