Files
open-im-server/pkg/common/db/relation/user_model.go
T

83 lines
2.7 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package relation
2021-05-26 19:32:30 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/db/table/relation"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/utils"
2023-02-16 17:20:08 +08:00
"context"
2023-01-13 14:35:58 +08:00
"gorm.io/gorm"
2021-05-26 19:32:30 +08:00
)
2023-02-16 17:20:08 +08:00
type UserGorm struct {
DB *gorm.DB
2023-01-13 14:35:58 +08:00
}
2023-02-21 12:59:44 +08:00
func NewUserGorm(DB *gorm.DB) relation.UserModelInterface {
2023-03-10 11:42:06 +08:00
return &UserGorm{DB: DB.Model(&relation.UserModel{})}
2021-05-26 19:32:30 +08:00
}
2023-02-16 17:20:08 +08:00
// 插入多条
2023-02-21 12:59:44 +08:00
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel) (err error) {
2023-02-16 17:20:08 +08:00
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}()
2023-02-21 12:59:44 +08:00
return utils.Wrap(u.DB.Create(&users).Error, "")
2022-04-28 10:46:21 +08:00
}
2023-02-16 17:20:08 +08:00
// 更新用户信息 零值
2023-02-21 12:59:44 +08:00
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
2023-02-16 17:20:08 +08:00
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args)
}()
2023-03-10 11:42:06 +08:00
return utils.Wrap(u.DB.Where("user_id = ?", userID).Updates(args).Error, "")
2021-05-26 19:32:30 +08:00
}
2023-02-16 17:20:08 +08:00
// 更新多个用户信息 非零值
2023-02-21 12:59:44 +08:00
func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel) (err error) {
2023-02-16 17:20:08 +08:00
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}()
2023-02-21 12:59:44 +08:00
return utils.Wrap(u.DB.Updates(&users).Error, "")
2022-08-12 18:37:51 +08:00
}
2023-02-16 17:20:08 +08:00
// 获取指定用户信息 不存在,也不返回错误
2023-02-21 12:59:44 +08:00
func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
2023-02-16 17:20:08 +08:00
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs, "users", users)
}()
2023-03-13 14:21:41 +08:00
err = utils.Wrap(u.DB.Debug().Where("user_id in ?", userIDs).Find(&users).Error, "")
2023-02-16 17:20:08 +08:00
return users, err
2021-05-26 19:32:30 +08:00
}
2021-12-17 14:28:43 +08:00
2023-02-16 17:20:08 +08:00
// 获取某个用户信息 不存在,则返回错误
2023-02-21 12:59:44 +08:00
func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
2023-02-16 17:20:08 +08:00
user = &relation.UserModel{}
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "user", *user)
}()
2023-02-21 12:59:44 +08:00
err = utils.Wrap(u.DB.Where("user_id = ?", userID).Take(&user).Error, "")
2023-02-16 17:20:08 +08:00
return user, err
2021-12-17 14:28:43 +08:00
}
2021-09-26 14:26:45 +08:00
2023-02-16 17:20:08 +08:00
// 获取用户信息 不存在,不返回错误
2023-02-21 12:59:44 +08:00
func (u *UserGorm) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error) {
2023-02-16 17:20:08 +08:00
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "users", users, "count", count)
}()
2023-03-10 11:42:06 +08:00
err = utils.Wrap(u.DB.Count(&count).Error, "")
2022-10-28 15:42:37 +08:00
if err != nil {
2023-02-16 17:20:08 +08:00
return
2022-01-25 19:18:04 +08:00
}
2023-02-21 12:59:44 +08:00
err = utils.Wrap(u.DB.Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
2023-02-16 17:20:08 +08:00
return
2022-01-26 18:43:01 +08:00
}
2023-02-16 17:20:08 +08:00
// 获取所有用户ID
2023-03-10 11:42:06 +08:00
func (u *UserGorm) GetAllUserID(ctx context.Context) (userIDs []string, err error) {
2023-02-21 10:29:09 +08:00
defer func() {
2023-03-10 11:42:06 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs)
2023-02-21 10:29:09 +08:00
}()
2023-03-10 11:42:06 +08:00
err = u.DB.Pluck("user_id", &userIDs).Error
return userIDs, err
2022-01-27 01:08:02 +08:00
}