Prechádzať zdrojové kódy

Merge branch 'v2' of http://192.168.0.224:3000/yongxu/sparrow into v2

lijian 2 rokov pred
rodič
commit
5f622f641b
2 zmenil súbory, kde vykonal 82 pridanie a 70 odobranie
  1. 25 19
      pkg/models/rulechain.go
  2. 57 51
      services/knowoapi/services/rule_chain.go

+ 25 - 19
pkg/models/rulechain.go

@@ -29,31 +29,33 @@ type CreatChainReq struct {
 type Cells []*Cell
 type Cells []*Cell
 
 
 type Cell struct {
 type Cell struct {
-	Id   string   `json:"id"`   // id
-	Data CellData `json:"data"` // 数据
+	Id     string   `json:"id"` // id
+	Shape  string   `json:"shape"`
+	Data   CellData `json:"data"`   // 数据
+	Source Branch   `json:"source"` // 上级节点
+	Target Branch   `json:"target"` // 下级节点
 }
 }
 
 
 type CellData struct {
 type CellData struct {
-	Name     string     `json:"name"`      // 名称
-	Id       string     `json:"id"`        // id
-	Type     string     `json:"type"`      // 节点类型
-	Desc     string     `json:"desc"`      // 备注
-	ZIndex   string     `json:"ZIndex"`    // 序号
-	Shape    string     `json:"shape"`     // 类型
-	FuncBody string     `json:"func_body"` // 代码
-	Labels   []string   `json:"labels"`
-	Source   string     `json:"source"` // 源节点id
-	Target   string     `json:"target"` // 目标节点id
-	Headers  []*Headers `json:"headers"`
+	Name     string `json:"name"`      // 名称
+	Id       string `json:"id"`        // id
+	Type     string `json:"type"`      // 节点类型
+	Desc     string `json:"desc"`      // 备注
+	ZIndex   string `json:"ZIndex"`    // 序号
+	FuncBody string `json:"func_body"` // 代码
+	Label    string `json:"label"`
+	Source   string `json:"source"` // 源节点id
+	Target   string `json:"target"` // 目标节点id
+	NodeConfiguration
 }
 }
 
 
 type NodeConfiguration struct {
 type NodeConfiguration struct {
-	Url       string            `json:"url"`
-	Method    string            `json:"method"`
-	Headers   map[string]string `json:"headers"`
-	Retry     int               `json:"retry"`
-	TimeOut   int               `json:"time_out"`
-	RetryWait int               `json:"retry_wait"`
+	Url       string     `json:"url"`
+	Method    string     `json:"method"`
+	Headers   []*Headers `json:"headers"`
+	Retry     int        `json:"retry"`
+	TimeOut   int        `json:"time_out"`
+	RetryWait int        `json:"retry_wait"`
 }
 }
 
 
 type Headers struct {
 type Headers struct {
@@ -61,6 +63,10 @@ type Headers struct {
 	Value string `json:"value"`
 	Value string `json:"value"`
 }
 }
 
 
+type Branch struct {
+	Cell string `json:"cell"`
+}
+
 // Validate ``
 // Validate ``
 func (a *RuleChain) Validate() error {
 func (a *RuleChain) Validate() error {
 	if a.Name == "" {
 	if a.Name == "" {

+ 57 - 51
services/knowoapi/services/rule_chain.go

@@ -39,68 +39,74 @@ func (a ruleChainService) Get(vendorId, recordId string) (models.RuleChain, erro
 
 
 func (a ruleChainService) Create(ruleChain *models.RuleChain) error {
 func (a ruleChainService) Create(ruleChain *models.RuleChain) error {
 	ruleChain.RecordId = guid.S()
 	ruleChain.RecordId = guid.S()
-	err := a.create(ruleChain)
-	if err != nil {
-		return err
+	if len(ruleChain.Cell) > 0 {
+		marshal, _ := json.Marshal(ruleChain.Cell)
+		ruleChain.Configuration = fmt.Sprintf("%s", marshal)
+		err := a.create(ruleChain)
+		if err != nil {
+			return err
+		}
 	}
 	}
 	return a.model.RuleChain.Create(ruleChain)
 	return a.model.RuleChain.Create(ruleChain)
 }
 }
 
 
 func (a ruleChainService) create(ruleChain *models.RuleChain) error {
 func (a ruleChainService) create(ruleChain *models.RuleChain) error {
-	fmt.Printf("++++++++++++++++%d+++++++++++++++", len(ruleChain.Cell))
-	if len(ruleChain.Cell) > 0 {
-		nodeMap := make(map[string]models.RuleNode)
-		for _, v := range ruleChain.Cell {
-			if v.Data.Shape != "edge" {
-				ruleNode := models.RuleNode{
-					Model:       gorm.Model{},
-					RecordId:    guid.S(),
-					RuleChainID: ruleChain.RecordId,
-					Type:        v.Data.Type,
-					Name:        v.Data.Name,
-					DebugModel:  true,
-					Intro:       v.Data.Desc,
-				}
-				if v.Data.Shape == "input-node" {
-					ruleChain.FirstRuleNodeID = ruleNode.RecordId
-					ruleNode.Name = "数据输入"
-				}
-
-				var configuration models.NodeConfiguration
-				if v.Data.Shape == "action_node" {
-					for _, header := range v.Data.Headers {
-						configuration.Headers[header.Key] = header.Value
-					}
-				}
-
-				marshal, _ := json.Marshal(configuration)
-				ruleNode.Configuration = fmt.Sprintf("%s", marshal)
-
-				nodeMap[v.Id] = ruleNode
-
-				err := a.model.RuleNode.Create(&ruleNode)
-				if err != nil {
-					return err
-				}
+
+	nodeMap := make(map[string]models.RuleNode)
+	for _, v := range ruleChain.Cell {
+		if v.Shape != "edge" {
+			ruleNode := models.RuleNode{
+				Model:       gorm.Model{},
+				RecordId:    guid.S(),
+				RuleChainID: ruleChain.RecordId,
+				Type:        v.Data.Type,
+				Name:        v.Data.Name,
+				DebugModel:  true,
+				Intro:       v.Data.Desc,
+			}
+			if v.Shape == "input-node" {
+				ruleChain.FirstRuleNodeID = ruleNode.RecordId
+				ruleNode.Name = "数据输入"
+			}
+
+			configuration := models.NodeConfiguration{
+				Url:       v.Data.Url,
+				Method:    v.Data.Method,
+				Headers:   v.Data.Headers,
+				Retry:     v.Data.Retry,
+				TimeOut:   v.Data.TimeOut,
+				RetryWait: v.Data.RetryWait,
+			}
+
+			marshal, _ := json.Marshal(configuration)
+			ruleNode.Configuration = fmt.Sprintf("%s", marshal)
+
+			nodeMap[v.Id] = ruleNode
+
+			err := a.model.RuleNode.Create(&ruleNode)
+			if err != nil {
+				return err
 			}
 			}
 		}
 		}
+	}
 
 
-		for _, v := range ruleChain.Cell {
-			if v.Data.Shape == "edge" {
-				err := a.model.Relation.Create(&models.Relation{
-					RecordId:    guid.S(),
-					RuleChainId: ruleChain.RecordId,
-					FromID:      nodeMap[v.Data.Source].RecordId,
-					FromType:    nodeMap[v.Data.Source].Type,
-					ToID:        nodeMap[v.Data.Target].RecordId,
-					ToType:      nodeMap[v.Data.Target].Type,
-				})
-				if err != nil {
-					return err
-				}
+	for _, v := range ruleChain.Cell {
+		if v.Shape == "edge" {
+			err := a.model.Relation.Create(&models.Relation{
+				RecordId:     guid.S(),
+				RuleChainId:  ruleChain.RecordId,
+				FromID:       nodeMap[v.Source.Cell].RecordId,
+				FromType:     nodeMap[v.Source.Cell].Type,
+				ToID:         nodeMap[v.Target.Cell].RecordId,
+				ToType:       nodeMap[v.Target.Cell].Type,
+				RelationType: v.Data.Label,
+			})
+			if err != nil {
+				return err
 			}
 			}
 		}
 		}
 	}
 	}
+
 	return nil
 	return nil
 }
 }