|
- package repo
-
- import (
- "gim/internal/logic/domain/group/model"
- "strconv"
- )
-
- 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
- if group.UserNum != int32(len(group.Members)) {
- group.UserNum = int32(len(group.Members))
- err = GroupDao.Save(group)
- if err != nil {
- return nil, err
- }
- }
- 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(strconv.FormatInt(limit, 10), 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
-
- if len(members) == int(limit) {
- err = GroupCache.SetLimit(strconv.FormatInt(limit, 10), 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
- }
|