parse.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(flowName, flowType, 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. Name: flowName,
  35. Type: flowType,
  36. BusinessObject: argObj,
  37. })
  38. root.Children.NodeLevel = 1
  39. root.Children.NamePrefix = "1-"
  40. err = traverseTree(flow, root.Children)
  41. if err != nil {
  42. return "", err
  43. }
  44. tmpStr, err := flow.Render(nil)
  45. if err != nil {
  46. return "", err
  47. }
  48. // 渲染模板
  49. return tmpStr, nil
  50. }
  51. func traverseTree(flow *Flow, node *Node) error {
  52. if node == nil {
  53. return nil
  54. }
  55. fmt.Printf("NodeID:%s,NodeName:%s,NodeType:%s\n", node.ID, node.Name, node.Type)
  56. // 生成模板逻辑
  57. switch node.Type {
  58. case NodeTypeApproval:
  59. err := buildApprovalFlow(flow, node)
  60. if err != nil {
  61. return err
  62. }
  63. default:
  64. return errors.New("不支持的节点类型")
  65. }
  66. // 递归遍历并行分支节点
  67. for index, branch := range node.Branchs {
  68. branch.Children.NodeLevel = node.NodeLevel + 1
  69. branch.Children.NamePrefix = fmt.Sprintf("%d-%d-", branch.Children.NodeLevel, index)
  70. err := traverseTree(flow, branch.Children)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. // 递归遍历子节点
  76. if node.Children != nil {
  77. node.Children.NodeLevel = node.NodeLevel + 1
  78. node.Children.NamePrefix = fmt.Sprintf("%d-", node.Children.NodeLevel)
  79. err := traverseTree(flow, node.Children)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. func buildApprovalFlow(flow *Flow, node *Node) error {
  87. if node == nil {
  88. return nil
  89. }
  90. var (
  91. singleFlag bool
  92. )
  93. // 判断审批指定人员或者角色是否为1个,如果只有一个,无论审批模式是会签还是或签,逻辑都一样。
  94. switch node.Props.AssignedType {
  95. case ApprovalAssignTypeUser:
  96. if len(node.Props.AssignedUser) == 1 {
  97. singleFlag = true
  98. }
  99. case ApprovalAssignTypeRole:
  100. if len(node.Props.AssignedRole) == 1 {
  101. singleFlag = true
  102. }
  103. default:
  104. return errors.New("暂不支持的审批签名类型")
  105. }
  106. if singleFlag {
  107. flow.AddNodes(NewSequenceSign(fmt.Sprintf("%s%s", node.NamePrefix, node.ID)))
  108. return nil
  109. }
  110. switch node.Props.Mode {
  111. case ApprovalModeAnd:
  112. case ApprovalModeOr:
  113. default:
  114. return errors.New("暂不支持的审批模式")
  115. }
  116. return nil
  117. }