forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
324 lines (272 loc) · 9.36 KB
/
process.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package table
import (
"strings"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/maps"
)
func init() {
// 注册处理器
gou.RegisterProcessHandler("xiang.table.Search", ProcessSearch)
gou.RegisterProcessHandler("xiang.table.Find", ProcessFind)
gou.RegisterProcessHandler("xiang.table.Select", ProcessSelect)
gou.RegisterProcessHandler("xiang.table.Save", ProcessSave)
gou.RegisterProcessHandler("xiang.table.Delete", ProcessDelete)
gou.RegisterProcessHandler("xiang.table.Insert", ProcessInsert)
gou.RegisterProcessHandler("xiang.table.UpdateWhere", ProcessUpdateWhere)
gou.RegisterProcessHandler("xiang.table.DeleteWhere", ProcessDeleteWhere)
gou.RegisterProcessHandler("xiang.table.QuickSave", ProcessQuickSave)
gou.RegisterProcessHandler("xiang.table.UpdateIn", ProcessUpdateIn)
gou.RegisterProcessHandler("xiang.table.DeleteIn", ProcessDeleteIn)
gou.RegisterProcessHandler("xiang.table.Setting", ProcessSetting)
}
// ProcessSearch xiang.table.Search
// 按条件查询数据记录, 请求成功返回符合查询条件带有分页信息的数据对象
func ProcessSearch(process *gou.Process) interface{} {
// 读取表格名称
process.ValidateArgNums(1)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["search"].ValidateLoop("xiang.table.search")
if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) {
return nil
}
// Before Hook
process.Args = table.Before(table.Hooks.BeforeSearch, process.Args)
// 参数表
process.ValidateArgNums(4)
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0)
page := process.ArgsInt(2, api.DefaultInt(1))
pagesize := process.ArgsInt(3, api.DefaultInt(2))
// 查询数据
response := gou.NewProcess(api.Process, param, page, pagesize).Run()
// After Hook
return table.After(table.Hooks.AfterSearch, response)
}
// ProcessFind xiang.table.Find
// 按主键值查询单条数据, 请求成功返回对应主键的数据记录
func ProcessFind(process *gou.Process) interface{} {
process.ValidateArgNums(1)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["find"].ValidateLoop("xiang.table.find")
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
return nil
}
// Before Hook
process.Args = table.Before(table.Hooks.BeforeFind, process.Args)
// 参数表
process.ValidateArgNums(2)
id := process.Args[1]
param := api.MergeDefaultQueryParam(gou.QueryParam{}, 1)
// 查询数据
response := gou.NewProcess(api.Process, id, param).Run()
// After Hook
return table.After(table.Hooks.AfterFind, response)
}
// ProcessSave xiang.table.Save
// 保存单条记录。如数据记录中包含主键字段则更新,不包含主键字段则创建记录;返回创建或更新的记录主键值
func ProcessSave(process *gou.Process) interface{} {
// 读取表格名称
process.ValidateArgNums(1)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["save"].ValidateLoop("xiang.table.save")
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
return nil
}
// Before Hook
process.Args = table.Before(table.Hooks.BeforeSave, process.Args)
// 参数处理
process.ValidateArgNums(2)
// 查询数据
response := gou.NewProcess(api.Process, process.Args[1]).Run()
// After Hook
return table.After(table.Hooks.AfterSave, response)
}
// ProcessDelete xiang.table.Delete
// 删除指定主键值的数据记录, 请求成功返回null
func ProcessDelete(process *gou.Process) interface{} {
process.ValidateArgNums(2)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete"].ValidateLoop("xiang.table.delete")
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
return nil
}
id := process.Args[1]
return gou.NewProcess(api.Process, id).Run()
}
// ProcessDeleteWhere xiang.table.DeleteWhere
// 按条件批量删除数据, 请求成功返回删除行数
func ProcessDeleteWhere(process *gou.Process) interface{} {
process.ValidateArgNums(2)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete-where"].ValidateLoop("xiang.table.DeleteWhere")
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
return nil
}
// 批量删除
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0)
if param.Limit == 0 { // 限定删除行
param.Limit = 10
}
return gou.NewProcess(api.Process, param).Run()
}
// ProcessDeleteIn xiang.table.DeleteIn
// 按条件批量删除数据, 请求成功返回删除行数
func ProcessDeleteIn(process *gou.Process) interface{} {
process.ValidateArgNums(3)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete-in"].ValidateLoop("xiang.table.DeleteIn")
if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) {
return nil
}
// 批量删除
ids := strings.Split(process.ArgsString(1), ",")
primary := process.ArgsString(2, "id")
param := gou.QueryParam{
Wheres: []gou.QueryWhere{
{Column: primary, OP: "in", Value: ids},
},
}
return gou.NewProcess(api.Process, param).Run()
}
// ProcessUpdateWhere xiang.table.UpdateWhere
// 按条件批量更新数据, 请求成功返回更新行数
func ProcessUpdateWhere(process *gou.Process) interface{} {
process.ValidateArgNums(3)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["update-where"].ValidateLoop("xiang.table.UpdateWhere")
if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) {
return nil
}
// 批量更新
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0)
if param.Limit == 0 { // 限定删除行
param.Limit = 10
}
return gou.NewProcess(api.Process, param, process.Args[2]).Run()
}
// ProcessUpdateIn xiang.table.UpdateWhere
// 按条件批量更新数据, 请求成功返回更新行数
func ProcessUpdateIn(process *gou.Process) interface{} {
process.ValidateArgNums(4)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["update-in"].ValidateLoop("xiang.table.UpdateIn")
if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) {
return nil
}
// 批量删除
ids := strings.Split(process.ArgsString(1), ",")
primary := process.ArgsString(2, "id")
param := gou.QueryParam{
Wheres: []gou.QueryWhere{
{Column: primary, OP: "in", Value: ids},
},
}
return gou.NewProcess(api.Process, param, process.Args[3]).Run()
}
// ProcessInsert xiang.table.Insert
// 插入多条数据记录,请求成功返回插入行数
func ProcessInsert(process *gou.Process) interface{} {
process.ValidateArgNums(3)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["insert"].ValidateLoop("xiang.table.Insert")
if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) {
return nil
}
return gou.NewProcess(api.Process, process.Args[1:]...).Run()
}
// ProcessSetting xiang.table.Setting
// 读取数据表格配置信息, 请求成功返回配置信息对象
func ProcessSetting(process *gou.Process) interface{} {
process.ValidateArgNums(2)
name := process.ArgsString(0)
field := process.ArgsString(1)
table := Select(name)
api := table.APIs["setting"]
if process.NumOfArgsIs(2) && api.IsAllow(process.Args[1]) {
return nil
}
fields := strings.Split(field, ",")
if api.ProcessIs("xiang.table.Setting") {
setting := maps.Map{
"name": table.Name,
"title": table.Title,
"decription": table.Decription,
"columns": table.Columns,
"filters": table.Filters,
"list": table.List,
"edit": table.Edit,
"view": table.View,
"insert": table.Insert,
}
if len(fields) == 1 && setting.Has(fields[0]) {
field := strings.TrimSpace(fields[0])
return setting.Get(field)
}
if len(fields) > 1 {
res := maps.Map{}
for _, field := range fields {
field = strings.TrimSpace(field)
if setting.Has(field) {
res.Set(field, setting.Get(field))
}
}
return res
}
return setting
}
return gou.NewProcess(api.Process, fields).Run()
}
// ProcessQuickSave xiang.table.QuickSave
// 保存多条记录。如数据记录中包含主键字段则更新,不包含主键字段则创建记录;返回创建或更新的记录主键值
func ProcessQuickSave(process *gou.Process) interface{} {
process.ValidateArgNums(2)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["quicksave"].ValidateLoop("xiang.table.quicksave")
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
return nil
}
args := []interface{}{}
payload := process.ArgsMap(1)
ids := []int{}
if payload.Has("delete") {
if v, ok := payload["delete"].([]int); ok {
ids = v
} else if vany, ok := payload["delete"].([]interface{}); ok {
for _, v := range vany {
ids = append(ids, any.Of(v).CInt())
}
}
}
args = append(args, ids)
args = append(args, payload.Get("data"))
if payload.Has("query") {
args = append(args, payload.Get("query"))
}
return gou.NewProcess(api.Process, args...).Run()
}
// ProcessSelect xiang.table.Select
// 单表数据查询,一般用于下拉菜单检索
func ProcessSelect(process *gou.Process) interface{} {
process.ValidateArgNums(1)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["select"].ValidateLoop("xiang.table.select")
if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) {
return nil
}
// Before Hook
process.Args = table.Before(table.Hooks.BeforeSelect, process.Args)
response := gou.NewProcess(api.Process, process.Args[1:]...).Run()
// After Hook
return table.After(table.Hooks.AfterSelect, response)
}