feat: 在用户列表展示角色信息
- 分页批量补全用户关联角色名称,避免逐行查询 - 支持多角色标签、溢出数量和完整信息提示
This commit is contained in:
@@ -139,7 +139,51 @@ public class SysAccountController extends BaseCurdController<SysAccountService,
|
||||
@Override
|
||||
@LogRecord("分页查询")
|
||||
protected Page<SysAccount> queryPage(Page<SysAccount> page, QueryWrapper queryWrapper) {
|
||||
return service.getMapper().paginateWithRelations(page, queryWrapper);
|
||||
Page<SysAccount> result = service.getMapper().paginateWithRelations(page, queryWrapper);
|
||||
fillRoleNames(result.getRecords());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按当前分页内的角色 ID 批量补全角色名称。
|
||||
*
|
||||
* @param accounts 当前页账号
|
||||
*/
|
||||
private void fillRoleNames(List<SysAccount> accounts) {
|
||||
if (accounts == null || accounts.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Set<BigInteger> roleIds = accounts.stream()
|
||||
.map(SysAccount::getRoleIds)
|
||||
.filter(java.util.Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.filter(java.util.Objects::nonNull)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
if (roleIds.isEmpty()) {
|
||||
accounts.forEach(account -> account.setRoleNames(List.of()));
|
||||
return;
|
||||
}
|
||||
|
||||
Map<BigInteger, String> roleNameMap = sysRoleService.listByIds(roleIds).stream()
|
||||
.filter(role -> role.getId() != null && StringUtil.hasText(role.getRoleName()))
|
||||
.collect(Collectors.toMap(
|
||||
SysRole::getId,
|
||||
SysRole::getRoleName,
|
||||
(first, ignored) -> first
|
||||
));
|
||||
accounts.forEach(account -> {
|
||||
List<BigInteger> accountRoleIds = account.getRoleIds();
|
||||
if (accountRoleIds == null || accountRoleIds.isEmpty()) {
|
||||
account.setRoleNames(List.of());
|
||||
return;
|
||||
}
|
||||
List<String> roleNames = accountRoleIds.stream()
|
||||
.map(roleNameMap::get)
|
||||
.filter(StringUtil::hasText)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
account.setRoleNames(roleNames);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user