61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package flow
|
|
|
|
import (
|
|
"ai-agent/workflow/consts/public"
|
|
"ai-agent/workflow/model/entity"
|
|
"context"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
)
|
|
|
|
var FlowCheckpointDao = &flowCheckpointDao{}
|
|
|
|
type flowCheckpointDao struct{}
|
|
|
|
// Upsert 插入或更新checkpoint数据(按 checkpoint_id 冲突则更新)
|
|
func (d *flowCheckpointDao) Upsert(ctx context.Context, checkpointId string, data string) error {
|
|
record := &entity.FlowCheckpoint{
|
|
CheckpointId: checkpointId,
|
|
Data: data,
|
|
}
|
|
// Save 在 PostgreSQL 中自动执行 INSERT ON CONFLICT DO UPDATE
|
|
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).
|
|
Model(ctx, public.TableNameFlowCheckpoint).
|
|
Save(record)
|
|
return err
|
|
}
|
|
|
|
func (d *flowCheckpointDao) SaveOrUpdate(ctx context.Context, checkpointId string, data string) (err error) {
|
|
res := &entity.FlowCheckpoint{
|
|
CheckpointId: checkpointId,
|
|
Data: data,
|
|
}
|
|
_, err = gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameFlowCheckpoint).Data(res).OnConflict(entity.FlowCheckpointCol.CheckpointId).Save()
|
|
return err
|
|
}
|
|
|
|
// Get 根据 checkpoint_id 获取 checkpoint 数据
|
|
func (d *flowCheckpointDao) Get(ctx context.Context, checkpointId string) (res *entity.FlowCheckpoint, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).
|
|
Model(ctx, public.TableNameFlowCheckpoint).
|
|
Where(entity.FlowCheckpointCol.CheckpointId, checkpointId).
|
|
One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r == nil {
|
|
return nil, nil
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// Delete 根据 checkpoint_id 删除checkpoint数据
|
|
func (d *flowCheckpointDao) Delete(ctx context.Context, checkpointId string) error {
|
|
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).
|
|
Model(ctx, public.TableNameFlowCheckpoint).
|
|
Where(entity.FlowCheckpointCol.CheckpointId, checkpointId).
|
|
Delete()
|
|
return err
|
|
}
|