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.

group_repo.go 2.2 KiB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package repo
  2. import (
  3. "gim/internal/business/comm/utils"
  4. "gim/internal/logic/domain/group/model"
  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. err = GroupCache.Set(group)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return group, nil
  31. }
  32. // GetLimit 获取群组信息(限量)
  33. func (*groupRepo) GetLimit(groupId, limit int64) (*model.Group, error) {
  34. group, err := GroupCache.GetLimit(utils.Int64ToStr(limit), groupId)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if group != nil {
  39. return group, nil
  40. }
  41. group, err = GroupDao.Get(groupId)
  42. if err != nil {
  43. return nil, err
  44. }
  45. members, err := GroupUserRepo.ListUserLimit(groupId, limit)
  46. if err != nil {
  47. return nil, err
  48. }
  49. group.Members = members
  50. err = GroupCache.SetLimit(utils.Int64ToStr(limit), group)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return group, nil
  55. }
  56. // GetForBanned 获取群组禁言用户信息
  57. func (*groupRepo) GetForBanned(groupId int64) (*model.Group, error) {
  58. group, err := GroupDao.Get(groupId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. members, err := GroupUserRepo.ListBannedUser(groupId)
  63. if err != nil {
  64. return nil, err
  65. }
  66. group.Members = members
  67. return group, nil
  68. }
  69. // Save 获取群组信息
  70. func (*groupRepo) Save(group *model.Group) error {
  71. groupId := group.Id
  72. err := GroupDao.Save(group)
  73. if err != nil {
  74. return err
  75. }
  76. members := group.Members
  77. for i := range members {
  78. members[i].GroupId = group.Id
  79. if members[i].UpdateType == model.UpdateTypeUpdate {
  80. err = GroupUserRepo.Save(&(members[i]))
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. if members[i].UpdateType == model.UpdateTypeDelete {
  86. err = GroupUserRepo.Delete(group.Id, members[i].UserId)
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. if groupId != 0 {
  93. err = GroupCache.Del(groupId)
  94. if err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }