-
Notifications
You must be signed in to change notification settings - Fork 0
/
goods.go
288 lines (258 loc) · 7.24 KB
/
goods.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
/**
* Copyright 2015 @ z3q.net.
* name : sale_goods
* author : jarryliu
* date : -- :
* description :
* history :
*/
package sale
import (
"fmt"
"go2o/src/core/domain"
"go2o/src/core/domain/interface/promotion"
"go2o/src/core/domain/interface/sale"
"go2o/src/core/domain/interface/valueobject"
"time"
)
var _ sale.IGoods = new(SaleGoods)
var _ domain.IDomain = new(SaleGoods)
type SaleGoods struct {
_goods sale.IItem
_value *sale.ValueGoods
_saleRep sale.ISaleRep
_goodsRep sale.IGoodsRep
_promRep promotion.IPromotionRep
_sale sale.ISale
_latestSnapshot *sale.GoodsSnapshot
_levelPrices []*sale.MemberPrice
_promDescribes map[string]string
}
func NewSaleGoods(s sale.ISale, goods sale.IItem, value *sale.ValueGoods, rep sale.ISaleRep,
goodsRep sale.IGoodsRep, promRep promotion.IPromotionRep) sale.IGoods {
v := &SaleGoods{
_goods: goods,
_value: value,
_saleRep: rep,
_goodsRep: goodsRep,
_promRep: promRep,
_sale: s,
_latestSnapshot: nil,
}
return v.init()
}
func (this *SaleGoods) init() sale.IGoods {
this._value.Price = this._value.Price
this._value.SalePrice = this._goods.GetValue().SalePrice
this._value.PromPrice = this._goods.GetValue().SalePrice
return this
}
//获取领域对象编号
func (this *SaleGoods) GetDomainId() int {
return this._value.Id
}
// 获取货品
func (this *SaleGoods) GetItem() sale.IItem {
return this._goods
}
// 设置值
func (this *SaleGoods) GetValue() *sale.ValueGoods {
return this._value
}
// 获取包装过的商品信息
func (this *SaleGoods) GetPackedValue() *valueobject.Goods {
item := this.GetItem().GetValue()
gv := this.GetValue()
goods := &valueobject.Goods{
Item_Id: item.Id,
CategoryId: item.CategoryId,
Name: item.Name,
GoodsNo: item.GoodsNo,
Image: item.Image,
Price: item.Price,
SalePrice: item.SalePrice,
PromPrice: item.SalePrice,
GoodsId: this.GetDomainId(),
SkuId: gv.SkuId,
IsPresent: gv.IsPresent,
PromotionFlag: gv.PromotionFlag,
StockNum: gv.StockNum,
SaleNum: gv.SaleNum,
}
return goods
}
// 获取促销信息
func (this *SaleGoods) GetPromotions() []promotion.IPromotion {
var vp []*promotion.ValuePromotion = this._promRep.GetPromotionOfGoods(this.GetDomainId())
var proms []promotion.IPromotion = make([]promotion.IPromotion, len(vp))
for i, v := range vp {
proms[i] = this._promRep.CreatePromotion(v)
}
return proms
}
// 获取会员价销价
func (this *SaleGoods) GetLevelPrice(level int) (bool, float32) {
lvp := this.GetLevelPrices()
for _, v := range lvp {
if level == v.Level && v.Price < this._value.SalePrice {
return true, v.Price
}
}
return false, this._value.SalePrice
}
// 获取促销价
func (this *SaleGoods) GetPromotionPrice(level int) float32 {
b, price := this.GetLevelPrice(level)
if b {
return price
}
return this._value.SalePrice
}
// 获取促销描述
func (this *SaleGoods) GetPromotionDescribe() map[string]string {
if this._promDescribes == nil {
proms := this.GetPromotions()
this._promDescribes = make(map[string]string, len(proms))
for _, v := range proms {
key := v.TypeName()
if txt, ok := this._promDescribes[key]; !ok {
this._promDescribes[key] = v.GetValue().ShortName
} else {
this._promDescribes[key] = txt + ";" + v.GetValue().ShortName
}
// if v.Type() == promotion.TypeFlagCashBack {
// if txt, ok := this._promDescribes[key]; !ok {
// this._promDescribes[key] = v.GetValue().ShortName
// } else {
// this._promDescribes[key] = txt + ";" + v.GetValue().ShortName
// }
// } else if v.Type() == promotion.TypeFlagCoupon {
// if txt, ok := this._promDescribes[key]; !ok {
// this._promDescribes[key] = v.GetValue().ShortName
// } else {
// this._promDescribes[key] = txt + ";" + v.GetValue().ShortName
// }
// }
//todo: other promotion implement
}
}
return this._promDescribes
}
// 获取会员价
func (this *SaleGoods) GetLevelPrices() []*sale.MemberPrice {
if this._levelPrices == nil {
this._levelPrices = this._goodsRep.GetGoodsLevelPrice(this.GetDomainId())
}
return this._levelPrices
}
// 保存会员价
func (this *SaleGoods) SaveLevelPrice(v *sale.MemberPrice) (int, error) {
v.GoodsId = this.GetDomainId()
if this._value.SalePrice == v.Price {
if v.Id > 0 {
this._goodsRep.RemoveGoodsLevelPrice(v.Id)
}
return -1, nil
}
return this._goodsRep.SaveGoodsLevelPrice(v)
}
// 设置值
func (this *SaleGoods) SetValue(v *sale.ValueGoods) error {
this._value.IsPresent = v.IsPresent
this._value.SaleNum = v.SaleNum
this._value.StockNum = v.StockNum
this._value.SkuId = v.SkuId
//this._value.PromotionFlag = v.PromotionFlag
return nil
}
// 保存
func (this *SaleGoods) Save() (int, error) {
id, err := this._goodsRep.SaveValueGoods(this._value)
if err == nil {
_, err = this.GenerateSnapshot()
}
this._value.Id = id
return id, err
//todo: save promotion
// return id,err
}
// 生成快照
func (this *SaleGoods) GenerateSnapshot() (int, error) {
v := this._value
gi := this.GetItem()
gv := gi.GetValue()
if v.Id <= 0 {
return -1, sale.ErrNoSuchGoods
}
if !gi.IsOnShelves() {
return -1, sale.ErrNotOnShelves
}
partnerId := this._sale.GetAggregateRootId()
unix := time.Now().Unix()
cate := this._saleRep.GetCategory(partnerId, gv.CategoryId)
var gsn *sale.GoodsSnapshot = &sale.GoodsSnapshot{
Key: fmt.Sprintf("%d-g%d-%d", partnerId, v.Id, unix),
ItemId: gv.Id,
GoodsId: this.GetDomainId(),
GoodsName: gv.Name,
GoodsNo: gv.GoodsNo,
SmallTitle: gv.SmallTitle,
CategoryName: cate.Name,
Image: gv.Image,
Cost: gv.Cost,
SalePrice: gv.SalePrice,
Price: this._value.Price,
CreateTime: unix,
}
if this.isNewSnapshot(gsn) {
this._latestSnapshot = gsn
return this._saleRep.SaveSnapshot(gsn)
}
return 0, sale.ErrLatestSnapshot
}
// 更新销售数量
func (this *SaleGoods) AddSaleNum(quantity int) error {
// 减去库存
if quantity > 0 {
if quantity > this._value.StockNum {
return sale.ErrOutOfStock
}
this._value.StockNum -= quantity
this._value.SaleNum += quantity
_, err := this.Save()
return err
}
return sale.ErrGoodsNum
}
// 取消销售
func (this *SaleGoods) CancelSale(quantity int, orderNo string) error {
// 减去库存
if quantity > 0 {
this._value.StockNum += quantity
this._value.SaleNum -= quantity
_, err := this.Save()
return err
}
return sale.ErrGoodsNum
}
// 是否为新快照,与旧有快照进行数据对比
func (this *SaleGoods) isNewSnapshot(gsn *sale.GoodsSnapshot) bool {
latestGsn := this.GetLatestSnapshot()
if latestGsn != nil {
return latestGsn.GoodsName != gsn.GoodsName ||
latestGsn.SmallTitle != gsn.SmallTitle ||
latestGsn.CategoryName != gsn.CategoryName ||
latestGsn.Image != gsn.Image ||
latestGsn.Cost != gsn.Cost ||
latestGsn.Price != gsn.Price ||
latestGsn.SalePrice != gsn.SalePrice
}
return true
}
// 获取最新的快照
func (this *SaleGoods) GetLatestSnapshot() *sale.GoodsSnapshot {
if this._latestSnapshot == nil {
this._latestSnapshot = this._saleRep.GetLatestGoodsSnapshot(this.GetDomainId())
}
return this._latestSnapshot
}