Files
sundynix-radio-be/service/radio/voice_service.go
T
2026-04-28 10:16:23 +08:00

152 lines
4.4 KiB
Go

package radio
import (
"sundynix-go/global"
"sundynix-go/model/radio"
radioReq "sundynix-go/model/radio/request"
"gorm.io/gorm"
)
type VoiceService struct{}
// GetVoiceList 获取音色列表
func (s *VoiceService) GetVoiceList(req radioReq.GetVoiceList) ([]radio.RadioVoice, int64, error) {
db := global.DB.Model(&radio.RadioVoice{}).Preload("Audio")
var list []radio.RadioVoice
var total int64
if req.Name != "" {
db = db.Where("name LIKE ?", "%"+req.Name+"%")
}
if req.Status > 0 {
db = db.Where("status = ?", req.Status)
}
err := db.Count(&total).Error
if err != nil {
return nil, 0, err
}
offset := (req.Current - 1) * req.PageSize
err = db.Offset(offset).Limit(req.PageSize).Order("sort ASC").Find(&list).Error
if err != nil {
return nil, 0, err
}
return list, total, nil
}
// GetVoiceById 获取音色详情
func (s *VoiceService) GetVoiceById(id string) (*radio.RadioVoice, error) {
var voice radio.RadioVoice
err := global.DB.Preload("Audio").Where("id = ?", id).First(&voice).Error
return &voice, err
}
// GetVoiceBySpeakerId 根据SpeakerId获取音色
func (s *VoiceService) GetVoiceBySpeakerId(speakerId string) (*radio.RadioVoice, error) {
var voice radio.RadioVoice
err := global.DB.Where("speaker_id = ? AND status = ?", speakerId, 1).First(&voice).Error
return &voice, err
}
// GetDefaultVoice 获取默认音色
func (s *VoiceService) GetDefaultVoice() (*radio.RadioVoice, error) {
var voice radio.RadioVoice
err := global.DB.Where("is_default = ? AND status = ?", 1, 1).First(&voice).Error
if err != nil {
// 如果没有默认音色,返回第一个启用的音色
err = global.DB.Where("status = ?", 1).Order("sort ASC").First(&voice).Error
}
return &voice, err
}
// GetAllEnabledVoice 获取所有启用的音色(前端选择用)
func (s *VoiceService) GetAllEnabledVoice() ([]radio.RadioVoice, error) {
var list []radio.RadioVoice
err := global.DB.Where("status = ?", 1).Order("sort ASC").Find(&list).Error
if err != nil {
return nil, err
}
return list, nil
}
// SaveVoice 保存音色
func (s *VoiceService) SaveVoice(req radioReq.SaveVoice) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
// 如果设置为默认音色,先取消其他默认
if req.IsDefault == 1 {
if err := tx.Model(&radio.RadioVoice{}).Where("is_default = ?", 1).Update("is_default", 0).Error; err != nil {
return err
}
}
voice := radio.RadioVoice{
SpeakerId: req.SpeakerId,
Name: req.Name,
Description: req.Description,
Gender: req.Gender,
Icon: req.Icon,
AudioId: req.AudioId,
Sort: req.Sort,
Status: req.Status,
IsDefault: req.IsDefault,
}
return tx.Create(&voice).Error
})
}
// UpdateVoice 更新音色
func (s *VoiceService) UpdateVoice(req radioReq.UpdateVoice) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
updates := map[string]interface{}{
"speaker_id": req.SpeakerId,
"name": req.Name,
"description": req.Description,
"gender": req.Gender,
"icon": req.Icon,
"audio_id": req.AudioId,
"sort": req.Sort,
"status": req.Status,
}
// 如果设置为默认音色,先取消其他默认
if req.IsDefault == 1 {
if err := tx.Model(&radio.RadioVoice{}).Where("is_default = ? AND id != ?", 1, req.Id).Update("is_default", 0).Error; err != nil {
return err
}
updates["is_default"] = 1
}
return tx.Model(&radio.RadioVoice{}).Where("id = ?", req.Id).Updates(updates).Error
})
}
// DeleteVoice 删除音色
func (s *VoiceService) DeleteVoice(ids []string) error {
return global.DB.Where("id IN ?", ids).Delete(&radio.RadioVoice{}).Error
}
// IncrementUseCount 增加使用次数
func (s *VoiceService) IncrementUseCount(speakerId string) error {
return global.DB.Model(&radio.RadioVoice{}).Where("speaker_id = ?", speakerId).UpdateColumn("use_count", gorm.Expr("use_count + 1")).Error
}
// SetDefaultVoice 设置默认音色
func (s *VoiceService) SetDefaultVoice(id string) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
// 先清除其他默认
if err := tx.Model(&radio.RadioVoice{}).Where("is_default = ?", 1).Update("is_default", 0).Error; err != nil {
return err
}
// 设置当前为默认
if err := tx.Model(&radio.RadioVoice{}).Where("id = ?", id).Update("is_default", 1).Error; err != nil {
return err
}
return nil
})
}
var VoiceServiceApp = new(VoiceService)