/
product_update.go
350 lines (340 loc) · 13.8 KB
/
product_update.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
package MallCore
import (
"errors"
"fmt"
BaseConfig "github.com/fotomxq/weeekj_core/v5/base/config"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLAddress "github.com/fotomxq/weeekj_core/v5/core/sql/address"
CoreSQLConfig "github.com/fotomxq/weeekj_core/v5/core/sql/config"
ERPProductMod "github.com/fotomxq/weeekj_core/v5/erp/product/mod"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
"github.com/lib/pq"
"time"
)
// ArgsUpdateProduct 修改商品参数
type ArgsUpdateProduct struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
// -1 跳过
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//是否为虚拟商品
// 不会发生配送处理
IsVirtual bool `db:"is_virtual" json:"isVirtual" check:"bool"`
//排序
Sort int `db:"sort" json:"sort"`
//分类ID
SortID int64 `db:"sort_id" json:"sortID" check:"id"`
//标签ID列
Tags pq.Int64Array `db:"tags" json:"tags" check:"ids" empty:"true"`
//商品条形码编码
Code string `db:"code" json:"code"`
//标题
Title string `db:"title" json:"title" check:"title" min:"1" max:"300"`
//副标题
TitleDes string `db:"title_des" json:"titleDes" check:"title" min:"1" max:"300" empty:"true"`
//商品描述
Des string `db:"des" json:"des" check:"des" min:"1" max:"6000" empty:"true"`
//封面ID
CoverFileIDs pq.Int64Array `db:"cover_file_ids" json:"coverFileIDs" check:"ids" empty:"true"`
//描述图组
DesFiles pq.Int64Array `db:"des_files" json:"desFiles" check:"ids" empty:"true"`
// 货币
Currency int `db:"currency" json:"currency" check:"currency"`
// 实际费用
PriceReal int64 `db:"price_real" json:"priceReal" check:"price"`
//折扣截止
PriceExpireAt string `db:"price_expire_at" json:"priceExpireAt" check:"isoTime" empty:"true"`
//折扣前费用
Price int64 `db:"price" json:"price" check:"price"`
//不含税价格
PriceNoTax int64 `db:"price_no_tax" json:"priceNoTax" check:"price" empty:"true"`
//积分价格
Integral int64 `db:"integral" json:"integral" check:"price" empty:"true"`
//积分最多抵扣费用
IntegralPrice int64 `db:"integral_price" json:"integralPrice" check:"price" empty:"true"`
//积分包含配送费免费
IntegralTransportFree bool `db:"integral_transport_free" json:"integralTransportFree"`
//会员价格
// 会员配置分平台和商户,平台会员需参与活动才能使用,否则将禁止设置和后期使用
UserSubPrice FieldsUserSubPrices `db:"user_sub_price" json:"userSubPrice"`
//票据
// 可以使用的票据列,具体的配置在票据配置内进行设置
// 票据分平台和商户,平台票据需参与活动才能使用,否则将自动禁止设置和后期使用
UserTicket pq.Int64Array `db:"user_ticket" json:"userTicket" check:"ids" empty:"true"`
//配送费计费模版ID
TransportID int64 `db:"transport_id" json:"transportID" check:"id" empty:"true"`
//唯一送货地址
Address CoreSQLAddress.FieldsAddress `db:"address" json:"address"`
//关联仓库的产品
WarehouseProductID int64 `db:"warehouse_product_id" json:"warehouseProductID" check:"id" empty:"true"`
//货物重量
Weight int `db:"weight" json:"weight" check:"intThan0" empty:"true"`
//总库存
Count int `db:"count" json:"count" check:"intThan0" empty:"true"`
//关联附加选项
OtherOptions DataOtherOptions `db:"other_options" json:"otherOptions"`
//给与票据列
// 和赠礼区别在于,赠礼不可退,此票据会跟随订单取消设置是否退还
GivingTickets FieldsGivingTickets `db:"giving_tickets" json:"givingTickets"`
//扩展参数
Params CoreSQLConfig.FieldsConfigsType `db:"params" json:"params"`
//是否同步ERP产品
SyncERPProduct bool `json:"syncERPProduct" check:"bool"`
}
// UpdateProduct 修改商品
func UpdateProduct(args *ArgsUpdateProduct) (errCode string, err error) {
//处理时间
var priceExpireAt time.Time
if args.PriceExpireAt != "" {
priceExpireAt, err = CoreFilter.GetTimeByISO(args.PriceExpireAt)
if err != nil {
errCode = "price_expire_at"
return
}
}
//计算其他选项
var otherOptions FieldsOtherOption
otherOptions, err = args.OtherOptions.GetFields()
if err != nil {
errCode = "other_expire_at"
return
}
if len(otherOptions.DataList) > 0 {
var mallProductOtherOptionsMax int64
mallProductOtherOptionsMax, err = BaseConfig.GetDataInt64("MallProductOtherOptionsMax")
if err != nil {
mallProductOtherOptionsMax = 0
}
if mallProductOtherOptionsMax > 0 {
if int64(len(otherOptions.DataList)) > mallProductOtherOptionsMax {
errCode = "other_option_too_many"
err = errors.New("other option too many")
return
}
}
}
//获取原始数据
var oldData FieldsCore
oldData, err = GetProduct(&ArgsGetProduct{
ID: args.ID,
OrgID: args.OrgID,
})
if err != nil {
errCode = "not_exist"
err = errors.New("product not exist")
return
}
//检查当前数据是否为上级了
if oldData.ParentID > 0 {
errCode = "is_history"
err = errors.New("data have parent")
return
}
//修改商品并回到等待提交状态
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), is_virtual = :is_virtual, sort = :sort, publish_at = TO_TIMESTAMP(0), sort_id = :sort_id, tags = tags, code = :code, title = :title, title_des = :title_des, des = :des, cover_file_ids = :cover_file_ids, des_files = :des_files, currency = :currency, price_real = :price_real, price_expire_at = :price_expire_at, price = :price, price_no_tax = :price_no_tax, integral = :integral, integral_price = :integral_price, integral_transport_free = :integral_transport_free, user_sub_price = :user_sub_price, user_ticket = :user_ticket, transport_id = :transport_id, address = :address, warehouse_product_id = :warehouse_product_id, weight = :weight, count = :count, other_options = :other_options, giving_tickets = :giving_tickets, params = :params WHERE id = :id AND (:org_id < 1 OR org_id = :org_id)", map[string]interface{}{
"is_virtual": args.IsVirtual,
"sort": args.Sort,
"sort_id": args.SortID,
"tags": args.Tags,
"code": args.Code,
"title": args.Title,
"title_des": args.TitleDes,
"des": args.Des,
"cover_file_ids": args.CoverFileIDs,
"des_files": args.DesFiles,
"currency": args.Currency,
"price_real": args.PriceReal,
"price_expire_at": priceExpireAt,
"price": args.Price,
"price_no_tax": args.PriceNoTax,
"integral": args.Integral,
"integral_price": args.IntegralPrice,
"integral_transport_free": args.IntegralTransportFree,
"user_sub_price": args.UserSubPrice,
"user_ticket": args.UserTicket,
"transport_id": args.TransportID,
"address": args.Address,
"warehouse_product_id": args.WarehouseProductID,
"weight": args.Weight,
"count": args.Count,
"other_options": otherOptions,
"giving_tickets": args.GivingTickets,
"params": args.Params,
"id": args.ID,
"org_id": args.OrgID,
})
if err == nil {
_, err = CoreSQL.CreateOne(Router2SystemConfig.MainDB.DB, "INSERT INTO mall_core (create_at, update_at, sort, parent_id, org_id, product_type, is_virtual, sort_id, tags, code, title, title_des, des, cover_file_ids, des_files, currency, price_real, price_expire_at, price, price_no_tax, integral, integral_price, integral_transport_free, user_sub_price, user_ticket, transport_id, address, warehouse_product_id, weight, count, buy_count, other_options, giving_tickets, params) VALUES (:create_at, :update_at, :sort, :parent_id, :org_id, :product_type, :is_virtual, :sort_id, :tags, :code, :title, :title_des, :des, :cover_file_ids, :des_files, :currency, :price_real, :price_expire_at, :price, :price_no_tax, :integral, :integral_price, :integral_transport_free, :user_sub_price, :user_ticket, :transport_id, :address, :warehouse_product_id, :weight, :count, :buy_count, :other_options, :giving_tickets, :params)", map[string]interface{}{
"create_at": oldData.CreateAt,
"update_at": CoreFilter.GetNowTime(),
"sort": oldData.Sort,
"parent_id": oldData.ID,
"org_id": oldData.OrgID,
"product_type": oldData.ProductType,
"is_virtual": oldData.IsVirtual,
"sort_id": oldData.SortID,
"tags": oldData.Tags,
"code": args.Code,
"title": oldData.Title,
"title_des": oldData.TitleDes,
"des": oldData.Des,
"cover_file_ids": oldData.CoverFileIDs,
"des_files": oldData.DesFiles,
"currency": oldData.Currency,
"price_real": oldData.PriceReal,
"price_expire_at": oldData.PriceExpireAt,
"price": oldData.Price,
"price_no_tax": oldData.PriceNoTax,
"integral": oldData.Integral,
"integral_price": oldData.IntegralPrice,
"integral_transport_free": oldData.IntegralTransportFree,
"user_sub_price": oldData.UserSubPrice,
"user_ticket": oldData.UserTicket,
"transport_id": oldData.TransportID,
"address": oldData.Address,
"warehouse_product_id": oldData.WarehouseProductID,
"weight": oldData.Weight,
"count": oldData.Count,
"buy_count": oldData.BuyCount,
"other_options": oldData.OtherOptions,
"giving_tickets": oldData.GivingTickets,
"params": oldData.Params,
})
if err != nil {
errCode = "insert"
return
}
} else {
errCode = "update"
return
}
//删除缓冲
deleteProductCache(args.ID)
if args.SortID > 0 {
deleteProductSortCache(args.SortID)
}
if oldData.SortID > 0 {
deleteProductSortCache(oldData.SortID)
}
//同步ERP产品
if args.SyncERPProduct {
newData := getProductByID(args.ID)
if newData.ID > 0 && newData.WarehouseProductID > 0 {
ERPProductMod.UpdateProduct(ERPProductMod.ArgsUpdateProduct{
ID: newData.WarehouseProductID,
Title: newData.Title,
TitleDes: newData.TitleDes,
Des: newData.Des,
CoverFileIDs: newData.CoverFileIDs,
Weight: newData.Weight,
Price: newData.Price,
})
}
}
//反馈
return
}
// ArgsUpdateProductCount 修改库存参数
type ArgsUpdateProductCount struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
// -1 跳过
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//库存数量
Count int `db:"count" json:"count" check:"intThan0" empty:"true"`
}
// UpdateProductCount 修改库存
func UpdateProductCount(args *ArgsUpdateProductCount) (err error) {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), count = :count WHERE id = :id AND (:org_id < 1 OR org_id = :org_id)", args)
if err != nil {
return
}
//删除缓冲
deleteProductCache(args.ID)
//反馈
return
}
// UpdateProductAddCount 修改库存
func UpdateProductAddCount(productID int64, optionKey string, count int) (err error) {
//获取商品
productData := getProductByID(productID)
//如果存在选项
if len(productData.OtherOptions.DataList) > 0 {
for k, v := range productData.OtherOptions.DataList {
if v.Key != optionKey {
continue
}
productData.OtherOptions.DataList[k].Count += count
if productData.OtherOptions.DataList[k].Count < 0 {
err = errors.New("no count")
return
}
}
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), other_options = :other_options WHERE id = :id", map[string]interface{}{
"id": productID,
"other_options": productData.OtherOptions,
})
if err != nil {
return
}
} else {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), count = count + :count WHERE id = :id AND 0 <= count + :count", map[string]interface{}{
"id": productID,
"count": count,
})
if err != nil {
return
}
}
//删除缓冲
deleteProductCache(productID)
//反馈
return
}
// ArgsUpdateProductPublish 发布商品参数
type argsUpdateProductPublish struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
}
// UpdateProductPublish 发布商品
func updateProductPublish(args *argsUpdateProductPublish) (err error) {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), publish_at = NOW() WHERE id = :id", args)
if err != nil {
return
}
//删除缓冲
deleteProductCache(args.ID)
//反馈
return
}
// UpdateProductPublishDown 下架产品
func UpdateProductPublishDown(orgID int64, productID int64) (err error) {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), publish_at = to_timestamp(0) WHERE id = :id AND (:org_id < 1 OR org_id = :org_id)", map[string]interface{}{
"id": productID,
"org_id": orgID,
})
if err != nil {
return
}
//删除缓冲
deleteProductCache(productID)
//反馈
return
}
func UpdateProductBuy(productID int64, count int) (err error) {
_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), buy_count = buy_count + :count WHERE id = :id", map[string]interface{}{
"id": productID,
"count": count,
})
if err != nil {
err = errors.New(fmt.Sprint("update product buy count, id: ", productID, ", count: ", count, ", err: ", err))
return
}
//删除缓冲
deleteProductCache(productID)
//反馈
return
}