mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
gorm db
This commit is contained in:
parent
ff542e83fe
commit
5e6f9448bc
14
pkg/common/db/cache/user.go
vendored
14
pkg/common/db/cache/user.go
vendored
@ -73,13 +73,13 @@ func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userIn
|
||||
|
||||
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationTb.UserModel, error) {
|
||||
var users []*relationTb.UserModel
|
||||
for _, userID := range userIDs {
|
||||
user, err := GetUserInfoFromCache(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
//for _, userID := range userIDs {
|
||||
// user, err := GetUserInfoFromCache(ctx, userID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// users = append(users, user)
|
||||
//}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
|
@ -1,123 +1,90 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var ConversationDB *gorm.DB
|
||||
|
||||
//type Conversation struct {
|
||||
// OwnerUserID string `gorm:"column:owner_user_id;primary_key;type:char(128)" json:"OwnerUserID"`
|
||||
// ConversationID string `gorm:"column:conversation_id;primary_key;type:char(128)" json:"conversationID"`
|
||||
// ConversationType int32 `gorm:"column:conversation_type" json:"conversationType"`
|
||||
// UserID string `gorm:"column:user_id;type:char(64)" json:"userID"`
|
||||
// GroupID string `gorm:"column:group_id;type:char(128)" json:"groupID"`
|
||||
// RecvMsgOpt int32 `gorm:"column:recv_msg_opt" json:"recvMsgOpt"`
|
||||
// UnreadCount int32 `gorm:"column:unread_count" json:"unreadCount"`
|
||||
// DraftTextTime int64 `gorm:"column:draft_text_time" json:"draftTextTime"`
|
||||
// IsPinned bool `gorm:"column:is_pinned" json:"isPinned"`
|
||||
// IsPrivateChat bool `gorm:"column:is_private_chat" json:"isPrivateChat"`
|
||||
// BurnDuration int32 `gorm:"column:burn_duration;default:30" json:"burnDuration"`
|
||||
// GroupAtType int32 `gorm:"column:group_at_type" json:"groupAtType"`
|
||||
// IsNotInGroup bool `gorm:"column:is_not_in_group" json:"isNotInGroup"`
|
||||
// UpdateUnreadCountTime int64 `gorm:"column:update_unread_count_time" json:"updateUnreadCountTime"`
|
||||
// AttachedInfo string `gorm:"column:attached_info;type:varchar(1024)" json:"attachedInfo"`
|
||||
// Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`
|
||||
//}
|
||||
|
||||
func (Conversation) TableName() string {
|
||||
return "conversations"
|
||||
type Conversation interface {
|
||||
Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||
Delete(ctx context.Context, groupIDs []string, tx ...any) (err error)
|
||||
UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error)
|
||||
Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||
Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error)
|
||||
FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) ([]string, error)
|
||||
FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) ([]string, error)
|
||||
Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error)
|
||||
FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error)
|
||||
}
|
||||
type ConversationGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func SetConversation(conversation Conversation) (bool, error) {
|
||||
var isUpdate bool
|
||||
newConversation := conversation
|
||||
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
|
||||
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
|
||||
// if exist, then update record
|
||||
} else {
|
||||
//force update
|
||||
isUpdate = true
|
||||
return isUpdate, ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
|
||||
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt, "is_pinned": conversation.IsPinned, "is_private_chat": conversation.IsPrivateChat,
|
||||
"group_at_type": conversation.GroupAtType, "is_not_in_group": conversation.IsNotInGroup}).Error
|
||||
}
|
||||
}
|
||||
func SetOneConversation(conversation Conversation) error {
|
||||
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
|
||||
|
||||
func NewConversationGorm(DB *gorm.DB) Conversation {
|
||||
return &ConversationGorm{DB: DB}
|
||||
}
|
||||
|
||||
func PeerUserSetConversation(conversation Conversation) error {
|
||||
newConversation := conversation
|
||||
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
|
||||
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
|
||||
// if exist, then update record
|
||||
}
|
||||
//force update
|
||||
return ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
|
||||
Updates(map[string]interface{}{"is_private_chat": conversation.IsPrivateChat}).Error
|
||||
|
||||
func (c *ConversationGorm) Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Create(&conversations).Error, "")
|
||||
}
|
||||
|
||||
func SetRecvMsgOpt(conversation Conversation) (bool, error) {
|
||||
var isUpdate bool
|
||||
newConversation := conversation
|
||||
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
|
||||
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
|
||||
// if exist, then update record
|
||||
} else {
|
||||
//force update
|
||||
isUpdate = true
|
||||
return isUpdate, ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
|
||||
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt}).Error
|
||||
}
|
||||
func (c *ConversationGorm) Delete(ctx context.Context, groupIDs []string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.ConversationModel{}).Error, "")
|
||||
}
|
||||
|
||||
func GetUserAllConversations(ownerUserID string) ([]Conversation, error) {
|
||||
var conversations []Conversation
|
||||
err := ConversationDB.Where("owner_user_id=?", ownerUserID).Find(&conversations).Error
|
||||
return conversations, err
|
||||
}
|
||||
func GetMultipleUserConversationByConversationID(ownerUserIDList []string, conversationID string) ([]Conversation, error) {
|
||||
var conversations []Conversation
|
||||
err := ConversationDB.Where("owner_user_id IN ? and conversation_id=?", ownerUserIDList, conversationID).Find(&conversations).Error
|
||||
return conversations, err
|
||||
}
|
||||
func GetExistConversationUserIDList(ownerUserIDList []string, conversationID string) ([]string, error) {
|
||||
var resultArr []string
|
||||
err := ConversationDB.Table("conversations").Where(" owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Pluck("owner_user_id", &resultArr).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resultArr, nil
|
||||
func (c *ConversationGorm) UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDList", userIDList, "conversationID", conversationID)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func GetConversation(OwnerUserID, conversationID string) (Conversation, error) {
|
||||
var conversation Conversation
|
||||
err := ConversationDB.Table("conversations").Where("owner_user_id=? and conversation_id=?", OwnerUserID, conversationID).Take(&conversation).Error
|
||||
return conversation, err
|
||||
func (c *ConversationGorm) Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Updates(&conversations).Error, "")
|
||||
}
|
||||
|
||||
func GetConversations(OwnerUserID string, conversationIDs []string) ([]Conversation, error) {
|
||||
var conversations []Conversation
|
||||
err := ConversationDB.Model(&Conversation{}).Where("conversation_id IN (?) and owner_user_id=?", conversationIDs, OwnerUserID).Find(&conversations).Error
|
||||
func (c *ConversationGorm) Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "groups", conversations)
|
||||
}()
|
||||
err = utils.Wrap(getDBConn(c.DB, tx).Where("owner_user_id=? and conversation_id IN (?)", ownerUserID, conversationIDs).Find(&conversations).Error, "")
|
||||
return conversations, err
|
||||
}
|
||||
|
||||
func GetConversationsByConversationIDMultipleOwner(OwnerUserIDList []string, conversationID string) ([]Conversation, error) {
|
||||
var conversations []Conversation
|
||||
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", OwnerUserIDList, conversationID).Find(&conversations).Error
|
||||
return conversations, err
|
||||
func (c *ConversationGorm) Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error) {
|
||||
cc := &relation.ConversationModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "conversation", *conversation)
|
||||
}()
|
||||
return cc, utils.Wrap(getDBConn(c.DB, tx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error, "")
|
||||
}
|
||||
|
||||
func UpdateColumnsConversations(ownerUserIDList []string, conversationID string, args map[string]interface{}) error {
|
||||
return ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Updates(args).Error
|
||||
|
||||
func (c *ConversationGorm) FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) (existUserID []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userIDList, "existUserID", existUserID)
|
||||
}()
|
||||
return existUserID, utils.Wrap(getDBConn(c.DB, tx).Where(" owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Pluck("owner_user_id", &existUserID).Error, "")
|
||||
}
|
||||
|
||||
func GetConversationIDListByUserID(userID string) ([]string, error) {
|
||||
var IDList []string
|
||||
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &IDList).Error
|
||||
return IDList, err
|
||||
func (c *ConversationGorm) FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "existConversationIDList", existConversationID)
|
||||
}()
|
||||
return existConversationID, utils.Wrap(getDBConn(c.DB, tx).Where(" conversation_id IN (?) and owner_user_id=?", conversationIDList, userID).Pluck("conversation_id", &existConversationID).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) (conversationIDList []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "conversationIDList", conversationIDList)
|
||||
}()
|
||||
return conversationIDList, utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &conversationIDList).Error, "")
|
||||
}
|
||||
|
@ -1,100 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Conversation interface {
|
||||
TableName() string
|
||||
Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||
Delete(ctx context.Context, groupIDs []string) (err error)
|
||||
UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error)
|
||||
Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||
Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error)
|
||||
FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) ([]string, error)
|
||||
FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) ([]string, error)
|
||||
Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error)
|
||||
FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error)
|
||||
}
|
||||
type ConversationGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) TableName() string {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewConversationGorm(DB *gorm.DB) Conversation {
|
||||
return &ConversationGorm{DB: DB}
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Create(&conversations).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Delete(ctx context.Context, groupIDs []string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.ConversationModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDList", userIDList, "conversationID", conversationID)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Updates(&conversations).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error) {
|
||||
var newConversations []relation.ConversationModel
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "groups", conversations)
|
||||
}()
|
||||
err = utils.Wrap(getDBConn(c.DB, tx).Where("owner_user_id=? and conversation_id IN (?)", ownerUserID, conversationIDs).Find(&newConversations).Error, "")
|
||||
for _, v := range newConversations {
|
||||
v1 := v
|
||||
conversations = append(conversations, &v1)
|
||||
}
|
||||
return conversations, err
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error) {
|
||||
cc := &relation.ConversationModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "conversation", *conversation)
|
||||
}()
|
||||
return cc, utils.Wrap(getDBConn(c.DB, tx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) (existUserID []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userIDList, "existUserID", existUserID)
|
||||
}()
|
||||
return existUserID, utils.Wrap(getDBConn(c.DB, tx).Where(" owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Pluck("owner_user_id", &existUserID).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "existConversationIDList", existConversationID)
|
||||
}()
|
||||
return existConversationID, utils.Wrap(getDBConn(c.DB, tx).Where(" conversation_id IN (?) and owner_user_id=?", conversationIDList, userID).Pluck("conversation_id", &existConversationID).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) (conversationIDList []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "conversationIDList", conversationIDList)
|
||||
}()
|
||||
return conversationIDList, utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &conversationIDList).Error, "")
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var RegisterDB *gorm.DB
|
||||
|
||||
type Register struct {
|
||||
Account string `gorm:"column:account;primary_key;type:char(255)" json:"account"`
|
||||
Password string `gorm:"column:password;type:varchar(255)" json:"password"`
|
||||
Ex string `gorm:"column:ex;size:1024" json:"ex"`
|
||||
UserID string `gorm:"column:user_id;type:varchar(255)" json:"userID"`
|
||||
AreaCode string `gorm:"column:area_code;type:varchar(255)"`
|
||||
InvitationCode string `gorm:"column:invitation_code;type:varchar(255)"`
|
||||
RegisterIP string `gorm:"column:register_ip;type:varchar(255)"`
|
||||
}
|
||||
|
||||
func GetRegister(account, areaCode, userID string) (*Register, error) {
|
||||
var r Register
|
||||
return &r, RegisterDB.Table("registers").Where("user_id = ? and user_id != ? or account = ? or account =? and area_code=?",
|
||||
userID, "", account, account, areaCode).Take(&r).Error
|
||||
}
|
||||
|
||||
func SetPassword(account, password, ex, userID, areaCode, ip string) error {
|
||||
r := Register{
|
||||
Account: account,
|
||||
Password: password,
|
||||
Ex: ex,
|
||||
UserID: userID,
|
||||
RegisterIP: ip,
|
||||
AreaCode: areaCode,
|
||||
}
|
||||
return RegisterDB.Table("registers").Create(&r).Error
|
||||
}
|
||||
|
||||
func ResetPassword(account, password string) error {
|
||||
r := Register{
|
||||
Password: password,
|
||||
}
|
||||
return RegisterDB.Table("registers").Where("account = ?", account).Updates(&r).Error
|
||||
}
|
@ -1,83 +1,135 @@
|
||||
package relation
|
||||
|
||||
//import (
|
||||
// "fmt"
|
||||
// "time"
|
||||
//)
|
||||
//
|
||||
//func InsertToFriend(toInsertFollow *Friend) error {
|
||||
// toInsertFollow.CreateTime = time.Now()
|
||||
// err := FriendDB.Table("friends").Create(toInsertFollow).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*Friend, error) {
|
||||
// var friend Friend
|
||||
// err := FriendDB.Table("friends").Where("owner_user_id=? and friend_user_id=?", OwnerUserID, FriendUserID).Take(&friend).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return &friend, err
|
||||
//}
|
||||
//
|
||||
//func GetFriendListByUserID(OwnerUserID string) ([]Friend, error) {
|
||||
// var friends []Friend
|
||||
// var x Friend
|
||||
// x.OwnerUserID = OwnerUserID
|
||||
// err := FriendDB.Table("friends").Where("owner_user_id=?", OwnerUserID).Find(&friends).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return friends, nil
|
||||
//}
|
||||
//
|
||||
//func GetFriendIDListByUserID(OwnerUserID string) ([]string, error) {
|
||||
// var friendIDList []string
|
||||
// err := FriendDB.Table("friends").Where("owner_user_id=?", OwnerUserID).Pluck("friend_user_id", &friendIDList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return friendIDList, nil
|
||||
//}
|
||||
//
|
||||
//func UpdateFriendComment(OwnerUserID, FriendUserID, Remark string) error {
|
||||
// return FriendDB.Exec("update friends set remark=? where owner_user_id=? and friend_user_id=?", Remark, OwnerUserID, FriendUserID).Error
|
||||
//}
|
||||
//
|
||||
//func DeleteSingleFriendInfo(OwnerUserID, FriendUserID string) error {
|
||||
// return FriendDB.Table("friends").Where("owner_user_id=? and friend_user_id=?", OwnerUserID, FriendUserID).Delete(Friend{}).Error
|
||||
//}
|
||||
//
|
||||
//type FriendUser struct {
|
||||
// Friend
|
||||
// Nickname string `gorm:"column:name;size:255"`
|
||||
//}
|
||||
//
|
||||
//func GetUserFriendsCMS(ownerUserID, friendUserName string, pageNumber, showNumber int32) (friendUserList []*FriendUser, count int64, err error) {
|
||||
// DB := FriendDB.Table("friends").
|
||||
// Select("friends.*, users.name").
|
||||
// Where("friends.owner_user_id=?", ownerUserID).Limit(int(showNumber)).
|
||||
// Joins("left join users on friends.friend_user_id = users.user_id").
|
||||
// Offset(int(showNumber * (pageNumber - 1)))
|
||||
// if friendUserName != "" {
|
||||
// DB = DB.Where("users.name like ?", fmt.Sprintf("%%%s%%", friendUserName))
|
||||
// }
|
||||
// if err = DB.Count(&count).Error; err != nil {
|
||||
// return
|
||||
// }
|
||||
// err = DB.Find(&friendUserList).Error
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//func GetFriendByIDCMS(ownerUserID, friendUserID string) (friendUser *FriendUser, err error) {
|
||||
// friendUser = &FriendUser{}
|
||||
// err = FriendDB.Table("friends").
|
||||
// Select("friends.*, users.name").
|
||||
// Where("friends.owner_user_id=? and friends.friend_user_id=?", ownerUserID, friendUserID).
|
||||
// Joins("left join users on friends.friend_user_id = users.user_id").
|
||||
// Take(friendUser).Error
|
||||
// return friendUser, err
|
||||
//}
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FriendDB interface {
|
||||
Create(ctx context.Context, friends []*relation.FriendModel) (err error)
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, friends []*relation.FriendModel) (err error)
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.FriendModel, err error)
|
||||
}
|
||||
|
||||
type FriendGorm struct {
|
||||
DB *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
func NewFriendGorm(DB *gorm.DB) *FriendGorm {
|
||||
return &FriendGorm{DB: DB}
|
||||
}
|
||||
|
||||
type FriendUser struct {
|
||||
FriendGorm
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
}
|
||||
|
||||
// 插入多条记录
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 删除ownerUserID指定的好友
|
||||
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs)
|
||||
}()
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? AND friend_user_id in ( ?)", ownerUserID, friendUserIDs).Delete(&relation.FriendModel{}).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新ownerUserID单个好友信息 更新零值
|
||||
func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ? AND friend_user_id = ? ", ownerUserID, friendUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
// 更新好友信息的非零值
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Updates(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 更新好友备注(也支持零值 )
|
||||
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark)
|
||||
}()
|
||||
if remark != "" {
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
|
||||
}
|
||||
m := make(map[string]interface{}, 1)
|
||||
m["remark"] = ""
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "")
|
||||
|
||||
}
|
||||
|
||||
// 获取单个好友信息,如没找到 返回错误
|
||||
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string, tx ...any) (friend *relation.FriendModel, err error) {
|
||||
friend = &relation.FriendModel{}
|
||||
defer tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", *friend)
|
||||
return friend, utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
}
|
||||
|
||||
// 查找好友关系,如果是双向关系,则都返回
|
||||
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string, tx ...any) (friends []*relation.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID1", userID1, "userID2", userID2, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("(owner_user_id = ? and friend_user_id = ?) or (owner_user_id = ? and friend_user_id = ?)", userID1, userID2, userID2, userID1).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取 owner指定的好友列表 如果有friendUserIDs不存在,也不返回错误
|
||||
func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...any) (friends []*relation.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "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 如果有ownerUserIDs不存在,也不返回错误
|
||||
func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string, tx ...any) (friends []*relation.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendUserID", friendUserID, "ownerUserIDs", ownerUserIDs, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("friend_user_id = ? AND owner_user_id in (?)", friendUserID, ownerUserIDs).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取ownerUserID好友列表 支持翻页
|
||||
func (f *FriendGorm) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32, tx ...any) (friends []*relation.FriendModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
|
||||
}()
|
||||
err = getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ? ", ownerUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? ", ownerUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取哪些人添加了friendUserID 支持翻页
|
||||
func (f *FriendGorm) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32, tx ...any) (friends []*relation.FriendModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendUserID", friendUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
|
||||
}()
|
||||
err = getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("friend_user_id = ? ", friendUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Where("friend_user_id = ? ", friendUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
@ -1,135 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FriendDB interface {
|
||||
Create(ctx context.Context, friends []*relation.FriendModel) (err error)
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, friends []*relation.FriendModel) (err error)
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.FriendModel, err error)
|
||||
}
|
||||
|
||||
type FriendGorm struct {
|
||||
DB *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
func NewFriendGorm(DB *gorm.DB) *FriendGorm {
|
||||
return &FriendGorm{DB: DB}
|
||||
}
|
||||
|
||||
type FriendUser struct {
|
||||
FriendGorm
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
}
|
||||
|
||||
// 插入多条记录
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 删除ownerUserID指定的好友
|
||||
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs)
|
||||
}()
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? AND friend_user_id in ( ?)", ownerUserID, friendUserIDs).Delete(&relation.FriendModel{}).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新ownerUserID单个好友信息 更新零值
|
||||
func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ? AND friend_user_id = ? ", ownerUserID, friendUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
// 更新好友信息的非零值
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Updates(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 更新好友备注(也支持零值 )
|
||||
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark)
|
||||
}()
|
||||
if remark != "" {
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
|
||||
}
|
||||
m := make(map[string]interface{}, 1)
|
||||
m["remark"] = ""
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "")
|
||||
|
||||
}
|
||||
|
||||
// 获取单个好友信息,如没找到 返回错误
|
||||
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string, tx ...any) (friend *relation.FriendModel, err error) {
|
||||
friend = &relation.FriendModel{}
|
||||
defer tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", *friend)
|
||||
return friend, utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
}
|
||||
|
||||
// 查找好友关系,如果是双向关系,则都返回
|
||||
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string, tx ...any) (friends []*relation.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID1", userID1, "userID2", userID2, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("(owner_user_id = ? and friend_user_id = ?) or (owner_user_id = ? and friend_user_id = ?)", userID1, userID2, userID2, userID1).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取 owner指定的好友列表 如果有friendUserIDs不存在,也不返回错误
|
||||
func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...any) (friends []*relation.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "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 如果有ownerUserIDs不存在,也不返回错误
|
||||
func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string, tx ...any) (friends []*relation.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendUserID", friendUserID, "ownerUserIDs", ownerUserIDs, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("friend_user_id = ? AND owner_user_id in (?)", friendUserID, ownerUserIDs).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取ownerUserID好友列表 支持翻页
|
||||
func (f *FriendGorm) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32, tx ...any) (friends []*relation.FriendModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
|
||||
}()
|
||||
err = getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("owner_user_id = ? ", ownerUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? ", ownerUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取哪些人添加了friendUserID 支持翻页
|
||||
func (f *FriendGorm) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32, tx ...any) (friends []*relation.FriendModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendUserID", friendUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
|
||||
}()
|
||||
err = getDBConn(f.DB, tx).Model(&relation.FriendModel{}).Where("friend_user_id = ? ", friendUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Where("friend_user_id = ? ", friendUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
@ -21,7 +21,7 @@ type FriendRequestGorm struct {
|
||||
}
|
||||
|
||||
// 插入多条记录
|
||||
func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel, tx ...*gorm.DB) (err error) {
|
||||
func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendRequests", friendRequests)
|
||||
}()
|
||||
@ -29,7 +29,7 @@ func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relati
|
||||
}
|
||||
|
||||
// 删除记录
|
||||
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string, tx ...*gorm.DB) (err error) {
|
||||
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "fromUserID", fromUserID, "toUserID", toUserID)
|
||||
}()
|
||||
@ -37,7 +37,7 @@ func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID str
|
||||
}
|
||||
|
||||
// 更新零值
|
||||
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "formUserID", formUserID, "toUserID", toUserID, "args", args)
|
||||
}()
|
||||
@ -45,7 +45,7 @@ func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string,
|
||||
}
|
||||
|
||||
// 更新多条记录 (非零值)
|
||||
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relation.FriendRequestModel, tx ...*gorm.DB) (err error) {
|
||||
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relation.FriendRequestModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendRequests", friendRequests)
|
||||
}()
|
||||
@ -53,7 +53,7 @@ func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relati
|
||||
}
|
||||
|
||||
// 获取来指定用户的好友申请 未找到 不返回错误
|
||||
func (f *FriendRequestGorm) Find(ctx context.Context, fromUserID, toUserID string, tx ...*gorm.DB) (friendRequest *relation.FriendRequestModel, err error) {
|
||||
func (f *FriendRequestGorm) Find(ctx context.Context, fromUserID, toUserID string, tx ...any) (friendRequest *relation.FriendRequestModel, err error) {
|
||||
friendRequest = &relation.FriendRequestModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "fromUserID", fromUserID, "toUserID", toUserID, "friendRequest", *friendRequest)
|
||||
@ -62,7 +62,7 @@ func (f *FriendRequestGorm) Find(ctx context.Context, fromUserID, toUserID strin
|
||||
return
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string, tx ...*gorm.DB) (friendRequest *relation.FriendRequestModel, err error) {
|
||||
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string, tx ...any) (friendRequest *relation.FriendRequestModel, err error) {
|
||||
friendRequest = &relation.FriendRequestModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "fromUserID", fromUserID, "toUserID", toUserID, "friendRequest", *friendRequest)
|
||||
@ -72,7 +72,7 @@ func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID strin
|
||||
}
|
||||
|
||||
// 获取toUserID收到的好友申请列表
|
||||
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
|
||||
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, pageNumber, showNumber int32, tx ...any) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "toUserID", toUserID, "friendRequests", friendRequests)
|
||||
}()
|
||||
@ -86,7 +86,7 @@ func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, p
|
||||
}
|
||||
|
||||
// 获取fromUserID发出去的好友申请列表
|
||||
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
|
||||
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string, pageNumber, showNumber int32, tx ...any) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "fromUserID", fromUserID, "friendRequests", friendRequests)
|
||||
}()
|
||||
|
@ -1,204 +1,109 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitManager() {
|
||||
for k, v := range config.Config.Manager.AppManagerUid {
|
||||
_, err := GetUserByUserID(v)
|
||||
if err != nil {
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
var appMgr User
|
||||
appMgr.UserID = v
|
||||
if k == 0 {
|
||||
appMgr.Nickname = config.Config.Manager.AppSysNotificationName
|
||||
} else {
|
||||
appMgr.Nickname = "AppManager" + utils.IntToString(k+1)
|
||||
}
|
||||
appMgr.AppMangerLevel = constant.AppAdmin
|
||||
err = UserRegister(appMgr)
|
||||
if err != nil {
|
||||
fmt.Println("AppManager insert error ", err.Error(), appMgr)
|
||||
} else {
|
||||
fmt.Println("AppManager insert ", appMgr)
|
||||
}
|
||||
}
|
||||
type UserGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func UserRegister(user User) error {
|
||||
user.CreateTime = time.Now()
|
||||
if user.AppMangerLevel == 0 {
|
||||
user.AppMangerLevel = constant.AppOrdinaryUsers
|
||||
}
|
||||
if user.Birth.Unix() < 0 {
|
||||
user.Birth = utils.UnixSecondToTime(0)
|
||||
}
|
||||
err := UserDB.Table("users").Create(&user).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
func NewUserGorm(db *gorm.DB) *UserGorm {
|
||||
var user UserGorm
|
||||
user.DB = db
|
||||
return &user
|
||||
}
|
||||
|
||||
func GetAllUser() ([]User, error) {
|
||||
var userList []User
|
||||
err := UserDB.Table("users").Find(&userList).Error
|
||||
return userList, err
|
||||
// 插入多条
|
||||
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, "")
|
||||
}
|
||||
|
||||
func TakeUserByUserID(userID string) (*User, error) {
|
||||
var user User
|
||||
err := UserDB.Table("users").Where("user_id=?", userID).Take(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
// 更新用户信息 零值
|
||||
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, "")
|
||||
}
|
||||
|
||||
func GetUserByUserID(userID string) (*User, error) {
|
||||
var user User
|
||||
err := UserDB.Table("users").Where("user_id=?", userID).Take(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
// 更新多个用户信息 非零值
|
||||
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, "")
|
||||
}
|
||||
|
||||
func GetUsersByUserIDList(userIDList []string) ([]*User, error) {
|
||||
var userList []*User
|
||||
err := UserDB.Table("users").Where("user_id in (?)", userIDList).Find(&userList).Error
|
||||
return userList, err
|
||||
}
|
||||
|
||||
func GetUserNameByUserID(userID string) (string, error) {
|
||||
var user User
|
||||
err := UserDB.Table("users").Select("name").Where("user_id=?", userID).First(&user).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return user.Nickname, nil
|
||||
}
|
||||
|
||||
func UpdateUserInfo(user User) error {
|
||||
return UserDB.Where("user_id=?", user.UserID).Updates(&user).Error
|
||||
}
|
||||
|
||||
func UpdateUserInfoByMap(user User, m map[string]interface{}) error {
|
||||
err := UserDB.Where("user_id=?", user.UserID).Updates(m).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func SelectAllUserID() ([]string, error) {
|
||||
var resultArr []string
|
||||
err := UserDB.Pluck("user_id", &resultArr).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resultArr, nil
|
||||
}
|
||||
|
||||
func SelectSomeUserID(userIDList []string) ([]string, error) {
|
||||
var resultArr []string
|
||||
err := UserDB.Pluck("user_id", &resultArr).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resultArr, nil
|
||||
}
|
||||
|
||||
func GetUsers(showNumber, pageNumber int32) ([]User, error) {
|
||||
var users []User
|
||||
err := UserDB.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
|
||||
if err != nil {
|
||||
return users, err
|
||||
}
|
||||
// 获取指定用户信息 不存在,也不返回错误
|
||||
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
|
||||
}
|
||||
|
||||
func AddUser(userID string, phoneNumber string, name string, email string, gender int32, faceURL string, birth string) error {
|
||||
_birth, err := utils.TimeStringToTime(birth)
|
||||
// 获取某个用户信息 不存在,则返回错误
|
||||
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
|
||||
}
|
||||
|
||||
// 通过名字查找用户 不存在,不返回错误
|
||||
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, "")
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
user := User{
|
||||
UserID: userID,
|
||||
Nickname: name,
|
||||
FaceURL: faceURL,
|
||||
Gender: gender,
|
||||
PhoneNumber: phoneNumber,
|
||||
Birth: _birth,
|
||||
Email: email,
|
||||
Ex: "",
|
||||
CreateTime: time.Now(),
|
||||
}
|
||||
result := UserDB.Create(&user)
|
||||
return result.Error
|
||||
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
|
||||
}
|
||||
|
||||
func UsersIsBlock(userIDList []string) (inBlockUserIDList []string, err error) {
|
||||
err = BlackListDB.Where("uid in (?) and end_disable_time > now()", userIDList).Pluck("uid", &inBlockUserIDList).Error
|
||||
return inBlockUserIDList, err
|
||||
// 通过名字或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
|
||||
}
|
||||
err = utils.Wrap(db.Limit(int(showNumber)).Offset(int(showNumber*pageNumber)).Find(&users).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
type BlockUserInfo struct {
|
||||
User User
|
||||
BeginDisableTime time.Time
|
||||
EndDisableTime time.Time
|
||||
// 获取用户信息 不存在,不返回错误
|
||||
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, "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = utils.Wrap(getDBConn(u.DB, tx).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
func GetUserByName(userName string, showNumber, pageNumber int32) ([]User, error) {
|
||||
var users []User
|
||||
err := UserDB.Where(" name like ?", fmt.Sprintf("%%%s%%", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
|
||||
return users, err
|
||||
}
|
||||
|
||||
func GetUsersByNameAndID(content string, showNumber, pageNumber int32) ([]User, int64, error) {
|
||||
var users []User
|
||||
var count int64
|
||||
db := UserDB.Where(" name like ? or user_id = ? ", fmt.Sprintf("%%%s%%", content), content)
|
||||
if err := db.Count(&count).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
err := db.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
|
||||
return users, count, err
|
||||
}
|
||||
|
||||
func GetUserIDsByEmailAndID(phoneNumber, email string) ([]string, error) {
|
||||
if phoneNumber == "" && email == "" {
|
||||
return nil, nil
|
||||
}
|
||||
db := UserDB
|
||||
if phoneNumber != "" {
|
||||
db = db.Where("phone_number = ? ", phoneNumber)
|
||||
}
|
||||
if email != "" {
|
||||
db = db.Where("email = ? ", email)
|
||||
}
|
||||
var userIDList []string
|
||||
err := db.Pluck("user_id", &userIDList).Error
|
||||
return userIDList, err
|
||||
}
|
||||
|
||||
func GetUsersCount(userName string) (int32, error) {
|
||||
var count int64
|
||||
if err := UserDB.Where(" name like ? ", fmt.Sprintf("%%%s%%", userName)).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(count), nil
|
||||
}
|
||||
|
||||
func GetBlockUsersNumCount() (int32, error) {
|
||||
var count int64
|
||||
if err := BlackListDB.Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(count), nil
|
||||
// 获取所有用户ID
|
||||
func (u *UserGorm) GetAllUserID(ctx context.Context) ([]string, error) {
|
||||
var userIDs []string
|
||||
err := u.DB.Pluck("user_id", &userIDs).Error
|
||||
return userIDs, err
|
||||
}
|
||||
|
@ -1,109 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"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
|
||||
}
|
||||
|
||||
// 插入多条
|
||||
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, "")
|
||||
}
|
||||
|
||||
// 更新用户信息 零值
|
||||
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, "")
|
||||
}
|
||||
|
||||
// 更新多个用户信息 非零值
|
||||
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, "")
|
||||
}
|
||||
|
||||
// 获取指定用户信息 不存在,也不返回错误
|
||||
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
|
||||
}
|
||||
|
||||
// 获取某个用户信息 不存在,则返回错误
|
||||
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
|
||||
}
|
||||
|
||||
// 通过名字查找用户 不存在,不返回错误
|
||||
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, "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 通过名字或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
|
||||
}
|
||||
err = utils.Wrap(db.Limit(int(showNumber)).Offset(int(showNumber*pageNumber)).Find(&users).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户信息 不存在,不返回错误
|
||||
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, "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = utils.Wrap(getDBConn(u.DB, tx).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取所有用户ID
|
||||
func (u *UserGorm) GetAllUserID(ctx context.Context) ([]string, error) {
|
||||
var userIDs []string
|
||||
err := u.DB.Pluck("user_id", &userIDs).Error
|
||||
return userIDs, err
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user