feat(admin): 实现管理员账号搜索功能优化

- 搜索账号接口添加新版本标识
- 重构搜索逻辑,使用流式过滤替代模糊查询
- 优化部门层级绑定逻辑,支持更精确的树形结构构建
- 添加子节点递归处理功能,完善用户组织架构展示
- 修复部门与用户关联关系的数据转换问题
- 优化集合操作提高搜索性能
This commit is contained in:
曹鹏飞 2026-01-16 13:54:05 +08:00
parent 591d9cd518
commit 5ea168155f
3 changed files with 68 additions and 34 deletions

View File

@ -373,7 +373,7 @@ public class AdminUserController extends ControllerBase {
} }
/** /**
* 搜索账号 * 搜索账号
* @param request 请求参数 * @param request 请求参数
*/ */
@PostMapping("searchAccountNew") @PostMapping("searchAccountNew")

View File

@ -5,6 +5,7 @@ import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -83,5 +84,5 @@ public class AdminUserVO {
@JsonProperty("isGongfu") @JsonProperty("isGongfu")
private boolean isGongfu; private boolean isGongfu;
private List<AdminUserVO> children; private List<AdminUserVO> children = new ArrayList<>();
} }

View File

@ -443,34 +443,66 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
if (CollectionUtil.isEmpty(adminUsers)) { if (CollectionUtil.isEmpty(adminUsers)) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<AdminUserVO> userVOS = convert(roots, department, positions, roleMaps, roles); List<AdminUserVO> rootUserVOS = convert(roots, department, positions, roleMaps, roles);
getChildren(userVOS.get(0), departments, adminUsers, positions, roleMaps, roles); getChildren(rootUserVOS.get(0), departments, adminUsers, positions, roleMaps, roles);
for (int index = 1, size = userVOS.size(); index < size; index++) { for (int index = 1, size = rootUserVOS.size(); index < size; index++) {
userVOS.get(index).setChildren(userVOS.get(0).getChildren()); rootUserVOS.get(index).setChildren(rootUserVOS.get(0).getChildren());
} }
return userVOS; return rootUserVOS;
} else { } else {
List<AdminUser> adminUsers = lambdaQuery() List<AdminUser> adminUsers = lambdaQuery()
.eq(AdminUser::getIsDel, false) .eq(AdminUser::getIsDel, false)
.like(StrUtil.isNotBlank(request.getLoginName()), AdminUser::getLoginName, request.getLoginName())
.like(StrUtil.isNotBlank(request.getUserName()), AdminUser::getUserName, request.getUserName())
.eq(request.getState() != null, AdminUser::getState, request.getState())
.list(); .list();
if (CollectionUtil.isEmpty(adminUsers)) { List<AdminUser> searchUsers = adminUsers.stream()
.filter(u -> (StrUtil.isBlank(request.getLoginName()) || StrUtil.contains(u.getLoginName(), request.getLoginName()))
&& (StrUtil.isBlank(request.getUserName()) || StrUtil.contains(u.getUserName(), request.getUserName()))
&& (request.getState() == null || Objects.equals(u.getState(), request.getState()))
)
.collect(Collectors.toList());
if (CollectionUtil.isEmpty(searchUsers)) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<AdminUserVO> userVOS = convert1(adminUsers, departments, positions, roleMaps, roles); Set<TBaseDepartment> searchDepartments = departments.stream()
Set<TBaseDepartment> departmentVOS = departments.stream() .filter(d -> searchUsers.stream().map(AdminUser::getDepartmentId).anyMatch(dt -> Objects.equals(d.getId(), dt)))
.filter(d -> userVOS.stream().map(AdminUserVO::getDepartmentId).anyMatch(dt -> Objects.equals(d.getId(), dt)))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
departmentVOS.forEach(d -> bindParent(d, departmentVOS, departments)); searchDepartments.forEach(d -> bindParent(d, searchDepartments, departments));
List<TBaseDepartment> rootDepartments = departmentVOS.stream() List<TBaseDepartment> rootDepartments = searchDepartments.stream()
.filter(d -> d.getDeptParentId() == 0) .filter(d -> d.getDeptParentId() == 0)
.collect(Collectors.toList()); .collect(Collectors.toList());
// List<AdminUserVO> List<AdminUser> rootUsers = adminUsers.stream()
// rootDepartments.forEach(d -> getChildren1(d, departmentVOS, adminUsers, positions, roleMaps, roles)); .filter(u -> rootDepartments.stream()
//TODO 绑定树形结构 .map(TBaseDepartment::getId).anyMatch(dt -> Objects.equals(u.getDepartmentId(), dt))
return userVOS; ).collect(Collectors.toList());
List<AdminUserVO> rootUserVOS = convert1(rootUsers, departments, positions, roleMaps, roles);
rootUserVOS.forEach(uvo -> getChildren1(uvo, departments, adminUsers, searchUsers, positions, roleMaps, roles));
return rootUserVOS;
}
}
private void getChildren1(AdminUserVO user, List<TBaseDepartment> departments, List<AdminUser> users, List<AdminUser> searchUsers
, List<TBasePosition> positions, List<AdminUserRoleMap> roleMaps, List<AdminRole> roles) {
List<TBaseDepartment> cdepartments = departments.stream()
.filter(d -> Objects.equals(d.getDeptParentId(), user.getDepartmentId()))
.collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(cdepartments)) {
cdepartments.forEach(department -> {
List<AdminUser> csers = searchUsers.stream()
.filter(u -> Objects.equals(u.getDepartmentId(), department.getId()))
.collect(Collectors.toList());
if (CollectionUtil.isEmpty(csers)) {
csers = users.stream()
.filter(u -> Objects.equals(u.getDepartmentId(), department.getId()))
.collect(Collectors.toList());
}
if (CollectionUtil.isNotEmpty(csers)) {
List<AdminUserVO> cuserVOS = convert(csers, department, positions, roleMaps, roles);
user.getChildren().addAll(cuserVOS);
getChildren(cuserVOS.get(0), departments, users, positions, roleMaps, roles);
for (int index = 1, size = cuserVOS.size(); index < size; index++) {
cuserVOS.get(index).setChildren(cuserVOS.get(0).getChildren());
}
}
});
} }
} }
@ -540,22 +572,23 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
private void getChildren(AdminUserVO user, List<TBaseDepartment> departments, List<AdminUser> users private void getChildren(AdminUserVO user, List<TBaseDepartment> departments, List<AdminUser> users
, List<TBasePosition> positions, List<AdminUserRoleMap> roleMaps, List<AdminRole> roles) { , List<TBasePosition> positions, List<AdminUserRoleMap> roleMaps, List<AdminRole> roles) {
TBaseDepartment department = departments.stream() List<TBaseDepartment> cdepartments = departments.stream()
.filter(d -> Objects.equals(d.getDeptParentId(), user.getDepartmentId())) .filter(d -> Objects.equals(d.getDeptParentId(), user.getDepartmentId()))
.findFirst() .collect(Collectors.toList());
.orElse(null); if (CollectionUtil.isNotEmpty(cdepartments)) {
if (Objects.nonNull(department)) { cdepartments.forEach(department -> {
List<AdminUser> csers = users.stream() List<AdminUser> csers = users.stream()
.filter(u -> Objects.equals(u.getDepartmentId(), department.getId())) .filter(u -> Objects.equals(u.getDepartmentId(), department.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(csers)) { if (CollectionUtil.isNotEmpty(csers)) {
List<AdminUserVO> cuserVOS = convert(csers, department, positions, roleMaps, roles); List<AdminUserVO> cuserVOS = convert(csers, department, positions, roleMaps, roles);
user.setChildren(cuserVOS); user.getChildren().addAll(cuserVOS);
getChildren(cuserVOS.get(0), departments, users, positions, roleMaps, roles); getChildren(cuserVOS.get(0), departments, users, positions, roleMaps, roles);
for (int index = 1, size = cuserVOS.size(); index < size; index++) { for (int index = 1, size = cuserVOS.size(); index < size; index++) {
cuserVOS.get(index).setChildren(cuserVOS.get(0).getChildren()); cuserVOS.get(index).setChildren(cuserVOS.get(0).getChildren());
}
} }
} });
} }
} }
} }