2026-05-20 16:34:19 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2026-11-01 15:15:15 +08:00
|
|
|
|
"gorm.io/gorm/clause"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type LikeRepository struct {
|
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewLikeRepository(db *gorm.DB) *LikeRepository {
|
|
|
|
|
|
return &LikeRepository{db: db}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
// ApplyLike 插入点赞行,并在同一事务里给 likes_count 与 popularity 各 +1。
|
|
|
|
|
|
//
|
|
|
|
|
|
// created=false 表示这一行本来就在(唯一索引 idx_like_video_account 判重),
|
|
|
|
|
|
// 此时计数与热榜都不能再动:重复投递只能算一次。判重交给数据库,不做
|
|
|
|
|
|
// "先查再插"——那样并发下必然漏判。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 注意:DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。
|
|
|
|
|
|
func (r *LikeRepository) ApplyLike(ctx context.Context, like *Like) (created bool, err error) {
|
2026-05-20 16:34:19 +08:00
|
|
|
|
if like == nil || like.VideoID == 0 || like.AccountID == 0 {
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
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
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
return created, nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
// ApplyUnlike 删除点赞行,并在同一事务里把两个计数各 -1。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 返回被删的那一行:它的 created_at 是热榜回退时要用的时间。取消点赞必须
|
|
|
|
|
|
// 把分数减回点赞那一刻的桶,所以这个值必须来自库里,而不是调用方此刻的钟。
|
|
|
|
|
|
// deleted=false 表示没有这一行(重复投递),此时什么都不改。
|
|
|
|
|
|
func (r *LikeRepository) ApplyUnlike(ctx context.Context, videoID, accountID uint) (like *Like, deleted bool, err error) {
|
2026-05-20 16:34:19 +08:00
|
|
|
|
if videoID == 0 || accountID == 0 {
|
2026-11-01 15:15:15 +08:00
|
|
|
|
return nil, false, nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
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
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-15 11:54:01 +08:00
|
|
|
|
// 查询给定视频 ID 列表中,哪些视频被指定账户点赞过
|
2026-05-20 16:34:19 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|