feat: 新增工作流执行模块
新增流程执行记录的实体、DTO、DAO、控制器和服务层,支持工作流的执行、回调及结果树状列表查询;同时更新服务名称为 ai-agent。
This commit is contained in:
185
workflow/service/flow/flow_user_service.go
Normal file
185
workflow/service/flow/flow_user_service.go
Normal file
@@ -0,0 +1,185 @@
|
||||
package flow
|
||||
|
||||
import (
|
||||
"ai-agent/workflow/consts/flow"
|
||||
flowDao "ai-agent/workflow/dao/flow"
|
||||
flowDto "ai-agent/workflow/model/dto/flow"
|
||||
"ai-agent/workflow/model/entity"
|
||||
"context"
|
||||
|
||||
commonHttp "gitea.com/red-future/common/http"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var FlowUserService = &flowUserService{}
|
||||
|
||||
type flowUserService struct{}
|
||||
|
||||
// IsAdmin 调用admin-go服务检查是否是管理员
|
||||
func IsAdmin(ctx context.Context) (res bool, err error) {
|
||||
headers := make(map[string]string)
|
||||
if r := g.RequestFromCtx(ctx); r != nil {
|
||||
for k, v := range r.Request.Header {
|
||||
if len(v) > 0 {
|
||||
headers[k] = v[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
var r = make(map[string]bool)
|
||||
if err = commonHttp.Get(ctx, "admin-go/api/v1/system/user/checkIsSuperAdmin", headers, &r); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return r["isSuperAdmin"], err
|
||||
}
|
||||
|
||||
func (s *flowUserService) Create(ctx context.Context, req *flowDto.CreateFlowUserReq) (res *flowDto.CreateFlowUserRes, err error) {
|
||||
admin, err := IsAdmin(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.NodeInputParams = ExtractFlowNodeFrom(req.FlowContent)
|
||||
var id int64
|
||||
if admin {
|
||||
id, err = flowDao.FlowTemplateDao.Insert(ctx, &flowDto.CreateFlowTemplateReq{
|
||||
FlowTemplateName: req.FlowName,
|
||||
Description: req.Description,
|
||||
FlowContent: req.FlowContent,
|
||||
NodeInputParams: req.NodeInputParams,
|
||||
Status: flow.FlowTemplateStatusEnable.Code(),
|
||||
})
|
||||
} else {
|
||||
id, err = flowDao.FlowUserDao.Insert(ctx, req)
|
||||
}
|
||||
return &flowDto.CreateFlowUserRes{Id: id}, err
|
||||
}
|
||||
|
||||
func (s *flowUserService) Update(ctx context.Context, req *flowDto.UpdateFlowUserReq) (err error) {
|
||||
admin, err := IsAdmin(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.NodeInputParams = ExtractFlowNodeFrom(req.FlowContent)
|
||||
get, err := flowDao.FlowTemplateDao.Get(ctx, &flowDto.GetFlowTemplateReq{
|
||||
Id: req.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !g.IsEmpty(get) && !admin {
|
||||
_, err = flowDao.FlowUserDao.Insert(ctx, &flowDto.CreateFlowUserReq{
|
||||
FlowName: req.FlowName,
|
||||
Description: req.Description,
|
||||
FlowContent: req.FlowContent,
|
||||
NodeInputParams: req.NodeInputParams,
|
||||
SourceFlowTemplateId: get.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if admin {
|
||||
_, err = flowDao.FlowTemplateDao.Update(ctx, &flowDto.UpdateFlowTemplateReq{
|
||||
Id: req.Id,
|
||||
FlowTemplateName: req.FlowName,
|
||||
Description: req.Description,
|
||||
FlowContent: req.FlowContent,
|
||||
NodeInputParams: req.NodeInputParams,
|
||||
Status: flow.FlowTemplateStatusEnable.Code(),
|
||||
})
|
||||
} else {
|
||||
_, err = flowDao.FlowUserDao.Update(ctx, req)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ExtractFlowNodeFrom(flowContent *entity.FlowInfo) []*entity.FlowNode {
|
||||
var flowNodes []*entity.FlowNode
|
||||
for _, item := range flowContent.Nodes {
|
||||
flowNodes = append(flowNodes, &item)
|
||||
}
|
||||
return flowNodes
|
||||
}
|
||||
|
||||
func (s *flowUserService) Delete(ctx context.Context, req *flowDto.DeleteFlowUserReq) (err error) {
|
||||
admin, err := IsAdmin(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if admin {
|
||||
_, err = flowDao.FlowTemplateDao.Delete(ctx, &flowDto.DeleteFlowTemplateReq{
|
||||
Id: req.Id,
|
||||
})
|
||||
} else {
|
||||
_, err = flowDao.FlowUserDao.Delete(ctx, req)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *flowUserService) Get(ctx context.Context, req *flowDto.GetFlowUserReq) (res *flowDto.FlowUserVO, err error) {
|
||||
|
||||
var flowInfo *entity.FlowTemplate
|
||||
flowInfo, err = flowDao.FlowTemplateDao.Get(ctx, &flowDto.GetFlowTemplateReq{
|
||||
Id: req.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if flowInfo != nil {
|
||||
res = new(flowDto.FlowUserVO)
|
||||
res.FlowName = flowInfo.FlowTemplateName
|
||||
err = gconv.Struct(flowInfo, res)
|
||||
return
|
||||
}
|
||||
|
||||
var flowUserInfo *entity.FlowUser
|
||||
flowUserInfo, err = flowDao.FlowUserDao.Get(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = new(flowDto.FlowUserVO)
|
||||
err = gconv.Struct(flowUserInfo, res)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *flowUserService) List(ctx context.Context, req *flowDto.ListFlowUserReq) (res *flowDto.ListFlowRes, err error) {
|
||||
|
||||
l, t, err := flowDao.FlowTemplateDao.List(ctx, &flowDto.ListFlowTemplateReq{
|
||||
Keyword: req.Keyword,
|
||||
Page: req.Page,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r := &flowDto.ListFlowTemplateRes{
|
||||
Total: t,
|
||||
}
|
||||
err = gconv.Struct(l, &r.List)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
list, total, err := flowDao.FlowUserDao.List(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
re := &flowDto.ListFlowUserRes{
|
||||
Total: total,
|
||||
}
|
||||
err = gconv.Struct(list, &re.List)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
admin, err := IsAdmin(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = &flowDto.ListFlowRes{
|
||||
ListFlowUserRes: re,
|
||||
ListFlowTemplateRes: r,
|
||||
IsAdmin: admin,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user