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

99 lines
2.7 KiB

2 years ago
package relation
3 years ago
import (
2 years ago
"Open_IM/pkg/common/constant"
3 years ago
"Open_IM/pkg/utils"
3 years ago
"fmt"
2 years ago
3 years ago
"time"
)
func InsertIntoGroup(groupInfo Group) error {
3 years ago
if groupInfo.GroupName == "" {
groupInfo.GroupName = "Group Chat"
}
3 years ago
groupInfo.CreateTime = time.Now()
2 years ago
if groupInfo.NotificationUpdateTime.Unix() < 0 {
groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0)
}
2 years ago
err := GroupDB.Create(groupInfo).Error
if err != nil {
return err
}
return nil
}
func TakeGroupInfoByGroupID(groupID string) (*Group, error) {
var groupInfo Group
2 years ago
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
return &groupInfo, err
}
func GetGroupInfoByGroupID(groupID string) (*Group, error) {
var groupInfo Group
2 years ago
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
2 years ago
return &groupInfo, err
}
func SetGroupInfo(groupInfo Group) error {
2 years ago
return GroupDB.Where("group_id=?", groupInfo.GroupID).Updates(&groupInfo).Error
}
3 years ago
2 years ago
type GroupWithNum struct {
Group
2 years ago
MemberCount int `gorm:"column:num"`
}
2 years ago
func GetGroupsByName(groupName string, pageNumber, showNumber int32) ([]GroupWithNum, int64, error) {
2 years ago
var groups []GroupWithNum
2 years ago
var count int64
2 years ago
sql := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
2 years ago
Where(" name like ? and status != ?", fmt.Sprintf("%%%s%%", groupName), constant.GroupStatusDismissed)
if err := sql.Count(&count).Error; err != nil {
return nil, 0, err
}
err := sql.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groups).Error
return groups, count, err
3 years ago
}
2 years ago
func GetGroups(pageNumber, showNumber int) ([]GroupWithNum, error) {
var groups []GroupWithNum
2 years ago
if err := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
2 years ago
Limit(showNumber).Offset(showNumber * (pageNumber - 1)).Find(&groups).Error; err != nil {
3 years ago
return groups, err
}
3 years ago
return groups, nil
}
3 years ago
func OperateGroupStatus(groupId string, groupStatus int32) error {
group := Group{
3 years ago
GroupID: groupId,
Status: groupStatus,
3 years ago
}
3 years ago
if err := SetGroupInfo(group); err != nil {
return err
}
return nil
}
func GetGroupsCountNum(group Group) (int32, error) {
2 years ago
var count int64
2 years ago
if err := GroupDB.Where(" name like ? ", fmt.Sprintf("%%%s%%", group.GroupName)).Count(&count).Error; err != nil {
3 years ago
return 0, err
}
2 years ago
return int32(count), nil
3 years ago
}
3 years ago
3 years ago
func UpdateGroupInfoDefaultZero(groupID string, args map[string]interface{}) error {
2 years ago
return GroupDB.Where("group_id = ? ", groupID).Updates(args).Error
3 years ago
}
3 years ago
2 years ago
func GetGroupIDListByGroupType(groupType int) ([]string, error) {
var groupIDList []string
2 years ago
if err := GroupDB.Where("group_type = ? ", groupType).Pluck("group_id", &groupIDList).Error; err != nil {
2 years ago
return nil, err
}
return groupIDList, nil
}