diff --git a/internal/api/user.go b/internal/api/user.go index 738b369b7..e8632c468 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -3,6 +3,7 @@ package api import ( "OpenIM/internal/api/a2r" "OpenIM/pkg/common/config" + "OpenIM/pkg/common/log" "OpenIM/pkg/discoveryregistry" "OpenIM/pkg/proto/user" "context" @@ -19,38 +20,41 @@ type User struct { c discoveryregistry.SvcDiscoveryRegistry } -func (o *User) client() (user.UserClient, error) { - conn, err := o.c.GetConn(config.Config.RpcRegisterName.OpenImUserName) +func (u *User) client() (user.UserClient, error) { + conn, err := u.c.GetConn(config.Config.RpcRegisterName.OpenImUserName) if err != nil { return nil, err } + defer func() { + log.NewInfo("client", conn, err) + }() return user.NewUserClient(conn), nil } -func (o *User) UserRegister(c *gin.Context) { - a2r.Call(user.UserClient.UserRegister, o.client, c) +func (u *User) UserRegister(c *gin.Context) { + a2r.Call(user.UserClient.UserRegister, u.client, c) } -func (o *User) UpdateUserInfo(c *gin.Context) { - a2r.Call(user.UserClient.UpdateUserInfo, o.client, c) +func (u *User) UpdateUserInfo(c *gin.Context) { + a2r.Call(user.UserClient.UpdateUserInfo, u.client, c) } -func (o *User) SetGlobalRecvMessageOpt(c *gin.Context) { - a2r.Call(user.UserClient.SetGlobalRecvMessageOpt, o.client, c) +func (u *User) SetGlobalRecvMessageOpt(c *gin.Context) { + a2r.Call(user.UserClient.SetGlobalRecvMessageOpt, u.client, c) } -func (o *User) GetUsersPublicInfo(c *gin.Context) { - a2r.Call(user.UserClient.GetDesignateUsers, o.client, c) +func (u *User) GetUsersPublicInfo(c *gin.Context) { + a2r.Call(user.UserClient.GetDesignateUsers, u.client, c) } -func (o *User) GetAllUsersID(c *gin.Context) { - a2r.Call(user.UserClient.GetDesignateUsers, o.client, c) +func (u *User) GetAllUsersID(c *gin.Context) { + a2r.Call(user.UserClient.GetDesignateUsers, u.client, c) } func (u *User) AccountCheck(c *gin.Context) { a2r.Call(user.UserClient.AccountCheck, u.client, c) } -func (o *User) GetUsers(c *gin.Context) { - a2r.Call(user.UserClient.GetPaginationUsers, o.client, c) +func (u *User) GetUsers(c *gin.Context) { + a2r.Call(user.UserClient.GetPaginationUsers, u.client, c) } diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index a5b2c042d..16d9d9214 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -105,20 +105,5 @@ func (u *userDatabase) IsExist(ctx context.Context, userIDs []string) (exist boo } func (u *userDatabase) GetAllUserID(ctx context.Context) (userIDs []string, err error) { - pageNumber := int32(0) - for { - tmp, total, err := u.userDB.PageUserID(ctx, pageNumber, constant.ShowNumber) - if err != nil { - return nil, err - } - if len(tmp) == 0 { - if total == int64(len(userIDs)) { - return userIDs, nil - } - return nil, errs.ErrData.Wrap("The total number of results and expectations are different, but result is nil") - } - userIDs = append(userIDs, tmp...) - pageNumber++ - } - return userIDs, nil + return u.userDB.GetAllUserID(ctx) } diff --git a/pkg/common/db/relation/user_model.go b/pkg/common/db/relation/user_model.go index 9ad182d17..697024e39 100644 --- a/pkg/common/db/relation/user_model.go +++ b/pkg/common/db/relation/user_model.go @@ -13,7 +13,7 @@ type UserGorm struct { } func NewUserGorm(DB *gorm.DB) relation.UserModelInterface { - return &UserGorm{DB: DB} + return &UserGorm{DB: DB.Model(&relation.UserModel{})} } // 插入多条 @@ -29,7 +29,7 @@ func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[stri defer func() { tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args) }() - return utils.Wrap(u.DB.Model(&relation.UserModel{}).Where("user_id = ?", userID).Updates(args).Error, "") + return utils.Wrap(u.DB.Where("user_id = ?", userID).Updates(args).Error, "") } // 更新多个用户信息 非零值 @@ -64,7 +64,7 @@ func (u *UserGorm) Page(ctx context.Context, pageNumber, showNumber int32) (user defer func() { tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "users", users, "count", count) }() - err = utils.Wrap(u.DB.Model(&relation.UserModel{}).Count(&count).Error, "") + err = utils.Wrap(u.DB.Count(&count).Error, "") if err != nil { return } @@ -73,14 +73,10 @@ func (u *UserGorm) Page(ctx context.Context, pageNumber, showNumber int32) (user } // 获取所有用户ID -func (u *UserGorm) PageUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, count int64, err error) { +func (u *UserGorm) GetAllUserID(ctx context.Context) (userIDs []string, err error) { defer func() { - tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "userIDs", userIDs, "count", count) + tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs) }() - err = utils.Wrap(u.DB.Model(&relation.UserModel{}).Count(&count).Error, "") - if err != nil { - return - } - err = u.DB.Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Pluck("user_id", &userIDs).Error - return userIDs, count, err + err = u.DB.Pluck("user_id", &userIDs).Error + return userIDs, err } diff --git a/pkg/common/db/table/relation/user.go b/pkg/common/db/table/relation/user.go index 2b0d2d441..047046a02 100644 --- a/pkg/common/db/table/relation/user.go +++ b/pkg/common/db/table/relation/user.go @@ -38,5 +38,5 @@ type UserModelInterface interface { Take(ctx context.Context, userID string) (user *UserModel, err error) // 获取用户信息 不存在,不返回错误 Page(ctx context.Context, pageNumber, showNumber int32) (users []*UserModel, count int64, err error) - PageUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, count int64, err error) + GetAllUserID(ctx context.Context) (userIDs []string, err error) }