feat: 音色管理
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/radio"
|
||||
radioReq "sundynix-go/model/radio/request"
|
||||
radioRes "sundynix-go/model/radio/response"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -12,8 +11,8 @@ import (
|
||||
type VoiceService struct{}
|
||||
|
||||
// GetVoiceList 获取音色列表
|
||||
func (s *VoiceService) GetVoiceList(req radioReq.GetVoiceList) ([]radioRes.VoiceResponse, int64, error) {
|
||||
db := global.DB.Model(&radio.RadioVoice{})
|
||||
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
|
||||
|
||||
@@ -34,51 +33,14 @@ func (s *VoiceService) GetVoiceList(req radioReq.GetVoiceList) ([]radioRes.Voice
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 转换为响应结构
|
||||
var resp []radioRes.VoiceResponse
|
||||
for _, v := range list {
|
||||
resp = append(resp, radioRes.VoiceResponse{
|
||||
Id: v.Id,
|
||||
SpeakerId: v.SpeakerId,
|
||||
Name: v.Name,
|
||||
Description: v.Description,
|
||||
Gender: v.Gender,
|
||||
Icon: v.Icon,
|
||||
AudioId: v.AudioId,
|
||||
Sort: v.Sort,
|
||||
Status: v.Status,
|
||||
IsDefault: v.IsDefault,
|
||||
UseCount: v.UseCount,
|
||||
})
|
||||
}
|
||||
return resp, total, nil
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// GetVoiceById 获取音色详情
|
||||
func (s *VoiceService) GetVoiceById(id string) (*radioRes.VoiceDetailResponse, error) {
|
||||
func (s *VoiceService) GetVoiceById(id string) (*radio.RadioVoice, error) {
|
||||
var voice radio.RadioVoice
|
||||
err := global.DB.Preload("Audio").Where("id = ?", id).First(&voice).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &radioRes.VoiceDetailResponse{
|
||||
Id: voice.Id,
|
||||
SpeakerId: voice.SpeakerId,
|
||||
Name: voice.Name,
|
||||
Description: voice.Description,
|
||||
Gender: voice.Gender,
|
||||
Icon: voice.Icon,
|
||||
AudioId: voice.AudioId,
|
||||
Audio: voice.Audio,
|
||||
Sort: voice.Sort,
|
||||
Status: voice.Status,
|
||||
IsDefault: voice.IsDefault,
|
||||
UseCount: voice.UseCount,
|
||||
CreateTime: voice.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: voice.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
return &voice, err
|
||||
}
|
||||
|
||||
// GetVoiceBySpeakerId 根据SpeakerId获取音色
|
||||
@@ -100,30 +62,19 @@ func (s *VoiceService) GetDefaultVoice() (*radio.RadioVoice, error) {
|
||||
}
|
||||
|
||||
// GetAllEnabledVoice 获取所有启用的音色(前端选择用)
|
||||
func (s *VoiceService) GetAllEnabledVoice() ([]radioRes.VoiceResponse, error) {
|
||||
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
|
||||
}
|
||||
|
||||
var resp []radioRes.VoiceResponse
|
||||
for _, v := range list {
|
||||
resp = append(resp, radioRes.VoiceResponse{
|
||||
Id: v.Id,
|
||||
SpeakerId: v.SpeakerId,
|
||||
Name: v.Name,
|
||||
Gender: v.Gender,
|
||||
Icon: v.Icon,
|
||||
IsDefault: v.IsDefault,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user