-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
220 lines (190 loc) · 4.04 KB
/
category.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
package main
import "log"
type categorymessage struct {
category *category
err error
action string
}
type category struct {
ID int
ParentID int
Name string
Slug string
SiteID int
Search string
Description string
CreatedByID int
DBAction int
}
func (c *category) getName() string {
return c.Name
}
func (c category) getEntityType() string {
return "category"
}
func (c *category) setSiteID(siteID int) {
c.SiteID = siteID
}
func (c *category) getDescription() string {
return c.Description
}
func (c *category) getSearchString() string {
return c.Search
}
func (c *category) getCategoryID() int {
return c.ID
}
func (c *category) getCreatedByID() int {
return c.CreatedByID
}
func (c *category) getDBAction() int {
return c.DBAction
}
func (c *category) setDBAction(action int) {
c.DBAction = action
}
func (c *category) setCreatedByID(createdById int) {
c.CreatedByID = createdById
}
func (c *category) getID() int {
return c.ID
}
func (c *category) setCategoryID(id int) {
c.ID = id
}
func (c *category) selectProducts(s *session) ([]categoryproduct, error) {
var categoryProducts []categoryproduct
rows, err := s.selectCategoryProductsByCategoryIDStmt.Query(c.ID)
if err != nil {
log.Println(err)
return categoryProducts, err
}
defer rows.Close()
for rows.Next() {
cp := categoryproduct{}
err := rows.Scan(
&cp.ID,
&cp.product.SiteID,
&cp.product.FeedID,
&cp.product.Name,
&cp.product.NameByUser,
&cp.product.Identifier,
&cp.product.Price,
&cp.product.RegularPrice,
&cp.product.Description,
&cp.product.DescriptionByUser,
&cp.product.Currency,
&cp.product.ProductURL,
&cp.product.GraphicURL,
&cp.product.ShippingPrice,
&cp.product.InStock,
&cp.product.Points,
&cp.product.HasCategories,
&cp.product.Active,
&cp.product.DeletedAt,
&cp.category.ID,
&cp.product.ID,
&cp.Forced,
)
if err != nil {
log.Println(err)
return categoryProducts, err
} else {
categoryProducts = append(categoryProducts, cp)
}
}
err = rows.Err()
return categoryProducts, err
}
func (c *category) insert(s *session) error {
_, err := s.db.Exec(
"INSERT INTO categories (name, slug, site_id, created_at, updated_at) "+
"VALUES (?,?,?,now(),now())",
c.Name,
c.Slug,
c.SiteID,
false,
)
return err
}
func (c *category) update(s *session) error {
var err error
return err
}
func (c *category) delete(s *session) error {
_, err := s.db.Exec("UPDATE categories SET deleted_at = NOW() WHERE id = ?", c.ID)
return err
}
func (c *category) syncProducts(s *session) error {
var err error
activeProducts, err := c.selectProducts(s)
if err != nil {
log.Println(err)
}
searchProducts := []product{}
log.Println(s.site.ID, c.Search)
rows, err := s.searchCategoryProductsStmt.Query(s.site.ID, c.Search)
if err != nil {
log.Println(err)
} else {
defer rows.Close()
for rows.Next() {
p := product{}
err := rows.Scan(
&p.ID,
&p.SiteID,
&p.FeedID,
&p.BrandID,
&p.NameByUser,
&p.Name,
&p.Slug,
&p.Identifier,
&p.Price,
&p.RegularPrice,
&p.DescriptionByUser,
&p.Description,
&p.Currency,
&p.ProductURL,
&p.GraphicURL,
&p.ShippingPrice,
&p.InStock,
&p.Points,
&p.HasCategories,
&p.Active,
&p.CreatedAt,
&p.UpdatedAt,
&p.DeletedAt,
)
if err != nil {
log.Println(err)
} else {
searchProducts = append(searchProducts, p)
}
}
for _, p := range searchProducts {
indexes := p.indexesOf(activeProducts)
if len(indexes) == 0 {
p.attachCategory(s, c)
}
}
for _, p := range activeProducts {
indexes := []int{}
for i, ele := range searchProducts {
if ele.ID == p.product.ID {
indexes = append(indexes, i)
}
}
if len(indexes) == 0 {
cp, err := p.selectCategoryProduct(s, c)
if err != nil {
log.Println(err)
}
if cp.Forced == false {
p.detachCategory(s, cp)
}
}
}
}
s.CategoryDone <- categorymessage{category: c, err: nil, action: "syncProducts"}
return err
}