2023-01-28 13:19:36 +08:00
package relation
2021-05-26 19:32:30 +08:00
import (
2023-02-16 17:20:08 +08:00
"Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
2021-10-11 22:12:01 +08:00
"Open_IM/pkg/utils"
2023-02-16 17:20:08 +08:00
"context"
2021-11-10 15:24:59 +08:00
"fmt"
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-16 17:20:08 +08:00
func NewUserGorm ( db * gorm . DB ) * UserGorm {
var user UserGorm
user . DB = db
return & user
2021-05-26 19:32:30 +08:00
}
2023-02-16 17:20:08 +08:00
// 插入多条
func ( u * UserGorm ) Create ( ctx context . Context , users [ ] * relation . UserModel , tx ... any ) ( err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "users" , users )
} ( )
return utils . Wrap ( getDBConn ( u . DB , tx ) . Create ( & users ) . Error , "" )
2022-04-28 10:46:21 +08:00
}
2023-02-16 17:20:08 +08:00
// 更新用户信息 零值
func ( u * UserGorm ) UpdateByMap ( ctx context . Context , userID string , args map [ string ] interface { } , tx ... any ) ( err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "args" , args )
} ( )
return utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Where ( "user_id = ?" , userID ) . Updates ( args ) . Error , "" )
2021-05-26 19:32:30 +08:00
}
2023-02-16 17:20:08 +08:00
// 更新多个用户信息 非零值
func ( u * UserGorm ) Update ( ctx context . Context , users [ ] * relation . UserModel , tx ... any ) ( err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "users" , users )
} ( )
return utils . Wrap ( getDBConn ( u . DB , tx ) . Updates ( & users ) . Error , "" )
2022-08-12 18:37:51 +08:00
}
2023-02-16 17:20:08 +08:00
// 获取指定用户信息 不存在,也不返回错误
func ( u * UserGorm ) Find ( ctx context . Context , userIDs [ ] string , tx ... any ) ( users [ ] * relation . UserModel , err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userIDs" , userIDs , "users" , users )
} ( )
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Where ( "user_id in (?)" , userIDs ) . Find ( & users ) . Error , "" )
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
// 获取某个用户信息 不存在,则返回错误
func ( u * UserGorm ) Take ( ctx context . Context , userID string , tx ... any ) ( user * relation . UserModel , err error ) {
user = & relation . UserModel { }
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "user" , * user )
} ( )
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Where ( "user_id = ?" , userID ) . Take ( & user ) . Error , "" )
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
// 通过名字查找用户 不存在,不返回错误
func ( u * UserGorm ) GetByName ( ctx context . Context , userName string , pageNumber , showNumber int32 , tx ... any ) ( users [ ] * relation . UserModel , count int64 , err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userName" , userName , "pageNumber" , pageNumber , "showNumber" , showNumber , "users" , users , "count" , count )
} ( )
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . Where ( " name like ?" , fmt . Sprintf ( "%%%s%%" , userName ) ) . Count ( & count ) . Error , "" )
2021-09-26 14:26:45 +08:00
if err != nil {
2023-02-16 17:20:08 +08:00
return
2021-12-17 14:28:43 +08:00
}
2023-02-16 17:20:08 +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
2021-12-17 14:28:43 +08:00
}
2022-01-24 01:40:49 +08:00
2023-02-16 17:20:08 +08:00
// 通过名字或userID查找用户 不存在,不返回错误
func ( u * UserGorm ) GetByNameAndID ( ctx context . Context , content string , pageNumber , showNumber int32 , tx ... any ) ( users [ ] * relation . UserModel , count int64 , err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "content" , content , "pageNumber" , pageNumber , "showNumber" , showNumber , "users" , users , "count" , count )
} ( )
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
2022-01-24 01:40:49 +08:00
}
2023-02-16 17:20:08 +08:00
err = utils . Wrap ( db . Limit ( int ( showNumber ) ) . Offset ( int ( showNumber * pageNumber ) ) . Find ( & users ) . Error , "" )
return
2022-01-25 19:18:04 +08:00
}
2023-02-16 17:20:08 +08:00
// 获取用户信息 不存在,不返回错误
func ( u * UserGorm ) Page ( ctx context . Context , pageNumber , showNumber int32 , tx ... any ) ( users [ ] * relation . UserModel , count int64 , err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "pageNumber" , pageNumber , "showNumber" , showNumber , "users" , users , "count" , count )
} ( )
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Model ( & relation . UserModel { } ) . 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-16 17:20:08 +08:00
err = utils . Wrap ( getDBConn ( u . DB , tx ) . Limit ( int ( showNumber ) ) . Offset ( int ( pageNumber * showNumber ) ) . Find ( & users ) . Error , "" )
return
2022-01-26 18:43:01 +08:00
}
2023-02-16 17:20:08 +08:00
// 获取所有用户ID
func ( u * UserGorm ) GetAllUserID ( ctx context . Context ) ( [ ] string , error ) {
var userIDs [ ] string
err := u . DB . Pluck ( "user_id" , & userIDs ) . Error
return userIDs , err
2022-01-27 01:08:02 +08:00
}