2023-01-28 13:19:36 +08:00
|
|
|
package relation
|
2023-01-04 17:21:33 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-02-01 17:07:56 +08:00
|
|
|
"Open_IM/pkg/common/db/table"
|
2023-01-30 11:10:26 +08:00
|
|
|
"Open_IM/pkg/common/tracelog"
|
2023-01-04 17:21:33 +08:00
|
|
|
"Open_IM/pkg/utils"
|
2023-01-04 19:35:07 +08:00
|
|
|
"context"
|
2023-01-11 16:23:16 +08:00
|
|
|
"gorm.io/gorm"
|
2023-01-04 17:21:33 +08:00
|
|
|
)
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
type GroupGorm struct {
|
|
|
|
|
DB *gorm.DB
|
2023-01-16 20:14:26 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func NewGroupDB(db *gorm.DB) *GroupGorm {
|
|
|
|
|
return &GroupGorm{DB: db}
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func (g *GroupGorm) Create(ctx context.Context, groups []*table.GroupModel, tx ...*gorm.DB) (err error) {
|
2023-01-05 15:30:33 +08:00
|
|
|
defer func() {
|
2023-01-30 11:10:26 +08:00
|
|
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
2023-01-05 15:30:33 +08:00
|
|
|
}()
|
2023-01-29 10:42:58 +08:00
|
|
|
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func (g *GroupGorm) Delete(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) {
|
2023-01-05 15:30:33 +08:00
|
|
|
defer func() {
|
2023-01-30 11:10:26 +08:00
|
|
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
2023-01-05 15:30:33 +08:00
|
|
|
}()
|
2023-02-01 17:07:56 +08:00
|
|
|
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&table.GroupModel{}).Error, "")
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func (g *GroupGorm) UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
2023-01-05 15:30:33 +08:00
|
|
|
defer func() {
|
2023-01-30 11:10:26 +08:00
|
|
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
2023-01-05 15:30:33 +08:00
|
|
|
}()
|
2023-01-28 18:49:36 +08:00
|
|
|
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(g).Updates(args).Error, "")
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func (g *GroupGorm) Update(ctx context.Context, groups []*table.GroupModel, tx ...*gorm.DB) (err error) {
|
2023-01-05 15:30:33 +08:00
|
|
|
defer func() {
|
2023-01-30 11:10:26 +08:00
|
|
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
2023-01-05 15:30:33 +08:00
|
|
|
}()
|
2023-01-28 18:47:02 +08:00
|
|
|
return utils.Wrap(getDBConn(g.DB, tx).Updates(&groups).Error, "")
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (groups []*table.GroupModel, err error) {
|
2023-01-05 15:30:33 +08:00
|
|
|
defer func() {
|
2023-01-30 11:10:26 +08:00
|
|
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
2023-01-05 15:30:33 +08:00
|
|
|
}()
|
2023-01-29 10:42:58 +08:00
|
|
|
return groups, utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:07:56 +08:00
|
|
|
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...*gorm.DB) (group *table.GroupModel, err error) {
|
|
|
|
|
group = &table.GroupModel{}
|
2023-01-05 15:30:33 +08:00
|
|
|
defer func() {
|
2023-01-30 11:10:26 +08:00
|
|
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group)
|
2023-01-05 15:30:33 +08:00
|
|
|
}()
|
2023-01-29 10:42:58 +08:00
|
|
|
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
2023-01-04 17:21:33 +08:00
|
|
|
}
|