128 lines
3.2 KiB
Go
128 lines
3.2 KiB
Go
package radio
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"sundynix-go/global"
|
|
"sundynix-go/model/radio"
|
|
radioReq "sundynix-go/model/radio/request"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ChannelService struct{}
|
|
|
|
// GetChannelList 获取频道列表
|
|
func (s *ChannelService) GetChannelList(userId string, info radioReq.GetChannelList) ([]radio.RadioChannel, int64, error) {
|
|
db := global.DB.Model(&radio.RadioChannel{})
|
|
var list []radio.RadioChannel
|
|
var total int64
|
|
|
|
if info.CategoryId != "" {
|
|
db = db.Where("category_id = ?", info.CategoryId)
|
|
}
|
|
if info.Name != "" {
|
|
db = db.Where("name LIKE ?", "%"+info.Name+"%")
|
|
}
|
|
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("sort ASC").Find(&list).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// 批量查询用户订阅的频道,避免N+1问题
|
|
if userId != "" {
|
|
subService := &SubscriptionService{}
|
|
subscribedChannelIds, _ := subService.GetUserSubscriptionHistory(userId)
|
|
// 转换为map以便快速查找
|
|
subscribedMap := make(map[string]bool)
|
|
for _, cid := range subscribedChannelIds {
|
|
subscribedMap[cid] = true
|
|
}
|
|
// 填充HasSubscribed字段
|
|
for i := range list {
|
|
if subscribedMap[list[i].Id] {
|
|
list[i].HasSubscribed = 1
|
|
} else {
|
|
list[i].HasSubscribed = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
return list, total, nil
|
|
}
|
|
|
|
// GetChannelById 获取频道详情
|
|
func (s *ChannelService) GetChannelById(userId, id string) (radio.RadioChannel, error) {
|
|
var channel radio.RadioChannel
|
|
err := global.DB.Where("id = ?", id).Preload("Cover").First(&channel).Error
|
|
if err != nil {
|
|
return channel, err
|
|
}
|
|
|
|
// 填充HasSubscribed字段
|
|
if userId != "" {
|
|
subService := &SubscriptionService{}
|
|
hasSub, _ := subService.HasSubscription(userId, channel.Id)
|
|
if hasSub {
|
|
channel.HasSubscribed = 1
|
|
} else {
|
|
channel.HasSubscribed = 0
|
|
}
|
|
}
|
|
|
|
return channel, nil
|
|
}
|
|
|
|
// SaveChannel 保存频道
|
|
func (s *ChannelService) SaveChannel(req radioReq.SaveChannel) error {
|
|
channel := radio.RadioChannel{
|
|
CategoryId: req.CategoryId,
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
CoverId: req.CoverId,
|
|
Tags: req.Tags,
|
|
IsVipOnly: req.IsVipOnly,
|
|
Sort: req.Sort,
|
|
Status: req.Status,
|
|
}
|
|
return global.DB.Create(&channel).Error
|
|
}
|
|
|
|
// UpdateChannel 更新频道
|
|
func (s *ChannelService) UpdateChannel(req radioReq.UpdateChannel) error {
|
|
updates := map[string]interface{}{
|
|
"category_id": req.CategoryId,
|
|
"name": req.Name,
|
|
"description": req.Description,
|
|
"cover_id": req.CoverId,
|
|
"tags": req.Tags,
|
|
"is_vip_only": req.IsVipOnly,
|
|
"sort": req.Sort,
|
|
"status": req.Status,
|
|
}
|
|
return global.DB.Model(&radio.RadioChannel{}).Where("id = ?", req.Id).Updates(updates).Error
|
|
}
|
|
|
|
// DeleteChannel 删除频道
|
|
func (s *ChannelService) DeleteChannel(id string) error {
|
|
return global.DB.Transaction(func(tx *gorm.DB) error {
|
|
// 检查是否有节目使用此频道
|
|
var count int64
|
|
tx.Model(&radio.RadioProgram{}).Where("channel_id = ?", id).Count(&count)
|
|
if count > 0 {
|
|
return errors.New("该频道下存在节目,无法删除")
|
|
}
|
|
return tx.Where("id = ?", id).Delete(&radio.RadioChannel{}).Error
|
|
})
|
|
}
|