Files
open-im-server/pkg/common/db/relation/black.go
T

79 lines
3.0 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package relation
2021-05-26 19:32:30 +08:00
import (
2023-02-03 12:16:48 +08:00
"Open_IM/pkg/common/db/table/relation"
2023-01-30 11:14:18 +08:00
"Open_IM/pkg/common/tracelog"
2022-01-06 19:59:13 +08:00
"Open_IM/pkg/utils"
2023-01-04 17:21:33 +08:00
"context"
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2021-05-26 19:32:30 +08:00
)
2023-02-01 17:39:08 +08:00
type BlackGorm struct {
DB *gorm.DB
2023-01-13 18:20:48 +08:00
}
2023-02-01 17:39:08 +08:00
func NewBlackGorm(db *gorm.DB) *BlackGorm {
var black BlackGorm
2023-01-30 15:26:39 +08:00
black.DB = db
2023-01-17 14:54:08 +08:00
return &black
2023-01-11 16:23:16 +08:00
}
2023-02-03 12:16:48 +08:00
func (b *BlackGorm) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
2023-01-13 14:10:46 +08:00
}()
2023-02-03 12:16:48 +08:00
return utils.Wrap(b.DB.Model(&relation.BlackModel{}).Create(&blacks).Error, "")
2023-01-13 14:10:46 +08:00
}
2023-02-03 12:16:48 +08:00
func (b *BlackGorm) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
2023-01-13 14:10:46 +08:00
}()
2023-02-03 12:16:48 +08:00
return utils.Wrap(b.DB.Model(&relation.BlackModel{}).Delete(blacks).Error, "")
2023-01-13 14:10:46 +08:00
}
2023-02-01 17:39:08 +08:00
func (b *BlackGorm) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) {
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "args", args)
2023-01-13 14:10:46 +08:00
}()
2023-02-03 12:16:48 +08:00
return utils.Wrap(b.DB.Model(&relation.BlackModel{}).Where("block_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Updates(args).Error, "")
2023-01-13 14:10:46 +08:00
}
2023-02-03 12:16:48 +08:00
func (b *BlackGorm) Update(ctx context.Context, blacks []*relation.BlackModel) (err error) {
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
2023-01-13 14:10:46 +08:00
}()
2023-02-03 12:16:48 +08:00
return utils.Wrap(b.DB.Model(&relation.BlackModel{}).Updates(&blacks).Error, "")
2023-01-13 14:10:46 +08:00
}
2023-02-03 12:16:48 +08:00
func (b *BlackGorm) Find(ctx context.Context, blacks []*relation.BlackModel) (blackList []*relation.BlackModel, err error) {
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks, "blackList", blackList)
2023-01-13 14:10:46 +08:00
}()
var where [][]interface{}
for _, black := range blacks {
where = append(where, []interface{}{black.OwnerUserID, black.BlockUserID})
}
2023-02-03 12:16:48 +08:00
return blackList, utils.Wrap(b.DB.Model(&relation.BlackModel{}).Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "")
2023-01-13 14:10:46 +08:00
}
2023-02-03 12:16:48 +08:00
func (b *BlackGorm) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.BlackModel, err error) {
black = &relation.BlackModel{}
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "black", *black)
2023-01-13 14:10:46 +08:00
}()
2023-02-03 12:16:48 +08:00
return black, utils.Wrap(b.DB.Model(&relation.BlackModel{}).Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error, "")
2023-01-13 14:10:46 +08:00
}
2023-02-03 12:16:48 +08:00
func (b *BlackGorm) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error) {
2023-01-13 14:10:46 +08:00
defer func() {
2023-01-31 15:45:24 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blacks", blacks)
2023-01-13 14:10:46 +08:00
}()
2023-02-03 12:16:48 +08:00
err = b.DB.Model(&relation.BlackModel{}).Model(b).Count(&total).Error
2022-04-28 10:46:21 +08:00
if err != nil {
2023-01-31 15:45:24 +08:00
return nil, 0, utils.Wrap(err, "")
2022-04-28 10:46:21 +08:00
}
2023-02-03 12:16:48 +08:00
err = utils.Wrap(b.DB.Model(&relation.BlackModel{}).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&blacks).Error, "")
2023-01-31 15:45:24 +08:00
return
2022-04-28 10:46:21 +08:00
}