2023-01-28 13:19:36 +08:00
package relation
2023-01-05 15:30:33 +08:00
import (
2023-02-01 16:19:44 +08:00
"Open_IM/pkg/common/db/table"
2023-01-30 11:14:18 +08:00
"Open_IM/pkg/common/tracelog"
2023-01-05 15:30:33 +08:00
"Open_IM/pkg/utils"
"context"
2023-01-11 11:15:46 +08:00
"gorm.io/gorm"
2023-01-05 15:30:33 +08:00
)
2023-02-01 15:56:17 +08:00
type FriendDB interface {
2023-02-01 16:19:44 +08:00
Create ( ctx context . Context , friends [ ] * table . FriendModel ) ( err error )
2023-02-01 15:56:17 +08:00
Delete ( ctx context . Context , ownerUserID string , friendUserIDs string ) ( err error )
UpdateByMap ( ctx context . Context , ownerUserID string , args map [ string ] interface { } ) ( err error )
2023-02-01 16:19:44 +08:00
Update ( ctx context . Context , friends [ ] * table . FriendModel ) ( err error )
2023-02-01 15:56:17 +08:00
UpdateRemark ( ctx context . Context , ownerUserID , friendUserID , remark string ) ( err error )
2023-02-01 16:19:44 +08:00
FindOwnerUserID ( ctx context . Context , ownerUserID string ) ( friends [ ] * table . FriendModel , err error )
2023-02-01 15:56:17 +08:00
}
type FriendGorm struct {
DB * gorm . DB ` gorm:"-" `
2023-01-13 18:20:48 +08:00
}
2023-02-01 15:56:17 +08:00
func NewFriendGorm ( DB * gorm . DB ) * FriendGorm {
return & FriendGorm { DB : DB }
2023-01-30 19:49:18 +08:00
}
2023-02-01 15:56:17 +08:00
type FriendUser struct {
FriendGorm
Nickname string ` gorm:"column:name;size:255" `
2023-01-05 15:30:33 +08:00
}
2023-02-02 16:39:29 +08:00
func ( f * FriendGorm ) Create ( ctx context . Context , friends [ ] * table . FriendModel , tx ... * gorm . DB ) ( err error ) {
2023-01-05 15:30:33 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "friends" , friends )
2023-01-05 15:30:33 +08:00
} ( )
2023-02-02 16:39:29 +08:00
return utils . Wrap ( getDBConn ( f . DB , tx ) . Model ( & table . FriendModel { } ) . Create ( & friends ) . Error , "" )
2023-01-05 15:30:33 +08:00
}
2023-02-01 15:56:17 +08:00
func ( f * FriendGorm ) Delete ( ctx context . Context , ownerUserID string , friendUserIDs string ) ( err error ) {
2023-01-05 15:30:33 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "ownerUserID" , ownerUserID , "friendUserIDs" , friendUserIDs )
2023-01-05 15:30:33 +08:00
} ( )
2023-02-01 16:44:50 +08:00
err = utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "owner_user_id = ? and friend_user_id = ?" , ownerUserID , friendUserIDs ) . Delete ( & table . FriendModel { } ) . Error , "" )
2023-01-05 15:30:33 +08:00
return err
}
2023-02-01 15:56:17 +08:00
func ( f * FriendGorm ) UpdateByMap ( ctx context . Context , ownerUserID string , args map [ string ] interface { } ) ( err error ) {
2023-01-05 15:30:33 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "ownerUserID" , ownerUserID , "args" , args )
2023-01-05 15:30:33 +08:00
} ( )
2023-02-01 16:44:50 +08:00
return utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "owner_user_id = ?" , ownerUserID ) . Updates ( args ) . Error , "" )
2023-01-05 15:30:33 +08:00
}
2023-02-02 16:39:29 +08:00
func ( f * FriendGorm ) Update ( ctx context . Context , friends [ ] * table . FriendModel , tx ... * gorm . DB ) ( err error ) {
2023-01-05 15:30:33 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "friends" , friends )
2023-01-05 15:30:33 +08:00
} ( )
2023-02-01 16:44:50 +08:00
return utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Updates ( & friends ) . Error , "" )
2023-01-05 15:30:33 +08:00
}
2023-02-01 15:56:17 +08:00
func ( f * FriendGorm ) UpdateRemark ( ctx context . Context , ownerUserID , friendUserID , remark string ) ( err error ) {
2023-01-16 15:53:40 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "ownerUserID" , ownerUserID , "friendUserID" , friendUserID , "remark" , remark )
2023-01-16 15:53:40 +08:00
} ( )
2023-02-01 16:19:44 +08:00
return utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "owner_user_id = ? and friend_user_id = ?" , ownerUserID , friendUserID ) . Update ( "remark" , remark ) . Error , "" )
2023-01-16 15:53:40 +08:00
}
2023-02-01 16:19:44 +08:00
func ( f * FriendGorm ) FindOwnerUserID ( ctx context . Context , ownerUserID string ) ( friends [ ] * table . FriendModel , err error ) {
2023-01-05 15:30:33 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "ownerUserID" , ownerUserID , "friends" , friends )
2023-01-05 15:30:33 +08:00
} ( )
2023-02-01 16:44:50 +08:00
return friends , utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "owner_user_id = ?" , ownerUserID ) . Find ( & friends ) . Error , "" )
2023-01-05 15:30:33 +08:00
}
2023-02-01 16:19:44 +08:00
func ( f * FriendGorm ) FindFriendUserID ( ctx context . Context , friendUserID string ) ( friends [ ] * table . FriendModel , err error ) {
2023-01-17 17:31:22 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "friendUserID" , friendUserID , "friends" , friends )
2023-01-17 17:31:22 +08:00
} ( )
2023-02-01 16:44:50 +08:00
return friends , utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "friend_user_id = ?" , friendUserID ) . Find ( & friends ) . Error , "" )
2023-01-17 17:31:22 +08:00
}
2023-02-01 16:19:44 +08:00
func ( f * FriendGorm ) Take ( ctx context . Context , ownerUserID , friendUserID string ) ( friend * table . FriendModel , err error ) {
friend = & table . FriendModel { }
2023-01-30 11:10:26 +08:00
defer tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "ownerUserID" , ownerUserID , "friendUserID" , friendUserID , "friend" , friend )
2023-02-01 16:44:50 +08:00
return friend , utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "owner_user_id = ? and friend_user_id" , ownerUserID , friendUserID ) . Take ( friend ) . Error , "" )
2023-01-16 15:53:40 +08:00
}
2023-02-01 16:19:44 +08:00
func ( f * FriendGorm ) FindUserState ( ctx context . Context , userID1 , userID2 string ) ( friends [ ] * table . FriendModel , err error ) {
2023-01-16 15:53:40 +08:00
defer func ( ) {
2023-01-30 11:10:26 +08:00
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "userID1" , userID1 , "userID2" , userID2 )
2023-01-16 15:53:40 +08:00
} ( )
2023-02-01 16:44:50 +08:00
return friends , utils . Wrap ( f . DB . Model ( & table . FriendModel { } ) . Where ( "(owner_user_id = ? and friend_user_id = ?) or (owner_user_id = ? and friend_user_id = ?)" , userID1 , userID2 , userID2 , userID1 ) . Find ( & friends ) . Error , "" )
2023-01-05 15:30:33 +08:00
}
2023-02-02 16:39:29 +08:00
// 获取 owner的好友列表
func ( f * FriendGorm ) FindFriends ( ctx context . Context , ownerUserID string , friendUserIDs [ ] string , tx ... * gorm . DB ) ( friends [ ] * table . FriendModel , err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "friendUserIDs" , friendUserIDs , "friends" , friends )
} ( )
return friends , utils . Wrap ( getDBConn ( f . DB , tx ) . Where ( "owner_user_id = ? AND friend_user_id in (?)" , ownerUserID , friendUserIDs ) . Find ( & friends ) . Error , "" )
}
// 获取哪些人添加了friendUserID
func ( f * FriendGorm ) FindReversalFriends ( ctx context . Context , friendUserID string , ownerUserIDs [ ] string , tx ... * gorm . DB ) ( friends [ ] * table . FriendModel , err error ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetSelfFuncName ( ) , err , "friendUserID" , friendUserID , "friends" , friends )
} ( )
return friends , utils . Wrap ( getDBConn ( f . DB , tx ) . Where ( "friend_user_id = ? AND owner_user_id in (?)" , friendUserID , ownerUserIDs ) . Find ( & friends ) . Error , "" )
}