调用模型,返回事件的时间线
This commit is contained in:
136
dao/video/analysis_task_dao.go
Normal file
136
dao/video/analysis_task_dao.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
dto "media/model/dto/video"
|
||||
entity "media/model/entity/video"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var AnalysisTask = new(analysisTaskDao)
|
||||
|
||||
type analysisTaskDao struct{}
|
||||
|
||||
const analysisTaskTable = "video_analysis_task"
|
||||
|
||||
// Insert 创建任务(排除 id 字段,让数据库自增)
|
||||
func (d *analysisTaskDao) Insert(ctx context.Context, data *entity.AnalysisTask) (id int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, analysisTaskTable).
|
||||
Data(data).
|
||||
FieldsEx(entity.AnalysisTaskCols.Id).
|
||||
Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// GetByTaskID 根据taskId查询任务
|
||||
func (d *analysisTaskDao) GetByTaskID(ctx context.Context, taskID string) (res *entity.AnalysisTask, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, analysisTaskTable).
|
||||
Where(entity.AnalysisTaskCols.TaskID, taskID).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r == nil {
|
||||
return nil, nil
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateProcessing 更新为处理中
|
||||
func (d *analysisTaskDao) UpdateProcessing(ctx context.Context, taskID string) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, analysisTaskTable).
|
||||
Data(g.Map{
|
||||
entity.AnalysisTaskCols.Status: "processing",
|
||||
}).
|
||||
Where(entity.AnalysisTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateProgress 更新视频处理计数
|
||||
func (d *analysisTaskDao) UpdateProgress(ctx context.Context, taskID string, successCount, failedCount int) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, analysisTaskTable).
|
||||
Data(g.Map{
|
||||
entity.AnalysisTaskCols.SuccessCount: successCount,
|
||||
entity.AnalysisTaskCols.FailedCount: failedCount,
|
||||
}).
|
||||
Where(entity.AnalysisTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateSuccess 更新为成功
|
||||
func (d *analysisTaskDao) UpdateSuccess(ctx context.Context, taskID string, successCount, failedCount int) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, analysisTaskTable).
|
||||
Data(g.Map{
|
||||
entity.AnalysisTaskCols.Status: "success",
|
||||
entity.AnalysisTaskCols.SuccessCount: successCount,
|
||||
entity.AnalysisTaskCols.FailedCount: failedCount,
|
||||
entity.AnalysisTaskCols.ErrorMessage: "",
|
||||
}).
|
||||
Where(entity.AnalysisTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateError 更新为失败
|
||||
func (d *analysisTaskDao) UpdateError(ctx context.Context, taskID string, errMsg string) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, analysisTaskTable).
|
||||
Data(g.Map{
|
||||
entity.AnalysisTaskCols.Status: "failed",
|
||||
entity.AnalysisTaskCols.ErrorMessage: errMsg,
|
||||
}).
|
||||
Where(entity.AnalysisTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// EntityToTaskRes 实体转DTO
|
||||
func AnalysisEntityToTaskRes(e *entity.AnalysisTask, details []*entity.AnalysisTaskDetail) *dto.GetAnalysisTaskRes {
|
||||
res := &dto.GetAnalysisTaskRes{
|
||||
TaskID: e.TaskID,
|
||||
Status: e.Status,
|
||||
Total: e.Total,
|
||||
SuccessCount: e.SuccessCount,
|
||||
FailedCount: e.FailedCount,
|
||||
Processed: e.SuccessCount + e.FailedCount,
|
||||
CreatedAt: gconv.Int64(e.CreatedAt.Timestamp()),
|
||||
}
|
||||
if e.CreatedAt == nil {
|
||||
res.CreatedAt = time.Now().UnixMilli()
|
||||
}
|
||||
if e.Status == "failed" {
|
||||
res.ErrorMessage = e.ErrorMessage
|
||||
}
|
||||
|
||||
// 转换详情列表
|
||||
for _, d := range details {
|
||||
item := dto.AnalysisDetailItem{
|
||||
VideoURL: d.VideoURL,
|
||||
VideoSavePath: d.VideoSavePath,
|
||||
Status: d.Status,
|
||||
FailReason: d.FailReason,
|
||||
}
|
||||
// caption_result 如果非空,尝试解析为 interface{}
|
||||
if d.CaptionResult != "" {
|
||||
var captionResult interface{}
|
||||
if err := gconv.Scan(d.CaptionResult, &captionResult); err == nil {
|
||||
item.CaptionResult = captionResult
|
||||
} else {
|
||||
item.CaptionResult = d.CaptionResult
|
||||
}
|
||||
}
|
||||
res.List = append(res.List, item)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user