-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopping.go
187 lines (176 loc) · 5.48 KB
/
shopping.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
package MallCoreV3
import (
BaseQiniu "github.com/fotomxq/weeekj_core/v5/base/qiniu"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
"gopkg.in/errgo.v2/fmt/errors"
"time"
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLPages "github.com/fotomxq/weeekj_core/v5/core/sql/pages"
)
// ArgsGetShoppingList 获取购物车列表参数
type ArgsGetShoppingList struct {
//分页
Pages CoreSQLPages.ArgsDataList `json:"pages"`
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id"`
}
// DataGetShoppingList 获取商品列表数据
type DataGetShoppingList struct {
//商品ID
ProductID int64 `db:"product_id" json:"productID"`
//商品key
ProductKey string `db:"product_key" json:"productKey" check:"mark" empty:"true"`
//创建时间
CreateAt time.Time `db:"create_at" json:"createAt"`
//商品删除时间
DeleteAt time.Time `json:"deleteAt"`
//商品封面
CoverFileURL string `json:"coverFileURL"`
//商品名称
ProductTitle string `json:"productTitle"`
//商品原价
Price int64 `json:"price"`
//折扣截止
PriceExpireAt time.Time `db:"price_expire_at" json:"priceExpireAt"`
//商品优惠价
RealPrice int64 `json:"realPrice"`
//购买件数
Count int64 `json:"count"`
//可用票据
UseTickets []int64 `json:"useTickets"`
}
// GetShoppingList 获取商品列表
func GetShoppingList(args *ArgsGetShoppingList) (dataList []DataGetShoppingList, dataCount int64, err error) {
where := "user_id = :user_id"
maps := map[string]interface{}{
"user_id": args.UserID,
}
var rawList []FieldsShopping
tableName := "mall_core_shopping"
dataCount, err = CoreSQL.GetListPageAndCount(
Router2SystemConfig.MainDB.DB,
&rawList,
tableName,
"id",
"SELECT id, create_at, product_id, product_key, count "+"FROM "+tableName+" WHERE "+where,
where,
maps,
&args.Pages,
[]string{"id", "create_at"},
)
if err != nil || len(rawList) < 1 {
err = errors.New("no data")
return
}
for _, v := range rawList {
appendData := DataGetShoppingList{
ProductID: v.ProductID,
ProductKey: v.ProductKey,
CreateAt: v.CreateAt,
DeleteAt: time.Time{},
CoverFileURL: "",
ProductTitle: "",
Price: 0,
PriceExpireAt: time.Time{},
RealPrice: 0,
Count: v.Count,
UseTickets: []int64{},
}
var productData FieldsProduct
productData, _ = GetProduct(&ArgsGetProduct{
ID: v.ProductID,
OrgID: -1,
})
if productData.ID > 0 {
appendData.DeleteAt = productData.DeleteAt
appendData.ProductTitle = productData.Title
if v.ProductKey == "" {
appendData.Price = productData.Price
appendData.PriceExpireAt = productData.PriceExpireAt
appendData.RealPrice = productData.PriceReal
} else {
isFind := false
for _, v2 := range productData.OtherOptions.DataList {
if v2.Key == v.ProductKey {
isFind = true
appendData.Price = v2.Price
appendData.PriceExpireAt = v2.PriceExpireAt
appendData.RealPrice = v2.PriceReal
break
}
}
if !isFind {
appendData.Price = productData.Price
appendData.PriceExpireAt = productData.PriceExpireAt
appendData.RealPrice = productData.PriceReal
}
}
appendData.UseTickets = productData.UserTicket
if len(productData.CoverFileIDs) > 0 {
appendData.CoverFileURL, _ = BaseQiniu.GetPublicURLStr(productData.CoverFileIDs[0])
}
}
dataList = append(dataList, appendData)
}
return
}
// ArgsAddShopping 添加购物车参数
type ArgsAddShopping struct {
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id"`
//商品ID
ProductID int64 `db:"product_id" json:"productID" check:"id"`
//商品key
ProductKey string `db:"product_key" json:"productKey" check:"mark" empty:"true"`
//数量
Count int64 `db:"count" json:"count"`
}
// AddShopping 添加购物车
func AddShopping(args *ArgsAddShopping) (err error) {
if args.Count < 1 {
args.Count = 0
}
if args.Count > 9999 {
args.Count = 9999
}
var id int64
err = Router2SystemConfig.MainDB.Get(&id, "SELECT id FROM mall_core_shopping WHERE user_id = $1 AND product_id = $2", args.UserID, args.ProductID)
if err == nil && id > 0 {
if args.Count < 1 {
return DeleteShopping(&ArgsDeleteShopping{
UserID: args.UserID,
ProductID: args.ProductID,
})
} else {
_, err = CoreSQL.UpdateOne(Router2SystemConfig.MainDB.DB, "UPDATE mall_core_shopping SET product_key = :product_key, count = :count WHERE id = :id", map[string]interface{}{
"id": id,
"product_key": args.ProductKey,
"count": args.Count,
})
return
}
}
_, err = CoreSQL.CreateOne(Router2SystemConfig.MainDB.DB, "INSERT INTO mall_core_shopping (user_id, product_id, product_key, count) VALUES (:user_id,:product_id,:product_key,:count)", args)
return
}
// ArgsDeleteShopping 删除购物车参数
type ArgsDeleteShopping struct {
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id"`
//商品ID
ProductID int64 `db:"product_id" json:"productID" check:"id"`
}
// DeleteShopping 删除购物车
func DeleteShopping(args *ArgsDeleteShopping) (err error) {
_, err = CoreSQL.DeleteAll(Router2SystemConfig.MainDB.DB, "mall_core_shopping", "user_id = :user_id AND product_id = :product_id", args)
return
}
// ArgsClearShopping 清空购物车参数
type ArgsClearShopping struct {
//用户ID
UserID int64 `db:"user_id" json:"userID" check:"id"`
}
func ClearShopping(args *ArgsClearShopping) (err error) {
_, err = CoreSQL.DeleteAll(Router2SystemConfig.MainDB.DB, "mall_core_shopping", "user_id = :user_id", args)
return
}