Files
sundynix-radio-be/service/radio/program_service.go
T
2026-03-09 17:25:23 +08:00

145 lines
4.0 KiB
Go

package radio
import (
"fmt"
"sundynix-go/global"
"sundynix-go/model/radio"
radioReq "sundynix-go/model/radio/request"
"gorm.io/gorm"
)
type ProgramService struct{}
// GetProgramList 获取节目列表
func (s *ProgramService) GetProgramList(info radioReq.GetProgramList) ([]radio.RadioProgram, int64, error) {
db := global.DB.Model(&radio.RadioProgram{}).Preload("Audio")
var list []radio.RadioProgram
var total int64
if info.ChannelId != "" {
db = db.Where("channel_id = ?", info.ChannelId)
}
if info.Title != "" {
db = db.Where("title LIKE ?", "%"+info.Title+"%")
}
if info.Status > 0 {
db = db.Where("status = ?", info.Status)
}
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
}
// GetProgramById 获取节目详情
func (s *ProgramService) GetProgramById(id, userId string) (*radio.RadioProgram, error) {
var program radio.RadioProgram
err := global.DB.Where("id = ?", id).Preload("Audio").First(&program).Error
program.HasLiked = 0
program.HasFavorite = 0
liked, err := InteractionServiceApp.IsLiked(userId, id)
if liked {
program.HasLiked = 1
}
favorite, err := InteractionServiceApp.IsFavorited(userId, id)
if favorite {
program.HasFavorite = 1
}
return &program, err
}
// SaveProgram 保存节目
func (s *ProgramService) SaveProgram(req radioReq.SaveProgram) error {
var channel radio.RadioChannel
if err := global.DB.Where("id = ?", req.ChannelId).First(&channel).Error; err != nil {
return err
}
program := radio.RadioProgram{
ChannelId: req.ChannelId,
Title: req.Title,
Description: req.Description,
Content: req.Content,
Cover: channel.Cover,
AudioId: req.AudioId,
Duration: req.Duration,
Tags: req.Tags,
Status: req.Status,
}
return global.DB.Create(&program).Error
}
// UpdateProgram 更新节目
func (s *ProgramService) UpdateProgram(req radioReq.UpdateProgram) error {
updates := map[string]interface{}{
"channel_id": req.ChannelId,
"title": req.Title,
"description": req.Description,
"content": req.Content,
"cover": req.Cover,
"audio_id": req.AudioId,
"duration": req.Duration,
"tags": req.Tags,
"status": req.Status,
}
return global.DB.Model(&radio.RadioProgram{}).Where("id = ?", req.Id).Updates(updates).Error
}
// DeleteProgram 删除节目
func (s *ProgramService) DeleteProgram(ids []string) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
// 删除相关的收藏记录
tx.Where("program_id in ?", ids).Delete(&radio.RadioFavorite{})
// 删除相关的点赞记录
tx.Where("program_id in ?", ids).Delete(&radio.RadioLike{})
// 删除相关的历史记录
tx.Where("program_id in ?", ids).Delete(&radio.RadioHistory{})
// 删除相关的评论
tx.Where("program_id in ?", ids).Delete(&radio.RadioComment{})
// 删除节目
return tx.Where("id in ?", ids).Delete(&radio.RadioProgram{}).Error
})
}
// IncrementPlayCount 增加播放次数
func (s *ProgramService) IncrementPlayCount(id string) error {
return global.DB.Model(&radio.RadioProgram{}).Where("id = ?", id).
UpdateColumn("play_count", gorm.Expr("play_count + ?", 1)).Error
}
// GenerateTTS 生成TTS语音并更新节目 (异步)
func (s *ProgramService) GenerateTTS(programId string) error {
// 1. 获取节目内容
var program radio.RadioProgram
if err := global.DB.Where("id = ?", programId).First(&program).Error; err != nil {
return err
}
if program.Content == "" {
return fmt.Errorf("节目内容为空")
}
// 2. 调用TTS提交任务 (异步,后台处理)
ttsReq := TTSTextToSpeechRequest{
Text: program.Content,
//VoiceType: 101001, // 智瑜 情感女声
VoiceType: 101013, // 智辉 新闻男声
Speed: 0, // 正常语速
Volume: 0, // 正常音量
ProgramId: programId,
}
_, err := TTSServiceApp.SubmitTTSTask(ttsReq)
if err != nil {
return err
}
// 任务已提交,异步处理中
return nil
}