You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/relation/user_model.go

67 lines
2.3 KiB

2 years ago
package relation
import (
2 years ago
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"gorm.io/gorm"
)
2 years ago
type UserGorm struct {
2 years ago
*MetaDB
}
func NewUserGorm(db *gorm.DB) relation.UserModelInterface {
2 years ago
return &UserGorm{NewMetaDB(db, &relation.UserModel{})}
}
2 years ago
// 插入多条
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel) (err error) {
2 years ago
return utils.Wrap(u.db(ctx).Create(&users).Error, "")
}
2 years ago
// 更新用户信息 零值
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
2 years ago
return utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Updates(args).Error, "")
}
2 years ago
// 更新多个用户信息 非零值
2 years ago
func (u *UserGorm) Update(ctx context.Context, user *relation.UserModel) (err error) {
2 years ago
return utils.Wrap(u.db(ctx).Model(user).Updates(user).Error, "")
2 years ago
}
2 years ago
// 获取指定用户信息 不存在,也不返回错误
func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
2 years ago
err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "")
2 years ago
return users, err
}
2 years ago
// 获取某个用户信息 不存在,则返回错误
func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
2 years ago
user = &relation.UserModel{}
2 years ago
err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "")
2 years ago
return user, err
}
2 years ago
// 获取用户信息 不存在,不返回错误
func (u *UserGorm) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error) {
2 years ago
err = utils.Wrap(u.db(ctx).Count(&count).Error, "")
if err != nil {
2 years ago
return
3 years ago
}
2 years ago
err = utils.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Find(&users).Order("create_time DESC").Error, "")
2 years ago
return
3 years ago
}
2 years ago
// 获取所有用户ID
2 years ago
func (u *UserGorm) GetAllUserID(ctx context.Context) (userIDs []string, err error) {
2 years ago
err = u.db(ctx).Pluck("user_id", &userIDs).Error
2 years ago
return userIDs, err
3 years ago
}
2 years ago
func (u *UserGorm) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
err = u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Pluck("global_recv_msg_opt", &opt).Error
return opt, err
}