1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package approve_former
- import (
- "encoding/json"
- "fmt"
- "github.com/pkg/errors"
- )
- const (
- NodeTypeApproval = "APPROVAL" //审批节点类型
- )
- var (
- ErrNodeEmpty = errors.New("请最少设置一个流程节点")
- )
- // ParseFlowFromJson 解析当前云极和ERP使用的前端json格式,输出argo支持的模板字符串
- func ParseFlowFromJson(flowName, frontJson string, argObj interface{}) (string, error) {
- var root Node
- err := json.Unmarshal([]byte(frontJson), &root)
- if err != nil {
- return "", err
- }
- if root.Children == nil {
- return "", ErrNodeEmpty
- }
- // 递归遍历流程节点,生成argo模板
- flow := NewFlow(flowName)
- err = traverseTree(flow, root.Children)
- if err != nil {
- return "", err
- }
- tmpStr, err := flow.Render(nil)
- if err != nil {
- return "", err
- }
- // 渲染模板
- return tmpStr, nil
- }
- func traverseTree(flow *Flow, node *Node) error {
- if node == nil {
- return nil
- }
- fmt.Printf("NodeID:%s,NodeName:%s,NodeType:%s\n", node.ID, node.Name, node.NodeType)
- // 生成模板逻辑
- switch node.Type {
- case NodeTypeApproval:
- default:
- return errors.New("不支持的节点类型")
- }
- // 递归遍历并行分支节点
- for _, branch := range node.Branchs {
- err := traverseTree(flow, branch.Children)
- if err != nil {
- return err
- }
- }
- // 递归遍历子节点
- if node.Children != nil {
- err := traverseTree(flow, node.Children)
- if err != nil {
- return err
- }
- }
- return nil
- }
|