99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
package radio
|
|
|
|
import (
|
|
"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("Cover").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 string) (*radio.RadioProgram, error) {
|
|
var program radio.RadioProgram
|
|
err := global.DB.Where("id = ?", id).Preload("Cover").Preload("Audio").First(&program).Error
|
|
return &program, err
|
|
}
|
|
|
|
// SaveProgram 保存节目
|
|
func (s *ProgramService) SaveProgram(req radioReq.SaveProgram) error {
|
|
program := radio.RadioProgram{
|
|
ChannelId: req.ChannelId,
|
|
Title: req.Title,
|
|
Description: req.Description,
|
|
Content: req.Content,
|
|
CoverId: req.CoverId,
|
|
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_id": req.CoverId,
|
|
"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
|
|
}
|