parse.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package approve_former
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. )
  7. const (
  8. NodeTypeApproval = "APPROVAL" //审批节点类型
  9. )
  10. var (
  11. ErrNodeEmpty = errors.New("请最少设置一个流程节点")
  12. )
  13. // ParseFlowFromJson 解析当前云极和ERP使用的前端json格式,输出argo支持的模板字符串
  14. func ParseFlowFromJson(flowName, frontJson string, argObj interface{}) (string, error) {
  15. var root Node
  16. err := json.Unmarshal([]byte(frontJson), &root)
  17. if err != nil {
  18. return "", err
  19. }
  20. if root.Children == nil {
  21. return "", ErrNodeEmpty
  22. }
  23. // 递归遍历流程节点,生成argo模板
  24. flow := NewFlow(flowName)
  25. err = traverseTree(flow, root.Children)
  26. if err != nil {
  27. return "", err
  28. }
  29. tmpStr, err := flow.Render(nil)
  30. if err != nil {
  31. return "", err
  32. }
  33. // 渲染模板
  34. return tmpStr, nil
  35. }
  36. func traverseTree(flow *Flow, node *Node) error {
  37. if node == nil {
  38. return nil
  39. }
  40. fmt.Printf("NodeID:%s,NodeName:%s,NodeType:%s\n", node.ID, node.Name, node.NodeType)
  41. // 生成模板逻辑
  42. switch node.Type {
  43. case NodeTypeApproval:
  44. default:
  45. return errors.New("不支持的节点类型")
  46. }
  47. // 递归遍历并行分支节点
  48. for _, branch := range node.Branchs {
  49. err := traverseTree(flow, branch.Children)
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. // 递归遍历子节点
  55. if node.Children != nil {
  56. err := traverseTree(flow, node.Children)
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }