Files
VLoop/backend/internal/feed/repo.go

127 lines
4.1 KiB
Go
Raw Normal View History

2026-05-20 16:34:19 +08:00
package feed
import (
"context"
"feedsystem_video_go/internal/social"
"feedsystem_video_go/internal/video"
"time"
"gorm.io/gorm"
)
type FeedRepository struct {
db *gorm.DB
}
func NewFeedRepository(db *gorm.DB) *FeedRepository {
return &FeedRepository{db: db}
}
// 查询最新视频. limit: 查询数量 latestBefore: 查询早于此时间的视频(零值则不限制)
2026-05-20 16:34:19 +08:00
func (repo *FeedRepository) ListLatest(ctx context.Context, limit int, latestBefore time.Time) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("create_time DESC")
if !latestBefore.IsZero() {
query = query.Where("create_time < ?", latestBefore)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
// 查询点赞数最多的视频. limit: 查询数量 cursor: 游标
2026-05-20 16:34:19 +08:00
func (repo *FeedRepository) ListLikesCountWithCursor(ctx context.Context, limit int, cursor *LikesCountCursor) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("likes_count DESC, id DESC")
if cursor != nil {
query = query.Where(
"(likes_count < ?) OR (likes_count = ? AND id < ?)",
cursor.LikesCount,
cursor.LikesCount, cursor.ID,
)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
// 查询关注用户的视频. limit: 查询数量 viewerAccountID: 查看者账户ID latestBefore: 查询早于此时间的视频(零值则不限制)
2026-05-20 16:34:19 +08:00
func (repo *FeedRepository) ListByFollowing(ctx context.Context, limit int, viewerAccountID uint, latestBefore time.Time) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("create_time DESC")
if viewerAccountID > 0 {
followingSubQuery := repo.db.WithContext(ctx).
Model(&social.Social{}).
Select("vlogger_id").
Where("follower_id = ?", viewerAccountID)
query = query.Where("author_id IN (?)", followingSubQuery)
}
if !latestBefore.IsZero() {
query = query.Where("create_time < ?", latestBefore)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
type HotWindowRow struct {
VideoID uint `gorm:"column:video_id"`
Score int64 `gorm:"column:score"`
}
2026-05-20 16:34:19 +08:00
// ListHotWindow 在 MySQL 上重算热榜窗口。排序定义必须与 Redis 路径一致:
// 分数降序,同分按 video_id 降序(ZSET 成员补零后就是同一个顺序)。
//
// 两个子查询各走一个 (created_at, video_id) 覆盖索引,UNION ALL 后再
// GROUP BY,避免把两张大表 JOIN 起来。
func (repo *FeedRepository) ListHotWindow(ctx context.Context, from, to time.Time, limit, offset int) ([]HotWindowRow, error) {
const q = `
SELECT video_id, CAST(SUM(cnt) AS SIGNED) AS score FROM (
SELECT video_id, COUNT(*) AS cnt FROM likes
WHERE created_at >= ? AND created_at < ? GROUP BY video_id
UNION ALL
SELECT video_id, COUNT(*) AS cnt FROM comments
WHERE created_at >= ? AND created_at < ? GROUP BY video_id
) t
GROUP BY video_id
ORDER BY score DESC, video_id DESC
LIMIT ? OFFSET ?`
var rows []HotWindowRow
if err := repo.db.WithContext(ctx).Raw(q, from, to, from, to, limit, offset).Scan(&rows).Error; err != nil {
2026-05-20 16:34:19 +08:00
return nil, err
}
return rows, nil
2026-05-20 16:34:19 +08:00
}
func (repo *FeedRepository) GetByIDs(ctx context.Context, ids []uint) ([]*video.Video, error) {
var videos []*video.Video
if len(ids) == 0 {
return videos, nil
}
if err := repo.db.WithContext(ctx).Model(&video.Video{}).
Where("id IN ?", ids).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
func (repo *FeedRepository) ListByTag(ctx context.Context, tagName string, limit int) ([]*video.Video, error) {
var videos []*video.Video
err := repo.db.WithContext(ctx).Model(&video.Video{}).Table("videos").
Joins("JOIN video_tags ON video_tags.video_id = videos.id").
Joins("JOIN tags ON tags.id = video_tags.tag_id").
Where("tags.name = ?", tagName).
Order("videos.create_time desc").
Limit(limit).
Find(&videos).Error
return videos, err
}