From dec0f08d24035c816330eee64567884e17490b57 Mon Sep 17 00:00:00 2001 From: iamhujiebin <458249864@qq.com> Date: Mon, 24 Jul 2023 22:50:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=89=B9=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/user_m/vip.go | 29 +++++++++++++++++++++++++---- test/user_test.go | 9 +++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/domain/model/user_m/vip.go b/domain/model/user_m/vip.go index 2b06dd7..9e231d5 100644 --- a/domain/model/user_m/vip.go +++ b/domain/model/user_m/vip.go @@ -2,6 +2,7 @@ package user_m import ( "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/resource/mysql" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -59,14 +60,34 @@ func GetVip(db *gorm.DB, userId uint64) (*UserVip, error) { return nil, nil } +// εˆ†ζ‰ΉθŽ·ε– func BatchGetVips(userIds []uint64) (map[uint64]*int64, error) { + result := make(map[uint64]*int64, 0) rows := make([]UserVip, 0) - err := mysql.Db.Where("user_id IN ?", userIds).Find(&rows).Error - if err != nil { - return nil, err + end := 500 + if end > len(userIds) { + end = len(userIds) + } + start := 0 + for end <= len(userIds) { + if end > len(userIds) { + end = len(userIds) + } + tmp := make([]UserVip, 0) + err := mysql.Db.Where("user_id IN ?", userIds).Find(&tmp).Error + if err != nil { + return nil, err + } + if err != nil { + return result, err + } else { + rows = append(rows, tmp...) + } + start += 500 + end += 500 + mylogrus.MyLog.Infof("BatchGetVips start:%v-end:%v", start, end) } - result := make(map[uint64]*int64, 0) for _, i := range userIds { result[i] = nil } diff --git a/test/user_test.go b/test/user_test.go index 2c55f54..b4b8e57 100644 --- a/test/user_test.go +++ b/test/user_test.go @@ -14,3 +14,12 @@ func TestGetUserMap(t *testing.T) { res, err := user_m.GetUserMapByIds(domain.CreateModelNil(), userIds) t.Logf("%v-%v", res, err) } + +func TestGetUserVipMap(t *testing.T) { + var userIds []uint64 + for i := 0; i < 15000; i++ { + userIds = append(userIds, 7642) + } + res, err := user_m.BatchGetVips(userIds) + t.Logf("%v-%v", res, err) +} -- 2.22.0