user_follow.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package model
  2. import (
  3. "context"
  4. "gorm.io/gorm"
  5. )
  6. type UserFollow struct {
  7. *Model
  8. UserId int64 `gorm:"column:user_id;default:0;NOT NULL"` // 用户id
  9. ToUserId int64 `gorm:"column:to_user_id;default:0;NOT NULL"` // 被关注的用户id
  10. }
  11. func (m *UserFollow) TableName() string {
  12. return "p_user_follow"
  13. }
  14. type UserFollowModel struct {
  15. conn *gorm.DB
  16. }
  17. func NewUserFollowModel(conn *gorm.DB) UserFollowModel {
  18. return UserFollowModel{
  19. conn: conn,
  20. }
  21. }
  22. // GetFansList 获取用户的粉丝列表
  23. func (m *UserFollowModel) GetFansList(ctx context.Context, userId int64, page, pageSize int) ([]*UserFollow, error) {
  24. var follows []*UserFollow
  25. offset := (page - 1) * pageSize
  26. err := m.conn.WithContext(ctx).
  27. Where("to_user_id = ? AND is_del = 0", userId).
  28. Offset(offset).
  29. Limit(pageSize).
  30. Order("created_on desc").
  31. Find(&follows).Error
  32. if err != nil {
  33. return nil, err
  34. }
  35. return follows, nil
  36. }
  37. // GetFollowsList 获取用户的关注列表
  38. func (m *UserFollowModel) GetFollowsList(ctx context.Context, userId int64, page, pageSize int) ([]*UserFollow, error) {
  39. var follows []*UserFollow
  40. offset := (page - 1) * pageSize
  41. err := m.conn.WithContext(ctx).
  42. Where("user_id = ? AND is_del = 0", userId).
  43. Offset(offset).
  44. Limit(pageSize).
  45. Order("created_on desc").
  46. Find(&follows).Error
  47. if err != nil {
  48. return nil, err
  49. }
  50. return follows, nil
  51. }
  52. // CheckMutualFollows 批量检查用户之间的关注关系
  53. func (m *UserFollowModel) CheckMutualFollows(ctx context.Context, userId int64, targetUserIds []int64) (map[int64]bool, error) {
  54. var follows []*UserFollow
  55. result := make(map[int64]bool)
  56. // 初始化结果map
  57. for _, targetId := range targetUserIds {
  58. result[targetId] = false
  59. }
  60. // 查询当前用户是否关注了目标用户们
  61. err := m.conn.WithContext(ctx).
  62. Where("user_id = ? AND to_user_id IN ? AND is_del = 0", userId, targetUserIds).
  63. Find(&follows).Error
  64. if err != nil {
  65. return nil, err
  66. }
  67. // 标记已关注的用户
  68. for _, follow := range follows {
  69. result[follow.ToUserId] = true
  70. }
  71. return result, nil
  72. }
  73. // UnFollow 取消关注
  74. func (m *UserFollowModel) UnFollow(ctx context.Context, userId, toUserId int64) error {
  75. return m.conn.WithContext(ctx).
  76. Model(&UserFollow{}).
  77. Where("user_id = ? AND to_user_id = ? AND is_del = 0", userId, toUserId).
  78. Update("is_del", 1).Error
  79. }
  80. // Follow 关注用户
  81. func (m *UserFollowModel) Follow(ctx context.Context, userId, toUserId int64) error {
  82. return m.conn.WithContext(ctx).Create(&UserFollow{
  83. UserId: userId,
  84. ToUserId: toUserId,
  85. }).Error
  86. }