parent
ff542e83fe
commit
5e6f9448bc
@ -1,123 +1,90 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"Open_IM/pkg/common/db/table/relation"
|
||||||
|
"Open_IM/pkg/common/tracelog"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"context"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ConversationDB *gorm.DB
|
type Conversation interface {
|
||||||
|
Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||||
//type Conversation struct {
|
Delete(ctx context.Context, groupIDs []string, tx ...any) (err error)
|
||||||
// OwnerUserID string `gorm:"column:owner_user_id;primary_key;type:char(128)" json:"OwnerUserID"`
|
UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error)
|
||||||
// ConversationID string `gorm:"column:conversation_id;primary_key;type:char(128)" json:"conversationID"`
|
Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||||
// ConversationType int32 `gorm:"column:conversation_type" json:"conversationType"`
|
Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error)
|
||||||
// UserID string `gorm:"column:user_id;type:char(64)" json:"userID"`
|
FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) ([]string, error)
|
||||||
// GroupID string `gorm:"column:group_id;type:char(128)" json:"groupID"`
|
FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) ([]string, error)
|
||||||
// RecvMsgOpt int32 `gorm:"column:recv_msg_opt" json:"recvMsgOpt"`
|
Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error)
|
||||||
// UnreadCount int32 `gorm:"column:unread_count" json:"unreadCount"`
|
FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error)
|
||||||
// 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 ConversationGorm struct {
|
||||||
func SetConversation(conversation Conversation) (bool, error) {
|
DB *gorm.DB
|
||||||
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 {
|
func (c *ConversationGorm) Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||||
newConversation := conversation
|
defer func() {
|
||||||
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||||
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
|
}()
|
||||||
// if exist, then update record
|
return utils.Wrap(getDBConn(c.DB, tx).Create(&conversations).Error, "")
|
||||||
}
|
|
||||||
//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 SetRecvMsgOpt(conversation Conversation) (bool, error) {
|
func (c *ConversationGorm) Delete(ctx context.Context, groupIDs []string, tx ...any) (err error) {
|
||||||
var isUpdate bool
|
defer func() {
|
||||||
newConversation := conversation
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||||
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
|
}()
|
||||||
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
|
return utils.Wrap(getDBConn(c.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.ConversationModel{}).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 GetUserAllConversations(ownerUserID string) ([]Conversation, error) {
|
func (c *ConversationGorm) UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error) {
|
||||||
var conversations []Conversation
|
defer func() {
|
||||||
err := ConversationDB.Where("owner_user_id=?", ownerUserID).Find(&conversations).Error
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDList", userIDList, "conversationID", conversationID)
|
||||||
return conversations, err
|
}()
|
||||||
}
|
return utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args).Error, "")
|
||||||
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 GetConversation(OwnerUserID, conversationID string) (Conversation, error) {
|
func (c *ConversationGorm) Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||||
var conversation Conversation
|
defer func() {
|
||||||
err := ConversationDB.Table("conversations").Where("owner_user_id=? and conversation_id=?", OwnerUserID, conversationID).Take(&conversation).Error
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||||
return conversation, err
|
}()
|
||||||
|
return utils.Wrap(getDBConn(c.DB, tx).Updates(&conversations).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConversations(OwnerUserID string, conversationIDs []string) ([]Conversation, error) {
|
func (c *ConversationGorm) Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error) {
|
||||||
var conversations []Conversation
|
defer func() {
|
||||||
err := ConversationDB.Model(&Conversation{}).Where("conversation_id IN (?) and owner_user_id=?", conversationIDs, OwnerUserID).Find(&conversations).Error
|
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
|
return conversations, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConversationsByConversationIDMultipleOwner(OwnerUserIDList []string, conversationID string) ([]Conversation, error) {
|
func (c *ConversationGorm) Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error) {
|
||||||
var conversations []Conversation
|
cc := &relation.ConversationModel{}
|
||||||
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", OwnerUserIDList, conversationID).Find(&conversations).Error
|
defer func() {
|
||||||
return conversations, err
|
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) {
|
||||||
func UpdateColumnsConversations(ownerUserIDList []string, conversationID string, args map[string]interface{}) error {
|
defer func() {
|
||||||
return ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Updates(args).Error
|
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) {
|
||||||
func GetConversationIDListByUserID(userID string) ([]string, error) {
|
defer func() {
|
||||||
var IDList []string
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "existConversationIDList", existConversationID)
|
||||||
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &IDList).Error
|
}()
|
||||||
return IDList, err
|
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,204 +1,109 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"Open_IM/pkg/common/config"
|
"Open_IM/pkg/common/db/table/relation"
|
||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/tracelog"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitManager() {
|
type UserGorm struct {
|
||||||
for k, v := range config.Config.Manager.AppManagerUid {
|
DB *gorm.DB
|
||||||
_, 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserRegister(user User) error {
|
func NewUserGorm(db *gorm.DB) *UserGorm {
|
||||||
user.CreateTime = time.Now()
|
var user UserGorm
|
||||||
if user.AppMangerLevel == 0 {
|
user.DB = db
|
||||||
user.AppMangerLevel = constant.AppOrdinaryUsers
|
return &user
|
||||||
}
|
|
||||||
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 GetAllUser() ([]User, error) {
|
// 插入多条
|
||||||
var userList []User
|
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel, tx ...any) (err error) {
|
||||||
err := UserDB.Table("users").Find(&userList).Error
|
defer func() {
|
||||||
return userList, err
|
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
|
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}, tx ...any) (err error) {
|
||||||
err := UserDB.Table("users").Where("user_id=?", userID).Take(&user).Error
|
defer func() {
|
||||||
if err != nil {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args)
|
||||||
return nil, err
|
}()
|
||||||
}
|
return utils.Wrap(getDBConn(u.DB, tx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Updates(args).Error, "")
|
||||||
return &user, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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 GetUsersByUserIDList(userIDList []string) ([]*User, error) {
|
// 更新多个用户信息 非零值
|
||||||
var userList []*User
|
func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel, tx ...any) (err error) {
|
||||||
err := UserDB.Table("users").Where("user_id in (?)", userIDList).Find(&userList).Error
|
defer func() {
|
||||||
return userList, err
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(u.DB, tx).Updates(&users).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserNameByUserID(userID string) (string, error) {
|
// 获取指定用户信息 不存在,也不返回错误
|
||||||
var user User
|
func (u *UserGorm) Find(ctx context.Context, userIDs []string, tx ...any) (users []*relation.UserModel, err error) {
|
||||||
err := UserDB.Table("users").Select("name").Where("user_id=?", userID).First(&user).Error
|
defer func() {
|
||||||
if err != nil {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs, "users", users)
|
||||||
return "", err
|
}()
|
||||||
}
|
err = utils.Wrap(getDBConn(u.DB, tx).Where("user_id in (?)", userIDs).Find(&users).Error, "")
|
||||||
return user.Nickname, nil
|
return users, err
|
||||||
}
|
|
||||||
|
|
||||||
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
|
func (u *UserGorm) Take(ctx context.Context, userID string, tx ...any) (user *relation.UserModel, err error) {
|
||||||
err := UserDB.Pluck("user_id", &resultArr).Error
|
user = &relation.UserModel{}
|
||||||
if err != nil {
|
defer func() {
|
||||||
return nil, err
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "user", *user)
|
||||||
}
|
}()
|
||||||
return resultArr, nil
|
err = utils.Wrap(getDBConn(u.DB, tx).Where("user_id = ?", userID).Take(&user).Error, "")
|
||||||
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func SelectSomeUserID(userIDList []string) ([]string, error) {
|
// 通过名字查找用户 不存在,不返回错误
|
||||||
var resultArr []string
|
func (u *UserGorm) GetByName(ctx context.Context, userName string, pageNumber, showNumber int32, tx ...any) (users []*relation.UserModel, count int64, err error) {
|
||||||
err := UserDB.Pluck("user_id", &resultArr).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 {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
return resultArr, nil
|
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 GetUsers(showNumber, pageNumber int32) ([]User, error) {
|
// 通过名字或userID查找用户 不存在,不返回错误
|
||||||
var users []User
|
func (u *UserGorm) GetByNameAndID(ctx context.Context, content string, pageNumber, showNumber int32, tx ...any) (users []*relation.UserModel, count int64, err error) {
|
||||||
err := UserDB.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
|
defer func() {
|
||||||
if err != nil {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "content", content, "pageNumber", pageNumber, "showNumber", showNumber, "users", users, "count", count)
|
||||||
return users, err
|
}()
|
||||||
|
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
|
||||||
}
|
}
|
||||||
return users, err
|
err = utils.Wrap(db.Limit(int(showNumber)).Offset(int(showNumber*pageNumber)).Find(&users).Error, "")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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) 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 {
|
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)
|
err = utils.Wrap(getDBConn(u.DB, tx).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
|
||||||
return result.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
|
|
||||||
}
|
|
||||||
|
|
||||||
type BlockUserInfo struct {
|
|
||||||
User User
|
|
||||||
BeginDisableTime time.Time
|
|
||||||
EndDisableTime time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserByName(userName string, showNumber, pageNumber int32) ([]User, error) {
|
// 获取所有用户ID
|
||||||
var users []User
|
func (u *UserGorm) GetAllUserID(ctx context.Context) ([]string, error) {
|
||||||
err := UserDB.Where(" name like ?", fmt.Sprintf("%%%s%%", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
|
var userIDs []string
|
||||||
return users, err
|
err := u.DB.Pluck("user_id", &userIDs).Error
|
||||||
}
|
return userIDs, 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
|
|
||||||
}
|
}
|
||||||
|
@ -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…
Reference in new issue