-
Notifications
You must be signed in to change notification settings - Fork 263
/
cart_repo.go
executable file
·309 lines (275 loc) · 7.78 KB
/
cart_repo.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
/**
* Copyright 2015 @ 56x.net.
* name : cart_repo.go
* author : jarryliu
* date : 2016-06-29 22:54
* description :
* history :
*/
package repos
import (
"database/sql"
"log"
"time"
cartImpl "github.com/ixre/go2o/core/domain/cart"
"github.com/ixre/go2o/core/domain/interface/cart"
"github.com/ixre/go2o/core/domain/interface/item"
"github.com/ixre/go2o/core/domain/interface/member"
"github.com/ixre/go2o/core/domain/interface/merchant"
"github.com/ixre/gof/db"
"github.com/ixre/gof/db/orm"
)
var _ cart.ICartRepo = new(cartRepo)
type cartRepo struct {
db.Connector
o orm.Orm
_itemRepo item.IItemRepo
_memberRepo member.IMemberRepo
_mchRepo merchant.IMerchantRepo
}
func NewCartRepo(o orm.Orm, memberRepo member.IMemberRepo,
_mchRepo merchant.IMerchantRepo,
itemRepo item.IItemRepo) cart.ICartRepo {
return &cartRepo{
Connector: o.Connector(),
o: o,
_memberRepo: memberRepo,
_mchRepo: _mchRepo,
_itemRepo: itemRepo,
}
}
// 获取买家的购物车
func (c *cartRepo) GetMyCart(buyerId int64, k cart.Kind) cart.ICart {
switch k {
case cart.KNormal:
return c.getMyNormalCart(buyerId)
case cart.KWholesale:
return c.getMyWholesaleCart(buyerId)
}
return nil
}
// 获取普通购物车
func (c *cartRepo) getMyNormalCart(buyerId int64) cart.ICart {
v := c.getNormalCart(buyerId)
if v != nil {
return c.CreateNormalCart(v)
}
return nil
}
// 获取批发购物车
func (c *cartRepo) getMyWholesaleCart(buyerId int64) cart.ICart {
v := c.getWholesaleCart(buyerId)
if v == nil {
unix := time.Now().Unix()
v = &cart.WsCart{
BuyerId: buyerId,
CreateTime: unix,
UpdateTime: unix,
}
}
return cartImpl.CreateWholesaleCart(v, c,
c._memberRepo, c._mchRepo, c._itemRepo)
}
func (c *cartRepo) getNormalCart(buyerId int64) *cart.NormalCart {
e := cart.NormalCart{}
err := c.o.GetBy(&e, "buyer_id= $1", buyerId)
if err == nil {
return &e
}
if err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCart")
}
return nil
}
func (w *cartRepo) getWholesaleCart(buyerId int64) *cart.WsCart {
e := cart.WsCart{}
err := w.o.GetBy(&e, "buyer_id= $1", buyerId)
if err == nil {
return &e
}
if err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:WsCart")
}
return nil
}
// 创建购物车对象
func (c *cartRepo) CreateNormalCart(v *cart.NormalCart) cart.ICart {
return cartImpl.NewNormalCart(v, c, c._memberRepo, c._itemRepo)
}
// 创建一个购物车
func (c *cartRepo) NewTempNormalCart(buyerId int, code string) cart.ICart {
return cartImpl.CreateTempNormalCart(buyerId, code, c, c._memberRepo, c._itemRepo)
}
// 获取购物车
func (c *cartRepo) GetNormalCart(id int32) cart.ICart {
v := c.getSaleCart(id)
if v != nil {
return c.CreateNormalCart(v)
}
return nil
}
// Get SaleCart
func (s *cartRepo) getSaleCart(primary interface{}) *cart.NormalCart {
e := cart.NormalCart{}
err := s.o.Get(primary, &e)
if err == nil {
return &e
}
if err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCart")
}
return nil
}
// Save SaleCart
func (s *cartRepo) SaveNormalCart(v *cart.NormalCart) (int, error) {
id, err := orm.Save(s.o, v, int(v.Id))
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCart")
}
return id, err
}
// Delete SaleCart
func (s *cartRepo) DeleteNormalCart(primary interface{}) error {
err := s.o.DeleteByPk(cart.NormalCart{}, primary)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCart")
}
return err
}
// Get SaleCartItem
func (s *cartRepo) GetSaleCartItem(primary interface{}) *cart.NormalCartItem {
e := cart.NormalCartItem{}
err := s.o.Get(primary, &e)
if err == nil {
return &e
}
if err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCartItem")
}
return nil
}
// Select SaleCartItem
func (s *cartRepo) SelectNormalCartItem(where string, v ...interface{}) []*cart.NormalCartItem {
list := []*cart.NormalCartItem{}
err := s.o.Select(&list, where, v...)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCartItem")
}
return list
}
// Save SaleCartItem
func (s *cartRepo) SaveNormalCartItem(v *cart.NormalCartItem) (int, error) {
id, err := orm.Save(s.o, v, int(v.Id))
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCartItem")
}
return id, err
}
// Delete SaleCartItem
func (s *cartRepo) DeleteNormalCartItem(primary interface{}) error {
err := s.o.DeleteByPk(cart.NormalCartItem{}, primary)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCartItem")
}
return err
}
// Batch Delete SaleCartItem
func (s *cartRepo) BatchDeleteNormalCartItem(where string, v ...interface{}) (int64, error) {
r, err := s.o.Delete(cart.NormalCartItem{}, where, v...)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:SaleCartItem")
}
return r, err
}
// 获取购物车
func (c *cartRepo) GetShoppingCartByKey(key string) cart.ICart {
ca := c.GetShoppingCart(key)
if ca != nil {
return c.CreateNormalCart(ca)
}
return nil
}
// 获取购物车
func (c *cartRepo) GetShoppingCart(key string) *cart.NormalCart {
var v = &cart.NormalCart{}
if c.o.GetBy(v, "code= $1", key) == nil {
var items []*cart.NormalCartItem
c.o.Select(&items, "cart_id= $1", v.Id)
v.Items = items
return v
}
return nil
}
// 获取最新的购物车
func (c *cartRepo) GetLatestCart(buyerId int64) *cart.NormalCart {
var v = &cart.NormalCart{}
if c.o.GetBy(v, "buyer_id= $1 ORDER BY id DESC", buyerId) == nil {
var items []*cart.NormalCartItem
c.o.Select(&items, "cart_id= $1", v.Id)
v.Items = items
return v
}
return nil
}
// 保存购物车
func (c *cartRepo) SaveShoppingCart(v *cart.NormalCart) (int32, error) {
return orm.I32(orm.Save(c.o, v, int(v.Id)))
}
// 移出购物车项
func (c *cartRepo) RemoveCartItem(id int32) error {
return c.o.DeleteByPk(cart.NormalCartItem{}, id)
}
// 保存购物车项
func (c *cartRepo) SaveCartItem(v *cart.NormalCartItem) (int32, error) {
return orm.I32(orm.Save(c.o, v, int(v.Id)))
}
// 清空购物车项
func (c *cartRepo) EmptyCartItems(cartId int32) error {
_, err := c.o.Delete(cart.NormalCartItem{}, "cart_id= $1", cartId)
return err
}
// 删除购物车
func (c *cartRepo) DeleteCart(cartId int32) error {
return c.o.DeleteByPk(cart.NormalCart{}, cartId)
}
// Save WsCart
func (w *cartRepo) SaveWsCart(v *cart.WsCart) (int, error) {
id, err := orm.Save(w.o, v, int(v.ID))
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:WsCart")
}
return id, err
}
// Delete WsCart
func (w *cartRepo) DeleteWsCart(primary interface{}) error {
err := w.o.DeleteByPk(cart.WsCart{}, primary)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:WsCart")
}
return err
}
// Select WsCartItem
func (w *cartRepo) SelectWsCartItem(where string, v ...interface{}) []*cart.WsCartItem {
var list []*cart.WsCartItem
err := w.o.Select(&list, where, v...)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:WsCartItem")
}
return list
}
// Save WsCartItem
func (w *cartRepo) SaveWsCartItem(v *cart.WsCartItem) (int, error) {
id, err := orm.Save(w.o, v, int(v.ID))
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:WsCartItem")
}
return id, err
}
// Batch Delete WsCartItem
func (w *cartRepo) BatchDeleteWsCartItem(where string, v ...interface{}) (int64, error) {
r, err := w.o.Delete(cart.WsCartItem{}, where, v...)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:WsCartItem")
}
return r, err
}