groundingUpgradeList.js
4.82 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
/**
* merchantGoodsInfo.rule.selectList 返回的扁平 list(可与 merchantGoodsCode 独立的 commodityCode)
* → 分账「具体商品」下拉统一结构
* @param {Array<{upgradeGoodsId?: *, goodsShowName?: *, packageName?: *, merchantGoodsCode?: *, commodityCode?: *}>|undefined} list
* @returns {Array<{upgradeGoodsId: *, upgradeGoodsName: string, commodityCode: string, merchantGoodsCode: string}>}
*/
export function mapProductSearchListToUpgradeGoodsOptions(list) {
return (list || [])
.map((row) => {
const upgradeGoodsId = row && row.upgradeGoodsId
const goodsName =
row && row.goodsShowName != null ? String(row.goodsShowName) : ""
let pkg = ""
if (row && row.packageName != null) {
const t = String(row.packageName).trim()
if (t !== "" && t !== "null" && t !== "undefined") pkg = t
}
const upgradeGoodsName = pkg ? `${pkg}-${goodsName}` : goodsName
const rawCode = row && row.merchantGoodsCode
const merchantGoodsCode =
rawCode != null && String(rawCode).trim() !== ""
? String(rawCode).trim()
: ""
const commodityCode = trimCommodityCode(row)
return { upgradeGoodsId, upgradeGoodsName, commodityCode, merchantGoodsCode }
})
.filter((row) => row.merchantGoodsCode !== "")
}
/**
* 仅读取 row.commodityCode(trim),与 merchantGoodsCode 分离,不用商户码回填。
* @param {object|undefined} row
* @returns {string} trim 后非空则返回,否则 ""
*/
export function trimCommodityCode(row) {
if (!row || typeof row !== "object") return ""
if (row.commodityCode == null) return ""
const t = String(row.commodityCode).trim()
if (t === "" || t === "null" || t === "undefined") return ""
return t
}
/**
* @param {object|undefined} row
* @returns {string} trim 后非空则返回,否则 ""
*/
export function trimMerchantGoodsCode(row) {
if (!row || typeof row !== "object") return ""
if (row.merchantGoodsCode == null) return ""
const t = String(row.merchantGoodsCode).trim()
if (t === "" || t === "null" || t === "undefined") return ""
return t
}
/**
* 仅保留含非空 trim 后 merchantGoodsCode 的具体商品行(不依赖 commodityCode)。
* @param {Array|undefined} arr
* @returns {Array}
*/
export function filterUpgradeGoodsByMerchantCode(arr) {
return (arr || []).filter((row) => trimMerchantGoodsCode(row) !== "")
}
/**
* 具体商品行主键:仅 merchantGoodsCode(trim);无码返回 ""
* @param {object|undefined} row
* @returns {string}
*/
export function getUpgradeGoodsRowKey(row) {
const code = trimMerchantGoodsCode(row)
if (!code) return ""
return `c:${code}`
}
/**
* @param {object|undefined} a
* @param {object|undefined} b
* @returns {boolean}
*/
export function sameUpgradeGoodsRow(a, b) {
const ka = getUpgradeGoodsRowKey(a)
const kb = getUpgradeGoodsRowKey(b)
return ka !== "" && ka === kb
}
/**
* @deprecated 旧版 list 项含 upgradeGoodsList 嵌套;现具体商品下拉已改用 mapProductSearchListToUpgradeGoodsOptions
*/
export function flatMapUpgradeGoodsFromGoodsListPage(list) {
return (list || []).reduce((arr, val) => {
;(val.upgradeGoodsList || []).forEach((el) => {
const { upgradeGoodsName, id, merchantGoodsCode: mg } = el
const code = mg != null && String(mg).trim() !== "" ? String(mg).trim() : ""
if (!code) return
arr.push({
upgradeGoodsId: id,
upgradeGoodsName,
commodityCode: trimCommodityCode(el),
merchantGoodsCode: code
})
})
return arr
}, [])
}
/**
* selectList 分页:优先用 res.pages;缺失时按本页条数推断,避免 loadmore 死循环。
* @param {*} res 接口返回
* @param {number} currentPage 当前请求页(1-based)
* @param {number} pageSize
* @returns {number} 总页数(至少为 1)
*/
export function resolveProductSearchPages(res, currentPage, pageSize) {
if (res == null) return Math.max(1, currentPage || 1)
const p = res.pages
if (p != null && p !== "" && !Number.isNaN(Number(p)) && Number(p) >= 1) {
return Number(p)
}
const list = res.list || []
const cp = currentPage || 1
const ps = pageSize || 20
if (list.length === 0) return cp
if (list.length < ps) return cp
return cp + 1
}
export function uniqueUpgradeGoodsById(arr) {
const res = new Map()
return (arr || []).filter((a) => {
const k = getUpgradeGoodsRowKey(a)
if (!k) return false
return !res.has(k) && res.set(k, 1)
})
}
/**
* 套餐商品列表按 packageCode 去重,保留首次出现顺序(grounding 分页与下拉共用)。
* @param {Array<{packageCode: unknown}>|undefined} arr
* @returns {Array<{packageCode: unknown}>}
*/
export function uniquePackageByPackageCode(arr) {
const res = new Map()
return (arr || []).filter(
(a) => !res.has(a.packageCode) && res.set(a.packageCode, 1)
)
}