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}) } }