-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
531 lines (436 loc) · 12.6 KB
/
repository.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package items
import (
"context"
"database/sql"
"fmt"
"regexp"
"sort"
sq "github.com/Masterminds/squirrel"
"github.com/autowp/goautowp/pictures"
"github.com/autowp/goautowp/util"
"github.com/doug-martin/goqu/v9"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
const (
TopBrandsCount = 150
NewDays = 7
TopPersonsCount = 5
TopFactoriesCount = 8
TopCategoriesCount = 15
TopTwinsBrandsCount = 20
)
type TreeItem struct {
ID int64
Name string
Childs []TreeItem
ItemType ItemType
}
var languagePriority = map[string][]string{
"xx": {"en", "it", "fr", "de", "es", "pt", "ru", "be", "uk", "zh", "jp", "xx"},
"en": {"en", "it", "fr", "de", "es", "pt", "ru", "be", "uk", "zh", "jp", "xx"},
"fr": {"fr", "en", "it", "de", "es", "pt", "ru", "be", "uk", "zh", "jp", "xx"},
"pt-br": {"pt", "en", "it", "fr", "de", "es", "ru", "be", "uk", "zh", "jp", "xx"},
"ru": {"ru", "en", "it", "fr", "de", "es", "pt", "be", "uk", "zh", "jp", "xx"},
"be": {"be", "ru", "uk", "en", "it", "fr", "de", "es", "pt", "zh", "jp", "xx"},
"uk": {"uk", "ru", "en", "it", "fr", "de", "es", "pt", "be", "zh", "jp", "xx"},
"zh": {"zh", "en", "it", "fr", "de", "es", "pt", "ru", "be", "uk", "jp", "xx"},
"es": {"es", "en", "it", "fr", "de", "pt", "ru", "be", "uk", "zh", "jp", "xx"},
}
type ItemType int
const (
VEHICLE ItemType = 1
ENGINE ItemType = 2
CATEGORY ItemType = 3
TWINS ItemType = 4
BRAND ItemType = 5
FACTORY ItemType = 6
MUSEUM ItemType = 7
PERSON ItemType = 8
COPYRIGHT ItemType = 9
)
// Repository Main Object.
type Repository struct {
db *goqu.Database
}
type Item struct {
ID int64
Catname string
Name string
ItemsCount int32
NewItemsCount int32
ChildItemsCount int32
NewChildItemsCount int32
DescendantsCount int32
NewDescendantsCount int32
}
// NewRepository constructor.
func NewRepository(
db *goqu.Database,
) *Repository {
return &Repository{
db: db,
}
}
type PicturesOptions struct {
Status pictures.Status
ItemPicture *ItemPicturesOptions
}
type ItemPicturesOptions struct {
TypeID pictures.ItemPictureType
Pictures *PicturesOptions
PerspectiveID int32
}
type ListPreviewPicturesPictureFields struct {
NameText bool
}
type ListPreviewPicturesFields struct {
Route bool
Picture ListPreviewPicturesPictureFields
}
type ListFields struct {
Name bool
NameHTML bool
NameDefault bool
Description bool
HasText bool
PreviewPictures ListPreviewPicturesFields
TotalPictures bool
ItemsCount bool
NewItemsCount bool
ChildItemsCount bool
NewChildItemsCount bool
DescendantsCount bool
NewDescendantsCount bool
}
type ListOptions struct {
Language string
Fields ListFields
TypeID []ItemType
DescendantPictures *ItemPicturesOptions
PreviewPictures *ItemPicturesOptions
Limit uint64
OrderBy string
SortByName bool
ChildItems *ListOptions
DescendantItems *ListOptions
ParentItems *ListOptions
AncestorItems *ListOptions
NoParents bool
}
func applyPicture(alias string, sqSelect sq.SelectBuilder, options *PicturesOptions) sq.SelectBuilder {
joinPicture := false
pAlias := alias + "_p"
if options.Status != "" {
joinPicture = true
sqSelect = sqSelect.Where(sq.Eq{pAlias + ".status": options.Status})
}
if options.ItemPicture != nil {
joinPicture = true
sqSelect = applyItemPicture(pAlias, sqSelect, options.ItemPicture)
}
if joinPicture {
sqSelect = sqSelect.Join("pictures AS " + pAlias + " ON " + alias + ".picture_id = " + pAlias + ".id")
}
return sqSelect
}
func applyItemPicture(alias string, sqSelect sq.SelectBuilder, options *ItemPicturesOptions) sq.SelectBuilder {
piAlias := alias + "_pi"
sqSelect = sqSelect.Join("picture_item AS " + piAlias + " ON " + alias + ".id = " + piAlias + ".item_id")
if options.TypeID != 0 {
sqSelect = sqSelect.Where(sq.Eq{piAlias + ".type": options.TypeID})
}
if options.PerspectiveID != 0 {
sqSelect = sqSelect.Where(sq.Eq{piAlias + ".perspective_id": options.PerspectiveID})
}
if options.Pictures != nil {
sqSelect = applyPicture(piAlias, sqSelect, options.Pictures)
}
return sqSelect
}
func applyItem(alias string, sqSelect sq.SelectBuilder, fields bool, options *ListOptions) (sq.SelectBuilder, error) {
var err error
if options.TypeID != nil && len(options.TypeID) > 0 {
sqSelect = sqSelect.Where(sq.Eq{alias + ".item_type_id": options.TypeID})
}
if options.DescendantPictures != nil {
sqSelect = applyItemPicture(alias, sqSelect, options.DescendantPictures)
}
ipcAlias := alias + "_ipc"
if options.ChildItems != nil {
iAlias := alias + "_ic"
sqSelect = sqSelect.
Join("item_parent AS " + ipcAlias + " ON " + alias + ".id = " + ipcAlias + ".parent_id").
Join("item AS " + iAlias + " ON " + ipcAlias + ".item_id = " + iAlias + ".id")
sqSelect, err = applyItem(iAlias, sqSelect, fields, options.ChildItems)
if err != nil {
return sqSelect, err
}
}
if options.ParentItems != nil {
iAlias := alias + "_ip"
ippAlias := alias + "_ipc"
sqSelect = sqSelect.
Join("item_parent AS " + ippAlias + " ON " + alias + ".id = " + ippAlias + ".item_id").
Join("item AS " + iAlias + " ON " + ippAlias + ".parent_id = " + iAlias + ".id")
sqSelect, err = applyItem(iAlias, sqSelect, fields, options.ParentItems)
if err != nil {
return sqSelect, err
}
}
if options.DescendantItems != nil {
ipcdAlias := alias + "_ipcd"
iAlias := alias + "_id"
sqSelect = sqSelect.
Join("item_parent_cache AS " + ipcdAlias + " ON " + alias + ".id = " + ipcdAlias + ".parent_id").
Join("item AS " + iAlias + " ON " + ipcdAlias + ".item_id = " + iAlias + ".id")
sqSelect, err = applyItem(iAlias, sqSelect, fields, options.DescendantItems)
if err != nil {
return sqSelect, err
}
}
if options.AncestorItems != nil {
ipcaAlias := alias + "_ipca"
iAlias := alias + "_ia"
sqSelect = sqSelect.
Join("item_parent_cache AS " + ipcaAlias + " ON " + alias + ".id = " + ipcaAlias + ".item_id").
Join("item AS " + iAlias + " ON " + ipcaAlias + ".parent_id = " + iAlias + ".id")
sqSelect, err = applyItem(iAlias, sqSelect, fields, options.AncestorItems)
if err != nil {
return sqSelect, err
}
}
if options.NoParents {
ipnpAlias := alias + "_ipnp"
sqSelect = sqSelect.
LeftJoin("item_parent AS " + ipnpAlias + " ON " + alias + ".id = " + ipnpAlias + ".item_id").
Where(ipnpAlias + ".parent_id IS NULL")
}
if fields {
if options.Fields.ChildItemsCount {
sqSelect = sqSelect.Column("count(distinct " + ipcAlias + ".item_id) AS child_items_count")
}
if options.Fields.NewChildItemsCount {
sqSelect = sqSelect.Column(
"count(distinct IF("+ipcAlias+".timestamp > DATE_SUB(NOW(), INTERVAL ? DAY), "+
ipcAlias+".item_id, NULL)) AS new_child_items_count",
NewDays,
)
}
if options.Fields.Name {
langPriority, ok := languagePriority[options.Language]
if !ok {
return sqSelect, fmt.Errorf("language `%s` not found", options.Language)
}
s := make([]interface{}, len(langPriority))
for i, v := range langPriority {
s[i] = v
}
sqSelect = sqSelect.Column(`
IFNULL(
(SELECT name
FROM item_language
WHERE item_id = `+alias+`.id AND length(name) > 0
ORDER BY FIELD(language, `+sq.Placeholders(len(s))+`)
LIMIT 1),
`+alias+`.name
) AS name
`, s...)
}
if options.Fields.ItemsCount {
sqSelect = sqSelect.Column("count(distinct " + alias + ".id) AS items_count")
}
if options.Fields.NewItemsCount {
sqSelect = sqSelect.Column(`
count(distinct if(`+alias+`.add_datetime > date_sub(NOW(), INTERVAL ? DAY), `+alias+`.id, null)) AS new_items_count
`, NewDays)
}
if options.Fields.DescendantsCount {
sqSelect = sqSelect.Column(`
(
SELECT count(distinct product1.id)
FROM item AS product1
JOIN item_parent_cache ON product1.id = item_parent_cache.item_id
WHERE item_parent_cache.parent_id = ` + alias + `.id
AND item_parent_cache.item_id <> item_parent_cache.parent_id
LIMIT 1
) AS descendants_count
`)
}
if options.Fields.NewDescendantsCount {
sqSelect = sqSelect.Column(`
(
SELECT count(distinct product2.id)
FROM item AS product2
JOIN item_parent_cache ON product2.id = item_parent_cache.item_id
WHERE item_parent_cache.parent_id = `+alias+`.id
AND item_parent_cache.item_id <> item_parent_cache.parent_id
AND product2.add_datetime > DATE_SUB(NOW(), INTERVAL ? DAY)
) AS new_descendants_count
`, NewDays)
}
}
return sqSelect, nil
}
func (s *Repository) Count(options ListOptions) (int, error) {
var err error
sqSelect := sq.Select("COUNT(1)").From("item AS i")
sqSelect, err = applyItem("i", sqSelect, false, &options)
if err != nil {
return 0, err
}
var count int
err = sqSelect.RunWith(s.db).QueryRow().Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (s *Repository) CountDistinct(options ListOptions) (int, error) {
var err error
sqSelect := sq.Select("COUNT(distinct i.id)").From("item AS i")
sqSelect, err = applyItem("i", sqSelect, false, &options)
if err != nil {
return 0, err
}
var count int
err = sqSelect.RunWith(s.db).QueryRow().Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (s *Repository) List(options ListOptions) ([]Item, error) {
/*langPriority, ok := languagePriority[options.Language]
if !ok {
return nil, fmt.Errorf("language `%s` not found", options.Language)
}*/
var err error
sqSelect := sq.Select("i.id", "i.catname").From("item AS i").GroupBy("i.id")
sqSelect, err = applyItem("i", sqSelect, true, &options)
if err != nil {
return nil, err
}
if len(options.OrderBy) > 0 {
sqSelect = sqSelect.OrderBy(options.OrderBy)
}
if options.Limit > 0 {
sqSelect = sqSelect.Limit(options.Limit)
}
rows, err := sqSelect.RunWith(s.db).Query()
if err != nil {
return nil, err
}
defer util.Close(rows)
columnNames, err := rows.Columns()
if err != nil {
return nil, err
}
var result []Item
for rows.Next() {
var r Item
var catname sql.NullString
pointers := make([]interface{}, len(columnNames))
for i, colName := range columnNames {
switch colName {
case "id":
pointers[i] = &r.ID
case "name":
pointers[i] = &r.Name
case "catname":
pointers[i] = &catname
case "items_count":
pointers[i] = &r.ItemsCount
case "new_items_count":
pointers[i] = &r.NewItemsCount
case "descendants_count":
pointers[i] = &r.DescendantsCount
case "new_descendants_count":
pointers[i] = &r.NewDescendantsCount
case "child_items_count":
pointers[i] = &r.ChildItemsCount
case "new_child_items_count":
pointers[i] = &r.NewChildItemsCount
default:
pointers[i] = nil
}
}
err = rows.Scan(pointers...)
if err != nil {
return nil, err
}
if catname.Valid {
r.Catname = catname.String
}
result = append(result, r)
}
if options.SortByName {
tag := language.English
switch options.Language {
case "ru":
tag = language.Russian
case "zh":
tag = language.SimplifiedChinese
case "fr":
tag = language.French
case "es":
tag = language.Spanish
case "uk":
tag = language.Ukrainian
case "be":
tag = language.Russian
case "pt-br":
tag = language.BrazilianPortuguese
}
cyrillic := regexp.MustCompile(`^\p{Cyrillic}`)
han := regexp.MustCompile(`^\p{Han}`)
cl := collate.New(tag, collate.IgnoreCase, collate.IgnoreDiacritics)
sort.SliceStable(result, func(i, j int) bool {
a := result[i].Name
b := result[j].Name
switch options.Language {
case "ru", "uk", "be":
aIsCyrillic := cyrillic.MatchString(a)
bIsCyrillic := cyrillic.MatchString(b)
if aIsCyrillic && !bIsCyrillic {
return true
}
if bIsCyrillic && !aIsCyrillic {
return false
}
case "zh":
aIsHan := han.MatchString(a)
bIsHan := han.MatchString(b)
if aIsHan && !bIsHan {
return true
}
if bIsHan && !aIsHan {
return false
}
}
return cl.CompareString(a, b) == -1
})
}
return result, nil
}
func (s *Repository) Tree(ctx context.Context, id string) (*TreeItem, error) {
type row struct {
ID int64 `db:"id"`
Name string `db:"name"`
ItemType ItemType `db:"item_type_id"`
}
var item row
success, err := s.db.Select("id", "name", "").From("item").
Where(goqu.I("id").Eq(id)).ScanStructContext(ctx, item)
if err != nil {
return nil, err
}
if !success {
return nil, nil //nolint: nilnil
}
return &TreeItem{
ID: item.ID,
Name: item.Name,
ItemType: item.ItemType,
}, nil
}