-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.go
100 lines (91 loc) · 2.94 KB
/
service.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
package liflow
import (
"context"
"github.com/BeanWei/li/li-engine/contrib/liflow/ent"
"github.com/BeanWei/li/li-engine/contrib/liflow/ent/flowinstance"
"github.com/BeanWei/li/li-engine/contrib/liflow/ent/flownodeinstance"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
type (
FlowElementInstance struct {
NodeKey string `json:"node_key"`
NodeName string `json:"node_name"`
Status int8 `json:"status"`
}
)
func GetHistoryUserTaskList(ctx context.Context, flowInstanceID string) ([]*ent.FlowNodeInstance, error) {
historyNodeInstanceList, err := ent.DB().FlowNodeInstance.
Query().
Where(flownodeinstance.FlowInstanceIDEQ(flowInstanceID)).
All(ctx)
if err != nil {
return nil, err
} else if len(historyNodeInstanceList) == 0 {
return nil, nil
}
flowInstance, err := ent.DB().FlowInstance.Query().
Where(flowinstance.IDEQ(flowInstanceID)).
WithFlowDeployment().
Only(ctx)
if err != nil {
return nil, err
}
if flowInstance.Edges.FlowDeployment == nil {
return nil, nil
}
flowElementMap := flowInstance.Edges.FlowDeployment.Model.ElementMap()
userTaskList := make([]*ent.FlowNodeInstance, 0)
for _, node := range historyNodeInstanceList {
if node.Status != FlowNodeInstanceStatusCompleted && node.Status != FlowNodeInstanceStatusActive {
continue
}
if ele := flowElementMap[node.NodeKey]; ele.FlowType != FlowElementFlowTypeUserTask {
continue
}
userTaskList = append(userTaskList, node)
}
return userTaskList, nil
}
func GetHistoryElementList(ctx context.Context, flowInstanceID string) ([]*FlowElementInstance, error) {
historyNodeInstanceList, err := ent.DB().FlowNodeInstance.
Query().
Where(flownodeinstance.FlowInstanceIDEQ(flowInstanceID)).
All(ctx)
if err != nil || len(historyNodeInstanceList) == 0 {
return nil, err
}
flowInstance, err := ent.DB().FlowInstance.Query().
Where(flowinstance.IDEQ(flowInstanceID)).
WithFlowDeployment().
Only(ctx)
if err != nil {
return nil, err
}
if flowInstance.Edges.FlowDeployment == nil {
return nil, gerror.NewCode(gcode.CodeInvalidRequest, "flow deployment is empty")
}
flowElementMap := flowInstance.Edges.FlowDeployment.Model.ElementMap()
elementList := make([]*FlowElementInstance, 0)
for _, node := range historyNodeInstanceList {
if sourceFlowElement := flowElementMap[node.SourceNodeKey]; sourceFlowElement != nil {
sourceSequenceFlowStatus := node.Status
if node.Status == FlowNodeInstanceStatusActive {
sourceSequenceFlowStatus = FlowNodeInstanceStatusCompleted
}
elementList = append(elementList, &FlowElementInstance{
NodeKey: sourceFlowElement.Key,
NodeName: sourceFlowElement.Name,
Status: sourceSequenceFlowStatus,
})
}
if flowElement := flowElementMap[node.NodeKey]; flowElement != nil {
elementList = append(elementList, &FlowElementInstance{
NodeKey: node.NodeKey,
NodeName: flowElement.Name,
Status: node.Status,
})
}
}
return elementList, nil
}