177 lines
5.2 KiB
Go
177 lines
5.2 KiB
Go
package radio
|
|
|
|
import (
|
|
"errors"
|
|
"sundynix-go/global"
|
|
"sundynix-go/model/radio"
|
|
radioReq "sundynix-go/model/radio/request"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type InteractionService struct{}
|
|
|
|
// AddHistory 添加收听历史
|
|
func (s *InteractionService) AddHistory(userId string, req radioReq.AddHistory) error {
|
|
// 先查找是否已存在记录
|
|
var history radio.RadioHistory
|
|
err := global.DB.Where("user_id = ? AND program_id = ?", userId, req.ProgramId).First(&history).Error
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
// 不存在,创建新记录
|
|
history = radio.RadioHistory{
|
|
UserId: userId,
|
|
ProgramId: req.ProgramId,
|
|
Progress: req.Progress,
|
|
Duration: req.Duration,
|
|
}
|
|
return global.DB.Create(&history).Error
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 存在,更新进度
|
|
return global.DB.Model(&history).Updates(map[string]interface{}{
|
|
"progress": req.Progress,
|
|
"duration": req.Duration,
|
|
}).Error
|
|
}
|
|
|
|
// GetHistoryList 获取收听历史列表
|
|
func (s *InteractionService) GetHistoryList(userId string, info radioReq.GetHistoryList) ([]radio.RadioHistory, int64, error) {
|
|
db := global.DB.Model(&radio.RadioHistory{}).Where("user_id = ?", userId).Preload("RadioProgram")
|
|
var list []radio.RadioHistory
|
|
var total int64
|
|
|
|
err := db.Count(&total).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (info.Current - 1) * info.PageSize
|
|
err = db.Offset(offset).Limit(info.PageSize).Order("created_at DESC").Find(&list).Error
|
|
return list, total, err
|
|
}
|
|
|
|
// ToggleLike 切换点赞状态
|
|
func (s *InteractionService) ToggleLike(userId, programId string) (bool, error) {
|
|
var like radio.RadioLike
|
|
err := global.DB.Where("user_id = ? AND program_id = ?", userId, programId).First(&like).Error
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
// 未点赞,添加点赞
|
|
like = radio.RadioLike{
|
|
UserId: userId,
|
|
ProgramId: programId,
|
|
}
|
|
if err := global.DB.Create(&like).Error; err != nil {
|
|
return false, err
|
|
}
|
|
// 增加节目点赞数
|
|
global.DB.Model(&radio.RadioProgram{}).Where("id = ?", programId).
|
|
UpdateColumn("like_count", gorm.Expr("like_count + ?", 1))
|
|
return true, nil
|
|
}
|
|
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 已点赞,取消点赞
|
|
if err := global.DB.Delete(&like).Error; err != nil {
|
|
return false, err
|
|
}
|
|
// 减少节目点赞数
|
|
global.DB.Model(&radio.RadioProgram{}).Where("id = ?", programId).
|
|
UpdateColumn("like_count", gorm.Expr("like_count - ?", 1))
|
|
return false, nil
|
|
}
|
|
|
|
// IsLiked 检查是否已点赞
|
|
func (s *InteractionService) IsLiked(userId, programId string) (bool, error) {
|
|
var count int64
|
|
err := global.DB.Model(&radio.RadioLike{}).Where("user_id = ? AND program_id = ?", userId, programId).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
// AddFavorite 添加收藏
|
|
func (s *InteractionService) AddFavorite(userId, programId string) error {
|
|
// 检查是否已收藏
|
|
var existing radio.RadioFavorite
|
|
err := global.DB.Where("user_id = ? AND program_id = ?", userId, programId).First(&existing).Error
|
|
if err == nil {
|
|
return errors.New("已经收藏过该节目")
|
|
}
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return err
|
|
}
|
|
|
|
favorite := radio.RadioFavorite{
|
|
UserId: userId,
|
|
ProgramId: programId,
|
|
}
|
|
return global.DB.Create(&favorite).Error
|
|
}
|
|
|
|
// RemoveFavorite 取消收藏
|
|
func (s *InteractionService) RemoveFavorite(userId, programId string) error {
|
|
return global.DB.Where("user_id = ? AND program_id = ?", userId, programId).Delete(&radio.RadioFavorite{}).Error
|
|
}
|
|
|
|
// GetFavoriteList 获取收藏列表
|
|
func (s *InteractionService) GetFavoriteList(userId string, info radioReq.GetFavoriteList) ([]radio.RadioFavorite, int64, error) {
|
|
db := global.DB.Model(&radio.RadioFavorite{}).Where("user_id = ?", userId)
|
|
var list []radio.RadioFavorite
|
|
var total int64
|
|
|
|
err := db.Count(&total).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (info.Current - 1) * info.PageSize
|
|
err = db.Offset(offset).Limit(info.PageSize).Order("created_at DESC").Find(&list).Error
|
|
return list, total, err
|
|
}
|
|
|
|
// IsFavorited 检查是否已收藏
|
|
func (s *InteractionService) IsFavorited(userId, programId string) (bool, error) {
|
|
var count int64
|
|
err := global.DB.Model(&radio.RadioFavorite{}).Where("user_id = ? AND program_id = ?", userId, programId).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
// AddComment 添加评论
|
|
func (s *InteractionService) AddComment(userId string, req radioReq.AddComment) error {
|
|
comment := radio.RadioComment{
|
|
ProgramId: req.ProgramId,
|
|
UserId: userId,
|
|
ParentId: req.ParentId,
|
|
Content: req.Content,
|
|
}
|
|
return global.DB.Create(&comment).Error
|
|
}
|
|
|
|
// DeleteComment 删除评论
|
|
func (s *InteractionService) DeleteComment(userId, commentId string) error {
|
|
return global.DB.Where("id = ? AND user_id = ?", commentId, userId).Delete(&radio.RadioComment{}).Error
|
|
}
|
|
|
|
// GetCommentList 获取评论列表
|
|
func (s *InteractionService) GetCommentList(programId string, info radioReq.GetCommentList) ([]radio.RadioComment, int64, error) {
|
|
db := global.DB.Model(&radio.RadioComment{}).Where("program_id = ?", programId)
|
|
var list []radio.RadioComment
|
|
var total int64
|
|
|
|
err := db.Count(&total).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (info.Current - 1) * info.PageSize
|
|
err = db.Offset(offset).Limit(info.PageSize).Order("created_at DESC").Find(&list).Error
|
|
return list, total, err
|
|
}
|