2023-01-28 13:19:36 +08:00
package relation
2023-01-13 14:34:34 +08:00
import (
2023-02-03 12:16:48 +08:00
"Open_IM/pkg/common/db/table/relation"
2023-01-30 11:14:18 +08:00
"Open_IM/pkg/common/tracelog"
2023-01-13 14:34:34 +08:00
"Open_IM/pkg/utils"
"context"
2023-01-29 16:30:33 +08:00
"fmt"
2023-01-13 14:34:34 +08:00
"gorm.io/gorm"
)
2023-02-01 17:20:55 +08:00
type UserGorm struct {
DB * gorm . DB
2023-01-13 14:34:34 +08:00
}
2023-02-01 17:20:55 +08:00
func NewUserGorm ( db * gorm . DB ) * UserGorm {
var user UserGorm
2023-01-29 14:47:21 +08:00
user . DB = db
2023-01-17 11:15:39 +08:00
return & user
}
2023-02-07 20:28:34 +08:00
// 插入多条
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) Create ( ctx context . Context , users [ ] * relation . UserModel , tx ... any ) ( err error ) {
2023-01-13 14:34:34 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "users" , users )
2023-01-13 14:34:34 +08:00
} ( )
2023-02-07 20:28:34 +08:00
return utils . Wrap ( getDBConn ( u . DB , tx ) . Create ( & users ) . Error , "" )
2023-01-13 14:34:34 +08:00
}
2023-02-07 20:28:34 +08:00
// 更新用户信息 零值
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) UpdateByMap ( ctx context . Context , userID string , args map [ string ] interface { } , tx ... any ) ( err error ) {
2023-01-13 14:34:34 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "args" , args )
2023-01-13 14:34:34 +08:00
} ( )
2023-02-07 20:28:34 +08:00
return utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Where ( "user_id = ?" , userID ) . Updates ( args ) . Error , "" )
2023-01-13 14:34:34 +08:00
}
2023-02-07 20:28:34 +08:00
// 更新多个用户信息 非零值
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) Update ( ctx context . Context , users [ ] * relation . UserModel , tx ... any ) ( err error ) {
2023-01-13 14:34:34 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "users" , users )
2023-01-13 14:34:34 +08:00
} ( )
2023-02-07 20:28:34 +08:00
return utils . Wrap ( getDBConn ( u . DB , tx ) . Updates ( & users ) . Error , "" )
2023-01-13 14:34:34 +08:00
}
2023-02-07 20:28:34 +08:00
// 获取指定用户信息 不存在,也不返回错误
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) Find ( ctx context . Context , userIDs [ ] string , tx ... any ) ( users [ ] * relation . UserModel , err error ) {
2023-01-13 14:34:34 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userIDs" , userIDs , "users" , users )
2023-01-13 14:34:34 +08:00
} ( )
2023-02-07 20:28:34 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Where ( "user_id in (?)" , userIDs ) . Find ( & users ) . Error , "" )
2023-01-13 14:34:34 +08:00
return users , err
}
2023-02-07 20:28:34 +08:00
// 获取某个用户信息 不存在,则返回错误
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) Take ( ctx context . Context , userID string , tx ... any ) ( user * relation . UserModel , err error ) {
2023-02-03 12:16:48 +08:00
user = & relation . UserModel { }
2023-01-13 14:34:34 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "user" , * user )
2023-01-13 14:34:34 +08:00
} ( )
2023-02-07 20:28:34 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Where ( "user_id = ?" , userID ) . Take ( & user ) . Error , "" )
2023-01-13 14:39:23 +08:00
return user , err
2023-01-13 14:34:34 +08:00
}
2023-01-29 16:30:33 +08:00
2023-02-07 20:28:34 +08:00
// 通过名字查找用户 不存在,不返回错误
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) GetByName ( ctx context . Context , userName string , pageNumber , showNumber int32 , tx ... any ) ( users [ ] * relation . UserModel , count int64 , err error ) {
2023-01-29 16:30:33 +08:00
defer func ( ) {
2023-02-07 20:28:34 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userName" , userName , "pageNumber" , pageNumber , "showNumber" , showNumber , "users" , users , "count" , count )
2023-01-29 16:30:33 +08:00
} ( )
2023-02-07 20:28:34 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Where ( " name like ?" , fmt . Sprintf ( "%%%s%%" , userName ) ) . Count ( & count ) . Error , "" )
2023-01-29 17:46:11 +08:00
if err != nil {
2023-02-07 20:28:34 +08:00
return
2023-01-29 17:46:11 +08:00
}
2023-02-07 20:28:34 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Where ( " name like ?" , fmt . Sprintf ( "%%%s%%" , userName ) ) . Limit ( int ( showNumber ) ) . Offset ( int ( showNumber * pageNumber ) ) . Find ( & users ) . Error , "" )
return
2023-01-29 17:46:11 +08:00
}
2023-02-07 20:28:34 +08:00
// 通过名字或userID查找用户 不存在,不返回错误
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) GetByNameAndID ( ctx context . Context , content string , pageNumber , showNumber int32 , tx ... any ) ( users [ ] * relation . UserModel , count int64 , err error ) {
2023-01-29 16:30:33 +08:00
defer func ( ) {
2023-02-07 20:28:34 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "content" , content , "pageNumber" , pageNumber , "showNumber" , showNumber , "users" , users , "count" , count )
2023-01-29 16:30:33 +08:00
} ( )
2023-02-07 20:28:34 +08:00
db := getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Where ( " name like ? or user_id = ? " , fmt . Sprintf ( "%%%s%%" , content ) , content )
if err = db . Count ( & count ) . Error ; err != nil {
return
2023-01-29 17:46:11 +08:00
}
err = utils . Wrap ( db . Limit ( int ( showNumber ) ) . Offset ( int ( showNumber * pageNumber ) ) . Find ( & users ) . Error , "" )
return
}
2023-02-07 20:28:34 +08:00
// 获取用户信息 不存在,不返回错误
2023-02-09 12:52:47 +08:00
func ( u * UserGorm ) Page ( ctx context . Context , pageNumber , showNumber int32 , tx ... any ) ( users [ ] * relation . UserModel , count int64 , err error ) {
2023-01-29 17:46:11 +08:00
defer func ( ) {
2023-02-07 20:28:34 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "pageNumber" , pageNumber , "showNumber" , showNumber , "users" , users , "count" , count )
2023-01-29 17:46:11 +08:00
} ( )
2023-02-07 20:28:34 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Count ( & count ) . Error , "" )
2023-01-29 17:46:11 +08:00
if err != nil {
2023-02-07 20:28:34 +08:00
return
2023-01-29 17:46:11 +08:00
}
2023-02-07 20:28:34 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Limit ( int ( showNumber ) ) . Offset ( int ( pageNumber * showNumber ) ) . Find ( & users ) . Error , "" )
2023-01-29 17:46:11 +08:00
return
2023-01-29 16:30:33 +08:00
}