From 7a0f7c7506d26ec1f74c3a547e71fb4879c5a7ce Mon Sep 17 00:00:00 2001 From: hujiebin Date: Mon, 24 Jul 2023 10:47:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=89=B9=E8=8E=B7=E5=8F=96userIds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/user_m/user.go | 24 ++++++++++++++++++++---- test/user_test.go | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/user_test.go diff --git a/domain/model/user_m/user.go b/domain/model/user_m/user.go index 8a0dc8f..4561c1b 100644 --- a/domain/model/user_m/user.go +++ b/domain/model/user_m/user.go @@ -85,12 +85,28 @@ func GetUsersByIds(model *domain.Model, userIds []mysql.ID) ([]User, error) { return users, nil } +// 分批获取userIds func GetUserMapByIds(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]User, error) { - rows, err := GetUsersByIds(model, userIds) - if err != nil { - return nil, err - } result := make(map[mysql.ID]User, 0) + end := 500 + if end > len(userIds) { + end = len(userIds) + } + var rows []User + start := 0 + for end <= len(userIds) { + if end > len(userIds) { + end = len(userIds) + } + tmp, err := GetUsersByIds(model, userIds[start:end]) + if err != nil { + return result, err + } else { + rows = append(rows, tmp...) + } + start += 500 + end += 500 + } for _, i := range rows { result[i.ID] = i } diff --git a/test/user_test.go b/test/user_test.go new file mode 100644 index 0000000..2c55f54 --- /dev/null +++ b/test/user_test.go @@ -0,0 +1,16 @@ +package test + +import ( + "git.hilo.cn/hilo-common/domain" + "hilo-group/domain/model/user_m" + "testing" +) + +func TestGetUserMap(t *testing.T) { + var userIds []uint64 + for i := 0; i < 10502; i++ { + userIds = append(userIds, 7642) + } + res, err := user_m.GetUserMapByIds(domain.CreateModelNil(), userIds) + t.Logf("%v-%v", res, err) +} -- 2.22.0