query_set.go
3.0 KB
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
package domain
import (
"fmt"
"github.com/google/uuid"
"github.com/zeromicro/go-zero/core/collection"
"sort"
"time"
)
// 查询集合
type QuerySet struct {
// 查询集合ID
QuerySetId int `json:"querySetId"`
// Schema:方案 SubProcess:子过程
Type string `json:"type"`
// 标识 分组:Group 子过程/方案:Set
Flag string `json:"flag"`
// 名称
Name string `json:"name"`
// 拼音(排序使用)
PinName string `json:"pinName"`
// 父级ID
ParentId int `json:"parentId"`
// 状态 1:启用 2:关闭 (子过程默认启用)
Status int `json:"status"`
// 查询集合信息
QuerySetInfo *QuerySetInfo `json:"querySetInfo"`
// 查询组件
QueryComponents []*QueryComponent `json:"queryComponents"`
// 排序
Sort int `json:"sort"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
// 扩展
Context *Context `json:"context"`
}
type QuerySetRepository interface {
Save(querySet *QuerySet) (*QuerySet, error)
Remove(querySet *QuerySet) (*QuerySet, error)
FindOne(queryOptions map[string]interface{}) (*QuerySet, error)
Find(queryOptions map[string]interface{}) (int64, []*QuerySet, error)
}
func (querySet *QuerySet) Identify() interface{} {
if querySet.QuerySetId == 0 {
return nil
}
return querySet.QuerySetId
}
func (querySet *QuerySet) Update(queryComponents []*QueryComponent, tableId int) error {
for i := range queryComponents {
if len(queryComponents[i].Id) == 0 {
uuid, _ := uuid.NewUUID()
queryComponents[i].Id = uuid.String()
}
for j := range queryComponents[i].Conditions {
if len(queryComponents[i].Conditions[j].Id) == 0 {
uuid, _ := uuid.NewUUID()
queryComponents[i].Conditions[j].Id = uuid.String()
}
}
for j := range queryComponents[i].Selects {
if len(queryComponents[i].Selects[j].Id) == 0 {
uuid, _ := uuid.NewUUID()
queryComponents[i].Selects[j].Id = uuid.String()
}
}
}
querySet.QueryComponents = queryComponents
if querySet.QuerySetInfo.BindTableId == 0 {
querySet.QuerySetInfo.BindTableId = tableId
}
querySet.UpdatedAt = time.Now()
return nil
}
func ValidQuerySetType(t string) error {
if t == SchemaTable.ToString() || t == SubProcessTable.ToString() {
return nil
}
return fmt.Errorf("类型有误")
}
func ValidQuerySetFlag(t string) error {
if t == FlagGroup || t == FlagSet {
return nil
}
return fmt.Errorf("标识有误")
}
func (querySet *QuerySet) GetDependencyTables(queryComponents []*QueryComponent) []int {
set := collection.NewSet()
for i := range queryComponents {
for _, c := range queryComponents[i].Conditions {
for _, f := range c.FieldLeft.TableFields {
set.AddInt(f.TableId)
}
for _, f := range c.FieldRight.TableFields {
set.AddInt(f.TableId)
}
}
for _, s := range queryComponents[i].Selects {
for _, f := range s.FieldLeft.TableFields {
set.AddInt(f.TableId)
}
for _, f := range s.FieldRight.TableFields {
set.AddInt(f.TableId)
}
}
}
res := set.KeysInt()
sort.Ints(res)
return res
}