package social import ( "context" "feedsystem_video_go/internal/account" "gorm.io/gorm" "gorm.io/gorm/clause" ) type SocialRepository struct { db *gorm.DB } func NewSocialRepository(db *gorm.DB) *SocialRepository { return &SocialRepository{db: db} } // Follow 返回本次是否真的插入关系:重复关注是幂等成功,不是错误。 // 判重交给唯一索引 idx_social_follower_vlogger,OnConflict 把冲突变成一次不报错的 SQL。 func (r *SocialRepository) Follow(ctx context.Context, social *Social) (bool, error) { res := r.db.WithContext(ctx). Clauses(clause.OnConflict{DoNothing: true}). Create(social) if res.Error != nil { return false, res.Error } return res.RowsAffected > 0, nil } // Unfollow 用影响行数回答"这次是否真的删掉":第二次取关是空操作。 func (r *SocialRepository) Unfollow(ctx context.Context, social *Social) (bool, error) { res := r.db.WithContext(ctx). Where("follower_id = ? AND vlogger_id = ?", social.FollowerID, social.VloggerID). Delete(&Social{}) if res.Error != nil { return false, res.Error } return res.RowsAffected > 0, nil } func (r *SocialRepository) GetAllFollowers(ctx context.Context, VloggerID uint) ([]*account.Account, error) { var relations []Social if err := r.db.WithContext(ctx). Model(&Social{}). Where("vlogger_id = ?", VloggerID). Limit(200). Find(&relations).Error; err != nil { return nil, err } followerIDs := make([]uint, 0, len(relations)) for _, rel := range relations { followerIDs = append(followerIDs, rel.FollowerID) } if len(followerIDs) == 0 { return []*account.Account{}, nil } var followers []*account.Account if err := r.db.WithContext(ctx). Model(&account.Account{}). Where("id IN ?", followerIDs). Find(&followers).Error; err != nil { return nil, err } return followers, nil } func (r *SocialRepository) GetAllVloggers(ctx context.Context, FollowerID uint) ([]*account.Account, error) { var relations []Social if err := r.db.WithContext(ctx). Model(&Social{}). Where("follower_id = ?", FollowerID). Limit(200). Find(&relations).Error; err != nil { return nil, err } vloggerIDs := make([]uint, 0, len(relations)) for _, rel := range relations { vloggerIDs = append(vloggerIDs, rel.VloggerID) } if len(vloggerIDs) == 0 { return []*account.Account{}, nil } var vloggers []*account.Account if err := r.db.WithContext(ctx). Model(&account.Account{}). Where("id IN ?", vloggerIDs). Find(&vloggers).Error; err != nil { return nil, err } return vloggers, nil } func (r *SocialRepository) IsFollowed(ctx context.Context, social *Social) (bool, error) { var count int64 if err := r.db.WithContext(ctx). Model(&Social{}). Where("follower_id = ? AND vlogger_id = ?", social.FollowerID, social.VloggerID). Count(&count).Error; err != nil { return false, err } return count > 0, nil } func (r *SocialRepository) CountFollowers(ctx context.Context, vloggerID uint) (int64, error) { var count int64 if err := r.db.WithContext(ctx).Model(&Social{}).Where("vlogger_id = ?", vloggerID).Count(&count).Error; err != nil { return 0, err } return count, nil } func (r *SocialRepository) CountVloggers(ctx context.Context, followerID uint) (int64, error) { var count int64 if err := r.db.WithContext(ctx).Model(&Social{}).Where("follower_id = ?", followerID).Count(&count).Error; err != nil { return 0, err } return count, nil }