feat: 音色管理
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/radio"
|
||||
radioReq "sundynix-go/model/radio/request"
|
||||
radioRes "sundynix-go/model/radio/response"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type VoiceService struct{}
|
||||
|
||||
// GetVoiceList 获取音色列表
|
||||
func (s *VoiceService) GetVoiceList(req radioReq.GetVoiceList) ([]radioRes.VoiceResponse, int64, error) {
|
||||
db := global.DB.Model(&radio.RadioVoice{})
|
||||
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
|
||||
}
|
||||
|
||||
// 转换为响应结构
|
||||
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
|
||||
}
|
||||
|
||||
// GetVoiceById 获取音色详情
|
||||
func (s *VoiceService) GetVoiceById(id string) (*radioRes.VoiceDetailResponse, 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
|
||||
}
|
||||
|
||||
// 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() ([]radioRes.VoiceResponse, 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
|
||||
}
|
||||
|
||||
// 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)
|
||||
Reference in New Issue
Block a user