mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
group db
This commit is contained in:
parent
cfa5420d9d
commit
66b6d5fc2f
@ -243,10 +243,11 @@ func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJo
|
||||
if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups, err := s.GroupInterface.FindJoinedGroup(ctx, req.FromUserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
|
||||
total, groups, err := s.GroupInterface.FindJoinedGroup(ctx, req.FromUserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Total = total
|
||||
if len(groups) == 0 {
|
||||
return resp, nil
|
||||
}
|
||||
@ -1039,7 +1040,7 @@ func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbGroup.Ge
|
||||
if len(req.GroupIDs) == 0 {
|
||||
return nil, constant.ErrArgs.Wrap("groupIDs empty")
|
||||
}
|
||||
members, err := s.GroupInterface.FindGroupMemberByUserID(ctx, req.UserID, req.GroupIDs)
|
||||
members, err := s.GroupInterface.FindGroupMember(ctx, req.UserID, req.GroupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relation2 "Open_IM/pkg/common/db/table/relation"
|
||||
unrelation2 "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
_ "github.com/dtm-labs/rockscache"
|
||||
@ -19,7 +21,7 @@ type GroupInterface interface {
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error)
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error)
|
||||
FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) ([]*relation2.GroupModel, error)
|
||||
FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
// groupMember
|
||||
@ -29,7 +31,6 @@ type GroupInterface interface {
|
||||
FindGroupMemberAll(ctx context.Context, groupID string) ([]*relation2.GroupMemberModel, error)
|
||||
FindGroupMemberFilterList(ctx context.Context, groupID string, filter int32, begin int32, maxNumber int32) ([]*relation2.GroupMemberModel, error) // relation.GetGroupMemberByGroupID(req.GroupID, req.Filter, req.NextSeq, 30)
|
||||
SearchGroupMember(ctx context.Context, groupID, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupMemberModel, error)
|
||||
FindGroupMemberByUserID(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupMemberModel, error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error)
|
||||
FindGroupOwnerUser(ctx context.Context, groupID []string) ([]*relation2.GroupMemberModel, error)
|
||||
CreateGroupMember(ctx context.Context, groupMember []*relation2.GroupMemberModel) error
|
||||
@ -76,7 +77,7 @@ func (g *GroupController) TakeGroup(ctx context.Context, groupID string) (group
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupController) FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) ([]*relation2.GroupModel, error) {
|
||||
func (g *GroupController) FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
@ -116,21 +117,11 @@ func (g *GroupController) FindGroupMemberFilterList(ctx context.Context, groupID
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupMembersByID(ctx context.Context, groupID string, userIDs []string) (groups []*relation2.GroupMemberModel, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupController) SearchGroupMember(ctx context.Context, groupID, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupMemberByUserID(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
@ -241,7 +232,7 @@ type GroupDataBaseInterface interface {
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error)
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error)
|
||||
FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) ([]*relation2.GroupModel, error)
|
||||
FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
// groupMember
|
||||
@ -251,7 +242,6 @@ type GroupDataBaseInterface interface {
|
||||
FindGroupMemberAll(ctx context.Context, groupID string) ([]*relation2.GroupMemberModel, error)
|
||||
FindGroupMemberFilterList(ctx context.Context, groupID string, filter int32, begin int32, maxNumber int32) ([]*relation2.GroupMemberModel, error) // relation.GetGroupMemberByGroupID(req.GroupID, req.Filter, req.NextSeq, 30)
|
||||
SearchGroupMember(ctx context.Context, groupID, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupMemberModel, error)
|
||||
FindGroupMemberByUserID(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupMemberModel, error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error)
|
||||
FindGroupOwnerUser(ctx context.Context, groupID []string) ([]*relation2.GroupMemberModel, error)
|
||||
CreateGroupMember(ctx context.Context, groupMember []*relation2.GroupMemberModel) error
|
||||
@ -392,53 +382,79 @@ func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.C
|
||||
//}
|
||||
|
||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupDB.Find(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) SearchGroup(ctx context.Context, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupDB.Search(ctx, name, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupDB.Take(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) ([]*relation2.GroupModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *GroupDataBase) FindJoinedGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupModel, error) {
|
||||
total, members, err := g.groupMemberDB.PageByUser(ctx, userID, pageNumber, showNumber)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
if len(members) == 0 {
|
||||
return total, []*relation2.GroupModel{}, nil
|
||||
}
|
||||
groupIDs := utils.Slice(members, func(e *relation2.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
groups, err := g.groupDB.Find(ctx, groupIDs)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
utils.OrderPtr(groupIDs, &groups, func(e *relation2.GroupModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
return total, groups, nil
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupDB.UpdateMap(ctx, groupID, data)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return utils.Wrap(g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.groupMemberDB.DeleteGroup(ctx, []string{groupID}, tx)
|
||||
}), "")
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMember []*relation2.GroupMemberModel) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMembers []*relation2.GroupMemberModel) error {
|
||||
if len(groups) > 0 && len(groupMembers) > 0 {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.groupMemberDB.Create(ctx, groupMembers, tx)
|
||||
})
|
||||
}
|
||||
if len(groups) > 0 {
|
||||
return g.groupDB.Create(ctx, groups)
|
||||
}
|
||||
if len(groupMembers) > 0 {
|
||||
return g.groupMemberDB.Create(ctx, groupMembers)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupMemberModel, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupMemberDB.Take(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMember(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupMemberDB.FindGroupUser(ctx, []string{groupID}, userIDs, nil)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMemberAll(ctx context.Context, groupID string) ([]*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupMemberDB.FindGroupUser(ctx, []string{groupID}, nil, nil)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMemberFilterList(ctx context.Context, groupID string, filter int32, begin int32, maxNumber int32) ([]*relation2.GroupMemberModel, error) {
|
||||
@ -446,29 +462,16 @@ func (g *GroupDataBase) FindGroupMemberFilterList(ctx context.Context, groupID s
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMembersByID(ctx context.Context, groupID string, userIDs []string) (groups []*relation2.GroupMemberModel, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) SearchGroupMember(ctx context.Context, groupID, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMemberByUserID(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *GroupDataBase) SearchGroupMember(ctx context.Context, groupID string, name string, pageNumber, showNumber int32) (int32, []*relation2.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.SearchMember(ctx, groupID, name, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return g.groupMemberDB.TakeOwner(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupOwnerUser(ctx context.Context, groupID []string) ([]*relation2.GroupMemberModel, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *GroupDataBase) FindGroupOwnerUser(ctx context.Context, groupIDs []string) ([]*relation2.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.FindGroupUser(ctx, groupIDs, nil, []int32{constant.GroupOwner})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroupMember(ctx context.Context, groupMember []*relation2.GroupMemberModel) error {
|
||||
|
@ -31,6 +31,13 @@ func (g *GroupMemberGorm) Delete(ctx context.Context, groupMembers []*relation.G
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Delete(groupMembers).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||
@ -54,6 +61,23 @@ func (g *GroupMemberGorm) Find(ctx context.Context, groupMembers []*relation.Gro
|
||||
return groupList, utils.Wrap(getDBConn(g.DB, tx).Where("(group_id, user_id) in ?", where).Find(&groupList).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...*gorm.DB) (groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx)
|
||||
if len(groupList) > 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, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
|
||||
@ -62,7 +86,7 @@ func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID strin
|
||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) TakeOwnerInfo(ctx context.Context, groupID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
||||
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMember", *groupMember)
|
||||
}()
|
||||
@ -70,214 +94,16 @@ func (g *GroupMemberGorm) TakeOwnerInfo(ctx context.Context, groupID string, tx
|
||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
|
||||
}
|
||||
|
||||
//func InsertIntoGroupMember(toInsertInfo GroupMemberModel) error {
|
||||
// toInsertInfo.JoinTime = time.Now()
|
||||
// if toInsertInfo.RoleLevel == 0 {
|
||||
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
|
||||
// }
|
||||
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
||||
// err := GroupMemberDB.Table("group_members").Create(toInsertInfo).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func BatchInsertIntoGroupMember(toInsertInfoList []*GroupMemberModel) error {
|
||||
// for _, toInsertInfo := range toInsertInfoList {
|
||||
// toInsertInfo.JoinTime = time.Now()
|
||||
// if toInsertInfo.RoleLevel == 0 {
|
||||
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
|
||||
// }
|
||||
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
||||
// }
|
||||
// return GroupMemberDB.Create(toInsertInfoList).Error
|
||||
//
|
||||
//}
|
||||
//
|
||||
//func FindGroupMember(userID string) ([]GroupMemberModel, error) {
|
||||
// var groupMemberList []GroupMemberModel
|
||||
// err := GroupMemberDB.Table("group_members").Where("user_id=?", userID).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberListByGroupID(groupID string) ([]GroupMemberModel, error) {
|
||||
// var groupMemberList []GroupMemberModel
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=?", groupID).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberIDListByGroupID(groupID string) ([]string, error) {
|
||||
// var groupMemberIDList []string
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=?", groupID).Pluck("user_id", &groupMemberIDList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberIDList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]GroupMemberModel, error) {
|
||||
// var groupMemberList []GroupMemberModel
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=? and role_level=?", groupID, roleLevel).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*GroupMemberModel, error) {
|
||||
// var groupMember GroupMemberModel
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Take(&groupMember).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return &groupMember, nil
|
||||
//}
|
||||
//
|
||||
//func DeleteGroupMemberByGroupIDAndUserID(groupID, userID string) error {
|
||||
// return GroupMemberDB.Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Delete(GroupMemberModel{}).Error
|
||||
//}
|
||||
//
|
||||
//func DeleteGroupMemberByGroupID(groupID string) error {
|
||||
// return GroupMemberDB.Table("group_members").Where("group_id=? ", groupID).Delete(GroupMemberModel{}).Error
|
||||
//}
|
||||
//
|
||||
//func UpdateGroupMemberInfo(groupMemberInfo GroupMemberModel) error {
|
||||
// return GroupMemberDB.Table("group_members").Where("group_id=? and user_id=?", groupMemberInfo.GroupID, groupMemberInfo.UserID).Updates(&groupMemberInfo).Error
|
||||
//}
|
||||
//
|
||||
//func UpdateGroupMemberInfoByMap(groupMemberInfo GroupMemberModel, m map[string]interface{}) error {
|
||||
// return GroupMemberDB.Table("group_members").Where("group_id=? and user_id=?", groupMemberInfo.GroupID, groupMemberInfo.UserID).Updates(m).Error
|
||||
//}
|
||||
//
|
||||
//func GetOwnerManagerByGroupID(groupID string) ([]GroupMemberModel, error) {
|
||||
// var groupMemberList []GroupMemberModel
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=? and role_level>?", groupID, constant.GroupOrdinaryUsers).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberNumByGroupID(groupID string) (int64, error) {
|
||||
// var number int64
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=?", groupID).Count(&number).Error
|
||||
// if err != nil {
|
||||
// return 0, utils.Wrap(err, "")
|
||||
// }
|
||||
// return number, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMemberModel, error) {
|
||||
// omList, err := GetOwnerManagerByGroupID(groupID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// for _, v := range omList {
|
||||
// if v.RoleLevel == constant.GroupOwner {
|
||||
// return &v, nil
|
||||
// }
|
||||
// }
|
||||
// return nil, utils.Wrap(constant.ErrGroupNoOwner, "")
|
||||
//}
|
||||
//
|
||||
//func IsExistGroupMember(groupID, userID string) bool {
|
||||
// var number int64
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id = ? and user_id = ?", groupID, userID).Count(&number).Error
|
||||
// if err != nil {
|
||||
// return false
|
||||
// }
|
||||
// if number != 1 {
|
||||
// return false
|
||||
// }
|
||||
// return true
|
||||
//}
|
||||
//
|
||||
//func CheckIsExistGroupMember(ctx context.Context, groupID, userID string) error {
|
||||
// var number int64
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id = ? and user_id = ?", groupID, userID).Count(&number).Error
|
||||
// if err != nil {
|
||||
// return constant.ErrDB.Wrap()
|
||||
// }
|
||||
// if number != 1 {
|
||||
// return constant.ErrData.Wrap()
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]GroupMember, error) {
|
||||
// var memberList []GroupMember
|
||||
// var err error
|
||||
// if filter >= 0 {
|
||||
// memberList, err = GetGroupMemberListByGroupIDAndRoleLevel(groupID, filter) //sorted by join time
|
||||
// } else {
|
||||
// memberList, err = GetGroupMemberListByGroupID(groupID)
|
||||
// }
|
||||
//
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// if begin >= int32(len(memberList)) {
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// var end int32
|
||||
// if begin+int32(maxNumber) < int32(len(memberList)) {
|
||||
// end = begin + maxNumber
|
||||
// } else {
|
||||
// end = int32(len(memberList))
|
||||
// }
|
||||
// return memberList[begin:end], nil
|
||||
//}
|
||||
//
|
||||
//func GetJoinedGroupIDListByUserID(userID string) ([]string, error) {
|
||||
// memberList, err := FindGroupMember(userID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// var groupIDList []string
|
||||
// for _, v := range memberList {
|
||||
// groupIDList = append(groupIDList, v.GroupID)
|
||||
// }
|
||||
// return groupIDList, nil
|
||||
//}
|
||||
//
|
||||
//func IsGroupOwnerAdmin(groupID, UserID string) bool {
|
||||
// groupMemberList, err := GetOwnerManagerByGroupID(groupID)
|
||||
// if err != nil {
|
||||
// return false
|
||||
// }
|
||||
// for _, v := range groupMemberList {
|
||||
// if v.UserID == UserID && v.RoleLevel > constant.GroupOrdinaryUsers {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
//}
|
||||
//
|
||||
//func GetGroupMembersByGroupIdCMS(groupId string, userName string, showNumber, pageNumber int32) ([]GroupMember, error) {
|
||||
// var groupMembers []GroupMember
|
||||
// err := GroupMemberDB.Table("group_members").Where("group_id=?", groupId).Where(fmt.Sprintf(" nickname like '%%%s%%' ", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groupMembers).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMembers, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMembersCount(groupID, userName string) (int64, error) {
|
||||
// var count int64
|
||||
// if err := GroupMemberDB.Table("group_members").Where("group_id=?", groupID).Where(fmt.Sprintf(" nickname like '%%%s%%' ", userName)).Count(&count).Error; err != nil {
|
||||
// return count, err
|
||||
// }
|
||||
// return count, nil
|
||||
//}
|
||||
//
|
||||
//func UpdateGroupMemberInfoDefaultZero(groupMemberInfo GroupMember, args map[string]interface{}) error {
|
||||
// return GroupMemberDB.Model(groupMemberInfo).Updates(args).Error
|
||||
//}
|
||||
func (g *GroupMemberGorm) PageByUser(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "total", total, "groupList", groupList)
|
||||
}()
|
||||
return gormPage[relation.GroupMemberModel](getDBConn(g.DB, tx).Where("user_id = ?", userID), pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, groupID string, name string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "name", name, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groupList", groupList)
|
||||
}()
|
||||
return gormSearch[relation.GroupMemberModel](getDBConn(g.DB, tx).Where("group_id = ?", groupID), "nickname", name, pageNumber, showNumber)
|
||||
}
|
||||
|
@ -30,20 +30,27 @@ func (g *GroupGorm) Delete(ctx context.Context, groupIDs []string, tx ...*gorm.D
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(g).Updates(args).Error, "")
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Update(ctx context.Context, groups []*relation.GroupModel, tx ...*gorm.DB) (err error) {
|
||||
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "status", status)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Updates(&groups).Error, "")
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
||||
}
|
||||
|
||||
//func (g *GroupGorm) Update(ctx context.Context, groups []*relation.GroupModel, tx ...*gorm.DB) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Updates(&groups).Error, "")
|
||||
//}
|
||||
|
||||
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (groups []*relation.GroupModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
||||
@ -54,7 +61,14 @@ func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...*gorm.DB)
|
||||
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...*gorm.DB) (group *relation.GroupModel, err error) {
|
||||
group = &relation.GroupModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", group)
|
||||
}()
|
||||
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Search(ctx context.Context, name string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groups []*relation.GroupModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "name", name, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||
}()
|
||||
return gormSearch[relation.GroupModel](getDBConn(g.DB, tx), "name", name, pageNumber, showNumber)
|
||||
}
|
||||
|
25
pkg/common/db/relation/utils.go
Normal file
25
pkg/common/db/relation/utils.go
Normal file
@ -0,0 +1,25 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (int32, []*E, error) {
|
||||
var count int64
|
||||
if err := db.Model(new(E)).Count(&count).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
}
|
||||
var es []*E
|
||||
if err := db.Limit(int(showNumber)).Offset(int(pageNumber * showNumber)).Find(&es).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
}
|
||||
return int32(count), es, nil
|
||||
}
|
||||
|
||||
func gormSearch[E any](db *gorm.DB, field string, value string, pageNumber, showNumber int32) (int32, []*E, error) {
|
||||
if field != "" && value != "" {
|
||||
db = db.Where(field+" like ?", "%"+value+"%")
|
||||
}
|
||||
return gormPage[E](db, pageNumber, showNumber)
|
||||
}
|
@ -343,6 +343,34 @@ func Single[E comparable](a, b []E) []E {
|
||||
return v
|
||||
}
|
||||
|
||||
// Order 将ts按es排序
|
||||
func Order[E comparable, T any](es []E, ts []T, fn func(t T) E) []T {
|
||||
if len(es) == 0 || len(ts) == 0 {
|
||||
return ts
|
||||
}
|
||||
kv := make(map[E][]T)
|
||||
for i := 0; i < len(ts); i++ {
|
||||
t := ts[i]
|
||||
k := fn(t)
|
||||
kv[k] = append(kv[k], t)
|
||||
}
|
||||
rs := make([]T, 0, len(ts))
|
||||
for _, e := range es {
|
||||
vs := kv[e]
|
||||
delete(kv, e)
|
||||
rs = append(rs, vs...)
|
||||
}
|
||||
for k := range kv {
|
||||
rs = append(rs, kv[k]...)
|
||||
}
|
||||
return rs
|
||||
}
|
||||
|
||||
func OrderPtr[E comparable, T any](es []E, ts *[]T, fn func(t T) E) []T {
|
||||
*ts = Order(es, *ts, fn)
|
||||
return *ts
|
||||
}
|
||||
|
||||
func UniqueJoin(s ...string) string {
|
||||
data, _ := json.Marshal(s)
|
||||
return string(data)
|
||||
|
Loading…
x
Reference in New Issue
Block a user