Next: 优化数据结构

This commit is contained in:
Max 2021-11-27 12:01:19 +08:00
parent 72b1108ec5
commit 5b5772e259
3 changed files with 51 additions and 22 deletions

View file

@ -53,15 +53,24 @@
"props": { "src": "/workflows/assign/approve.html" }
},
"actions": ["驳回并重填", "驳回并关闭", "同意"],
"user": { "process": "flows.select.manager", "args": [2] }
"user": { "process": "flows.select.manager", "args": [2] },
"next": [
{
"when": [{ "审批通过": "{{$data.审批结果}}", "=": "通过" }],
"goto": "审批通过"
},
{
"when": [{ "审批通过": "{{$data.审批结果}}", "=": "驳回" }],
"goto": "审批驳回"
}
]
},
{
"name": "审批通过",
"when": [{ "审批通过": "{{$out.审批结果}}", "=": "通过" }],
"body": {
"type": "markdown",
"props": {
"content": "审批通过, {{$out.项目名称}}商务负责人为: **{{$out.商务负责人名称}}**"
"content": "审批通过, {{$data.项目名称}}商务负责人为: **{{$data.商务负责人名称}}**"
}
},
"actions": ["关闭"],
@ -69,11 +78,10 @@
},
{
"name": "审批驳回",
"when": [{ "审批通过": "{{$out.审批结果}}", "=": "驳回" }],
"body": {
"type": "markdown",
"props": {
"content": "您的请求被驳回, 驳回原因: **{{$out.驳回原因}}**"
"content": "您的请求被驳回, 驳回原因: **{{$data.驳回原因}}**"
}
},
"actions": ["关闭"],

View file

@ -19,11 +19,17 @@ type WorkFlow struct {
// Node 工作流节点
type Node struct {
Name string `json:"name"`
Body share.Render `json:"body,omitempty"`
Name string `json:"name"`
Body share.Render `json:"body,omitempty"`
Actions []string `json:"actions,omitempty"`
User User `json:"user,omitempty"`
Next []Next `json:"next,omitempty"`
}
// Next 下一个节点描述
type Next struct {
Conditions []helper.Condition `json:"when,omitempty"`
Actions []string `json:"actions,omitempty"`
User User `json:"user,omitempty"`
Goto string `json:"goto,omitempty"`
}
// User 工作流相关用户读取条件

View file

@ -240,6 +240,7 @@ func (workflow *WorkFlow) Next(uid int, id int, output map[string]interface{}) m
"$input": wflow["input"],
"$out": output,
"$outupt": output,
"$data": output,
}
nextNode := workflow.nextNode(currNode, data)
nextUID := nextNode.GetUID()
@ -278,25 +279,34 @@ func (node *Node) GetUID() int {
// nextNode 查找下一个节点
func (workflow *WorkFlow) nextNode(currentNode string, data map[string]interface{}) *Node {
next := -1
var curr *Node
nextIndex := -1
for i, node := range workflow.Nodes {
if node.Name == currentNode {
next = i + 1
nextIndex = i + 1
curr = &node
break
}
}
if next < 0 {
if nextIndex < 0 {
exception.New("流程数据异常: 未找到工作流节点", 500).Ctx(currentNode).Throw()
}
if nextIndex == workflow.Len() {
exception.New("流程数据异常: 当前节点为最后一个节点", 500).Ctx(currentNode).Throw()
}
// 未声明 Next 节点, 转到下一个节点
if curr.Next == nil {
return &workflow.Nodes[nextIndex]
}
// 声明 Next 节点, 按条件到指定节点
data = maps.Of(data).Dot()
for i := next; i < workflow.Len(); i++ {
node := workflow.Nodes[i]
if node.Conditions == nil || len(node.Conditions) == 0 {
return &node
}
// 替换数据中的变量
nextNode := ""
for _, next := range curr.Next {
conditions := []helper.Condition{}
for _, cond := range node.Conditions {
for _, cond := range next.Conditions {
if left, ok := cond.Left.(string); ok {
cond.Left = gshare.Bind(left, data)
}
@ -306,11 +316,16 @@ func (workflow *WorkFlow) nextNode(currentNode string, data map[string]interface
conditions = append(conditions, cond)
}
if helper.When(conditions) {
return &node
nextNode = next.Goto
for i := nextIndex; i < workflow.Len(); i++ {
node := workflow.Nodes[i]
if node.Name == nextNode {
return &node
}
}
}
}
exception.New("流程数据异常: 未找到符合条件的工作流节点", 500).Ctx(currentNode).Throw()
exception.New("流程数据异常: 未找到符合条件的工作流节点", 500).Ctx(map[string]interface{}{"current": currentNode, "next": nextNode, "data": data}).Throw()
return nil
}