query_set.go
4.3 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package domain
import (
"fmt"
"github.com/google/uuid"
"github.com/zeromicro/go-zero/core/collection"
"sort"
"time"
)
// QuerySet 查询集合
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() ||
t == CalculateItem.ToString() ||
t == CalculateTable.ToString() ||
t == CalculateSet.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 {
if queryComponents[i].MasterTable != nil && queryComponents[i].MasterTable.TableId > 0 {
set.AddInt(queryComponents[i].MasterTable.TableId)
}
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)
}
}
if queryComponents[i].Formula != nil {
for _, f := range queryComponents[i].Formula.TableFields {
set.AddInt(f.TableId)
}
}
if queryComponents[i].Layout != nil {
for _, c := range queryComponents[i].Layout.LayoutCells() {
set.AddInt(c.Data.TableField.TableId)
}
}
}
res := set.KeysInt()
sort.Ints(res)
return res
}
func (querySet *QuerySet) Valid(queryComponents []*QueryComponent) error {
switch querySet.Type {
case CalculateTable.ToString():
if len(queryComponents) == 0 {
return fmt.Errorf("行、值不能同时为空")
}
qc := queryComponents[0]
set := collection.NewSet()
for _, f := range qc.Aggregation.AggregationFields() {
if !set.Contains(f.DisplayName) {
set.AddStr(f.DisplayName)
} else {
return fmt.Errorf("字段'%s'存在重名,请进行重命名", f.DisplayName)
}
}
}
return nil
}
type QuerySets []*QuerySet
func (querySets QuerySets) ToMap() map[int]*QuerySet {
var result = make(map[int]*QuerySet)
for i := range querySets {
result[querySets[i].QuerySetId] = querySets[i]
}
return result
}