-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
365 lines (356 loc) · 12.1 KB
/
collection.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package mydictionary
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/360EntSecGroup-Skylar/excelize"
)
// collection
type collectionStruct struct {
name string
readable bool
writable bool
onlineSource string
index int
xlsx *excelize.File
sheetName string
columnIndex map[string]int
content []VocabularyAnswerStruct
}
// open and check .xlsx file
func (collection *collectionStruct) check(filePath string) (err error) {
var (
recheckCounter int
recheckUpperLimit int
contentTemp [][]string
columnNumber int
)
// file -> ram image
collection.xlsx, err = excelize.OpenFile(filePath)
if err != nil {
return
}
// recheck
recheckCounter = 0
recheckUpperLimit = 10
RECHECK:
collection.sheetName = collection.xlsx.GetSheetMap()[1]
if strings.Compare(collection.sheetName, "") == 0 {
// no worksheet in workbook: create a worksheet
if strings.Compare(collection.name, "") == 0 {
collection.xlsx.NewSheet("collection")
} else {
collection.xlsx.NewSheet(collection.name)
}
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
contentTemp = collection.xlsx.GetRows(collection.sheetName)
if len(contentTemp) == 0 {
// empty worksheet: create row 1
collection.xlsx.SetCellValue(collection.sheetName, "A1", sn)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
columnNumber = len(contentTemp[0])
// check existence of sheet header (column) in row 1
collection.columnIndex = map[string]int{wd: -1, def: -1, sn: -1, qc: -1, qt: -1, nt: -1}
for i := 0; i < columnNumber; i++ {
switch contentTemp[0][i] {
case wd:
collection.columnIndex[wd] = i
break
case def:
collection.columnIndex[def] = i
break
case sn:
collection.columnIndex[sn] = i
break
case qc:
collection.columnIndex[qc] = i
break
case qt:
collection.columnIndex[qt] = i
break
case nt:
collection.columnIndex[nt] = i
default:
break
}
}
if collection.columnIndex[wd] == -1 {
collection.xlsx.SetCellValue(collection.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", wd)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if collection.columnIndex[def] == -1 {
collection.xlsx.SetCellValue(collection.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", def)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if collection.columnIndex[sn] == -1 {
collection.xlsx.SetCellValue(collection.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", sn)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if collection.columnIndex[qc] == -1 {
collection.xlsx.SetCellValue(collection.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", qc)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if collection.columnIndex[qt] == -1 {
collection.xlsx.SetCellValue(collection.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", qt)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if collection.columnIndex[nt] == -1 {
collection.xlsx.SetCellValue(collection.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", nt)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
return
}
// read data from .xlsx file and put to collection and collection
func (collection *collectionStruct) read(filePath string) (err error) {
var (
str string
vocabularyAnswer VocabularyAnswerStruct
)
if collection.readable {
// check
err = collection.check(filePath)
if err != nil {
return
}
// get space of content
collection.content = make([]VocabularyAnswerStruct, 0)
// ram image -> content
for i := 2; ; i++ {
// `xlsx:wd` -> .Word
str = collection.xlsx.GetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[wd]), i))
if strings.Compare(str, "") == 0 {
break
}
vocabularyAnswer.Word = str
// `xlsx:def` -> .Define
str = collection.xlsx.GetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[def]), i))
str = strings.TrimSpace(str)
vocabularyAnswer.Definition = strings.Split(str, "\n")
if len(vocabularyAnswer.Definition) == 1 &&
strings.Compare(vocabularyAnswer.Definition[0], "") == 0 {
vocabularyAnswer.Definition = nil
}
// `xlsx:sn` -> .SerialNumber
vocabularyAnswer.SerialNumber, err = strconv.Atoi(collection.xlsx.GetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[sn]), i)))
if err != nil {
vocabularyAnswer.SerialNumber = i - 1
}
// `xlsx:qc` -> .QueryCounter
vocabularyAnswer.QueryCounter, err = strconv.Atoi(collection.xlsx.GetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[qc]), i)))
if err != nil {
vocabularyAnswer.QueryCounter = 0
}
// reset err
err = nil
// `xlsx:qt` -> .QueryTime
vocabularyAnswer.QueryTime = collection.xlsx.GetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[qt]), i))
// `xlsx:nt` -> .Note
str = collection.xlsx.GetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[nt]), i))
str = strings.TrimSpace(str)
vocabularyAnswer.Note = strings.Split(str, "\n")
if len(vocabularyAnswer.Note) == 1 &&
strings.Compare(vocabularyAnswer.Note[0], "") == 0 {
vocabularyAnswer.Note = nil
}
// others
vocabularyAnswer.SourceName = collection.name
vocabularyAnswer.Location.TableType = Collection
vocabularyAnswer.Status = ""
// add to collection
collection.content = append(collection.content, vocabularyAnswer)
// set location
collection.content[len(collection.content)-1].Location.TableIndex = collection.index
collection.content[len(collection.content)-1].Location.ItemIndex = len(collection.content) - 1
}
}
return
}
// get data from collection and collection and write to .xlsx file
func (collection *collectionStruct) write() (information string, err error) {
if collection.readable && collection.writable {
// content -> ram image
for i := 0; i < len(collection.content); i++ {
// set row height
collection.xlsx.SetRowHeight(collection.sheetName, i+1, collection.xlsx.GetRowHeight(collection.sheetName, 0))
// .Word -> `xlsx:wd`
collection.xlsx.SetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[wd]), i+2), collection.content[i].Word)
// .Define -> `xlsx:def`
collection.xlsx.SetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[def]), i+2), strings.Join(collection.content[i].Definition, "\n"))
// .SerialNumber -> `xlsx:sn`
collection.xlsx.SetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[sn]), i+2), collection.content[i].SerialNumber)
// .QueryCounter -> `xlsx:qc`
collection.xlsx.SetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[qc]), i+2), collection.content[i].QueryCounter)
// .QueryTime -> `xlsx:qt`
collection.xlsx.SetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[qt]), i+2), collection.content[i].QueryTime)
// .Note -> `xlsx:nt`
collection.xlsx.SetCellValue(collection.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(collection.columnIndex[nt]), i+2), strings.Join(collection.content[i].Note, "\n"))
}
// ram image -> file
err = collection.xlsx.Save()
if err != nil {
return
}
// output
information = fmt.Sprintf("Collection \"%s\" has been updated.\n\n", collection.xlsx.Path)
}
return
}
// query and update
func (collection *collectionStruct) queryAndUpdate(vocabularyAsk VocabularyAskStruct) (vocabularyAnswerList []VocabularyAnswerStruct) {
var (
vocabularyAnswer VocabularyAnswerStruct
tm time.Time
)
if collection.readable {
for i := 0; i < len(collection.content); i++ {
// basic
if strings.Compare(collection.content[i].Word, vocabularyAsk.Word) == 0 {
if collection.writable {
// update collection or collection
if vocabularyAsk.DoNotRecord == false {
// update
tm = time.Now()
collection.content[i].QueryCounter++
collection.content[i].QueryTime = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second())
}
}
vocabularyAnswer = collection.content[i]
vocabularyAnswer.Status = Basic
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
if vocabularyAsk.Advance {
continue
} else {
break
}
}
if vocabularyAsk.Advance {
// advance
if strings.Contains(collection.content[i].Word, vocabularyAsk.Word) {
vocabularyAnswer = collection.content[i]
vocabularyAnswer.Status = Advance
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
goto ADVANCE_END
}
for j := 0; j < len(collection.content[i].Definition); j++ {
if strings.Contains(collection.content[i].Definition[j], vocabularyAsk.Word) {
vocabularyAnswer = collection.content[i]
vocabularyAnswer.Status = Advance
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
goto ADVANCE_END
}
}
for j := 0; j < len(collection.content[i].Note); j++ {
if strings.Contains(collection.content[i].Note[j], vocabularyAsk.Word) {
vocabularyAnswer = collection.content[i]
vocabularyAnswer.Status = Advance
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
goto ADVANCE_END
}
}
ADVANCE_END:
}
}
}
return
}
// add vocabulary to collection
func (collection *collectionStruct) add(vocabularyAnswerList []VocabularyAnswerStruct) {
var (
existent bool
index int
vocabularyAnswer VocabularyAnswerStruct
tm time.Time
)
if collection.readable && collection.writable {
// only available for collection which is readable and writable
existent = false
index = -1
for i := 0; i < len(vocabularyAnswerList); i++ {
if strings.Compare(vocabularyAnswerList[i].Status, Basic) == 0 {
// only for vocabulary with define from basic query
if vocabularyAnswerList[i].Location.TableType == Online {
// from online: check whether online source index match or not
if strings.Compare(vocabularyAnswerList[i].SourceName, collection.onlineSource) == 0 {
index = i
}
} else {
// from dictionary: check existence
existent = true
}
}
}
if existent == false && index != -1 {
// add to collection
vocabularyAnswer = vocabularyAnswerList[index]
// prepare
tm = time.Now()
vocabularyAnswer.SerialNumber = len(collection.content) + 1
vocabularyAnswer.QueryCounter = 1
vocabularyAnswer.QueryTime = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second())
vocabularyAnswer.SourceName = collection.name
vocabularyAnswer.Location.TableType = Collection
vocabularyAnswer.Status = ""
// add
collection.content = append(collection.content, vocabularyAnswer)
}
}
}