parse.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package approve_former
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. )
  7. const (
  8. NodeTypeRoot = "root"
  9. NodeTypeApproval = "APPROVAL" //审批节点类型
  10. )
  11. const (
  12. ApprovalModeAnd = "NEXT"
  13. ApprovalModeOr = "OR"
  14. )
  15. const (
  16. ApprovalAssignTypeUser = "ASSIGN_USER"
  17. ApprovalAssignTypeRole = "ASSIGN_ROLE"
  18. )
  19. var (
  20. ErrNodeEmpty = errors.New("请最少设置一个流程节点")
  21. )
  22. // ParseFlowFromJson 解析当前云极和ERP使用的前端json格式,输出argo支持的模板字符串
  23. func ParseFlowFromJson(identifier, matter, createUserID, frontJson, argObj string) (string, error) {
  24. var root Node
  25. err := json.Unmarshal([]byte(frontJson), &root)
  26. if err != nil {
  27. return "", err
  28. }
  29. if root.Children == nil {
  30. return "", ErrNodeEmpty
  31. }
  32. // 递归遍历流程节点,生成argo模板
  33. flow := NewFlow(&FlowBaseInfo{
  34. Identifier: identifier,
  35. Matter: matter,
  36. CreateUserID: createUserID,
  37. BusinessObject: argObj,
  38. })
  39. root.Children.NodeLevel = 1
  40. root.Children.NamePrefix = "1-"
  41. err = traverseTree(flow, root.Children)
  42. if err != nil {
  43. return "", err
  44. }
  45. tmpStr, err := flow.Render(nil)
  46. if err != nil {
  47. return "", err
  48. }
  49. // 渲染模板
  50. return tmpStr, nil
  51. }
  52. func traverseTree(flow *Flow, node *Node) error {
  53. if node == nil {
  54. return nil
  55. }
  56. fmt.Printf("NodeID:%s,NodeName:%s,NodeType:%s\n", node.ID, node.Name, node.Type)
  57. // 生成模板逻辑
  58. switch node.Type {
  59. case NodeTypeApproval:
  60. err := buildApprovalFlow(flow, node)
  61. if err != nil {
  62. return err
  63. }
  64. default:
  65. return errors.New("不支持的节点类型")
  66. }
  67. // 递归遍历并行分支节点
  68. for index, branch := range node.Branchs {
  69. branch.Children.NodeLevel = node.NodeLevel + 1
  70. branch.Children.NamePrefix = fmt.Sprintf("%d-%d-", branch.Children.NodeLevel, index)
  71. err := traverseTree(flow, branch.Children)
  72. if err != nil {
  73. return err
  74. }
  75. }
  76. // 递归遍历子节点
  77. if node.Children != nil {
  78. node.Children.NodeLevel = node.NodeLevel + 1
  79. node.Children.NamePrefix = fmt.Sprintf("%d-", node.Children.NodeLevel)
  80. err := traverseTree(flow, node.Children)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. return nil
  86. }
  87. func buildApprovalFlow(flow *Flow, node *Node) error {
  88. if node == nil {
  89. return nil
  90. }
  91. var (
  92. singleFlag bool
  93. )
  94. // 判断审批指定人员或者角色是否为1个,如果只有一个,无论审批模式是会签还是或签,逻辑都一样。
  95. switch node.Props.AssignedType {
  96. case ApprovalAssignTypeUser:
  97. if len(node.Props.AssignedUser) == 1 {
  98. singleFlag = true
  99. }
  100. case ApprovalAssignTypeRole:
  101. if len(node.Props.AssignedRole) == 1 {
  102. singleFlag = true
  103. }
  104. default:
  105. return errors.New("暂不支持的审批签名类型")
  106. }
  107. if singleFlag {
  108. flow.AddNodes(NewSequenceSign(fmt.Sprintf("%s%s", node.NamePrefix, node.ID)))
  109. return nil
  110. }
  111. switch node.Props.Mode {
  112. case ApprovalModeAnd:
  113. case ApprovalModeOr:
  114. default:
  115. return errors.New("暂不支持的审批模式")
  116. }
  117. return nil
  118. }