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_member_model.go

102 lines
4.2 KiB

2 years ago
package relation
2 years ago
import (
2 years ago
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/table/relation"
"OpenIM/pkg/utils"
2 years ago
"context"
"gorm.io/gorm"
)
3 years ago
2 years ago
var _ relation.GroupMemberModelInterface = (*GroupMemberGorm)(nil)
2 years ago
type GroupMemberGorm struct {
DB *gorm.DB
}
3 years ago
2 years ago
func NewGroupMemberDB(db *gorm.DB) relation.GroupMemberModelInterface {
return &GroupMemberGorm{DB: db}
}
2 years ago
func (g *GroupMemberGorm) NewTx(tx any) relation.GroupMemberModelInterface {
return &GroupMemberGorm{DB: tx.(*gorm.DB)}
}
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel) (err error) {
return utils.Wrap(g.DB.Create(&groupMemberList).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string) (err error) {
return utils.Wrap(g.DB.Where("group_id = ? and user_id in (?)", groupID, userIDs).Delete(&relation.GroupMemberModel{}).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string) (err error) {
return utils.Wrap(g.DB.Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any) (err error) {
return utils.Wrap(g.DB.Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(data).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32) (rowsAffected int64, err error) {
db := g.DB.Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
2 years ago
"role_level": roleLevel,
})
return db.RowsAffected, utils.Wrap(db.Error, "")
}
2 years ago
func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) (groupList []*relation.GroupMemberModel, err error) {
db := g.DB
2 years ago
if len(groupIDs) > 0 {
db = db.Where("group_id in (?)", groupIDs)
}
if len(userIDs) > 0 {
db = db.Where("user_id in (?)", userIDs)
}
if len(roleLevels) > 0 {
db = db.Where("role_level in (?)", roleLevels)
}
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
}
2 years ago
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string) (groupMember *relation.GroupMemberModel, err error) {
2 years ago
groupMember = &relation.GroupMemberModel{}
2 years ago
return groupMember, utils.Wrap(g.DB.Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string) (groupMember *relation.GroupMemberModel, err error) {
2 years ago
groupMember = &relation.GroupMemberModel{}
2 years ago
return groupMember, utils.Wrap(g.DB.Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, groupList []*relation.GroupMemberModel, err error) {
db := g.DB
2 years ago
gormIn(&db, "group_id", groupIDs)
gormIn(&db, "user_id", userIDs)
gormIn(&db, "role_level", roleLevels)
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
}
2 years ago
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string) (count map[string]uint32, err error) {
return mapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string) (groupUsers map[string][]string, err error) {
2 years ago
var items []struct {
GroupID string `gorm:"group_id"`
UserID string `gorm:"user_id"`
}
2 years ago
if err := g.DB.Model(&relation.GroupMemberModel{}).Where("group_id in (?)", groupIDs).Find(&items).Error; err != nil {
2 years ago
return nil, utils.Wrap(err, "")
}
groupUsers = make(map[string][]string)
for _, item := range items {
groupUsers[item.GroupID] = append(groupUsers[item.GroupID], item.UserID)
}
return groupUsers, nil
}
2 years ago
2 years ago
func (g *GroupMemberGorm) FindMemberUserID(ctx context.Context, groupID string) (userIDs []string, err error) {
return userIDs, utils.Wrap(g.DB.Model(&relation.GroupMemberModel{}).Where("group_id = ?", groupID).Pluck("user_id", &userIDs).Error, "")
2 years ago
}