productStrategy.js
11.9 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* 商品操作策略模式实现
*
* 设计意图:
* 针对 createOrder 页面中存在的多种商品类型(普通商品、多规格商品、套餐商品),
* 采用策略模式(Strategy Pattern)统一封装商品的数据获取、价格计算、校验逻辑。
*
* 结构说明:
* - ProductStrategy (基类): 定义所有商品类型共有的接口(如 getData, getPrice, validatePrice 等)。
* - NormalProductStrategy: 处理普通商品 (item.goods)。
* - MultiSpecProductStrategy: 处理多规格商品 (item.multipleSpecificationsGoods)。
* - PackageProductStrategy: 处理套餐商品 (item.packageGoods),包含对子商品的处理逻辑。
* - getProductStrategy (工厂函数): 根据商品数据结构自动匹配对应的策略实例。
*/
import NP from 'number-precision'
export const ORDER_TYPE = {
NORMAL: 1,
DISCOUNT: 2,
GIFT: 3,
IMPULSE: 4,
COMBINATION: 5,
INTERNAL_SETTLEMENT: 6
}
export const DISCOUNT_TYPE = {
RATIO: 1,
AMOUNT: 2,
NONE: 3,
GIFT: 4,
NON_GIFT: 5
}
/**
* 商品策略基类
*/
function ProductStrategy(item) {
this.item = item
}
ProductStrategy.prototype.getData = function () {
return {}
}
ProductStrategy.prototype.getPrice = function () {
return 0
}
ProductStrategy.prototype.setPrice = function (price) {
// override
}
ProductStrategy.prototype.getMinPrice = function () {
const data = this.getData()
return data.minSellPrice || 0
}
ProductStrategy.prototype.getCount = function () {
return this.item.count || 1
}
ProductStrategy.prototype.calculateSubtotal = function () {
return NP.times(parseFloat(this.getCount()), parseFloat(this.getPrice()))
}
ProductStrategy.prototype.validatePrice = function (price) {
const checkPrice = price !== undefined ? price : this.getPrice()
const data = this.getData()
if (data.discountType === DISCOUNT_TYPE.GIFT) {
return { valid: true }
}
if (
data.validMinSellPrice === 'Y' &&
Number(checkPrice) < Number(data.minSellPrice)
) {
return { valid: false, message: '单价不可低于商品最低价' }
}
return { valid: true }
}
ProductStrategy.prototype.initBasePrice = function () {
const data = this.getData()
if (data.basePrice === undefined) {
data.basePrice = this.getPrice()
}
}
ProductStrategy.prototype.setBasePrice = function (price) {
const data = this.getData()
data.basePrice = price
this.setPrice(price)
this.applyDiscount()
}
ProductStrategy.prototype.getBasePrice = function () {
const data = this.getData()
// 优先使用 sellPrice (基础价格),避免 getPrice() 返回 finalPrice (最终价格) 导致校验错误
if (data.basePrice !== undefined) {
return data.basePrice
}
if (data.sellPrice !== undefined) {
return data.sellPrice
}
return this.getPrice()
}
ProductStrategy.prototype.resetDiscount = function () {
this.setDiscount(DISCOUNT_TYPE.NONE, 0)
}
ProductStrategy.prototype.setDiscount = function (type, value) {
const data = this.getData()
this.initBasePrice()
data.discountType = type
data.discountValue = value
this.applyDiscount()
}
ProductStrategy.prototype.applyDiscount = function () {
const data = this.getData()
const basePrice =
data.basePrice !== undefined ? data.basePrice : this.getPrice()
let finalPrice = basePrice
const type = data.discountType
const value = data.discountValue
if (type == DISCOUNT_TYPE.RATIO && value) {
if (value > 0 && value <= 10) {
finalPrice = NP.times(basePrice, NP.divide(value, 10))
// 折扣比例:结果向上取整到分(两位小数)
finalPrice = Math.ceil(finalPrice * 100) / 100
}
} else if (type == DISCOUNT_TYPE.AMOUNT && value) {
finalPrice = NP.minus(basePrice, value)
if (finalPrice < 0) finalPrice = 0
} else if (type == DISCOUNT_TYPE.GIFT) {
finalPrice = 0
} else if (type == DISCOUNT_TYPE.NONE || type == DISCOUNT_TYPE.NON_GIFT) {
finalPrice = basePrice
}
// this.setPrice(finalPrice)
data.finalPrice = finalPrice
data.discountAmount = NP.minus(basePrice, finalPrice)
}
ProductStrategy.prototype.setError = function (isError) {
const data = this.getData()
if (data) {
data.errorTarget = isError
data.checkError = isError
}
}
ProductStrategy.prototype.clearError = function () {
this.setError(false)
}
ProductStrategy.prototype.performValidation = function () {
// 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice())
// 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红
const result = this.validatePrice(this.getBasePrice())
this.setError(!result.valid)
const data = this.getData()
if (data.sellPrice !== undefined && data.validMinSellPrice === 'Y') {
if (Number(data.sellPrice) < Number(data.minSellPrice)) {
data.basePriceError = true
} else {
data.basePriceError = false
}
}
return result
}
/**
* 普通商品策略
*/
function NormalProductStrategy(item) {
ProductStrategy.call(this, item)
}
NormalProductStrategy.prototype = Object.create(ProductStrategy.prototype)
NormalProductStrategy.prototype.constructor = NormalProductStrategy
NormalProductStrategy.prototype.getData = function () {
return this.item.goods
}
NormalProductStrategy.prototype.getPrice = function () {
return this.item.goods.finalPrice !== undefined
? this.item.goods.finalPrice
: this.item.goods.sellPrice
}
NormalProductStrategy.prototype.setPrice = function (price) {
this.item.goods.sellPrice = price
}
/**
* 多规格商品策略
*/
function MultiSpecProductStrategy(item) {
ProductStrategy.call(this, item)
}
MultiSpecProductStrategy.prototype = Object.create(ProductStrategy.prototype)
MultiSpecProductStrategy.prototype.constructor = MultiSpecProductStrategy
MultiSpecProductStrategy.prototype.getData = function () {
return this.item.multipleSpecificationsGoods.goods
}
MultiSpecProductStrategy.prototype.getPrice = function () {
return this.item.multipleSpecificationsGoods.goods.finalPrice !== undefined
? this.item.multipleSpecificationsGoods.goods.finalPrice
: this.item.multipleSpecificationsGoods.goods.sellPrice
}
MultiSpecProductStrategy.prototype.setPrice = function (price) {
this.item.multipleSpecificationsGoods.goods.sellPrice = price
}
/**
* 套餐商品策略
*/
function PackageProductStrategy(item) {
ProductStrategy.call(this, item)
}
PackageProductStrategy.prototype = Object.create(ProductStrategy.prototype)
PackageProductStrategy.prototype.constructor = PackageProductStrategy
PackageProductStrategy.prototype.getData = function () {
return this.item.packageGoods
}
PackageProductStrategy.prototype.getPrice = function () {
return this.item.packageGoods.price
}
PackageProductStrategy.prototype.setPrice = function (price) {
this.item.packageGoods.price = price
}
PackageProductStrategy.prototype.calculateSubtotal = function () {
const packagePrice = this.item.packageGoods.goodsList.reduce((prev, cur) => {
const price = cur.finalPrice !== undefined ? cur.finalPrice : cur.price
return NP.plus(prev, price)
}, 0)
this.item.packageGoods.price = packagePrice
const total = NP.times(packagePrice, this.getCount())
this.item.totalPrice = packagePrice
return total
}
PackageProductStrategy.prototype.performValidation = function () {
let hasError = false
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach(subItem => {
// 优先校验 basePrice 或 sellPrice,避免因折扣导致的 finalPrice (即 price) 低于 minSellPrice 而报错
const checkPrice =
subItem.basePrice !== undefined
? subItem.basePrice
: subItem.sellPrice !== undefined
? subItem.sellPrice
: subItem.price
if (
subItem.validMinSellPrice === 'Y' &&
Number(checkPrice) < Number(subItem.minSellPrice)
) {
subItem.errorTarget = true
subItem.checkError = true
hasError = true
} else {
subItem.errorTarget = false
subItem.checkError = false
}
})
}
return { valid: !hasError, message: hasError ? '部分商品价格低于最低价' : '' }
}
PackageProductStrategy.prototype.clearError = function () {
ProductStrategy.prototype.clearError.call(this)
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach(subItem => {
subItem.errorTarget = false
subItem.checkError = false
})
}
}
PackageProductStrategy.prototype.getSubItemData = function (index) {
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
return this.item.packageGoods.goodsList[index]
}
return {}
}
PackageProductStrategy.prototype.resetDiscount = function () {
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach((subItem, index) => {
this.updateSubItemDiscount(index, DISCOUNT_TYPE.NONE, 0)
})
}
}
PackageProductStrategy.prototype.getSubItemBasePrice = function (index) {
if (
this.item.packageGoods &&
this.item.packageGoods.goodsList &&
this.item.packageGoods.goodsList[index]
) {
const subItem = this.item.packageGoods.goodsList[index]
if (subItem.basePrice === undefined) {
subItem.basePrice = subItem.price
}
return subItem.basePrice
}
return 0
}
PackageProductStrategy.prototype.updateSubItemPrice = function (index, price) {
if (
this.item.packageGoods &&
this.item.packageGoods.goodsList &&
this.item.packageGoods.goodsList[index]
) {
const subItem = this.item.packageGoods.goodsList[index]
if (subItem.basePrice === undefined) {
subItem.basePrice = subItem.price
}
subItem.basePrice = price
subItem.sellPrice = price
this.applySubItemDiscount(subItem)
}
}
PackageProductStrategy.prototype.updateSubItemDiscount = function (
index,
type,
value
) {
if (
this.item.packageGoods &&
this.item.packageGoods.goodsList &&
this.item.packageGoods.goodsList[index]
) {
const subItem = this.item.packageGoods.goodsList[index]
if (subItem.basePrice === undefined) {
subItem.basePrice = subItem.price
}
subItem.discountType = type
subItem.discountValue = value
this.applySubItemDiscount(subItem)
}
}
PackageProductStrategy.prototype.applySubItemDiscount = function (subItem) {
const basePrice = subItem.basePrice
let finalPrice = basePrice
const type = subItem.discountType
const value = subItem.discountValue
if (Number(basePrice) === 0) {
subItem.finalPrice = 0
subItem.discountAmount = 0
return
}
if (type == DISCOUNT_TYPE.RATIO && value) {
if (value > 0 && value <= 10) {
finalPrice = NP.times(basePrice, NP.divide(value, 10))
// 折扣比例:结果向上取整到分(两位小数)
finalPrice = Math.ceil(finalPrice * 100) / 100
}
} else if (type == DISCOUNT_TYPE.AMOUNT && value) {
finalPrice = NP.minus(basePrice, value)
if (finalPrice < 0) finalPrice = 0
} else if (type == DISCOUNT_TYPE.GIFT) {
finalPrice = 0
} else if (type == DISCOUNT_TYPE.NONE || type == DISCOUNT_TYPE.NON_GIFT) {
finalPrice = basePrice
}
// subItem.price = finalPrice
subItem.finalPrice = finalPrice
subItem.discountAmount = NP.minus(basePrice, finalPrice)
}
PackageProductStrategy.prototype.validateSubItemPrice = function (
subItem,
price
) {
if (subItem.discountType === DISCOUNT_TYPE.GIFT) {
return { valid: true }
}
if (
subItem.validMinSellPrice === 'Y' &&
Number(price) < Number(subItem.minSellPrice)
) {
return { valid: false, message: '单价不可低于商品最低价' }
}
return { valid: true }
}
export function getProductStrategy(item) {
if (!item) return null
if (item.goods) {
return new NormalProductStrategy(item)
} else if (item.packageGoods) {
return new PackageProductStrategy(item)
} else if (item.multipleSpecificationsGoods) {
return new MultiSpecProductStrategy(item)
}
return null
}