mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-01 07:35:58 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
# Conflicts: # cmd/api/main.go # internal/api/a2r/api2rpc.go # internal/apiresp/resp.go # internal/msgtransfer/online_history_msg_handler.go # internal/push/consumer_init.go # pkg/common/db/controller/conversation.go # pkg/common/mw/gin.go # pkg/statistics/statistics.go
This commit is contained in:
@@ -32,7 +32,7 @@ type ConversationDatabase interface {
|
||||
SetUsersConversationFiledTx(ctx context.Context, userIDList []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error
|
||||
}
|
||||
|
||||
func NewConversationDatabase(conversation relation.Conversation, cache cache.ConversationCache, tx tx.Tx) ConversationDatabase {
|
||||
func NewConversationDatabase(conversation relationTb.ConversationModelInterface, cache cache.ConversationCache, tx tx.Tx) ConversationDatabase {
|
||||
return &ConversationDataBase{
|
||||
conversationDB: conversation,
|
||||
cache: cache,
|
||||
@@ -41,7 +41,7 @@ func NewConversationDatabase(conversation relation.Conversation, cache cache.Con
|
||||
}
|
||||
|
||||
type ConversationDataBase struct {
|
||||
conversationDB relation.Conversation
|
||||
conversationDB relationTb.ConversationModelInterface
|
||||
cache cache.ConversationCache
|
||||
tx tx.Tx
|
||||
}
|
||||
|
||||
@@ -46,9 +46,8 @@ func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel
|
||||
}
|
||||
miss := utils.SliceAnySub(users, result, func(e *relation.UserModel) string { return e.UserID })
|
||||
if len(miss) > 0 {
|
||||
u.userDB.Create(ctx, miss)
|
||||
_ = u.userDB.Create(ctx, miss)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
)
|
||||
|
||||
type BlackGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewBlackGorm(db *gorm.DB) relation.BlackModelInterface {
|
||||
return &BlackGorm{db}
|
||||
return &BlackGorm{NewMetaDB(db, &relation.BlackModel{})}
|
||||
}
|
||||
|
||||
func (b *BlackGorm) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
||||
|
||||
@@ -14,11 +14,11 @@ import (
|
||||
)
|
||||
|
||||
type ChatLogGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewChatLogGorm(db *gorm.DB) relation.ChatLogModelInterface {
|
||||
return &ChatLogGorm{DB: db}
|
||||
return &ChatLogGorm{NewMetaDB(db, &relation.ChatLogModel{})}
|
||||
}
|
||||
|
||||
func (c *ChatLogGorm) Create(msg pbMsg.MsgDataToMQ) error {
|
||||
|
||||
@@ -8,29 +8,16 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Conversation interface {
|
||||
Create(ctx context.Context, conversations []*relation.ConversationModel) (err error)
|
||||
Delete(ctx context.Context, groupIDs []string) (err error)
|
||||
UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, conversations []*relation.ConversationModel) (err error)
|
||||
Find(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []*relation.ConversationModel, err error)
|
||||
FindUserID(ctx context.Context, userIDList []string, conversationID string) ([]string, error)
|
||||
FindUserIDAllConversationID(ctx context.Context, userID string) ([]string, error)
|
||||
Take(ctx context.Context, userID, conversationID string) (conversation *relation.ConversationModel, err error)
|
||||
FindConversationID(ctx context.Context, userID string, conversationIDList []string) (existConversationID []string, err error)
|
||||
FindRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error)
|
||||
NewTx(tx any) Conversation
|
||||
}
|
||||
type ConversationGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewConversationGorm(DB *gorm.DB) Conversation {
|
||||
return &ConversationGorm{DB: DB}
|
||||
func NewConversationGorm(db *gorm.DB) relation.ConversationModelInterface {
|
||||
return &ConversationGorm{NewMetaDB(db, &relation.ConversationModel{})}
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) NewTx(tx any) Conversation {
|
||||
return &ConversationGorm{DB: tx.(*gorm.DB)}
|
||||
func (c *ConversationGorm) NewTx(tx any) relation.ConversationModelInterface {
|
||||
return &ConversationGorm{NewMetaDB(tx.(*gorm.DB), &relation.ConversationModel{})}
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Create(ctx context.Context, conversations []*relation.ConversationModel) (err error) {
|
||||
|
||||
@@ -8,46 +8,46 @@ import (
|
||||
)
|
||||
|
||||
type FriendGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewFriendGorm(db *gorm.DB) relation.FriendModelInterface {
|
||||
return &FriendGorm{DB: db}
|
||||
return &FriendGorm{NewMetaDB(db, &relation.FriendModel{})}
|
||||
}
|
||||
|
||||
func (f *FriendGorm) NewTx(tx any) relation.FriendModelInterface {
|
||||
return &FriendGorm{DB: tx.(*gorm.DB)}
|
||||
return &FriendGorm{NewMetaDB(tx.(*gorm.DB), &relation.FriendModel{})}
|
||||
}
|
||||
|
||||
// 插入多条记录
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel) (err error) {
|
||||
return utils.Wrap(f.DB.Create(&friends).Error, "")
|
||||
return utils.Wrap(f.db(ctx).Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 删除ownerUserID指定的好友
|
||||
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
|
||||
err = utils.Wrap(f.DB.Where("owner_user_id = ? AND friend_user_id in ( ?)", ownerUserID, friendUserIDs).Delete(&relation.FriendModel{}).Error, "")
|
||||
err = utils.Wrap(f.db(ctx).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{}) (err error) {
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ? AND friend_user_id = ? ", ownerUserID, friendUserID).Updates(args).Error, "")
|
||||
return utils.Wrap(f.db(ctx).Where("owner_user_id = ? AND friend_user_id = ? ", ownerUserID, friendUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
// 更新好友信息的非零值
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel) (err error) {
|
||||
return utils.Wrap(f.DB.Updates(&friends).Error, "")
|
||||
return utils.Wrap(f.db(ctx).Updates(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 更新好友备注(也支持零值 )
|
||||
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||||
if remark != "" {
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
|
||||
return utils.Wrap(f.db(ctx).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(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "")
|
||||
return utils.Wrap(f.db(ctx).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,16 +7,16 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewFriendRequestGorm(db *gorm.DB) relation.FriendRequestModelInterface {
|
||||
return &FriendRequestGorm{db}
|
||||
type FriendRequestGorm struct {
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
type FriendRequestGorm struct {
|
||||
DB *gorm.DB
|
||||
func NewFriendRequestGorm(db *gorm.DB) relation.FriendRequestModelInterface {
|
||||
return &FriendRequestGorm{NewMetaDB(db, &relation.FriendModel{})}
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) NewTx(tx any) relation.FriendRequestModelInterface {
|
||||
return &FriendRequestGorm{DB: tx.(*gorm.DB)}
|
||||
return &FriendRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.FriendModel{})}
|
||||
}
|
||||
|
||||
// 插入多条记录
|
||||
|
||||
@@ -11,15 +11,15 @@ import (
|
||||
var _ relation.GroupMemberModelInterface = (*GroupMemberGorm)(nil)
|
||||
|
||||
type GroupMemberGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewGroupMemberDB(db *gorm.DB) relation.GroupMemberModelInterface {
|
||||
return &GroupMemberGorm{DB: db}
|
||||
return &GroupMemberGorm{NewMetaDB(db, &relation.GroupMemberModel{})}
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) NewTx(tx any) relation.GroupMemberModelInterface {
|
||||
return &GroupMemberGorm{DB: tx.(*gorm.DB)}
|
||||
return &GroupMemberGorm{NewMetaDB(tx.(*gorm.DB), &relation.GroupMemberModel{})}
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel) (err error) {
|
||||
|
||||
@@ -10,15 +10,15 @@ import (
|
||||
var _ relation.GroupModelInterface = (*GroupGorm)(nil)
|
||||
|
||||
type GroupGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
|
||||
return &GroupGorm{DB: db}
|
||||
return &GroupGorm{NewMetaDB(db, &relation.GroupModel{})}
|
||||
}
|
||||
|
||||
func (g *GroupGorm) NewTx(tx any) relation.GroupModelInterface {
|
||||
return &GroupGorm{DB: tx.(*gorm.DB)}
|
||||
return &GroupGorm{NewMetaDB(tx.(*gorm.DB), &relation.GroupModel{})}
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel) (err error) {
|
||||
|
||||
@@ -8,21 +8,19 @@ import (
|
||||
)
|
||||
|
||||
type GroupRequestGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) NewTx(tx any) relation.GroupRequestModelInterface {
|
||||
return &GroupRequestGorm{
|
||||
DB: tx.(*gorm.DB),
|
||||
}
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewGroupRequest(db *gorm.DB) relation.GroupRequestModelInterface {
|
||||
return &GroupRequestGorm{
|
||||
DB: db,
|
||||
NewMetaDB(db, &relation.GroupRequestModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) NewTx(tx any) relation.GroupRequestModelInterface {
|
||||
return &GroupRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.GroupRequestModel{})}
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel) (err error) {
|
||||
return utils.Wrap(g.DB.Create(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MetaDB struct {
|
||||
DB *gorm.DB
|
||||
table interface{}
|
||||
}
|
||||
|
||||
func NewMetaDB(db *gorm.DB, table any) *MetaDB {
|
||||
return &MetaDB{
|
||||
DB: db,
|
||||
table: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *MetaDB) db(ctx context.Context) *gorm.DB {
|
||||
return g.DB.WithContext(ctx).Model(g.table)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
@@ -9,7 +10,7 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/driver/mysql"
|
||||
"strings"
|
||||
gormUtils "gorm.io/gorm/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -39,16 +40,9 @@ func newMysqlGormDB() (*gorm.DB, error) {
|
||||
}
|
||||
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
|
||||
newLogger := logger.New(
|
||||
Writer{},
|
||||
logger.Config{
|
||||
SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
|
||||
LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
|
||||
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
},
|
||||
)
|
||||
sqlLogger := NewSqlLogger(logger.LogLevel(config.Config.Mysql.LogLevel), true, time.Duration(config.Config.Mysql.SlowThreshold)*time.Millisecond)
|
||||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
Logger: sqlLogger,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -84,14 +78,65 @@ func IsMysqlDuplicateKey(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type Writer struct{}
|
||||
type SqlLogger struct {
|
||||
LogLevel logger.LogLevel
|
||||
IgnoreRecordNotFoundError bool
|
||||
SlowThreshold time.Duration
|
||||
}
|
||||
|
||||
func (w Writer) Printf(format string, args ...interface{}) {
|
||||
s := fmt.Sprintf(format, args...)
|
||||
l := strings.Split(s, "\n")
|
||||
if len(l) == 2 {
|
||||
log.ZDebug(context.Background(), "sql exec detail", "gorm", l[0], "sql", l[1])
|
||||
} else {
|
||||
log.ZDebug(context.Background(), "sql exec detail", "sql", s)
|
||||
func NewSqlLogger(logLevel logger.LogLevel, ignoreRecordNotFoundError bool, slowThreshold time.Duration) *SqlLogger {
|
||||
return &SqlLogger{
|
||||
LogLevel: logLevel,
|
||||
IgnoreRecordNotFoundError: ignoreRecordNotFoundError,
|
||||
SlowThreshold: slowThreshold,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SqlLogger) LogMode(logLevel logger.LogLevel) logger.Interface {
|
||||
newLogger := *l
|
||||
newLogger.LogLevel = logLevel
|
||||
return &newLogger
|
||||
}
|
||||
|
||||
func (SqlLogger) Info(ctx context.Context, msg string, args ...interface{}) {
|
||||
log.ZInfo(ctx, msg, args)
|
||||
}
|
||||
|
||||
func (SqlLogger) Warn(ctx context.Context, msg string, args ...interface{}) {
|
||||
log.ZWarn(ctx, msg, nil, args)
|
||||
}
|
||||
|
||||
func (SqlLogger) Error(ctx context.Context, msg string, args ...interface{}) {
|
||||
log.ZError(ctx, msg, nil, args)
|
||||
}
|
||||
|
||||
func (l SqlLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
|
||||
if l.LogLevel <= logger.Silent {
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(begin)
|
||||
switch {
|
||||
case err != nil && l.LogLevel >= logger.Error && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
|
||||
sql, rows := fc()
|
||||
if rows == -1 {
|
||||
log.ZError(ctx, "sql exec detail", err, "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "sql", sql)
|
||||
} else {
|
||||
log.ZError(ctx, "sql exec detail", err, "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "rows", rows, "sql", sql)
|
||||
}
|
||||
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= logger.Warn:
|
||||
sql, rows := fc()
|
||||
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
|
||||
if rows == -1 {
|
||||
log.ZWarn(ctx, "sql exec detail", nil, "gorm", gormUtils.FileWithLineNum(), nil, "slow sql", slowLog, "time(ms)", float64(elapsed.Nanoseconds())/1e6, "sql", sql)
|
||||
} else {
|
||||
log.ZWarn(ctx, "sql exec detail", nil, "gorm", gormUtils.FileWithLineNum(), nil, "slow sql", slowLog, "time(ms)", float64(elapsed.Nanoseconds())/1e6, "rows", rows, "sql", sql)
|
||||
}
|
||||
case l.LogLevel == logger.Info:
|
||||
sql, rows := fc()
|
||||
if rows == -1 {
|
||||
log.ZDebug(ctx, "sql exec detail", "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "sql", sql)
|
||||
} else {
|
||||
log.ZDebug(ctx, "sql exec detail", "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "rows", rows, "sql", sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,19 +7,19 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewObjectHash(db *gorm.DB) relation.ObjectHashModelInterface {
|
||||
return &ObjectHashGorm{
|
||||
DB: db,
|
||||
}
|
||||
type ObjectHashGorm struct {
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
type ObjectHashGorm struct {
|
||||
DB *gorm.DB
|
||||
func NewObjectHash(db *gorm.DB) relation.ObjectHashModelInterface {
|
||||
return &ObjectHashGorm{
|
||||
NewMetaDB(db, &relation.ObjectHashModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (o *ObjectHashGorm) NewTx(tx any) relation.ObjectHashModelInterface {
|
||||
return &ObjectHashGorm{
|
||||
DB: tx.(*gorm.DB),
|
||||
NewMetaDB(tx.(*gorm.DB), &relation.ObjectHashModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,19 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewObjectInfo(db *gorm.DB) relation.ObjectInfoModelInterface {
|
||||
return &ObjectInfoGorm{
|
||||
DB: db,
|
||||
}
|
||||
type ObjectInfoGorm struct {
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
type ObjectInfoGorm struct {
|
||||
DB *gorm.DB
|
||||
func NewObjectInfo(db *gorm.DB) relation.ObjectInfoModelInterface {
|
||||
return &ObjectInfoGorm{
|
||||
NewMetaDB(db, &relation.ObjectInfoModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (o *ObjectInfoGorm) NewTx(tx any) relation.ObjectInfoModelInterface {
|
||||
return &ObjectInfoGorm{
|
||||
DB: tx.(*gorm.DB),
|
||||
NewMetaDB(tx.(*gorm.DB), &relation.ObjectInfoModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,19 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewObjectPut(db *gorm.DB) relation.ObjectPutModelInterface {
|
||||
return &ObjectPutGorm{
|
||||
DB: db,
|
||||
}
|
||||
type ObjectPutGorm struct {
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
type ObjectPutGorm struct {
|
||||
DB *gorm.DB
|
||||
func NewObjectPut(db *gorm.DB) relation.ObjectPutModelInterface {
|
||||
return &ObjectPutGorm{
|
||||
NewMetaDB(db, &relation.ObjectPutModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (o *ObjectPutGorm) NewTx(tx any) relation.ObjectPutModelInterface {
|
||||
return &ObjectPutGorm{
|
||||
DB: tx.(*gorm.DB),
|
||||
NewMetaDB(tx.(*gorm.DB), &relation.ObjectPutModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,53 +8,53 @@ import (
|
||||
)
|
||||
|
||||
type UserGorm struct {
|
||||
DB *gorm.DB
|
||||
*MetaDB
|
||||
}
|
||||
|
||||
func NewUserGorm(DB *gorm.DB) relation.UserModelInterface {
|
||||
return &UserGorm{DB: DB.Model(&relation.UserModel{})}
|
||||
func NewUserGorm(db *gorm.DB) relation.UserModelInterface {
|
||||
return &UserGorm{NewMetaDB(db, &relation.UserModel{})}
|
||||
}
|
||||
|
||||
// 插入多条
|
||||
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel) (err error) {
|
||||
return utils.Wrap(u.DB.Create(&users).Error, "")
|
||||
return utils.Wrap(u.db(ctx).Create(&users).Error, "")
|
||||
}
|
||||
|
||||
// 更新用户信息 零值
|
||||
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
|
||||
return utils.Wrap(u.DB.Where("user_id = ?", userID).Updates(args).Error, "")
|
||||
return utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
// 更新多个用户信息 非零值
|
||||
func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel) (err error) {
|
||||
return utils.Wrap(u.DB.Updates(&users).Error, "")
|
||||
return utils.Wrap(u.db(ctx).Updates(&users).Error, "")
|
||||
}
|
||||
|
||||
// 获取指定用户信息 不存在,也不返回错误
|
||||
func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
||||
err = utils.Wrap(u.DB.Debug().Where("user_id in ?", userIDs).Find(&users).Error, "")
|
||||
err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "")
|
||||
return users, err
|
||||
}
|
||||
|
||||
// 获取某个用户信息 不存在,则返回错误
|
||||
func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
||||
user = &relation.UserModel{}
|
||||
err = utils.Wrap(u.DB.Where("user_id = ?", userID).Take(&user).Error, "")
|
||||
err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "")
|
||||
return user, err
|
||||
}
|
||||
|
||||
// 获取用户信息 不存在,不返回错误
|
||||
func (u *UserGorm) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error) {
|
||||
err = utils.Wrap(u.DB.Count(&count).Error, "")
|
||||
err = utils.Wrap(u.db(ctx).Count(&count).Error, "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = utils.Wrap(u.DB.Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
|
||||
err = utils.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&users).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取所有用户ID
|
||||
func (u *UserGorm) GetAllUserID(ctx context.Context) (userIDs []string, err error) {
|
||||
err = u.DB.Pluck("user_id", &userIDs).Error
|
||||
err = u.db(ctx).Pluck("user_id", &userIDs).Error
|
||||
return userIDs, err
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package relation
|
||||
|
||||
import "context"
|
||||
|
||||
const (
|
||||
conversationModelTableName = "conversations"
|
||||
)
|
||||
@@ -28,4 +30,15 @@ func (ConversationModel) TableName() string {
|
||||
}
|
||||
|
||||
type ConversationModelInterface interface {
|
||||
Create(ctx context.Context, conversations []*ConversationModel) (err error)
|
||||
Delete(ctx context.Context, groupIDs []string) (err error)
|
||||
UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, conversations []*ConversationModel) (err error)
|
||||
Find(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []*ConversationModel, err error)
|
||||
FindUserID(ctx context.Context, userIDList []string, conversationID string) ([]string, error)
|
||||
FindUserIDAllConversationID(ctx context.Context, userID string) ([]string, error)
|
||||
Take(ctx context.Context, userID, conversationID string) (conversation *ConversationModel, err error)
|
||||
FindConversationID(ctx context.Context, userID string, conversationIDList []string) (existConversationID []string, err error)
|
||||
FindRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error)
|
||||
NewTx(tx any) ConversationModelInterface
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user