|
- package repo
-
- import (
- "gim/internal/business/comm/utils"
- "gim/internal/logic/domain/group/model"
- )
-
- type groupRepo struct{}
-
- var GroupRepo = new(groupRepo)
-
- // Get 获取群组信息
- func (*groupRepo) Get(groupId int64) (*model.Group, error) {
- group, err := GroupCache.Get(groupId)
- if err != nil {
- return nil, err
- }
- if group != nil {
- return group, nil
- }
-
- group, err = GroupDao.Get(groupId)
- if err != nil {
- return nil, err
- }
- members, err := GroupUserRepo.ListUser(groupId)
- if err != nil {
- return nil, err
- }
- group.Members = members
-
- err = GroupCache.Set(group)
- if err != nil {
- return nil, err
- }
- return group, nil
- }
-
- // GetLimit 获取群组信息(限量)
- func (*groupRepo) GetLimit(groupId, limit int64) (*model.Group, error) {
- group, err := GroupCache.GetLimit(utils.Int64ToStr(limit), groupId)
- if err != nil {
- return nil, err
- }
- if group != nil {
- return group, nil
- }
-
- group, err = GroupDao.Get(groupId)
- if err != nil {
- return nil, err
- }
- members, err := GroupUserRepo.ListUserLimit(groupId, limit)
- if err != nil {
- return nil, err
- }
- group.Members = members
-
- err = GroupCache.SetLimit(utils.Int64ToStr(limit), group)
- if err != nil {
- return nil, err
- }
- return group, nil
- }
-
- // GetForBanned 获取群组禁言用户信息
- func (*groupRepo) GetForBanned(groupId int64) (*model.Group, error) {
- group, err := GroupDao.Get(groupId)
- if err != nil {
- return nil, err
- }
- members, err := GroupUserRepo.ListBannedUser(groupId)
- if err != nil {
- return nil, err
- }
- group.Members = members
- return group, nil
- }
-
- // Save 获取群组信息
- func (*groupRepo) Save(group *model.Group) error {
- groupId := group.Id
- err := GroupDao.Save(group)
- if err != nil {
- return err
- }
-
- members := group.Members
- for i := range members {
- members[i].GroupId = group.Id
- if members[i].UpdateType == model.UpdateTypeUpdate {
- err = GroupUserRepo.Save(&(members[i]))
- if err != nil {
- return err
- }
- }
- if members[i].UpdateType == model.UpdateTypeDelete {
- err = GroupUserRepo.Delete(group.Id, members[i].UserId)
- if err != nil {
- return err
- }
- }
- }
-
- if groupId != 0 {
- err = GroupCache.Del(groupId)
- if err != nil {
- return err
- }
- }
- return nil
- }
|