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_user_repo.go 2.5 KiB

2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package repo
  2. import (
  3. "gim/internal/logic/domain/group/model"
  4. "gim/pkg/db"
  5. "gim/pkg/gerrors"
  6. "gim/pkg/pb"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type groupUserRepo struct{}
  10. var GroupUserRepo = new(groupUserRepo)
  11. // ListByUserId 获取用户加入的群组信息
  12. func (*groupUserRepo) ListByUserId(userId int64) ([]model.Group, error) {
  13. var groups []model.Group
  14. err := db.DB.Select("g.id,g.name,g.avatar_url,g.introduction,g.user_num,g.extra,g.create_time,g.update_time").
  15. Table("group_user u").
  16. Joins("join `group` g on u.group_id = g.id").
  17. Where("u.user_id = ?", userId).
  18. Find(&groups).Error
  19. if err != nil {
  20. return nil, gerrors.WrapError(err)
  21. }
  22. return groups, nil
  23. }
  24. // ListUser 获取群组用户信息
  25. func (*groupUserRepo) ListUser(groupId int64) ([]model.GroupUser, error) {
  26. var groupUsers []model.GroupUser
  27. err := db.DB.Find(&groupUsers, "group_id = ?", groupId).Error
  28. if err != nil {
  29. return nil, gerrors.WrapError(err)
  30. }
  31. return groupUsers, nil
  32. }
  33. // ListUserLimit 获取群组用户信息(限量)
  34. func (*groupUserRepo) ListUserLimit(groupId, limit int64) ([]model.GroupUser, error) {
  35. var groupUsers []model.GroupUser
  36. err := db.DB.Limit(limit).Find(&groupUsers, "group_id = ?", groupId).Error
  37. if err != nil {
  38. return nil, gerrors.WrapError(err)
  39. }
  40. return groupUsers, nil
  41. }
  42. // ListBannedUser 获取群组禁言用户信息
  43. func (*groupUserRepo) ListBannedUser(groupId int64) ([]model.GroupUser, error) {
  44. var groupUsers []model.GroupUser
  45. err := db.DB.Find(&groupUsers, "group_id = ? and status = ?", groupId, pb.GroupUserStatusType_GROUP_USER_STATUS_Banned).Error
  46. if err != nil {
  47. return nil, gerrors.WrapError(err)
  48. }
  49. return groupUsers, nil
  50. }
  51. // Get 获取群组用户信息,用户不存在返回nil
  52. func (*groupUserRepo) Get(groupId, userId int64) (*model.GroupUser, error) {
  53. var groupUser model.GroupUser
  54. err := db.DB.First(&groupUser, "group_id = ? and user_id = ?", groupId, userId).Error
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, gerrors.WrapError(err)
  57. }
  58. if err == gorm.ErrRecordNotFound {
  59. return nil, nil
  60. }
  61. return &groupUser, nil
  62. }
  63. // Save 将用户添加到群组
  64. func (*groupUserRepo) Save(groupUser *model.GroupUser) error {
  65. err := db.DB.Save(&groupUser).Error
  66. if err != nil {
  67. return gerrors.WrapError(err)
  68. }
  69. return nil
  70. }
  71. // Delete 将用户从群组删除
  72. func (d *groupUserRepo) Delete(groupId int64, userId int64) error {
  73. err := db.DB.Exec("delete from group_user where group_id = ? and user_id = ?",
  74. groupId, userId).Error
  75. if err != nil {
  76. return gerrors.WrapError(err)
  77. }
  78. return nil
  79. }