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 }