init
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
type Demand struct {
|
||||
Tree
|
||||
DocID string `json:"doc_id" gorm:"column:doc_id;type:varchar(255);not null"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
ReqID string `json:"req_id" gorm:"column:req_id;type:varchar(50)"`
|
||||
ParentReqID string `json:"parent_req_id" gorm:"column:parent_req_id;type:varchar(50)"`
|
||||
Priority string `json:"priority" gorm:"column:priority;type:varchar(20)"`
|
||||
Type string `json:"type" gorm:"column:type;type:varchar(50)"`
|
||||
Status string `json:"status" gorm:"column:status;type:varchar(20)"`
|
||||
}
|
||||
|
||||
func (Demand) TableName() string {
|
||||
return "demands"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "app/cfg"
|
||||
|
||||
type Doc struct {
|
||||
BaseModel
|
||||
ProjectID string `json:"project_id" gorm:"column:project_id;type:varchar(255);not null"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255);not null"`
|
||||
Type string `json:"type" gorm:"column:type;type:varchar(50);not null"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
FilePath string `json:"file_path" gorm:"column:file_path;type:varchar(255)"`
|
||||
FileType string `json:"file_type" gorm:"column:file_type;type:varchar(50)"`
|
||||
FileSize int64 `json:"file_size" gorm:"column:file_size;type:bigint"`
|
||||
FileName string `json:"file_name" gorm:"column:file_name;type:varchar(255)"`
|
||||
AnalysisCompleted bool `json:"analysis_completed" gorm:"column:analysis_completed;type:boolean;default:false"`
|
||||
AnalysisPercent int `json:"analysis_percent" gorm:"column:analysis_percent;type:int;default:0"`
|
||||
AnalysisError string `json:"analysis_error" gorm:"column:analysis_error;type:text"`
|
||||
Merged bool `json:"merged" gorm:"column:merged;type:boolean;default:false"`
|
||||
}
|
||||
|
||||
func GetProjectIDByDocID(docID string) (string, error) {
|
||||
var doc Doc
|
||||
if err := cfg.DB().Where("id = ?", docID).First(&doc).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return doc.ProjectID, nil
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
type Endpoint struct {
|
||||
BaseModel
|
||||
DocID string `json:"doc_id" gorm:"column:doc_id;type:varchar(255);not null"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255);not null"`
|
||||
Path string `json:"path" gorm:"column:path;type:varchar(255);not null"`
|
||||
Method string `json:"method" gorm:"column:method;type:varchar(20);not null"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
BodyType string `json:"body_type" gorm:"column:body_type;type:varchar(50)"`
|
||||
Node string `json:"node" gorm:"column:node;type:text"`
|
||||
Merged bool `json:"merged" gorm:"column:merged;type:boolean;default:false"`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GraphLink struct {
|
||||
BaseModel
|
||||
StageID string `json:"stage_id" gorm:"column:stage_id;type:varchar(255);not null"`
|
||||
SourceID string `json:"source_id" gorm:"column:source_id;type:varchar(255);not null"`
|
||||
TargetID string `json:"target_id" gorm:"column:target_id;type:varchar(255);not null"`
|
||||
LinkID string `json:"link_id" gorm:"column:link_id;type:varchar(255);not null"`
|
||||
}
|
||||
|
||||
// 获取前置节点
|
||||
func (gl *GraphLink) SourceNode(db *gorm.DB) (*GraphNode, error) {
|
||||
var node GraphNode
|
||||
err := db.Where("id = ?", gl.SourceID).First(&node).Error
|
||||
return &node, err
|
||||
}
|
||||
|
||||
// 获取后置节点
|
||||
func (gl *GraphLink) TargetNode(db *gorm.DB) (*GraphNode, error) {
|
||||
var node GraphNode
|
||||
err := db.Where("id = ?", gl.TargetID).First(&node).Error
|
||||
return &node, err
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GraphNode struct {
|
||||
BaseModel
|
||||
TreeID string `json:"tree_id" gorm:"column:tree_id;type:varchar(255);not null"`
|
||||
StageID string `json:"stage_id" gorm:"column:stage_id;type:varchar(255);not null"`
|
||||
InstanceID string `json:"instance_id" gorm:"column:instance_id;type:varchar(255);not null"`
|
||||
X float64 `json:"x" gorm:"column:x;default:0"`
|
||||
Y float64 `json:"y" gorm:"column:y;default:0"`
|
||||
FX float64 `json:"fx" gorm:"column:fx;default:0"`
|
||||
FY float64 `json:"fy" gorm:"column:fy;default:0"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255)"`
|
||||
Level int `json:"level" gorm:"column:level;default:0"`
|
||||
ParentID string `json:"parent_id" gorm:"column:parent_id;type:varchar(255)"`
|
||||
}
|
||||
|
||||
// 获取关联的树节点
|
||||
func (gn *GraphNode) Tree(db *gorm.DB) (*Tree, error) {
|
||||
var tree Tree
|
||||
err := db.Where("id = ?", gn.TreeID).First(&tree).Error
|
||||
return &tree, err
|
||||
}
|
||||
|
||||
// 获取关联的所有连接
|
||||
func (gn *GraphNode) Links(db *gorm.DB) ([]GraphLink, error) {
|
||||
var links []GraphLink
|
||||
err := db.Where("source_id = ? OR target_id = ?", gn.ID, gn.ID).Find(&links).Error
|
||||
return links, err
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Copyright (C) 2024 veypi <i@veypi.com>
|
||||
// 2025-02-27 19:32:09
|
||||
// Distributed under terms of the MIT license.
|
||||
//
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BaseModel struct {
|
||||
// ID uint `json:"id" gorm:"primaryKey" methods:"get,patch,delete" parse:"path"`
|
||||
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" methods:"get,put,patch,delete" parse:"path"`
|
||||
CreatedAt time.Time `json:"created_at" methods:"*list" parse:"query"`
|
||||
UpdatedAt time.Time `json:"updated_at" methods:"*list" parse:"query"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.ID == "" {
|
||||
m.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package models
|
||||
// json parameters
|
||||
@@ -0,0 +1,2 @@
|
||||
package models
|
||||
// args default value
|
||||
@@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
type Parameter struct {
|
||||
BaseModel
|
||||
EndpointID string `json:"endpoint_id" gorm:"column:endpoint_id;type:varchar(255);not null"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255);not null"`
|
||||
Type string `json:"type" gorm:"column:type;type:varchar(50);not null"` // query, header, body
|
||||
ParamType string `json:"param_type" gorm:"column:param_type;type:varchar(50)"` // string, number, boolean
|
||||
Required bool `json:"required" gorm:"column:required;type:boolean;default:false"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
Example string `json:"example" gorm:"column:example;type:text"`
|
||||
Value string `json:"value" gorm:"column:value;type:text"` // 调试值
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
type Project struct {
|
||||
BaseModel
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255);not null;unique"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"app/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Demand struct {
|
||||
models.Tree
|
||||
DocID string `json:"doc_id" gorm:"column:doc_id;type:varchar(255);default:''"`
|
||||
ProjectID string `json:"project_id" gorm:"column:project_id;type:varchar(255);not null"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
ReqID string `json:"req_id" gorm:"column:req_id;type:varchar(50)"`
|
||||
ParentReqID string `json:"parent_req_id" gorm:"column:parent_req_id;type:varchar(50)"`
|
||||
Priority string `json:"priority" gorm:"column:priority;type:varchar(20)"`
|
||||
Type string `json:"type" gorm:"column:type;type:varchar(50)"`
|
||||
Status string `json:"status" gorm:"column:status;type:varchar(20)"`
|
||||
}
|
||||
|
||||
func (Demand) TableName() string {
|
||||
return "project_demands"
|
||||
}
|
||||
|
||||
func (t *Demand) Descendants(db *gorm.DB) ([]Demand, error) {
|
||||
var descendants []Demand
|
||||
|
||||
// 使用GORM递归获取所有后代节点
|
||||
var getAllDescendants func(parentID string) error
|
||||
getAllDescendants = func(parentID string) error {
|
||||
var children []Demand
|
||||
if err := db.Where("parent_id = ?", parentID).Find(&children).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, child := range children {
|
||||
descendants = append(descendants, child)
|
||||
if err := getAllDescendants(child.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 从当前节点开始递归
|
||||
if err := getAllDescendants(t.ID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return descendants, nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
M "app/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Stage struct {
|
||||
M.BaseModel
|
||||
ProjectID string `json:"project_id" gorm:"type:varchar(32);not null"`
|
||||
Name string `json:"name" gorm:"type:varchar(255);not null"`
|
||||
DemandID string `json:"demand_id" gorm:"column:tree_id;type:varchar(32)"`
|
||||
GraphStatus string `json:"graph_status" gorm:"type:varchar(50);default:'';index"` // 状态: pending, processing, completed, failed
|
||||
GraphError string `json:"graph_error" gorm:"type:text"`
|
||||
}
|
||||
|
||||
func (Stage) TableName() string {
|
||||
return "project_stages"
|
||||
}
|
||||
func (s *Stage) AvailableNodes(db *gorm.DB) ([]Demand, error) {
|
||||
// 如果没有指定树节点,返回空列表
|
||||
if s.DemandID == "" {
|
||||
return []Demand{}, nil
|
||||
}
|
||||
|
||||
// 如果是虚拟根节点,返回所有树节点
|
||||
if s.DemandID == "root" {
|
||||
var allNodes []Demand
|
||||
if err := db.Find(&allNodes).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return allNodes, nil
|
||||
}
|
||||
|
||||
// 获取指定的树节点
|
||||
var tree Demand
|
||||
if err := db.Where("id = ?", s.DemandID).First(&tree).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取该树节点及其所有子孙节点
|
||||
descendants, err := tree.Descendants(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 将当前节点也添加到可用节点列表中
|
||||
result := append([]Demand{tree}, descendants...)
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
M "app/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GraphLink struct {
|
||||
M.BaseModel
|
||||
StageID string `json:"stage_id" gorm:"column:stage_id;type:varchar(255);not null"`
|
||||
SourceID string `json:"source_id" gorm:"column:source_id;type:varchar(255);not null"`
|
||||
TargetID string `json:"target_id" gorm:"column:target_id;type:varchar(255);not null"`
|
||||
LinkID string `json:"link_id" gorm:"column:link_id;type:varchar(255);not null"`
|
||||
Meta string `json:"meta" gorm:"column:meta;type:mediumtext"`
|
||||
}
|
||||
|
||||
func (GraphLink) TableName() string {
|
||||
return "stage_graph_links"
|
||||
}
|
||||
|
||||
// 获取前置节点
|
||||
func (gl *GraphLink) SourceNode(db *gorm.DB) (*GraphNode, error) {
|
||||
var node GraphNode
|
||||
err := db.Where("id = ?", gl.SourceID).First(&node).Error
|
||||
return &node, err
|
||||
}
|
||||
|
||||
// 获取后置节点
|
||||
func (gl *GraphLink) TargetNode(db *gorm.DB) (*GraphNode, error) {
|
||||
var node GraphNode
|
||||
err := db.Where("id = ?", gl.TargetID).First(&node).Error
|
||||
return &node, err
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
M "app/models"
|
||||
P "app/models/project"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GraphNode struct {
|
||||
M.BaseModel
|
||||
DemandID string `json:"tree_id" gorm:"column:tree_id;type:varchar(255);not null"`
|
||||
StageID string `json:"stage_id" gorm:"column:stage_id;type:varchar(255);not null"`
|
||||
InstanceID string `json:"instance_id" gorm:"column:instance_id;type:varchar(255);not null"`
|
||||
X float64 `json:"x" gorm:"column:x;default:0"`
|
||||
Y float64 `json:"y" gorm:"column:y;default:0"`
|
||||
FX float64 `json:"fx" gorm:"column:fx;default:0"`
|
||||
FY float64 `json:"fy" gorm:"column:fy;default:0"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255)"`
|
||||
Level int `json:"level" gorm:"column:level;default:0"`
|
||||
ParentID string `json:"parent_id" gorm:"column:parent_id;type:varchar(255)"`
|
||||
Type string `json:"type" gorm:"column:type;type:varchar(255);default:'node'"`
|
||||
Meta string `json:"meta" gorm:"column:meta;type:mediumtext"`
|
||||
}
|
||||
|
||||
func (GraphNode) TableName() string {
|
||||
return "stage_graph_nodes"
|
||||
}
|
||||
|
||||
// 获取关联的树节点
|
||||
func (gn *GraphNode) Tree(db *gorm.DB) (*P.Demand, error) {
|
||||
var tree P.Demand
|
||||
err := db.Where("id = ?", gn.DemandID).First(&tree).Error
|
||||
return &tree, err
|
||||
}
|
||||
|
||||
// 获取关联的所有连接
|
||||
func (gn *GraphNode) Links(db *gorm.DB) ([]GraphLink, error) {
|
||||
var links []GraphLink
|
||||
err := db.Where("source_id = ? OR target_id = ?", gn.ID, gn.ID).Find(&links).Error
|
||||
return links, err
|
||||
}
|
||||
func (gn *GraphNode) IncomingLinks(db *gorm.DB) ([]GraphLink, error) {
|
||||
var links []GraphLink
|
||||
err := db.Where("target_id = ?", gn.ID).Find(&links).Error
|
||||
return links, err
|
||||
}
|
||||
|
||||
// OutgoingLinks 获取出度连接
|
||||
func (gn *GraphNode) OutgoingLinks(db *gorm.DB) ([]GraphLink, error) {
|
||||
var links []GraphLink
|
||||
err := db.Where("source_id = ?", gn.ID).Find(&links).Error
|
||||
return links, err
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
M "app/models"
|
||||
)
|
||||
|
||||
type Endpoint struct {
|
||||
M.BaseModel
|
||||
StageID string `json:"stage_id" gorm:"column:stage_id;type:varchar(255);"`
|
||||
ProjectID string `json:"project_id" gorm:"column:project_id;type:varchar(255);"`
|
||||
DocID string `json:"doc_id" gorm:"column:doc_id;type:varchar(255);"`
|
||||
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255);"`
|
||||
Path string `json:"path" gorm:"column:path;type:varchar(255);"`
|
||||
Method string `json:"method" gorm:"column:method;type:varchar(255);"`
|
||||
Description string `json:"description" gorm:"column:description;type:varchar(255);"`
|
||||
|
||||
Params string `json:"params" gorm:"column:params;type:mediumtext;"`
|
||||
Headers string `json:"headers" gorm:"column:headers;type:mediumtext;"`
|
||||
Body string `json:"body" gorm:"column:body;type:mediumtext;"`
|
||||
Response string `json:"response" gorm:"column:response;type:mediumtext;"`
|
||||
Merged bool `json:"merged" gorm:"column:merged;type:bool;"`
|
||||
}
|
||||
|
||||
func (Endpoint) TableName() string {
|
||||
return "stage_tool_endpoints"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
type Response struct {
|
||||
BaseModel
|
||||
EndpointID string `json:"endpoint_id" gorm:"column:endpoint_id;type:varchar(255);not null"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(255);not null"`
|
||||
StatusCode int `json:"status_code" gorm:"column:status_code;type:int;not null"`
|
||||
ContentType string `json:"content_type" gorm:"column:content_type;type:varchar(100)"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
Example string `json:"example" gorm:"column:example;type:text"`
|
||||
Content string `json:"content" gorm:"column:content;type:text"` // 实际响应内容
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Stage struct {
|
||||
BaseModel
|
||||
ProjectID string `json:"project_id" gorm:"type:varchar(32);not null"`
|
||||
Name string `json:"name" gorm:"type:varchar(255);not null"`
|
||||
TreeID string `json:"tree_id" gorm:"column:tree_id;type:varchar(32)"`
|
||||
DemandID string `json:"demand_id" gorm:"column:tree_id;type:varchar(32)"`
|
||||
}
|
||||
|
||||
func (s *Stage) AvailableNodes(db *gorm.DB) ([]Tree, error) {
|
||||
// 如果没有指定树节点,返回空列表
|
||||
if s.TreeID == "" {
|
||||
return []Tree{}, nil
|
||||
}
|
||||
|
||||
// 如果是虚拟根节点,返回所有树节点
|
||||
if s.TreeID == "root" {
|
||||
var allNodes []Tree
|
||||
if err := db.Find(&allNodes).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return allNodes, nil
|
||||
}
|
||||
|
||||
// 获取指定的树节点
|
||||
var tree Tree
|
||||
if err := db.Where("id = ?", s.TreeID).First(&tree).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取该树节点及其所有子孙节点
|
||||
descendants, err := tree.Descendants(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 将当前节点也添加到可用节点列表中
|
||||
result := append([]Tree{tree}, descendants...)
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Tree struct {
|
||||
BaseModel
|
||||
// ID int64 `json:"id" gorm:"column:id;primary_key;type:bigint(20) auto_increment;"`
|
||||
Name string `json:"name" gorm:"type:varchar(255);not null"`
|
||||
ParentID string `json:"parent_id" gorm:"column:parent_id; defualt:'-1' "`
|
||||
Level int `json:"level" gorm:"column:level; default:0 "`
|
||||
}
|
||||
|
||||
// 获取子节点
|
||||
func (t *Tree) Children(db *gorm.DB) ([]Tree, error) {
|
||||
var children []Tree
|
||||
err := db.Where("parent_id = ?", t.ID).Find(&children).Error
|
||||
return children, err
|
||||
}
|
||||
|
||||
// 获取所有后代节点
|
||||
//
|
||||
// func (t *Tree) Descendants(db *gorm.DB) ([]Tree, error) {
|
||||
// var descendants []Tree
|
||||
// err := db.Where("level > ? AND (parent_id = ? OR parent_id IN (SELECT id FROM trees WHERE level > ?))",
|
||||
// t.Level, t.ID, t.Level).Find(&descendants).Error
|
||||
// return descendants, err
|
||||
// }
|
||||
func (t *Tree) Descendants(db *gorm.DB) ([]Tree, error) {
|
||||
var descendants []Tree
|
||||
|
||||
// 使用GORM递归获取所有后代节点
|
||||
var getAllDescendants func(parentID string) error
|
||||
getAllDescendants = func(parentID string) error {
|
||||
var children []Tree
|
||||
if err := db.Where("parent_id = ?", parentID).Find(&children).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, child := range children {
|
||||
descendants = append(descendants, child)
|
||||
if err := getAllDescendants(child.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 从当前节点开始递归
|
||||
if err := getAllDescendants(t.ID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return descendants, nil
|
||||
}
|
||||
|
||||
func (t *Tree) ChildrenWithMinLevel(db *gorm.DB, minLevel int) ([]Tree, error) {
|
||||
var children []Tree
|
||||
if err := db.Where("parent_id = ? AND level >= ?", t.ID, minLevel).Find(&children).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return children, nil
|
||||
}
|
||||
Reference in New Issue
Block a user