field.go
7.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
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
327
328
package domain
import (
"fmt"
"github.com/linmadan/egglib-go/utils/xtime"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"strconv"
"strings"
)
// Field 字段
type Field struct {
// 字段Id
// FieldId int `json:"fieldId"`
// 索引序号
Index int `json:"index"`
// 名称
Name string `json:"name"`
// 对应数据库名称
SQLName string `json:"sqlName"`
// 对应数据库类型
SQLType string `json:"sqlType"`
// 描述
Description string `json:"description"`
// 标识 1.主键 2:主表字段 3:手动添加
Flag int `json:"flag"`
// 排序
Order string `json:"order,omitempty"`
}
func (f *Field) Valid() error {
if _, ok := SQLTypeMap[strings.ToUpper(f.SQLType)]; !ok {
return fmt.Errorf("unknown sql type:%v", f.SQLType)
}
if f.Index == 0 && f.Flag == ManualField {
return fmt.Errorf("field:%v index is 0", f.Name)
}
return nil
}
func (f *Field) Copy() *Field {
return &Field{
Index: f.Index,
Name: f.Name,
SQLName: f.SQLName,
SQLType: f.SQLType,
Flag: f.Flag,
}
}
type Fields []*Field
func (fields Fields) ToMap() map[string]*Field {
m := make(map[string]*Field)
for i := range fields {
m[fields[i].Name] = fields[i]
}
return m
}
func (fields Fields) ToMapBySqlName() map[string]*Field {
m := make(map[string]*Field)
for i := range fields {
m[fields[i].SQLName] = fields[i]
}
return m
}
func (fields Fields) NameArrayString() []string {
m := make([]string, 0)
for i := range fields {
m = append(m, fields[i].Name)
}
return m
}
func (fields Fields) Select(options map[string]interface{}) []*Field {
var result []*Field
for _, field := range fields {
if v, ok := options["flag"]; ok {
if v.(int) != field.Flag {
continue
}
}
result = append(result, field)
}
return result
}
func ValidFields(fields []*Field) error {
m := (Fields)(fields).ToMap()
if len(m) != len(fields) {
return fmt.Errorf("列名重复发")
}
for _, f := range fields {
if err := f.Valid(); err != nil {
return err
}
}
return nil
}
func FieldsChange(oldFields []*Field, newFields []*Field) (reserve []*Field, delete []*Field, add []*Field) {
var oldFieldsMap = (Fields)(oldFields).ToMap()
var newFieldsMap = (Fields)(newFields).ToMap()
for _, f := range newFields {
if _, ok := oldFieldsMap[f.Name]; ok {
reserve = append(reserve, f)
continue
} else {
add = append(add, f)
continue
}
}
for _, f := range oldFields {
if _, ok := newFieldsMap[f.Name]; !ok {
delete = append(delete, f)
continue
}
}
return
}
type FieldValues struct {
Number int `json:"rowIndex"`
FieldValues []*FieldValue `json:"fieldValues"`
}
func (f *FieldValues) Valid() error {
for _, item := range f.FieldValues {
if err := item.Valid(); err != nil {
return err
}
}
return nil
}
type FieldValue struct {
*Field
// 字段值(当前)
Value string `json:"value,omitempty"`
// 字段值(旧的,更新时有效)
OldValue string `json:"oldValue,omitempty"`
typeValue interface{}
}
func (f *FieldValue) CheckValue() error {
val, err := ValueToType(f.Value, f.SQLType)
if err != nil {
return err
}
f.typeValue = val
return nil
}
func (f *FieldValue) TypeValue() interface{} {
if f.typeValue == nil {
f.typeValue, _ = ValueToType(f.Value, f.SQLType)
}
return f.typeValue
}
func ValueToType(value string, sqlType string) (interface{}, error) {
var toTypeVal interface{}
var err error
numberString := utils.NewNumberString(value)
switch sqlType {
case String.ToString():
toTypeVal = value
case Int.ToString():
toTypeVal, err = numberString.Int()
if err != nil {
err = fmt.Errorf("[%v]不是有效的数值类型", value)
}
case BigInt.ToString():
toTypeVal, err = numberString.Int()
if err != nil {
err = fmt.Errorf("[%v]不是有效的数值类型", value)
}
case Float.ToString():
toTypeVal, err = numberString.Float64()
if err != nil {
err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
}
case DECIMALV2.ToString():
toTypeVal, err = numberString.Float64()
if err != nil {
err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
}
case DECIMAL279.ToString():
toTypeVal, err = numberString.Float64()
if err != nil {
err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
}
case Date.ToString():
toTypeVal, err = xtime.Parse(value)
if err != nil {
err = fmt.Errorf("[%v]不是有效的日期类型", value)
}
toTypeVal = value
case Datetime.ToString():
toTypeVal, err = xtime.Parse(value)
if err != nil {
err = fmt.Errorf("[%v]不是有效的时间类型", value)
}
toTypeVal = value
default:
return nil, fmt.Errorf("unknow sql type :%v", sqlType)
}
return toTypeVal, err
}
func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool) []map[string]string {
var result = make([]map[string]string, 0)
var key string
var ignoreEmptyString = false
if len(configs) > 0 {
ignoreEmptyString = configs[0]
}
for _, d := range data {
var item = make(map[string]string)
for j, f := range fields {
key = f.SQLName
if byName {
key = f.Name
}
if ignoreEmptyString && len(d[j]) == 0 {
continue
}
if len(d) >= j {
// 处理精度问题
item[key] = RoundFieldValue(f, d[j]) //d[j]
} else {
item[key] = ""
}
}
result = append(result, item)
}
return result
}
// RoundFieldValue 字段值精度处理
func RoundFieldValue(f *Field, v string) string {
if f.SQLType == Float.ToString() {
fv, err := strconv.ParseFloat(v, 64)
if err != nil {
return v
}
return utils.AssertString(fv) //fmt.Sprintf("%v", fv)
}
return v
//if f.SQLType != DECIMALV2.ToString() {
// return v
//}
//fv, err := strconv.ParseFloat(v, 64)
//if err != nil {
// return v
//}
//fv = utils.Round(fv, 6)
//return fmt.Sprintf("%v", fv)
}
func RoundValue(v string) string {
fv, err := strconv.ParseFloat(v, 64)
if err != nil {
return v
}
return utils.AssertString(fv) //fmt.Sprintf("%v", fv)
}
func GripData(data []map[string]string, total int64) map[string]interface{} {
if len(data) == 0 {
data = make([]map[string]string, 0)
}
return map[string]interface{}{
"list": data,
"total": total,
}
}
func PK() *Field {
return &Field{
Index: 0,
Name: "序号",
SQLName: "id",
SQLType: String.ToString(),
Description: "主键",
Flag: PKField,
}
}
func MakeToInterfaces(fields []*Field) func([]string) []interface{} {
return func(input []string) []interface{} {
output := make([]interface{}, len(input))
for i, v := range input {
if i < len(fields) {
// 处理精度问题
if fields[i].SQLType == Float.ToString() {
v = RoundFieldValue(fields[i], v)
}
convValue, err := ValueToType(v, fields[i].SQLType)
if err == nil {
output[i] = convValue
continue
}
}
output[i] = v
}
return output
}
}
func RangeFields(fields []*Field, set ...func(*Field)) []*Field {
for i := range fields {
for j := range set {
set[j](fields[i])
}
}
return fields
}
func ChangeFieldFlag(f *Field) {
f.Flag = MainTableField
}