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

284 lines
10 KiB

2 years ago
package relation
2 years ago
import (
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db/table"
2 years ago
"Open_IM/pkg/common/tracelog"
2 years ago
"Open_IM/pkg/utils"
"context"
2 years ago
"gorm.io/gorm"
2 years ago
)
2 years ago
type GroupMemberGorm struct {
DB *gorm.DB
2 years ago
}
2 years ago
func NewGroupMemberDB(db *gorm.DB) *GroupMemberGorm {
return &GroupMemberGorm{DB: db}
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*table.GroupMemberModel, tx ...*gorm.DB) (err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMemberList", groupMemberList)
2 years ago
}()
2 years ago
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupMemberList).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Delete(ctx context.Context, groupMembers []*table.GroupMemberModel, tx ...*gorm.DB) (err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMembers", groupMembers)
2 years ago
}()
2 years ago
return utils.Wrap(getDBConn(g.DB, tx).Delete(groupMembers).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
2 years ago
}()
2 years ago
return utils.Wrap(getDBConn(g.DB, tx).Model(&table.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(args).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Update(ctx context.Context, groupMembers []*table.GroupMemberModel, tx ...*gorm.DB) (err error) {
2 years ago
defer func() { tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMembers", groupMembers) }()
2 years ago
return utils.Wrap(getDBConn(g.DB, tx).Updates(&groupMembers).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Find(ctx context.Context, groupMembers []*table.GroupMemberModel, tx ...*gorm.DB) (groupList []*table.GroupMemberModel, err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMembers", groupMembers, "groupList", groupList)
2 years ago
}()
var where [][]interface{}
for _, groupMember := range groupMembers {
where = append(where, []interface{}{groupMember.GroupID, groupMember.UserID})
}
2 years ago
return groupList, utils.Wrap(getDBConn(g.DB, tx).Where("(group_id, user_id) in ?", where).Find(&groupList).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...*gorm.DB) (groupMember *table.GroupMemberModel, err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
2 years ago
}()
2 years ago
groupMember = &table.GroupMemberModel{}
2 years ago
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
2 years ago
}
2 years ago
func (g *GroupMemberGorm) TakeOwnerInfo(ctx context.Context, groupID string, tx ...*gorm.DB) (groupMember *table.GroupMemberModel, err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMember", *groupMember)
2 years ago
}()
2 years ago
groupMember = &table.GroupMemberModel{}
2 years ago
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
2 years ago
}
2 years ago
//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 GetGroupMemberListByUserID(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 := GetGroupMemberListByUserID(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
//}