Skip to content

Commit

Permalink
#2469 用户列表查询db优化
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjiawei committed Nov 22, 2024
1 parent 7e32aab commit 4774ea5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
20 changes: 20 additions & 0 deletions platform-auth-server/service/db/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,26 @@ func (UserRoleRsRepository) FindAllByRoleId(roleId string) ([]*model.UserRoleRsE
return userRoleRsList, nil
}

func (UserRoleRsRepository) FindAllUserRole() ([]*model.UserRoleRsEntity, error) {
var userRoleRsList []*model.UserRoleRsEntity
// 分页查询
pageSize := 1000
var offset int
for {
var subUserRoles []*model.UserRoleRsEntity
err := Engine.Where("is_deleted = ?", false).Limit(pageSize, offset).Find(&subUserRoles)
if err != nil {
return nil, err
}
if len(subUserRoles) == 0 {
break
}
userRoleRsList = append(userRoleRsList, subUserRoles...)
offset += pageSize
}
return userRoleRsList, nil
}

func (UserRoleRsRepository) FindAllByUserId(userId string) ([]*model.UserRoleRsEntity, error) {
var userRoleRsList []*model.UserRoleRsEntity
err := Engine.Where("user_id = ?", userId).And("is_deleted = ?", false).Find(&userRoleRsList)
Expand Down
40 changes: 25 additions & 15 deletions platform-auth-server/service/user_management_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,35 +771,45 @@ func (UserManagementService) RetrieveAllActiveUsers() ([]*model.SimpleLocalUserD
return result, nil
}

var allUserRoles []*model.UserRoleRsEntity
if allUserRoles, err = db.UserRoleRsRepositoryInstance.FindAllUserRole(); err != nil {
log.Logger.Error("failed to find all user roles", log.Error(err))
return nil, err
}
var userRolesMap = make(map[string][]*model.UserRoleRsEntity)
for _, userRole := range allUserRoles {
if arr, ok := userRolesMap[userRole.UserId]; ok {
arr = append(arr, userRole)
} else {
userRolesMap[userRole.UserId] = []*model.UserRoleRsEntity{userRole}
}
}
// 增加map,减少db查询次数
var existRoleIdMap = make(map[string]bool)
var found bool
for _, user := range userEntities {
userDto := convertToSimpleLocalUserDto(user, "")

userRoles, err := db.UserRoleRsRepositoryInstance.FindAllByUserId(user.Id)
if err != nil {
log.Logger.Error("failed to find all UserRoleRs", log.String("userId", user.Id),
log.Error(err))
return nil, err
}
userRoles := userRolesMap[user.Id]
if len(userRoles) > 0 {
for _, userRole := range userRoles {

found = false
role := &model.SysRoleEntity{}
found, err := db.Engine.ID(userRole.RoleId).Get(role)
if err != nil {
log.Logger.Error("failed to get role", log.String("roleId", userRole.RoleId),
log.Error(err))
return nil, err
if found = existRoleIdMap[userRole.RoleId]; !found {
found, err = db.Engine.ID(userRole.RoleId).Get(role)
if err != nil {
log.Logger.Error("failed to get role", log.String("roleId", userRole.RoleId), log.Error(err))
return nil, err
}
}

if found {
existRoleIdMap[userRole.RoleId] = true
roleDto := &model.SimpleLocalRoleDto{
ID: role.Id,
DisplayName: role.DisplayName,
Name: role.Name,
Email: role.EmailAddress,
Status: role.GetRoleDeletedStatus(),
}

userDto.AddRoles([]*model.SimpleLocalRoleDto{roleDto})
}
}
Expand Down

0 comments on commit 4774ea5

Please sign in to comment.