96 lines
3.4 KiB
Go
96 lines
3.4 KiB
Go
package node
|
|
|
|
import (
|
|
"ai-agent/workflow/consts/public"
|
|
nodeDto "ai-agent/workflow/model/dto/node"
|
|
"ai-agent/workflow/model/entity"
|
|
"context"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
var NodePromptDao = &nodePromptDao{}
|
|
|
|
type nodePromptDao struct{}
|
|
|
|
// Insert 插入节点提示词
|
|
func (d *nodePromptDao) Insert(ctx context.Context, req *nodeDto.CreateNodePromptReq) (id int64, err error) {
|
|
nodePrompt := new(entity.NodePrompt)
|
|
err = gconv.Struct(req, &nodePrompt)
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameNodePrompt).Insert(&nodePrompt)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新节点提示词
|
|
func (d *nodePromptDao) Update(ctx context.Context, req *nodeDto.UpdateNodePromptReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameNodePrompt).OmitEmpty().Data(&req).Where(entity.NodePromptCol.Id, req.Id).Update()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除节点提示词
|
|
func (d *nodePromptDao) Delete(ctx context.Context, req *nodeDto.DeleteNodePromptReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameNodePrompt).Where(entity.NodePromptCol.Id, req.Id).Delete()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Get 根据ID查询节点提示词
|
|
func (d *nodePromptDao) Get(ctx context.Context, req *nodeDto.GetNodePromptReq, fields ...string) (res *entity.NodePrompt, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameNodePrompt).NoTenantId(ctx).OmitEmpty().
|
|
Where(entity.NodePromptCol.Id, req.Id).
|
|
Where(entity.NodePromptCol.Prompt, req.Prompt).
|
|
Where(entity.NodePromptCol.Creator, req.Creator).
|
|
Fields(fields).One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.IsEmpty() {
|
|
return nil, nil
|
|
}
|
|
err = r.Struct(&res)
|
|
return res, err
|
|
}
|
|
|
|
// ListByOnlyCreator 查询仅当前创建人自己创建的提示词
|
|
func (d *nodePromptDao) ListByOnlyCreator(ctx context.Context, req *nodeDto.ListMyNodePromptReq, fields ...string) (res []*entity.NodePrompt, total int, err error) {
|
|
model := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameNodePrompt).NoTenantId(ctx).Fields(fields).OmitEmpty()
|
|
model.Where(entity.NodePromptCol.Creator, req.Creator)
|
|
model.Where(entity.NodePromptCol.NodeType, req.NodeType)
|
|
model.OrderDesc(entity.NodePromptCol.CreatedAt)
|
|
if req.Page != nil {
|
|
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
|
}
|
|
r, total, err := model.AllAndCount(false)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
err = r.Structs(&res)
|
|
return res, total, err
|
|
}
|
|
|
|
// ListByCreator 查询当前创建人的所有提示词(包含系统和用户)
|
|
func (d *nodePromptDao) ListByCreator(ctx context.Context, req *nodeDto.ListNodePromptReq, fields ...string) (res []*entity.NodePrompt, total int, err error) {
|
|
// 完整 SQL
|
|
sql := ` SELECT * FROM black_deacon_node_prompt WHERE (creator=? OR source_type=1) AND node_type=? AND "deleted_at" IS NULL ORDER BY created_at DESC `
|
|
queryParams := []interface{}{req.Creator, req.NodeType}
|
|
if req.Page != nil {
|
|
sql += " LIMIT ?,?"
|
|
queryParams = append(queryParams, req.Page.PageNum, req.Page.PageSize)
|
|
}
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).GetAll(ctx, sql, queryParams...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
err = r.Structs(&res)
|
|
return res, total, err
|
|
}
|