-
Notifications
You must be signed in to change notification settings - Fork 36
/
flow.go
136 lines (115 loc) · 3.64 KB
/
flow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package flow
import (
"context"
"encoding/json"
"net/http"
"github.com/antlinker/flow/expression/sql"
"github.com/antlinker/flow/schema"
"github.com/antlinker/flow/service/db"
)
var (
engine *Engine
)
// Init 初始化流程配置
func Init(opts ...db.Option) {
db, trace, err := db.NewMySQL(opts...)
if err != nil {
panic(err)
}
e, err := new(Engine).Init(NewXMLParser(), NewQLangExecer(), db, trace)
if err != nil {
panic(err)
}
engine = e
sql.Reg(db)
}
// SetParser 设定解析器
func SetParser(parser Parser) {
engine.SetParser(parser)
}
// SetExecer 设定表达式执行器
func SetExecer(execer Execer) {
engine.SetExecer(execer)
}
// LoadFile 加载流程文件数据
func LoadFile(name string) error {
return engine.LoadFile(name)
}
// StartFlow 启动流程
// flowCode 流程编号
// nodeCode 开始节点编号
// userID 发起人
// input 输入数据
func StartFlow(flowCode, nodeCode, userID string, input interface{}) (*HandleResult, error) {
return StartFlowWithContext(context.Background(), flowCode, nodeCode, userID, input)
}
// StartFlowWithContext 启动流程
// flowCode 流程编号
// nodeCode 开始节点编号
// userID 发起人
// input 输入数据
func StartFlowWithContext(ctx context.Context, flowCode, nodeCode, userID string, input interface{}) (*HandleResult, error) {
inputData, err := json.Marshal(input)
if err != nil {
return nil, err
}
return engine.StartFlow(ctx, flowCode, nodeCode, userID, inputData)
}
// HandleFlow 处理流程节点
// nodeInstanceID 节点实例内码
// userID 处理人
// input 输入数据
func HandleFlow(nodeInstanceID, userID string, input interface{}) (*HandleResult, error) {
return HandleFlowWithContext(context.Background(), nodeInstanceID, userID, input)
}
// HandleFlowWithContext 处理流程节点
// nodeInstanceID 节点实例内码
// userID 处理人
// input 输入数据
func HandleFlowWithContext(ctx context.Context, nodeInstanceID, userID string, input interface{}) (*HandleResult, error) {
inputData, err := json.Marshal(input)
if err != nil {
return nil, err
}
return engine.HandleFlow(ctx, nodeInstanceID, userID, inputData)
}
// StopFlow 停止流程
func StopFlow(nodeInstanceID string, allowStop func(*schema.FlowInstance) bool) error {
return engine.StopFlow(nodeInstanceID, allowStop)
}
// StopFlowInstance 停止流程实例
func StopFlowInstance(flowInstanceID string, allowStop func(*schema.FlowInstance) bool) error {
return engine.StopFlowInstance(flowInstanceID, allowStop)
}
// QueryTodoFlows 查询流程待办数据
// flowCode 流程编号
// userID 待办人
func QueryTodoFlows(flowCode, userID string) ([]*schema.FlowTodoResult, error) {
return engine.QueryTodoFlows(flowCode, userID)
}
// QueryFlowHistory 查询流程历史数据
// flowInstanceID 流程实例内码
func QueryFlowHistory(flowInstanceID string) ([]*schema.FlowHistoryResult, error) {
return engine.QueryFlowHistory(flowInstanceID)
}
// QueryDoneFlowIDs 查询已办理的流程实例ID列表
func QueryDoneFlowIDs(flowCode, userID string) ([]string, error) {
return engine.QueryDoneFlowIDs(flowCode, userID)
}
// QueryNodeCandidates 查询节点实例的候选人ID列表
func QueryNodeCandidates(nodeInstanceID string) ([]string, error) {
return engine.QueryNodeCandidates(nodeInstanceID)
}
// GetNodeInstance 获取节点实例
func GetNodeInstance(nodeInstanceID string) (*schema.NodeInstance, error) {
return engine.GetNodeInstance(nodeInstanceID)
}
// StartServer 启动管理服务
func StartServer(opts ...ServerOption) http.Handler {
srv := new(Server).Init(engine, opts...)
return srv
}
// DefaultEngine 默认引擎
func DefaultEngine() *Engine {
return engine
}