package video import ( "context" "errors" "gorm.io/gorm" "gorm.io/gorm/clause" ) type LikeRepository struct { db *gorm.DB } func NewLikeRepository(db *gorm.DB) *LikeRepository { return &LikeRepository{db: db} } // ApplyLike 插入点赞行,同一事务里给 likes_count 与 popularity 各 +1。 // // 判重交给唯一索引,不做"先查再插":并发下必然漏判。 // created=false 表示行本来就存在,此时两个计数都不能再动。 // // DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。 func (r *LikeRepository) ApplyLike(ctx context.Context, like *Like) (created bool, err error) { if like == nil || like.VideoID == 0 || like.AccountID == 0 { return false, nil } err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { res := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(like) if res.Error != nil { return res.Error } if res.RowsAffected == 0 { return nil } created = true if err := tx.Model(&Video{}).Where("id = ?", like.VideoID). UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + 1, 0)")).Error; err != nil { return err } return tx.Model(&Video{}).Where("id = ?", like.VideoID). UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + 1, 0)")).Error }) if err != nil { return false, err } return created, nil } // ApplyUnlike 删除点赞行并把两个计数各 -1,返回被删的那一行。 // // created_at 必须来自库里:取消点赞要把分数减回点赞那一刻的桶, // 用调用方此刻的钟会留下一个永远偏高的旧桶。 func (r *LikeRepository) ApplyUnlike(ctx context.Context, videoID, accountID uint) (like *Like, deleted bool, err error) { if videoID == 0 || accountID == 0 { return nil, false, nil } err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { var row Like if err := tx.Where("video_id = ? AND account_id = ?", videoID, accountID).First(&row).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil } return err } res := tx.Delete(&Like{}, row.ID) if res.Error != nil { return res.Error } if res.RowsAffected == 0 { return nil } deleted = true like = &row if err := tx.Model(&Video{}).Where("id = ?", videoID). UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count - 1, 0)")).Error; err != nil { return err } return tx.Model(&Video{}).Where("id = ?", videoID). UpdateColumn("popularity", gorm.Expr("GREATEST(popularity - 1, 0)")).Error }) if err != nil { return nil, false, err } return like, deleted, nil } func (r *LikeRepository) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) { var count int64 err := r.db.WithContext(ctx).Model(&Like{}). Where("video_id = ? AND account_id = ?", videoID, accountID). Count(&count).Error if err != nil { return false, err } return count > 0, nil } // 查询给定视频 ID 列表中,哪些视频被指定账户点赞过 func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, accountID uint) (map[uint]bool, error) { likeMap := make(map[uint]bool) if len(videoIDs) == 0 { return likeMap, nil } if accountID == 0 { return likeMap, nil } var likes []Like err := r.db.WithContext(ctx).Model(&Like{}). Where("video_id IN ? AND account_id = ?", videoIDs, accountID). Find(&likes).Error if err != nil { return nil, err } for _, like := range likes { likeMap[like.VideoID] = true } return likeMap, nil } func (r *LikeRepository) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) { var videos []Video if accountID == 0 { return videos, nil } err := r.db.WithContext(ctx). Model(&Video{}). Joins("JOIN likes ON likes.video_id = videos.id"). Where("likes.account_id = ?", accountID). Order("likes.created_at desc"). Limit(200). Find(&videos).Error if err != nil { return nil, err } return videos, nil }