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.
 
 
 
 

145 lines
2.8 KiB

  1. package repo
  2. import (
  3. "egg-im/internal/logic/domain/group/model"
  4. "strconv"
  5. )
  6. type groupRepo struct{}
  7. var GroupRepo = new(groupRepo)
  8. // Get 获取群组信息
  9. func (*groupRepo) Get(groupId int64) (*model.Group, error) {
  10. group, err := GroupCache.Get(groupId)
  11. if err != nil {
  12. return nil, err
  13. }
  14. if group != nil {
  15. return group, nil
  16. }
  17. group, err = GroupDao.Get(groupId)
  18. if err != nil {
  19. return nil, err
  20. }
  21. members, err := GroupUserRepo.ListUser(groupId)
  22. if err != nil {
  23. return nil, err
  24. }
  25. group.Members = members
  26. if group.UserNum != int32(len(group.Members)) {
  27. group.UserNum = int32(len(group.Members))
  28. err = GroupDao.Save(group)
  29. if err != nil {
  30. return nil, err
  31. }
  32. }
  33. err = GroupCache.Set(group)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return group, nil
  38. }
  39. // GetGroupNotice 获取群组公告信息
  40. func (*groupRepo) GetGroupNotice(groupId int64) (*model.GroupNotice, error) {
  41. group, err := GroupNoticeCache.Get(groupId)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if group != nil {
  46. return group, nil
  47. }
  48. group, err = GroupNoticeDao.Get(groupId)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if group == nil {
  53. return nil, nil
  54. }
  55. err = GroupNoticeCache.Set(group)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return group, nil
  60. }
  61. // GetLimit 获取群组信息(限量)
  62. func (*groupRepo) GetLimit(groupId, limit int64) (*model.Group, error) {
  63. group, err := GroupCache.GetLimit(strconv.FormatInt(limit, 10), groupId)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if group != nil {
  68. return group, nil
  69. }
  70. group, err = GroupDao.Get(groupId)
  71. if err != nil {
  72. return nil, err
  73. }
  74. members, err := GroupUserRepo.ListUserLimit(groupId, limit)
  75. if err != nil {
  76. return nil, err
  77. }
  78. group.Members = members
  79. if len(members) == int(limit) {
  80. err = GroupCache.SetLimit(strconv.FormatInt(limit, 10), group)
  81. if err != nil {
  82. return nil, err
  83. }
  84. }
  85. return group, nil
  86. }
  87. // GetForBanned 获取群组禁言用户信息
  88. func (*groupRepo) GetForBanned(groupId int64) (*model.Group, error) {
  89. group, err := GroupDao.Get(groupId)
  90. if err != nil {
  91. return nil, err
  92. }
  93. members, err := GroupUserRepo.ListBannedUser(groupId)
  94. if err != nil {
  95. return nil, err
  96. }
  97. group.Members = members
  98. return group, nil
  99. }
  100. // Save 获取群组信息
  101. func (*groupRepo) Save(group *model.Group) error {
  102. groupId := group.Id
  103. err := GroupDao.Save(group)
  104. if err != nil {
  105. return err
  106. }
  107. members := group.Members
  108. for i := range members {
  109. members[i].GroupId = group.Id
  110. if members[i].UpdateType == model.UpdateTypeUpdate {
  111. err = GroupUserRepo.Save(&(members[i]))
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. if members[i].UpdateType == model.UpdateTypeDelete {
  117. err = GroupUserRepo.Delete(group.Id, members[i].UserId)
  118. if err != nil {
  119. return err
  120. }
  121. }
  122. }
  123. if groupId != 0 {
  124. err = GroupCache.Del(groupId)
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. return nil
  130. }