From d707c3a5e68819d958f5012ebb9f965d90e9a61d Mon Sep 17 00:00:00 2001 From: tangjiawei Date: Fri, 22 Nov 2024 17:31:24 +0800 Subject: [PATCH] =?UTF-8?q?#2469=20=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2db=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform-auth-server/service/db/repository.go | 20 ++++++++++ .../service/user_management_service.go | 40 ++++++++++++------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/platform-auth-server/service/db/repository.go b/platform-auth-server/service/db/repository.go index fcfb33786c..4ec1c9eb4d 100644 --- a/platform-auth-server/service/db/repository.go +++ b/platform-auth-server/service/db/repository.go @@ -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) diff --git a/platform-auth-server/service/user_management_service.go b/platform-auth-server/service/user_management_service.go index 15339a5d1a..7eefd1e7d0 100644 --- a/platform-auth-server/service/user_management_service.go +++ b/platform-auth-server/service/user_management_service.go @@ -771,27 +771,38 @@ 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, @@ -799,7 +810,6 @@ func (UserManagementService) RetrieveAllActiveUsers() ([]*model.SimpleLocalUserD Email: role.EmailAddress, Status: role.GetRoleDeletedStatus(), } - userDto.AddRoles([]*model.SimpleLocalRoleDto{roleDto}) } }