2025-12-09 15:30:16 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
2025-12-30 01:23:54 +08:00
|
|
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
|
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
2026-10-30 13:25:48 +08:00
|
|
|
|
"log"
|
2025-12-09 15:30:16 +08:00
|
|
|
|
"time"
|
2025-12-23 02:20:08 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
|
|
"gorm.io/gorm"
|
2025-12-09 15:30:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type LikeService struct {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
repo *LikeRepository
|
|
|
|
|
|
VideoRepo *VideoRepository
|
|
|
|
|
|
cache *rediscache.Client
|
|
|
|
|
|
likeMQ *rabbitmq.LikeMQ
|
|
|
|
|
|
popularityMQ *rabbitmq.PopularityMQ
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 01:23:54 +08:00
|
|
|
|
func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository, cache *rediscache.Client, likeMQ *rabbitmq.LikeMQ, popularityMQ *rabbitmq.PopularityMQ) *LikeService {
|
|
|
|
|
|
return &LikeService{repo: repo, VideoRepo: videoRepo, cache: cache, likeMQ: likeMQ, popularityMQ: popularityMQ}
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-23 02:20:08 +08:00
|
|
|
|
func isDupKey(err error) bool {
|
|
|
|
|
|
var me *mysql.MySQLError
|
|
|
|
|
|
return errors.As(err, &me) && me.Number == 1062
|
|
|
|
|
|
}
|
2025-12-16 18:52:09 +08:00
|
|
|
|
|
2025-12-23 02:20:08 +08:00
|
|
|
|
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if like == nil {
|
|
|
|
|
|
return errors.New("like is nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
if like.VideoID == 0 || like.AccountID == 0 {
|
|
|
|
|
|
return errors.New("video_id and account_id are required")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if s.VideoRepo != nil {
|
|
|
|
|
|
ok, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
|
|
|
|
|
if err != nil {
|
2025-12-23 02:20:08 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return errors.New("video not found")
|
2025-12-23 02:20:08 +08:00
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isLiked, err := s.repo.IsLiked(ctx, like.VideoID, like.AccountID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if isLiked {
|
|
|
|
|
|
return errors.New("user has liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
like.CreatedAt = time.Now()
|
2026-10-30 13:25:48 +08:00
|
|
|
|
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// 同一个键喂给点赞事件与热度事件,两条消费端各自去重。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
intentKey, err := NewIntentKey()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
likeEnqueued := false
|
|
|
|
|
|
popEnqueued := false
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if s.likeMQ != nil {
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if err := s.likeMQ.Like(ctx, like.AccountID, like.VideoID, intentKey); err == nil {
|
|
|
|
|
|
likeEnqueued = true
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if s.popularityMQ != nil {
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if err := s.popularityMQ.Update(ctx, like.VideoID, 1, intentKey); err == nil {
|
|
|
|
|
|
popEnqueued = true
|
2025-12-23 02:20:08 +08:00
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// 两条轨各自独立降级:合并判断会在"点赞已入队、热度失败"时再插一次 likes,
|
|
|
|
|
|
// 撞 idx_like_video_account 并返回 500。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if !likeEnqueued {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
err := s.repo.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
|
if err := tx.Select("id").First(&Video{}, like.VideoID).Error; err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return errors.New("video not found")
|
|
|
|
|
|
}
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := tx.Create(like).Error; err != nil {
|
|
|
|
|
|
if isDupKey(err) {
|
|
|
|
|
|
return errors.New("user has liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := tx.Model(&Video{}).Where("id = ?", like.VideoID).
|
|
|
|
|
|
UpdateColumn("likes_count", gorm.Expr("likes_count + 1")).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return tx.Model(&Video{}).Where("id = ?", like.VideoID).
|
|
|
|
|
|
UpdateColumn("popularity", gorm.Expr("popularity + 1")).Error
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// 热度是展示数据:写失败只降级记日志,不回滚已经成立的点赞。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if !popEnqueued {
|
|
|
|
|
|
if err := UpdatePopularityCache(ctx, s.cache, like.VideoID, 1, intentKey); err != nil {
|
|
|
|
|
|
log.Printf("like: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
|
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if like == nil {
|
|
|
|
|
|
return errors.New("like is nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
if like.VideoID == 0 || like.AccountID == 0 {
|
|
|
|
|
|
return errors.New("video_id and account_id are required")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if s.VideoRepo != nil {
|
|
|
|
|
|
ok, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return errors.New("video not found")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isLiked, err := s.repo.IsLiked(ctx, like.VideoID, like.AccountID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !isLiked {
|
|
|
|
|
|
return errors.New("user has not liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
intentKey, err := NewIntentKey()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
likeEnqueued := false
|
|
|
|
|
|
popEnqueued := false
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if s.likeMQ != nil {
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if err := s.likeMQ.Unlike(ctx, like.AccountID, like.VideoID, intentKey); err == nil {
|
|
|
|
|
|
likeEnqueued = true
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if s.popularityMQ != nil {
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if err := s.popularityMQ.Update(ctx, like.VideoID, -1, intentKey); err == nil {
|
|
|
|
|
|
popEnqueued = true
|
2025-12-23 02:20:08 +08:00
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
// 与 Like 对称:两条轨各自降级,不做合并判断。
|
|
|
|
|
|
if !likeEnqueued {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
err := s.repo.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
|
del := tx.Where("video_id = ? AND account_id = ?", like.VideoID, like.AccountID).Delete(&Like{})
|
|
|
|
|
|
if del.Error != nil {
|
|
|
|
|
|
return del.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
if del.RowsAffected == 0 {
|
|
|
|
|
|
return errors.New("user has not liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 err
|
2025-12-23 02:20:08 +08:00
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
2025-12-16 18:52:09 +08:00
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if !popEnqueued {
|
|
|
|
|
|
if err := UpdatePopularityCache(ctx, s.cache, like.VideoID, -1, intentKey); err != nil {
|
|
|
|
|
|
log.Printf("unlike: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
|
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
|
|
|
|
|
return s.repo.IsLiked(ctx, videoID, accountID)
|
|
|
|
|
|
}
|
2025-12-26 18:23:55 +08:00
|
|
|
|
|
|
|
|
|
|
func (s *LikeService) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
|
|
|
|
|
|
return s.repo.ListLikedVideos(ctx, accountID)
|
|
|
|
|
|
}
|