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_k.go

94 lines
3.7 KiB

2 years ago
package relation
import (
2 years ago
"Open_IM/pkg/common/db/table/relation"
2 years ago
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
"fmt"
"gorm.io/gorm"
)
type UserGorm struct {
DB *gorm.DB
}
func NewUserGorm(db *gorm.DB) *UserGorm {
var user UserGorm
user.DB = db
return &user
}
2 years ago
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel, tx ...*gorm.DB) (err error) {
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}()
2 years ago
return utils.Wrap(getDBConn(u.DB, tx).Model(&relation.UserModel{}).Create(&users).Error, "")
}
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args)
}()
2 years ago
return utils.Wrap(u.DB.Model(&relation.UserModel{}).Where("user_id = ?", userID).Updates(args).Error, "")
}
2 years ago
func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel) (err error) {
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}()
2 years ago
return utils.Wrap(u.DB.Model(&relation.UserModel{}).Updates(&users).Error, "")
}
2 years ago
func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs, "users", users)
}()
2 years ago
err = utils.Wrap(u.DB.Model(&relation.UserModel{}).Where("user_id in (?)", userIDs).Find(&users).Error, "")
return users, err
}
2 years ago
func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
user = &relation.UserModel{}
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "user", *user)
}()
2 years ago
err = utils.Wrap(u.DB.Model(&relation.UserModel{}).Where("user_id = ?", userID).Take(&user).Error, "")
return user, err
}
2 years ago
func (u *UserGorm) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation.UserModel, count int64, err error) {
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userName", userName, "showNumber", showNumber, "pageNumber", pageNumber, "users", users, "count", count)
}()
2 years ago
err = u.DB.Model(&relation.UserModel{}).Where(" name like ?", fmt.Sprintf("%%%s%%", userName)).Limit(int(showNumber)).Offset(int(showNumber * pageNumber)).Find(&users).Error
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
2 years ago
return users, count, utils.Wrap(u.DB.Model(&relation.UserModel{}).Where(" name like ? ", fmt.Sprintf("%%%s%%", userName)).Count(&count).Error, "")
}
2 years ago
func (u *UserGorm) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*relation.UserModel, count int64, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "content", content, "showNumber", showNumber, "pageNumber", pageNumber, "users", users)
}()
2 years ago
db := u.DB.Model(&relation.UserModel{}).Where(" name like ? or user_id = ? ", fmt.Sprintf("%%%s%%", content), content)
if err := db.Model(&relation.UserModel{}).Count(&count).Error; err != nil {
return nil, 0, utils.Wrap(err, "")
}
err = utils.Wrap(db.Limit(int(showNumber)).Offset(int(showNumber*pageNumber)).Find(&users).Error, "")
return
}
2 years ago
func (u *UserGorm) Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation.UserModel, count int64, err error) {
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "showNumber", showNumber, "pageNumber", pageNumber, "users", users, "count", count)
}()
2 years ago
err = u.DB.Model(&relation.UserModel{}).Model(u).Count(&count).Error
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
2 years ago
err = utils.Wrap(u.DB.Model(&relation.UserModel{}).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
return
}