123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package approve_former
- import (
- "encoding/json"
- "fmt"
- "github.com/pkg/errors"
- )
- const (
- NodeTypeRoot = "root"
- NodeTypeApproval = "APPROVAL" //审批节点类型
- )
- const (
- ApprovalModeAnd = "NEXT"
- ApprovalModeOr = "OR"
- )
- const (
- ApprovalAssignTypeUser = "ASSIGN_USER"
- ApprovalAssignTypeRole = "ASSIGN_ROLE"
- )
- var (
- ErrNodeEmpty = errors.New("请最少设置一个流程节点")
- )
- // ParseFlowFromJson 解析当前云极和ERP使用的前端json格式,输出argo支持的模板字符串
- func ParseFlowFromJson(flowName, flowType, frontJson, argObj string) (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(&FlowBaseInfo{
- Name: flowName,
- Type: flowType,
- BusinessObject: argObj,
- })
- root.Children.NodeLevel = 1
- root.Children.NamePrefix = "1-"
- err = traverseTree(flow, root.Children, nil)
- if err != nil {
- return "", err
- }
- tmpStr, err := flow.Render(nil)
- if err != nil {
- return "", err
- }
- // 渲染模板
- return tmpStr, nil
- }
- func traverseTree(flow *Flow, node *Node, parentNode *Node) error {
- if node == nil {
- return nil
- }
- fmt.Printf("NodeID:%s,NodeName:%s,NodeType:%s\n", node.ID, node.Name, node.Type)
- // 生成模板逻辑
- switch node.Type {
- case NodeTypeApproval:
- err := buildApprovalFlow(flow, node, parentNode)
- if err != nil {
- return err
- }
- default:
- return errors.New("不支持的节点类型")
- }
- // 递归遍历并行分支节点
- for index, branch := range node.Branchs {
- branch.Children.NodeLevel = node.NodeLevel + 1
- branch.Children.NamePrefix = fmt.Sprintf("%d-%d-", branch.Children.NodeLevel, index)
- err := traverseTree(flow, branch.Children, node)
- if err != nil {
- return err
- }
- }
- // 递归遍历子节点
- if node.Children != nil {
- node.Children.NodeLevel = node.NodeLevel + 1
- node.Children.NamePrefix = fmt.Sprintf("%d-", node.Children.NodeLevel)
- err := traverseTree(flow, node.Children, node)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func buildApprovalFlow(flow *Flow, node *Node, parentNode *Node) error {
- if node == nil {
- return nil
- }
- var (
- singleFlag bool
- )
- // 判断审批指定人员或者角色是否为1个,如果只有一个,无论审批模式是会签还是或签,逻辑都一样。
- switch node.Props.AssignedType {
- case ApprovalAssignTypeUser:
- if len(node.Props.AssignedUser) == 1 {
- singleFlag = true
- }
- case ApprovalAssignTypeRole:
- if len(node.Props.AssignedRole) == 1 {
- singleFlag = true
- }
- default:
- return errors.New("暂不支持的审批签名类型")
- }
- if singleFlag {
- flow.AddNodes(NewSequenceSign(fmt.Sprintf("%s%s", node.NamePrefix, node.ID)))
- return nil
- }
- switch node.Props.Mode {
- case ApprovalModeAnd:
- case ApprovalModeOr:
- default:
- return errors.New("暂不支持的审批模式")
- }
- return nil
- }
|