41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"hyapp/pkg/xerr"
|
|
userdomain "hyapp/services/user-service/internal/domain/user"
|
|
)
|
|
|
|
const maxAdminProfileBatchSize = 500
|
|
|
|
// BatchGetUserAdminProfiles 一次读取后台列表所需的 user-service owner 事实。
|
|
// 调用方必须按返回 map 的 key 处理缺失用户,不能用请求 ID 构造占位资料。
|
|
func (s *Service) BatchGetUserAdminProfiles(ctx context.Context, userIDs []int64) (map[int64]userdomain.AdminProfile, error) {
|
|
if len(userIDs) == 0 {
|
|
return map[int64]userdomain.AdminProfile{}, nil
|
|
}
|
|
if s.userRepository == nil {
|
|
return nil, xerr.New(xerr.Unavailable, "user repository is not configured")
|
|
}
|
|
|
|
// 去重既限制 SQL IN 参数数量,也保证同一个用户的角色与封禁事实只聚合一次。
|
|
unique := make([]int64, 0, len(userIDs))
|
|
seen := make(map[int64]struct{}, len(userIDs))
|
|
for _, userID := range userIDs {
|
|
if userID <= 0 {
|
|
return nil, xerr.New(xerr.InvalidArgument, "user_ids contains invalid value")
|
|
}
|
|
if _, exists := seen[userID]; exists {
|
|
continue
|
|
}
|
|
seen[userID] = struct{}{}
|
|
unique = append(unique, userID)
|
|
}
|
|
if len(unique) > maxAdminProfileBatchSize {
|
|
return nil, xerr.New(xerr.InvalidArgument, "user_ids exceeds batch limit")
|
|
}
|
|
|
|
return s.userRepository.BatchGetUserAdminProfiles(ctx, unique, s.now().UTC().UnixMilli())
|
|
}
|