129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package dao
|
|
|
|
import (
|
|
"ai-agent/digital-human/consts"
|
|
"ai-agent/digital-human/consts/public"
|
|
"ai-agent/digital-human/model/dto"
|
|
"ai-agent/digital-human/model/entity"
|
|
"context"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
var Audio = &audio{}
|
|
|
|
type audio struct{}
|
|
|
|
// Insert 插入音频
|
|
func (d *audio) Insert(ctx context.Context, req *dto.CreateAudioReq) (id int64, err error) {
|
|
var res *entity.Audio
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAudio).Data(&res).Insert()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新音频
|
|
func (d *audio) Update(ctx context.Context, id int64, updateData *entity.Audio) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAudio).Where(entity.AudioCol.Id, id).Data(&updateData).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// UpdateStatus 更新音频状态
|
|
func (d *audio) UpdateStatus(ctx context.Context, id int64, status consts.AudioStatus, errorMsg string, audioURL string, duration int, externalID string) (rows int64, err error) {
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameAudio).Where(entity.AudioCol.Id, id)
|
|
|
|
updateData := gdb.Map{
|
|
entity.AudioCol.Status: int(status),
|
|
}
|
|
if errorMsg != "" {
|
|
updateData[entity.AudioCol.ErrorMsg] = errorMsg
|
|
}
|
|
if audioURL != "" {
|
|
updateData[entity.AudioCol.AudioURL] = audioURL
|
|
}
|
|
if duration > 0 {
|
|
updateData[entity.AudioCol.Duration] = duration
|
|
}
|
|
if externalID != "" {
|
|
updateData[entity.AudioCol.ExternalID] = externalID
|
|
}
|
|
|
|
r, err := model.Data(&updateData).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除音频
|
|
func (d *audio) Delete(ctx context.Context, id int64) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAudio).Where(entity.AudioCol.Id, id).Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// GetOne 获取单个音频
|
|
func (d *audio) GetOne(ctx context.Context, id int64) (audio *entity.Audio, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAudio).Where(entity.AudioCol.Id, id).One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&audio)
|
|
return
|
|
}
|
|
|
|
// List 获取音频列表
|
|
func (d *audio) List(ctx context.Context, req *dto.ListAudioReq) (res []*entity.Audio, total int, err error) {
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameAudio).OmitEmpty()
|
|
|
|
// 构建查询过滤条件
|
|
if req.Status != consts.AudioStatusGenerating && req.Status != consts.AudioStatusSuccess && req.Status != consts.AudioStatusFailed {
|
|
// 不添加状态过滤
|
|
} else {
|
|
model = model.Where(entity.AudioCol.Status+" = ?", req.Status)
|
|
}
|
|
|
|
if !g.IsEmpty(req.Keyword) {
|
|
like := "%" + req.Keyword + "%"
|
|
model = model.Where(
|
|
"("+entity.AudioCol.Name+
|
|
" LIKE ? OR "+entity.AudioCol.Description+
|
|
" LIKE ? OR "+entity.AudioCol.ScriptText+
|
|
" LIKE ?)",
|
|
like, like, like,
|
|
)
|
|
}
|
|
|
|
model = model.OrderDesc(entity.AudioCol.CreatedAt)
|
|
|
|
if req.Page != nil {
|
|
if req.Page.PageNum <= 0 {
|
|
req.Page.PageNum = 1
|
|
}
|
|
if req.Page.PageSize <= 0 {
|
|
req.Page.PageSize = 10
|
|
}
|
|
model = model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
|
}
|
|
|
|
r, total, err := model.AllAndCount(false)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&res)
|
|
return
|
|
}
|