-
Notifications
You must be signed in to change notification settings - Fork 311
/
column_default.go
326 lines (268 loc) · 7.1 KB
/
column_default.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
325
326
// Package views
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package views
import (
"github.com/gogf/gf/v2/text/gstr"
"hotgo/internal/consts"
"hotgo/internal/model/input/sysin"
)
// 默认表单组件映射 Ts -> 表单组件
var defaultFormModeMap = map[string]string{
TsTypeString: FormModeInput,
TsTypeNumber: FormModeInputNumber,
TsTypeBoolean: FormModeInputNumber,
TsTypeArray: FormModeInputDynamic,
TsTypeTuple: FormModeInputDynamic,
TsTypeAny: FormModeInput,
}
var defaultEditFields = map[string]bool{
"id": false,
"created_at": false,
"updated_at": false,
"deleted_at": false,
}
var defaultEditSwitch = map[string]bool{
"id": false,
"level": false,
"tree": false,
"created_by": false,
"updated_by": false,
"deleted_by": false,
"created_at": false,
"updated_at": false,
"deleted_at": false,
}
var defaultListSwitch = map[string]bool{
"level": false,
"tree": false,
"deleted_at": false,
}
var defaultExportSwitch = map[string]bool{
"level": false,
"tree": false,
"deleted_at": false,
}
var defaultQuerySwitch = map[string]bool{
"level": false,
"tree": false,
"deleted_at": false,
}
var defaultSort = map[string]bool{
"id": true,
"sort": true,
}
var defaultTreeFields = []string{"pid", "level", "tree"}
// 默认表单验证映射 物理类型命名识别
var defaultFormRoleMap = map[string]string{
"mobile": FormRolePhone,
"qq": FormRoleQq,
"email": FormRoleEmail,
"id_card": FormRoleIdCard,
"bank_card": FormRoleBankCard,
"password": FormRolePassword,
"pass": FormRolePassword,
"price": FormRoleAmount,
}
// 默认查询条件映射 go类型识别
var defaultWhereModeMap = map[string]string{
GoTypeString: WhereModeLike,
GoTypeDate: WhereModeEq,
GoTypeDatetime: WhereModeEq,
GoTypeInt: WhereModeEq,
GoTypeUint: WhereModeEq,
GoTypeInt64: WhereModeEq,
GoTypeUint64: WhereModeEq,
GoTypeIntSlice: WhereModeIn,
GoTypeInt64Slice: WhereModeIn,
GoTypeUint64Slice: WhereModeIn,
GoTypeFloat32: WhereModeEq,
GoTypeFloat64: WhereModeEq,
GoTypeBytes: WhereModeEq,
GoTypeTime: WhereModeEq,
GoTypeGTime: WhereModeEq,
GoTypeJson: WhereModeJsonContains,
}
// setDefault 设置默认属性
func setDefault(field *sysin.GenCodesColumnListModel) {
setDefaultEdit(field)
setDefaultFormMode(field)
setDefaultFormRole(field)
setDefaultDictType(field)
setDefaultList(field)
setDefaultExport(field)
setDefaultQuery(field)
setDefaultQueryWhere(field)
setDefaultValue(field)
if field.IsAllowNull == "NO" {
field.Required = true
}
if IsIndexUNI(field.Index) {
field.Unique = true
}
if df, ok := defaultSort[field.Name]; ok {
field.IsSort = df
}
if field.Dc == "" {
field.Dc = field.Name
}
}
// setDefaultEdit 设置默认编辑
func setDefaultEdit(field *sysin.GenCodesColumnListModel) {
field.IsEdit = true
if IsIndexPK(field.Index) {
field.IsEdit = false
return
}
if df, ok := defaultEditFields[field.Name]; ok {
field.IsEdit = df
}
if df, ok := defaultEditSwitch[field.Name]; ok {
field.IsEdit = df
}
}
// setDefaultFormMode 设置默认表单组件
func setDefaultFormMode(field *sysin.GenCodesColumnListModel) {
field.FormMode = FormModeInput
if df, ok := defaultFormModeMap[field.TsType]; ok {
field.FormMode = df
}
if gstr.HasSuffix(field.GoName, "Status") && IsNumberType(field.GoType) {
field.FormMode = FormModeSelect
return
}
if field.GoName == "CreatedAt" {
field.FormMode = FormModeTimeRange
return
}
if (field.GoName == "ProvinceId" || field.GoName == "CityId") && IsNumberType(field.GoType) {
field.FormMode = FormModeCitySelector
return
}
if field.DataType == "datetime" || field.DataType == "timestamp" || field.DataType == "timestamptz" {
field.FormMode = FormModeTime
return
}
if field.DataType == "date" {
field.FormMode = FormModeDate
return
}
if field.GoType == GoTypeString && field.Length >= 256 && field.Length <= 512 {
field.FormMode = FormModeInputTextarea
return
}
if field.GoType == GoTypeString && field.Length > 512 {
field.FormMode = FormModeInputEditor
return
}
}
// setDefaultFormRole 设置默认表单验证
func setDefaultFormRole(field *sysin.GenCodesColumnListModel) {
field.FormRole = FormRoleNone
switch field.GoType {
case GoTypeUint, GoTypeUint64:
field.FormRole = FormRoleNum
return
}
if df, ok := defaultFormRoleMap[field.Name]; ok {
field.FormRole = df
}
}
// setDefaultDictType 设置默认字典类型
func setDefaultDictType(field *sysin.GenCodesColumnListModel) {
if gstr.HasSuffix(field.GoName, "Status") && IsNumberType(field.GoType) {
field.DictType = 3 // 默认系统状态ID
return
}
}
// setDefaultList 设置默认列表
func setDefaultList(field *sysin.GenCodesColumnListModel) {
field.IsList = true
switch field.GoType {
case GoTypeIntSlice, GoTypeInt64Slice, GoTypeUint64Slice, GoTypeBytes, GoTypeJson:
field.IsList = false
return
}
if field.Length >= 500 {
field.IsList = false
return
}
if df, ok := defaultListSwitch[field.Name]; ok {
field.IsList = df
}
}
// setDefaultExport 设置默认导出
func setDefaultExport(field *sysin.GenCodesColumnListModel) {
field.IsExport = true
switch field.GoType {
case GoTypeIntSlice, GoTypeInt64Slice, GoTypeUint64Slice, GoTypeBytes, GoTypeJson:
field.IsExport = false
return
}
if field.Length >= 500 {
field.IsExport = false
return
}
if df, ok := defaultExportSwitch[field.Name]; ok {
field.IsExport = df
}
}
// setDefaultQuery 设置默认查询
func setDefaultQuery(field *sysin.GenCodesColumnListModel) {
field.IsQuery = false
if IsIndexPK(field.Index) {
field.IsQuery = true
return
}
if gstr.HasSuffix(field.GoName, "Status") && IsNumberType(field.GoType) {
field.IsQuery = true
return
}
if field.GoName == "CreatedAt" {
field.IsQuery = true
return
}
if df, ok := defaultQuerySwitch[field.Name]; ok {
field.IsQuery = df
}
}
// setDefaultQueryWhere 设置默认查询条件
func setDefaultQueryWhere(field *sysin.GenCodesColumnListModel) {
field.QueryWhere = WhereModeEq
if field.GoName == "CreatedAt" {
field.QueryWhere = WhereModeBetween
return
}
if field.Length >= 500 {
field.QueryWhere = WhereModeLikeAll
return
}
if df, ok := defaultWhereModeMap[field.GoType]; ok {
field.QueryWhere = df
}
}
// setDefaultValue 设置默认value
func setDefaultValue(field *sysin.GenCodesColumnListModel) {
var value interface{}
if field.DefaultValue == nil {
switch field.GoType {
case GoTypeString, GoTypeBytes, GoTypeDate, GoTypeDatetime, GoTypeTime, GoTypeGTime:
value = ""
case GoTypeIntSlice, GoTypeInt64Slice, GoTypeUint64Slice, GoTypeJson:
value = nil
case GoTypeInt, GoTypeUint, GoTypeInt64, GoTypeUint64:
value = 0
case GoTypeBool:
value = false
}
} else {
value = consts.ConvType(field.DefaultValue, field.GoType)
}
// 时间类型不做默认值处理
if field.GoType == GoTypeGTime {
value = ""
}
field.DefaultValue = value
}