120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package session
|
|
|
|
import (
|
|
"ai-agent/workflow/consts/public"
|
|
sessionDto "ai-agent/workflow/model/dto/session"
|
|
"ai-agent/workflow/model/entity"
|
|
"context"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
var ExecWorkflowDao = &execWorkflowDao{}
|
|
|
|
type execWorkflowDao struct{}
|
|
|
|
func (d *execWorkflowDao) Insert(ctx context.Context, req *sessionDto.CreateWorkflowReq) (id int64, err error) {
|
|
var s = new(entity.ExecWorkflow)
|
|
if err = gconv.Struct(req, &s); err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).Insert(s)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
func (d *execWorkflowDao) Delete(ctx context.Context, req *sessionDto.DeleteExecWorkflowReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).Where(entity.ExecWorkflowCol.Id, req.Id).Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
func (d *execWorkflowDao) Update(ctx context.Context, req *sessionDto.UpdateWorkflowReq) (rows int64, err error) {
|
|
if req.Id <= 0 {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).OmitEmpty().Data(&req).Where(entity.ExecWorkflowCol.Id, req.Id).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// ClearError 清空执行记录的报错信息(重新执行成功后调用,OmitEmpty 的 Update 会跳过空串,需显式写空)
|
|
func (d *execWorkflowDao) ClearError(ctx context.Context, id int64) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
|
Where(entity.ExecWorkflowCol.Id, id).
|
|
Data(map[string]any{
|
|
entity.ExecWorkflowCol.ErrorMessage: "",
|
|
entity.ExecWorkflowCol.Error: "",
|
|
}).
|
|
Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
func (d *execWorkflowDao) GetById(ctx context.Context, id int64) (res *entity.ExecWorkflow, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
|
Where(entity.ExecWorkflowCol.Id, id).
|
|
One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
func (d *execWorkflowDao) List(ctx context.Context, creator string, page *beans.Page) (res []*entity.ExecWorkflow, total int, err error) {
|
|
m := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
|
Where(entity.ExecWorkflowCol.Creator, creator)
|
|
m.OrderDesc(entity.ExecWorkflowCol.CreatedAt)
|
|
if page != nil {
|
|
m.Page(int(page.PageNum), int(page.PageSize))
|
|
}
|
|
r, total, err := m.AllAndCount(false)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&res)
|
|
return
|
|
}
|
|
|
|
// GetLatestBySessionAndFlow 查询会话+工作流下最近一次执行记录(按创建时间倒序,无记录返回 nil)
|
|
func (d *execWorkflowDao) GetLatestBySessionAndFlow(ctx context.Context, sessionId string, flowId int64) (res *entity.ExecWorkflow, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
|
Where(entity.ExecWorkflowCol.SessionId, sessionId).
|
|
Where(entity.ExecWorkflowCol.FlowId, flowId).
|
|
OrderDesc(entity.ExecWorkflowCol.CreatedAt).
|
|
Limit(1).
|
|
One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if r.IsEmpty() {
|
|
return nil, nil
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// ListBySession 查询会话下工作流执行记录(按创建时间倒序)
|
|
func (d *execWorkflowDao) ListBySession(ctx context.Context, sessionId string) (res []*entity.ExecWorkflow, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
|
Where(entity.ExecWorkflowCol.SessionId, sessionId).
|
|
OrderDesc(entity.ExecWorkflowCol.CreatedAt).
|
|
All()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&res)
|
|
return
|
|
}
|