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.
 
 
 
 

95 lines
2.7 KiB

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