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 请求参数
*/
@PostMapping("searchAccountNew")

View File

@ -5,6 +5,7 @@ import lombok.Data;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -83,5 +84,5 @@ public class AdminUserVO {
@JsonProperty("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)) {
return Collections.emptyList();
}
List<AdminUserVO> userVOS = convert(roots, department, positions, roleMaps, roles);
getChildren(userVOS.get(0), departments, adminUsers, positions, roleMaps, roles);
for (int index = 1, size = userVOS.size(); index < size; index++) {
userVOS.get(index).setChildren(userVOS.get(0).getChildren());
List<AdminUserVO> rootUserVOS = convert(roots, department, positions, roleMaps, roles);
getChildren(rootUserVOS.get(0), departments, adminUsers, positions, roleMaps, roles);
for (int index = 1, size = rootUserVOS.size(); index < size; index++) {
rootUserVOS.get(index).setChildren(rootUserVOS.get(0).getChildren());
}
return userVOS;
return rootUserVOS;
} else {
List<AdminUser> adminUsers = lambdaQuery()
.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();
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();
}
List<AdminUserVO> userVOS = convert1(adminUsers, departments, positions, roleMaps, roles);
Set<TBaseDepartment> departmentVOS = departments.stream()
.filter(d -> userVOS.stream().map(AdminUserVO::getDepartmentId).anyMatch(dt -> Objects.equals(d.getId(), dt)))
Set<TBaseDepartment> searchDepartments = departments.stream()
.filter(d -> searchUsers.stream().map(AdminUser::getDepartmentId).anyMatch(dt -> Objects.equals(d.getId(), dt)))
.collect(Collectors.toSet());
departmentVOS.forEach(d -> bindParent(d, departmentVOS, departments));
List<TBaseDepartment> rootDepartments = departmentVOS.stream()
searchDepartments.forEach(d -> bindParent(d, searchDepartments, departments));
List<TBaseDepartment> rootDepartments = searchDepartments.stream()
.filter(d -> d.getDeptParentId() == 0)
.collect(Collectors.toList());
// List<AdminUserVO>
// rootDepartments.forEach(d -> getChildren1(d, departmentVOS, adminUsers, positions, roleMaps, roles));
//TODO 绑定树形结构
return userVOS;
List<AdminUser> rootUsers = adminUsers.stream()
.filter(u -> rootDepartments.stream()
.map(TBaseDepartment::getId).anyMatch(dt -> Objects.equals(u.getDepartmentId(), dt))
).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
, 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()))
.findFirst()
.orElse(null);
if (Objects.nonNull(department)) {
List<AdminUser> 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.setChildren(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());
.collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(cdepartments)) {
cdepartments.forEach(department -> {
List<AdminUser> 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());
}
}
}
});
}
}
}