diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ListDistinctUtil.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ListDistinctUtil.java new file mode 100644 index 0000000..c01ecdd --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ListDistinctUtil.java @@ -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 List distinct(List list) { + if(CollUtil.isEmpty(list)){ + return ListUtil.empty(); + } + + List 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 List distinct(List list, Comparator comparator) { + if(CollUtil.isEmpty(list)){ + return ListUtil.empty(); + } + + List 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(){} + +} diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/OutputStreamUtil.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/OutputStreamUtil.java index 275ec84..c820483 100644 --- a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/OutputStreamUtil.java +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/OutputStreamUtil.java @@ -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 { diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/mapper/xml/UserMapper.xml b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/mapper/xml/UserMapper.xml index 0b78f37..d6a5ae7 100644 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/mapper/xml/UserMapper.xml +++ b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/mapper/xml/UserMapper.xml @@ -113,8 +113,6 @@ and c.type in ( '1', '3' ) and c.deleted = '0' and c.hidden = '0' - - group by c.id @@ -142,8 +140,6 @@ and a.user_id = #{userId} and c.deleted = '0' and c.hidden = '0' - - group by c.id diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserRoleRefServiceImpl.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserRoleRefServiceImpl.java index 1ffac72..22d5a41 100644 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserRoleRefServiceImpl.java +++ b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserRoleRefServiceImpl.java @@ -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 getUserIdListByRoleId(String roleId) { List users = mapper.getUserIdListByRoleId(roleId); + if(CollUtil.isEmpty(users)){ + return ListUtil.empty(); + } + // 去重 - return new ArrayList<>(new LinkedHashSet<>(users)); + return ListDistinctUtil.distinct(users); } @Override public List getUserIdListByMenuId(String roleId) { List users = mapper.getUserIdListByMenuId(roleId); + if(CollUtil.isEmpty(users)){ + return ListUtil.empty(); + } + // 去重 - return new ArrayList<>(new LinkedHashSet<>(users)); + return ListDistinctUtil.distinct(users); } @Override diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserServiceImpl.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserServiceImpl.java index 6cd8b3b..65b020e 100644 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserServiceImpl.java +++ b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/service/impl/UserServiceImpl.java @@ -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 getRoleCodeList(String userId) { List roles = mapper.getRoleCodeList(userId); // 去重 - return new ArrayList<>(new LinkedHashSet<>(roles)); + return ListDistinctUtil.distinct(roles); } @Override public List getRoleIdList(String userId) { List roles = mapper.getRoleIdList(userId); // 去重 - return new ArrayList<>(new LinkedHashSet<>(roles)); + return ListDistinctUtil.distinct(roles); } @Override @@ -435,7 +441,7 @@ public class UserServiceImpl extends CrudServiceImpl(); + return ListUtil.empty(); } List perms; @@ -457,7 +463,7 @@ public class UserServiceImpl extends CrudServiceImpl(new LinkedHashSet<>(perms)); + return ListDistinctUtil.distinct(perms); } @Override @@ -465,11 +471,10 @@ public class UserServiceImpl extends CrudServiceImpl(); + return ListUtil.empty(); } List menuList; - // 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限 if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){ QueryBuilder queryBuilder = new GenQueryBuilder<>(); @@ -482,7 +487,11 @@ public class UserServiceImpl extends CrudServiceImpl distinctList = ListDistinctUtil.distinct( + menuList, Comparator.comparing(ApiWrapper::getId)); + + return WrapperUtil.transformInstance(distinctList, MenuModel.class); } @Override @@ -490,11 +499,10 @@ public class UserServiceImpl extends CrudServiceImpl(); + return ListUtil.empty(); } List menuList; - // 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限 if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){ QueryBuilder queryBuilder = new GenQueryBuilder<>(); @@ -506,7 +514,15 @@ public class UserServiceImpl extends CrudServiceImpl distinctList = ListDistinctUtil.distinct( + menuList, Comparator.comparing(ApiWrapper::getId)); + + return WrapperUtil.transformInstance(distinctList, MenuModel.class); } @Override