-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.go
319 lines (296 loc) · 7.19 KB
/
translate.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
package audit_log
import (
"errors"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
)
type MappingField map[string]string
const (
KEY_LOG = "operatelog"
KEY_BEFORE = "operatelog-fields-before"
KEY_AFTER = "operatelog-fields-after"
KEY_NEED_FIELDS = "operatelog-fields-need"
KEY_EXPECT_FIELDS = "operatelog-fields-expect"
)
var (
defaultLang = "cn"
fieldMapping map[string]MappingField
joinMapping map[string]MappingField
lang = defaultLang
)
func SetLang(val string) {
lang = val
}
func SetFieldMapping(val map[string]MappingField) {
fieldMapping = val
}
func SetJoinMapping(val map[string]MappingField) {
joinMapping = val
}
func CreateFieldsLog(fields interface{}, needFields, expectFields []string) (string, error) {
result := ""
fieldsMap, err := decodeFields(fields)
if err != nil {
return result, errors.New("decodeFields " + err.Error())
}
fieldsMapRes := filtrationFields(fieldsMap, needFields, expectFields)
fieldsRes, err := translateFields(fieldsMapRes)
if err != nil {
return result, errors.New("Translate beforeFields " + err.Error())
}
for key := range fieldsRes {
if fmt.Sprintf("%v", fieldsRes[key]) == "" {
continue
}
result += fmt.Sprintf("%v%v%v%v%v",
key,
joinMapping["fieldBefore"][lang],
fieldsRes[key],
joinMapping["fieldAfter"][lang],
joinMapping["split"][lang],
)
}
result = strings.TrimRight(result, joinMapping["split"][lang])
result += joinMapping["end"][lang]
return result, nil
}
func UpdateFieldsLog(beforeFields interface{}, afterFields interface{}, needFields, expectFields []string) (string, error) {
result := ""
fieldsMap, err := decodeFields(beforeFields)
if err != nil {
return result, errors.New("decodeFields " + err.Error())
}
fieldsMapRes := filtrationFields(fieldsMap, needFields, expectFields)
before, err := translateFields(fieldsMapRes)
if err != nil {
return result, errors.New("Translate beforeFields " + err.Error())
}
fieldsMap, err = decodeFields(afterFields)
if err != nil {
return result, errors.New("decodeFields " + err.Error())
}
fieldsMapRes = filtrationFields(fieldsMap, needFields, expectFields)
after, err := translateFields(fieldsMapRes)
if err != nil {
return result, errors.New("Translate afterFields " + err.Error())
}
for key := range before {
if _, ok := after[key]; !ok {
continue
}
if fmt.Sprintf("%v", before[key]) == fmt.Sprintf("%v", after[key]) {
continue
}
result += fmt.Sprintf("%v%v%v%v%v%v%v%v%v%v",
key,
joinMapping["before"][lang],
joinMapping["fieldBefore"][lang],
before[key],
joinMapping["fieldAfter"][lang],
joinMapping["after"][lang],
joinMapping["fieldBefore"][lang],
after[key],
joinMapping["fieldAfter"][lang],
joinMapping["split"][lang],
)
}
result = strings.TrimRight(result, joinMapping["split"][lang])
result += joinMapping["end"][lang]
return result, nil
}
func GetFieldsLogSlice(beforeFields interface{}, afterFields interface{}, needFields, expectFields []string) ([]string, []string, []string, error) {
fieldsSlice, beforeSlice, afterSlice := []string{}, []string{}, []string{}
fieldsMap, err := decodeFields(beforeFields)
if err != nil {
return fieldsSlice, beforeSlice, afterSlice, errors.New("decodeFields " + err.Error())
}
fieldsMapRes := filtrationFields(fieldsMap, needFields, expectFields)
before, err := translateFields(fieldsMapRes)
if err != nil {
return fieldsSlice, beforeSlice, afterSlice, errors.New("Translate beforeFields " + err.Error())
}
fieldsMap, err = decodeFields(afterFields)
if err != nil {
return fieldsSlice, beforeSlice, afterSlice, errors.New("decodeFields " + err.Error())
}
fieldsMapRes = filtrationFields(fieldsMap, needFields, expectFields)
after, err := translateFields(fieldsMapRes)
if err != nil {
return fieldsSlice, beforeSlice, afterSlice, errors.New("Translate afterFields " + err.Error())
}
fields := []string{}
for k := range before {
fields = append(fields, k)
}
for k := range after {
fields = append(fields, k)
}
fields = removeRepeat(fields)
for _, v := range fields {
beforeVal, beforeOK := before[v]
afterVal, afterOK := after[v]
if !beforeOK && afterOK {
if afterVal != "" {
afterValStr := fmt.Sprintf("%v", afterVal)
afterSlice = append(afterSlice, afterValStr)
fieldsSlice = append(fieldsSlice, v)
}
} else if beforeOK && afterOK {
if fmt.Sprintf("%v", beforeVal) != fmt.Sprintf("%v", afterVal) {
beforeValStr := fmt.Sprintf("%v", beforeVal)
beforeSlice = append(beforeSlice, beforeValStr)
afterValStr := fmt.Sprintf("%v", afterVal)
afterSlice = append(afterSlice, afterValStr)
fieldsSlice = append(fieldsSlice, v)
}
}
}
return fieldsSlice, beforeSlice, afterSlice, nil
}
func decodeFields(fields interface{}) (map[string]interface{}, error) {
m := make(map[string]interface{}, 0)
err := mapstructure.Decode(fields, &m)
if err != nil {
return nil, err
}
return m, nil
}
func translateFields(fields map[string]interface{}) (map[string]interface{}, error) {
result := make(map[string]interface{})
for key, value := range fields {
key = toSnakeCase(key)
if object, ok := fieldMapping[key][lang]; ok {
result[object] = value
} else {
result[key] = value
}
}
return result, nil
}
func filtrationFields(fieldsMap map[string]interface{}, needFields, expectFields []string) map[string]interface{} {
fieldsMapRes := make(map[string]interface{})
for key, val := range fieldsMap {
if len(needFields) > 0 && contains(needFields, toSnakeCase(key)) == -1 {
continue
}
if len(expectFields) > 0 && contains(expectFields, toSnakeCase(key)) > -1 {
continue
}
fieldsMapRes[key] = val
}
return fieldsMapRes
}
// init default mapping
func init() {
lang = "cn"
joinMapping = map[string]MappingField{
"before": {
"cn": "由",
},
"after": {
"cn": "改为",
},
"split": {
"cn": ",",
},
"end": {
"cn": "",
},
"fieldBefore": {
"cn": "[",
},
"fieldAfter": {
"cn": "]",
},
}
fieldMapping = map[string]MappingField{
"username": {
"cn": "用户名",
},
"remark": {
"cn": "备注",
},
"status": {
"cn": "状态",
},
"mobile": {
"cn": "手机号",
},
"group_name": {
"cn": "组织名称",
},
"expire_time": {
"cn": "过期时间",
},
"role_name": {
"cn": "角色名称",
},
"permissions": {
"cn": "权限集合",
},
"device_type": {
"cn": "设备类型",
},
"device_name": {
"cn": "设备名称",
},
"province_mark": {
"cn": "省",
},
"city_mark": {
"cn": "市",
},
"district_mark": {
"cn": "区",
},
"address": {
"cn": "详细地址",
},
"place_id": {
"cn": "场所",
},
"name": {
"cn": "名称",
},
"weeks": {
"cn": "校验时间",
},
"gender": {
"cn": "性别",
},
"birthday": {
"cn": "生日",
},
"id_card_no": {
"cn": "身份证号",
},
"nation": {
"cn": "民族",
},
"native_place": {
"cn": "籍贯",
},
"number_type": {
"cn": "证件类型",
},
"number": {
"cn": "卡号",
},
"department": {
"cn": "部门",
},
"device_status": {
"cn": "设备状态",
},
"auth_time": {
"cn": "授权时间",
},
"data": {
"cn": "数据",
},
"config": {
"cn": "配置",
},
}
}