orderSubOrderExtras.js
5.4 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
/**
* @file 提单 subOrderList 扩展字段(marketingFlag / activityId / modifyFlag)
* 纯函数:不 setData、不访问页面实例
*/
import { isMarketingActive } from '../../utils/marketingFlag.js'
/**
* 记录目录原价 catalogSellPrice,供 modifyFlag 比对(不覆盖已有值)
* 优先取接口 upgradePrice,避免提交时用改价后的 sellPrice 覆盖导致 modifyFlag 恒为 0
* @param {object} goods 单品 / 规格 SKU / 套餐子商品
* @returns {object}
*/
export function ensureCatalogUpgradePrice(goods = {}) {
if (!goods || goods.catalogSellPrice != null) return goods
const catalogPrice =
goods.upgradePrice ??
goods.originalPrice ??
goods.sellPrice ??
goods.price
if (catalogPrice != null && catalogPrice !== '') {
goods.catalogSellPrice = catalogPrice
}
return goods
}
/**
* 活动商品写入审批弹层「活动价」快照(selectOrderGoods.list 的 sellPrice)
* 与改价后的 sellPrice 分离,避免 ensureCatalogUpgradePrice 落到 upgradePrice
* @param {object} goods 页面商品 / 套餐子项
* @param {object} [catalogGoods] 选品目录对应子项
* @returns {object}
*/
export function seedMarketingActivitySellPrice(goods = {}, catalogGoods) {
if (!goods) return goods
if (goods.activitySellPrice != null && goods.activitySellPrice !== '') {
return goods
}
const marketingFlag =
catalogGoods?.marketingFlag ?? goods.marketingFlag
const activityId = catalogGoods?.activityId ?? goods.activityId
if (!isMarketingActive(marketingFlag) || activityId == null || activityId === '') {
return goods
}
// 活动价快照不取改价后的 sellPrice,避免审批弹层活动价随低价提交变为 0
const activityPrice = catalogGoods
? (catalogGoods.sellPrice ?? catalogGoods.price)
: (goods.price ??
goods.catalogSellPrice ??
goods.upgradePrice ??
goods.sellPrice)
if (activityPrice != null && activityPrice !== '') {
goods.activitySellPrice = Number(activityPrice)
}
return goods
}
/**
* 多规格:为 specificationGoodsMap 内全部 SKU 写入活动价快照与目录价
* @param {object} multipleSpecificationsGoods
* @returns {object}
*/
export function seedMultiSpecSkuCatalogPrices(multipleSpecificationsGoods = {}) {
const map = multipleSpecificationsGoods.specificationGoodsMap
if (!map || typeof map !== 'object') return multipleSpecificationsGoods
Object.values(map).forEach(skuList => {
if (!Array.isArray(skuList)) return
skuList.forEach(sku => {
seedMarketingActivitySellPrice(sku)
ensureCatalogUpgradePrice(sku)
})
})
return multipleSpecificationsGoods
}
/**
* 再次编辑订单:用选品目录价写入 catalogSellPrice,避免订单回显价污染审批「活动价」
* @param {object} goods 订单回显商品
* @param {object} [catalogGoods] 选品目录 goods
* @returns {object}
*/
export function seedCatalogSellPriceFromCatalog(goods = {}, catalogGoods = {}) {
if (!goods) return goods
const catalogPrice =
// 活动商品优先取目录 sellPrice,避免取到目录原价(upgradePrice)
catalogGoods.sellPrice ??
catalogGoods.upgradePrice ??
catalogGoods.price
if (catalogPrice != null && catalogPrice !== '') {
goods.catalogSellPrice = Number(catalogPrice)
}
return goods
}
/**
* 解析提单子单活动标识:0-否,1-是
* @param {object} ctx
* @param {object} [ctx.lineItem] goodsList 行
* @param {object} [ctx.goods] 实际商品对象
* @returns {0|1}
*/
export function resolveSubmitMarketingFlag({ lineItem, goods } = {}) {
const raw =
goods?.marketingFlag ??
lineItem?.marketingFlag ??
lineItem?.goods?.marketingFlag ??
lineItem?.multipleSpecificationsGoods?.marketingFlag ??
lineItem?.multipleSpecificationsGoods?.goods?.marketingFlag ??
0
return isMarketingActive(raw) ? 1 : 0
}
/**
* 解析提单子单 activityId,与 marketingFlag 同源
* @param {object} ctx
* @param {object} [ctx.lineItem] goodsList 行
* @param {object} [ctx.goods] 实际商品对象
* @returns {*|null}
*/
export function resolveSubmitActivityId({ lineItem, goods } = {}) {
const raw =
goods?.activityId ??
lineItem?.activityId ??
lineItem?.goods?.activityId ??
lineItem?.multipleSpecificationsGoods?.activityId ??
lineItem?.multipleSpecificationsGoods?.goods?.activityId ??
lineItem?.packageGoods?.activityId ??
null
return raw != null && raw !== '' ? raw : null
}
/**
* 仅活动商品写入 activityId;无活动时不传该字段
* @param {object} subOrder subOrderList 子项
* @param {object} ctx 同 resolveSubmitActivityId
* @returns {object}
*/
export function appendSubmitActivityId(subOrder = {}, ctx = {}) {
if (resolveSubmitMarketingFlag(ctx) !== 1) return subOrder
const activityId = resolveSubmitActivityId(ctx)
if (activityId == null || activityId === '') return subOrder
subOrder.activityId = activityId
return subOrder
}
/**
* 解析商品价格修改标识:对比当前单价与目录价(basePrice/sellPrice vs catalogSellPrice/upgradePrice)
* @param {object} goods
* @returns {0|1}
*/
export function resolveSubmitModifyFlag(goods = {}) {
const currentPrice = goods.basePrice ?? goods.sellPrice ?? goods.price
const catalogPrice =
goods.catalogSellPrice ?? goods.upgradePrice ?? goods.originalPrice
if (currentPrice == null || catalogPrice == null) return 0
return Number(currentPrice) !== Number(catalogPrice) ? 1 : 0
}