You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/relation/group_model.go

74 lines
2.8 KiB

2 years ago
package relation
3 years ago
import (
2 years ago
"Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
3 years ago
"Open_IM/pkg/utils"
2 years ago
"context"
"gorm.io/gorm"
3 years ago
)
2 years ago
var _ relation.GroupModelInterface = (*GroupGorm)(nil)
2 years ago
type GroupGorm struct {
DB *gorm.DB
}
2 years ago
func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
return &GroupGorm{DB: db}
}
3 years ago
2 years ago
func (g *GroupGorm) NewTx(tx any) relation.GroupModelInterface {
return &GroupGorm{DB: tx.(*gorm.DB)}
}
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel) (err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
}()
2 years ago
return utils.Wrap(g.DB.Create(&groups).Error, "")
2 years ago
}
2 years ago
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}) (err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
}()
2 years ago
return utils.Wrap(g.DB.Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
3 years ago
}
2 years ago
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32) (err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "status", status)
}()
2 years ago
return utils.Wrap(g.DB.Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
3 years ago
}
2 years ago
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string) (groups []*relation.GroupModel, err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
}()
2 years ago
return groups, utils.Wrap(g.DB.Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
3 years ago
}
3 years ago
2 years ago
func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.GroupModel, err error) {
2 years ago
group = &relation.GroupModel{}
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", group)
}()
2 years ago
return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "")
3 years ago
}
3 years ago
2 years ago
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
}()
2 years ago
return gormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
2 years ago
}
2 years ago
func (g *GroupGorm) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupType", groupType, "groupIDs", groupIDs)
}()
return groupIDs, utils.Wrap(g.DB.Model(&relation.GroupModel{}).Where("group_type = ? ", groupType).Pluck("group_id", &groupIDs).Error, "")
2 years ago
}