golang-im聊天
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.
 
 
 
 

121 lines
2.3 KiB

  1. package repo
  2. import (
  3. "gim/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. // GetLimit 获取群组信息(限量)
  40. func (*groupRepo) GetLimit(groupId, limit int64) (*model.Group, error) {
  41. group, err := GroupCache.GetLimit(strconv.FormatInt(limit, 10), groupId)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if group != nil {
  46. return group, nil
  47. }
  48. group, err = GroupDao.Get(groupId)
  49. if err != nil {
  50. return nil, err
  51. }
  52. members, err := GroupUserRepo.ListUserLimit(groupId, limit)
  53. if err != nil {
  54. return nil, err
  55. }
  56. group.Members = members
  57. if len(members) == int(limit) {
  58. err = GroupCache.SetLimit(strconv.FormatInt(limit, 10), group)
  59. if err != nil {
  60. return nil, err
  61. }
  62. }
  63. return group, nil
  64. }
  65. // GetForBanned 获取群组禁言用户信息
  66. func (*groupRepo) GetForBanned(groupId int64) (*model.Group, error) {
  67. group, err := GroupDao.Get(groupId)
  68. if err != nil {
  69. return nil, err
  70. }
  71. members, err := GroupUserRepo.ListBannedUser(groupId)
  72. if err != nil {
  73. return nil, err
  74. }
  75. group.Members = members
  76. return group, nil
  77. }
  78. // Save 获取群组信息
  79. func (*groupRepo) Save(group *model.Group) error {
  80. groupId := group.Id
  81. err := GroupDao.Save(group)
  82. if err != nil {
  83. return err
  84. }
  85. members := group.Members
  86. for i := range members {
  87. members[i].GroupId = group.Id
  88. if members[i].UpdateType == model.UpdateTypeUpdate {
  89. err = GroupUserRepo.Save(&(members[i]))
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. if members[i].UpdateType == model.UpdateTypeDelete {
  95. err = GroupUserRepo.Delete(group.Id, members[i].UserId)
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. }
  101. if groupId != 0 {
  102. err = GroupCache.Del(groupId)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. return nil
  108. }