Files
media/dao/video/video_caption_task_dao.go
2026-06-22 09:18:45 +08:00

99 lines
3.0 KiB
Go

package video
import (
"context"
dto "media/model/dto/video"
entity "media/model/entity/video"
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
"github.com/gogf/gf/v2/frame/g"
)
// CaptionTask 字幕叠加任务 DAO 单例
var CaptionTask = new(captionTaskDao)
type captionTaskDao struct{}
const captionTaskTable = "video_caption_task"
// Insert 插入字幕任务记录
func (d *captionTaskDao) Insert(ctx context.Context, data *entity.VideoCaptionTask) (id int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, captionTaskTable).Data(data).Insert()
if err != nil {
return 0, err
}
return r.LastInsertId()
}
// GetByTaskID 按 taskId 查询
func (d *captionTaskDao) GetByTaskID(ctx context.Context, taskID string) (res *entity.VideoCaptionTask, err error) {
r, err := gfdb.DB(ctx).Model(ctx, captionTaskTable).
Where(entity.VideoCaptionTaskCols.TaskID, taskID).One()
if err != nil {
return nil, err
}
if r == nil {
return nil, nil
}
err = r.Struct(&res)
return
}
// UpdateRunning 更新任务为运行中
func (d *captionTaskDao) UpdateRunning(ctx context.Context, taskID string) error {
_, err := gfdb.DB(ctx).Model(ctx, captionTaskTable).
Data(g.Map{entity.VideoCaptionTaskCols.Status: "running"}).
Where(entity.VideoCaptionTaskCols.TaskID, taskID).Update()
return err
}
// UpdateResolution 更新视频分辨率
func (d *captionTaskDao) UpdateResolution(ctx context.Context, taskID string, width, height int) error {
_, err := gfdb.DB(ctx).Model(ctx, captionTaskTable).
Data(g.Map{
entity.VideoCaptionTaskCols.Width: width,
entity.VideoCaptionTaskCols.Height: height,
}).
Where(entity.VideoCaptionTaskCols.TaskID, taskID).Update()
return err
}
// UpdateSuccess 更新任务为成功
func (d *captionTaskDao) UpdateSuccess(ctx context.Context, taskID, fileURL string, fileSize int64, fileName, durationStr string) error {
_, err := gfdb.DB(ctx).Model(ctx, captionTaskTable).
Data(g.Map{
entity.VideoCaptionTaskCols.Status: "success",
entity.VideoCaptionTaskCols.FileURL: fileURL,
entity.VideoCaptionTaskCols.FileSize: fileSize,
entity.VideoCaptionTaskCols.FileName: fileName,
entity.VideoCaptionTaskCols.DurationStr: durationStr,
}).
Where(entity.VideoCaptionTaskCols.TaskID, taskID).Update()
return err
}
// UpdateError 更新任务为失败
func (d *captionTaskDao) UpdateError(ctx context.Context, taskID, errMsg string) error {
_, err := gfdb.DB(ctx).Model(ctx, captionTaskTable).
Data(g.Map{
entity.VideoCaptionTaskCols.Status: "failed",
entity.VideoCaptionTaskCols.ErrorMessage: errMsg,
}).
Where(entity.VideoCaptionTaskCols.TaskID, taskID).Update()
return err
}
// EntityToCaptionTaskRes 实体转查询响应
func EntityToCaptionTaskRes(e *entity.VideoCaptionTask) *dto.GetCaptionTaskRes {
return &dto.GetCaptionTaskRes{
TaskID: e.TaskID,
Status: e.Status,
FileURL: e.FileURL,
FileSize: e.FileSize,
FileName: e.FileName,
DurationStr: e.DurationStr,
ErrorMessage: e.ErrorMessage,
}
}