64 lines
2.6 KiB
Go
64 lines
2.6 KiB
Go
package flow
|
||
|
||
import (
|
||
"ai-agent/workflow/consts/public"
|
||
"ai-agent/workflow/model/entity"
|
||
"context"
|
||
"fmt"
|
||
|
||
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
||
)
|
||
|
||
var FlowSegmentResultDao = &flowSegmentResultDao{}
|
||
|
||
type flowSegmentResultDao struct{}
|
||
|
||
// Save 段成功后落库:唯一键 (execution_id, node_id, segment_index),冲突则更新
|
||
func (d *flowSegmentResultDao) Save(ctx context.Context, execId int64, nodeId string, segmentIndex int, videoKey, videoURL string) error {
|
||
rec := &entity.FlowSegmentResult{
|
||
ExecutionId: execId,
|
||
NodeId: nodeId,
|
||
SegmentIndex: segmentIndex,
|
||
VideoKey: videoKey,
|
||
VideoURL: videoURL,
|
||
}
|
||
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).
|
||
Model(ctx, public.TableNameFlowSegmentResult).
|
||
Data(rec).
|
||
OnConflict(entity.FlowSegmentResultCol.ExecutionId, entity.FlowSegmentResultCol.NodeId, entity.FlowSegmentResultCol.SegmentIndex).
|
||
Save()
|
||
return err
|
||
}
|
||
|
||
// ListByNode 返回该节点已成功段(段序号 → 视频引用)
|
||
func (d *flowSegmentResultDao) ListByNode(ctx context.Context, execId int64, nodeId string) (map[int]entity.SegmentRef, error) {
|
||
var list []*entity.FlowSegmentResult
|
||
err := gfdb.DB(ctx, public.DbNameBlackDeacon).
|
||
Model(ctx, public.TableNameFlowSegmentResult).
|
||
Where(entity.FlowSegmentResultCol.ExecutionId, execId).
|
||
Where(entity.FlowSegmentResultCol.NodeId, nodeId).
|
||
Scan(&list)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
m := make(map[int]entity.SegmentRef, len(list))
|
||
for _, r := range list {
|
||
m[r.SegmentIndex] = entity.SegmentRef{Key: r.VideoKey, URL: r.VideoURL}
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// DeleteByExecution 清理指定执行的段结果(全新执行前清旧段 / 工作流执行成功后清理)。
|
||
// 必须物理删除:实体嵌入 SQLBaseDO(含 deleted_at) 会让 gfdb Model.Delete() 退化为软删除,
|
||
// 而 Save 的 ON CONFLICT DO UPDATE SET 不含 deleted_at(OmitNil 丢弃 nil),
|
||
// 软删后同键重存无法复活该行 → 续跑复用静默失效(每次重执行都全量重生成)。
|
||
func (d *flowSegmentResultDao) DeleteByExecution(ctx context.Context, execId int64) error {
|
||
// 表名须用物理全名:raw Exec 不经过 GoFrame 的 config prefix(black_deacon_)自动加前缀,
|
||
// 与 update.sql 物理建表名 black_deacon_flow_segment_result 保持一致;常量是短名
|
||
// flow_segment_result(经 Model() 自动加前缀),不能在此复用。
|
||
const physicalTable = "black_deacon_flow_segment_result"
|
||
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).
|
||
Exec(ctx, fmt.Sprintf("DELETE FROM %s WHERE execution_id = ?", physicalTable), execId)
|
||
return err
|
||
}
|