forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
282 lines (245 loc) · 6.4 KB
/
import.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
package gou
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
)
// NewImport 创建导入器
func NewImport(chunk int) *Import {
return &Import{
Chunk: chunk,
Notify: func(line int, record string) {},
Option: map[string]interface{}{},
}
}
// Using 配置源数据与数据模型字段映射表或处理器
func (impt *Import) Using(process interface{}) *Import {
if process, ok := process.(map[string]string); ok {
impt.Mapping = process
}
if process, ok := process.(string); ok {
impt.Process = process
}
return impt
}
// Set 配置源数据与数据模型字段映射表或处理器
func (impt *Import) Set(key string, value interface{}) *Import {
impt.Option[key] = value
return impt
}
// NewResponse 创建导入器
func (impt *Import) NewResponse() *ResponseImport {
return &ResponseImport{
Errors: &ErrorsImport{},
Import: impt,
}
}
// InsertCSV 导入CSV使用 (insert)
func (impt *Import) InsertCSV(csvfile string, model string) *ResponseImport {
mod := Select(model)
resp := impt.NewResponse()
f, err := os.Open(csvfile)
defer f.Close()
if err != nil {
resp.Error("打开文件错误, %s", 400, err.Error()).FileIs(csvfile)
return resp
}
buf := bufio.NewReader(f)
reader := csv.NewReader(buf)
// CSV 分割符
if comma, has := impt.Option["comma"]; has {
if comma, ok := comma.(string); ok {
reader.Comma = []rune(comma)[0]
}
}
// CSV 换行符
if comment, has := impt.Option["comment"]; has {
if comment, ok := comment.(string); ok {
reader.Comma = []rune(comment)[0]
}
}
// CSV 去掉前导空格
reader.TrimLeadingSpace = true
if trimLeadingSpace, has := impt.Option["trim"]; has {
if trimLeadingSpace, ok := trimLeadingSpace.(bool); ok {
reader.TrimLeadingSpace = trimLeadingSpace
}
}
// CSV 记录位置
reader.FieldsPerRecord = 0
if fieldsPerRecord, has := impt.Option["fields"]; has {
if fieldsPerRecord, ok := fieldsPerRecord.(int); ok {
reader.FieldsPerRecord = fieldsPerRecord
}
}
// 遍历数据
line := 0
isColumn := true
fields := []string{}
data := []map[string]string{}
for {
line++
record, err := reader.Read()
if err == io.EOF {
break
}
if isColumn { // 字段清单
fields = record
isColumn = false
continue
}
if len(record) < len(fields) {
resp.Error("缺少字段", 400).FileIs(csvfile).AtLine(line)
continue
}
row := map[string]string{}
for i := range fields {
key := fields[i]
value := record[i]
row[key] = value
}
data = append(data, row)
size := len(data)
if size == impt.Chunk {
resp.InsertChunk(line-size, data, mod)
data = []map[string]string{}
}
}
// 插入最后一批数据
size := len(data)
if size >= 0 {
resp.InsertChunk(line-size, data, mod)
}
return resp
}
// InsertChunk 导入数据 (insert)
func (resp *ResponseImport) InsertChunk(line int, data []map[string]string, model *Model) {
total := len(data)
resp.Total = resp.Total + total
columns, values := resp.CastInsert(line, data, model)
err := model.Insert(columns, values)
if err != nil {
resp.Failure = resp.Failure + total
resp.Error(err.Error(), 500).AtLine(line).ValueIs(data)
return
}
resp.Success = resp.Success + total
}
// CastInsert 数据格式转换
func (resp *ResponseImport) CastInsert(line int, data []map[string]string, model *Model) ([]string, [][]interface{}) {
records := []map[string]interface{}{}
if resp.Import.Mapping != nil {
records = resp.castByMapping(line, data, model)
} else if resp.Import.Process != "" {
records = resp.castByProcess(line, data, model)
} else {
records = resp.castType(line, data, model)
}
if len(records) == 0 {
return nil, nil
}
columns := []string{}
values := [][]interface{}{}
for key := range records[0] {
columns = append(columns, key)
}
for _, record := range records {
value := []interface{}{}
for _, field := range columns {
value = append(value, record[field])
}
values = append(values, value)
}
return columns, values
}
// castByMapping 按映射表转换
func (resp *ResponseImport) castType(line int, data []map[string]string, model *Model) []map[string]interface{} {
res := []map[string]interface{}{}
return res
}
// castByMapping 按映射表转换
func (resp *ResponseImport) castByMapping(line int, data []map[string]string, model *Model) []map[string]interface{} {
res := []map[string]interface{}{}
if len(data) == 0 {
return res
}
// 是否仅导入 mapping 中定义字段
strict := false
if strictOption, has := resp.Import.Option["strict"]; has {
if strictOption, ok := strictOption.(bool); ok {
strict = strictOption
}
}
// 导入数据
mappingReverse := map[string]string{}
mapping := resp.Import.Mapping
for _, name := range model.ColumnNames {
if name, ok := name.(string); ok {
if key, has := mapping[name]; has {
mappingReverse[key] = name
} else if !strict { // 仅导入映射表中定义字段
if _, has := data[0][name]; has {
mappingReverse[name] = name
}
}
}
}
// 转换数据
for _, record := range data {
line++
row := map[string]interface{}{}
for key, name := range mappingReverse {
row[name] = record[key]
}
// 数值校验()
errs := model.Validate(row)
if len(errs) > 0 {
for _, err := range errs {
resp.Error(err.Messages[0], 400).AtLine(line).FieldIs(err.Column)
}
continue
}
res = append(res, row)
}
return res
}
// castByMapping 按处理器转换
func (resp *ResponseImport) castByProcess(line int, data []map[string]string, model *Model) []map[string]interface{} {
res := []map[string]interface{}{}
return res
}
// IsError 错误检查
func (resp *ResponseImport) IsError(line int, process interface{}) bool {
return len(*resp.Errors) > 0
}
// Error 导入数据时错误
func (resp *ResponseImport) Error(mesage string, code int, args ...interface{}) *ErrorOfImport {
err := &ErrorOfImport{
Code: code,
Message: fmt.Sprintf(mesage, args...),
}
*resp.Errors = append(*resp.Errors, err)
return err
}
// FileIs 设置出错文件
func (err *ErrorOfImport) FileIs(filename string) *ErrorOfImport {
err.File = filename
return err
}
// AtLine 设置出错行
func (err *ErrorOfImport) AtLine(line int) *ErrorOfImport {
err.Line = line
return err
}
// FieldIs 设置出错字段
func (err *ErrorOfImport) FieldIs(name string) *ErrorOfImport {
err.Field = name
return err
}
// ValueIs 设置出错字段
func (err *ErrorOfImport) ValueIs(value interface{}) *ErrorOfImport {
err.Value = value
return err
}