marketingFlag.js
5.45 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
// 活动标识:marketingFlag 为 1 时显示活动 icon
const SHOW_MARKETING_VALUE = 1
function findActiveMarketingInList(list) {
if (!Array.isArray(list)) return undefined
const activeGoods = list.find(
g => g.marketingFlag == SHOW_MARKETING_VALUE || g.marketingFlag === String(SHOW_MARKETING_VALUE)
)
return activeGoods?.marketingFlag
}
function findActiveActivityInList(list) {
if (!Array.isArray(list)) return undefined
const activeGoods = list.find(
g => g.marketingFlag == SHOW_MARKETING_VALUE || g.marketingFlag === String(SHOW_MARKETING_VALUE)
)
const activityId = activeGoods?.activityId
if (activityId === undefined || activityId === null || activityId === '') return undefined
return activityId
}
function hasActivityValue(value) {
return value != null && value !== ''
}
export function getMarketingFlag(item) {
if (!item) return undefined
if (item.marketingFlag !== undefined && item.marketingFlag !== null && item.marketingFlag !== '') {
return item.marketingFlag
}
const nested =
item.goods?.marketingFlag ??
item.packageGoods?.marketingFlag ??
item.multipleSpecificationsGoods?.marketingFlag ??
item.multipleSpecificationsGoods?.goods?.marketingFlag
if (nested !== undefined && nested !== null && nested !== '') {
return nested
}
const activeFromPackageGoods =
findActiveMarketingInList(item.packageGoods?.goodsList) ??
findActiveMarketingInList(item.packageGoodsOrderList) ??
findActiveMarketingInList(item.goodsList)
if (activeFromPackageGoods !== undefined) return activeFromPackageGoods
return undefined
}
export function shouldShowMarketingIcon(item, enabled = false) {
if (!enabled) return false
const flag = getMarketingFlag(item)
return flag == SHOW_MARKETING_VALUE || flag === String(SHOW_MARKETING_VALUE)
}
export function isMarketingGoods(goods, orderItem) {
const flag = getMarketingFlag(orderItem || { goods }) ?? goods?.marketingFlag
return flag == SHOW_MARKETING_VALUE || flag === String(SHOW_MARKETING_VALUE)
}
export function getActivityId(item) {
if (!item) return undefined
if (hasActivityValue(item.activityId)) {
return item.activityId
}
const nested =
item.goods?.activityId ??
item.packageGoods?.activityId ??
item.multipleSpecificationsGoods?.activityId ??
item.multipleSpecificationsGoods?.goods?.activityId
if (hasActivityValue(nested)) {
return nested
}
const activeFromPackageGoods =
findActiveActivityInList(item.packageGoods?.goodsList) ??
findActiveActivityInList(item.packageGoodsOrderList) ??
findActiveActivityInList(item.goodsList)
if (activeFromPackageGoods !== undefined) return activeFromPackageGoods
return undefined
}
export function getSubmitMarketingFlag(orderItem, goods) {
const flag = getMarketingFlag(orderItem) ?? goods?.marketingFlag
return flag == SHOW_MARKETING_VALUE || flag === String(SHOW_MARKETING_VALUE) ? 1 : 0
}
export function getSubmitActivityId(orderItem, goods) {
if (getSubmitMarketingFlag(orderItem, goods) !== 1) return undefined
const activityId = goods?.activityId ?? getActivityId(orderItem)
if (!hasActivityValue(activityId)) return undefined
return activityId
}
// 商品加入订单时快照商品列表 sellPrice,供活动价展示和 modifyFlag 对比
export function snapshotOriginalPrice(goods) {
if (!goods) return
if (goods.oldPrice != null && goods.oldPrice !== '') return
if (goods.sellPrice == null || goods.sellPrice === '') return
goods.oldPrice = goods.sellPrice
}
function findCatalogPackageGoods(catalogItem, merchantGoodsId) {
const list = catalogItem?.packageGoods?.goodsList
if (!Array.isArray(list) || merchantGoodsId == null) return undefined
return list.find(g => g.merchantGoodsId == merchantGoodsId)
}
// 订单回显时,从商品列表恢复活动价快照(不覆盖用户已改动的 sellPrice)
export function snapshotActivityPriceFromCatalog(goods, catalogItem) {
if (!goods || !catalogItem) return
if (goods.oldPrice != null && goods.oldPrice !== '') return
let catalogSellPrice
if (catalogItem.goodsFlag == 1 && catalogItem.goods) {
catalogSellPrice = catalogItem.goods.sellPrice
} else if (catalogItem.goodsFlag == 2) {
catalogSellPrice = catalogItem.multipleSpecificationsGoods?.goods?.sellPrice
} else if (catalogItem.goodsFlag == 3) {
catalogSellPrice = findCatalogPackageGoods(catalogItem, goods.merchantGoodsId)?.sellPrice
}
if (catalogSellPrice == null || catalogSellPrice === '') return
goods.oldPrice = catalogSellPrice
}
export function snapshotOrderItemOriginalPrice(item) {
if (!item) return
if (item.goodsFlag == 1 && item.goods) {
snapshotOriginalPrice(item.goods)
} else if (item.goodsFlag == 2 && item.multipleSpecificationsGoods?.goods) {
snapshotOriginalPrice(item.multipleSpecificationsGoods.goods)
} else if (item.goodsFlag == 3 && item.packageGoods?.goodsList) {
item.packageGoods.goodsList.forEach(snapshotOriginalPrice)
}
}
export function snapshotOrderItemsOriginalPrice(items) {
if (!Array.isArray(items)) return
items.forEach(snapshotOrderItemOriginalPrice)
}
export function getSubmitModifyFlag(goods) {
if (!goods) return 0
const current = parseFloat(goods.sellPrice ?? 0)
const originalSource = goods.oldPrice ?? goods.upgradePrice ?? goods.price
if (originalSource == null || originalSource === '') return 0
const original = parseFloat(originalSource)
if (Number.isNaN(current) || Number.isNaN(original)) return 0
return Math.abs(current - original) > 0.001 ? 1 : 0
}