From 99c1c3d17690995ee4220b023c4f9145c99118f7 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 14 Nov 2023 14:54:20 +0800 Subject: [PATCH] user --- pkg/common/db/controller/msg_test.go | 2 +- pkg/common/db/controller/user.go | 2 +- pkg/common/db/newmgo/mgotool/tool.go | 81 ++++++++++++++++++++++++++ pkg/common/db/newmgo/user.go | 86 ++++++++++++++++++++++++++++ pkg/common/db/newmgo/user_mgo.go | 1 + 5 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 pkg/common/db/newmgo/mgotool/tool.go create mode 100644 pkg/common/db/newmgo/user.go create mode 100644 pkg/common/db/newmgo/user_mgo.go diff --git a/pkg/common/db/controller/msg_test.go b/pkg/common/db/controller/msg_test.go index ba5aecd25..cfb969b3e 100644 --- a/pkg/common/db/controller/msg_test.go +++ b/pkg/common/db/controller/msg_test.go @@ -235,7 +235,7 @@ func Test_FindBySeq(t *testing.T) { func TestName(t *testing.T) { db := GetDB() var seqs []int64 - for i := int64(1); i <= 4; i++ { + for i := int64(1); i <= 50; i++ { seqs = append(seqs, i) } msgs, err := db.getMsgBySeqsRange(context.Background(), "4931176757", "si_3866692501_4931176757", seqs, seqs[0], seqs[len(seqs)-1]) diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 9c6fdc5c4..b3726123f 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -38,7 +38,7 @@ type UserDatabase interface { // Create Insert multiple external guarantees that the userID is not repeated and does not exist in the db Create(ctx context.Context, users []*relation.UserModel) (err error) // Update update (non-zero value) external guarantee userID exists - Update(ctx context.Context, user *relation.UserModel) (err error) + //Update(ctx context.Context, user *relation.UserModel) (err error) // UpdateByMap update (zero value) external guarantee userID exists UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) // Page If not found, no error is returned diff --git a/pkg/common/db/newmgo/mgotool/tool.go b/pkg/common/db/newmgo/mgotool/tool.go new file mode 100644 index 000000000..25ee9851f --- /dev/null +++ b/pkg/common/db/newmgo/mgotool/tool.go @@ -0,0 +1,81 @@ +package mgotool + +import ( + "context" + "github.com/OpenIMSDK/tools/errs" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type Pagination interface { + GetPageNumber() int32 + GetShowNumber() int32 +} + +func Anys[T any](ts []T) []any { + val := make([]any, len(ts)) + for i := range ts { + val[i] = ts[i] + } + return val +} + +func InsertMany[T any](ctx context.Context, coll *mongo.Collection, val []T, opts ...*options.InsertManyOptions) error { + _, err := coll.InsertMany(ctx, Anys(val), opts...) + if err != nil { + return errs.Wrap(err) + } + return nil +} + +func UpdateOne(ctx context.Context, coll *mongo.Collection, filter any, update any, notMatchedErr bool, opts ...*options.UpdateOptions) error { + res, err := coll.UpdateOne(ctx, filter, update, opts...) + if err != nil { + return errs.Wrap(err) + } + if notMatchedErr && res.MatchedCount == 0 { + return errs.Wrap(mongo.ErrNoDocuments) + } + return nil +} + +func Find[T any](ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.FindOptions) ([]T, error) { + cur, err := coll.Find(ctx, filter, opts...) + if err != nil { + return nil, errs.Wrap(err) + } + defer cur.Close(ctx) + var res []T + if err := cur.All(ctx, &res); err != nil { + return nil, errs.Wrap(err) + } + return res, nil +} + +func FindOne[T any](ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.FindOneOptions) (res T, err error) { + cur := coll.FindOne(ctx, filter, opts...) + if err := cur.Err(); err != nil { + return res, errs.Wrap(err) + } + if err := cur.Decode(&res); err != nil { + return res, errs.Wrap(err) + } + return res, nil +} + +func FindPage[T any](ctx context.Context, coll *mongo.Collection, filter any, pagination Pagination, opts ...*options.FindOptions) (int64, []T, error) { + count, err := Count[T](ctx, coll, filter) + if err != nil { + return 0, nil, err + } + opt := options.Find().SetSkip(int64(pagination.GetPageNumber() * pagination.GetShowNumber())).SetLimit(int64(pagination.GetShowNumber())) + res, err := Find[T](ctx, coll, filter, append(opts, opt)...) + if err != nil { + return 0, nil, err + } + return count, res, nil +} + +func Count(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.CountOptions) (int64, error) { + return coll.CountDocuments(ctx, filter, opts...) +} diff --git a/pkg/common/db/newmgo/user.go b/pkg/common/db/newmgo/user.go new file mode 100644 index 000000000..1b5b9036b --- /dev/null +++ b/pkg/common/db/newmgo/user.go @@ -0,0 +1,86 @@ +package newmgo + +import ( + "context" + "github.com/openimsdk/open-im-server/v3/pkg/common/db/newmgo/mgotool" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "time" +) + +type UserModel struct { + UserID string `bson:"user_id"` + Nickname string `bson:"nickname"` + FaceURL string `bson:"face_url"` + Ex string `bson:"ex"` + AppMangerLevel int32 `bson:"app_manger_level"` + GlobalRecvMsgOpt int32 `bson:"global_recv_msg_opt"` + CreateTime time.Time `bson:"create_time"` +} + +type UserModelInterface interface { + Create(ctx context.Context, users []*UserModel) (err error) + UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) + // 获取指定用户信息 不存在,也不返回错误 + Find(ctx context.Context, userIDs []string) (users []*UserModel, err error) + // 获取某个用户信息 不存在,则返回错误 + Take(ctx context.Context, userID string) (user *UserModel, err error) + // 获取用户信息 不存在,不返回错误 + Page(ctx context.Context, pageNumber, showNumber int32) (users []*UserModel, count int64, err error) + GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) + GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) + // 获取用户总数 + CountTotal(ctx context.Context, before *time.Time) (count int64, err error) + // 获取范围内用户增量 + CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) +} + +type UserMgo struct { + coll *mongo.Collection +} + +func (u *UserMgo) Create(ctx context.Context, users []*UserModel) error { + return mgotool.InsertMany(ctx, u.coll, users) +} + +func (u *UserMgo) UpdateOneByMap(ctx context.Context, userID string, args map[string]any) error { + if len(args) == 0 { + return nil + } + return mgotool.UpdateOne(ctx, u.coll, bson.M{"user_id": userID}, bson.M{"$set": args}, true) +} + +func (u *UserMgo) Find(ctx context.Context, userIDs []string) (users []*UserModel, err error) { + return mgotool.Find[*UserModel](ctx, u.coll, bson.M{"user_id": bson.M{"$in": userIDs}}) +} + +func (u *UserMgo) Take(ctx context.Context, userID string) (user *UserModel, err error) { + return mgotool.FindOne[*UserModel](ctx, u.coll, bson.M{"user_id": userID}) +} + +func (u *UserMgo) Page(ctx context.Context, pagination mgotool.Pagination) (count int64, users []*UserModel, err error) { + return mgotool.FindPage[*UserModel](ctx, u.coll, bson.M{}, pagination) +} + +func (u *UserMgo) GetAllUserID(ctx context.Context, pagination mgotool.Pagination) (int64, []string, error) { + return mgotool.FindPage[string](ctx, u.coll, bson.M{}, pagination, options.Find().SetProjection(bson.M{"user_id": 1})) +} + +func (u *UserMgo) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) { + return mgotool.FindOne[int](ctx, u.coll, bson.M{"user_id": userID}, options.FindOne().SetProjection(bson.M{"global_recv_msg_opt": 1})) +} + +func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) { + return mgotool.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}}) +} + +func (u *UserMgo) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) { + //type Temp struct { + // CreateTime time.Time `bson:"create_time"` + // Number int64 `bson:"number"` + //} + //mgotool.Find(ctx, u.coll, bson.M{"create_time": bson.M{"$gte": start, "$lt": end}}, options.Find().SetProjection(bson.M{"create_time": 1})) + panic("implement me") + return nil, nil +} diff --git a/pkg/common/db/newmgo/user_mgo.go b/pkg/common/db/newmgo/user_mgo.go new file mode 100644 index 000000000..edbd61e3f --- /dev/null +++ b/pkg/common/db/newmgo/user_mgo.go @@ -0,0 +1 @@ +package newmgo