优化寄售仓库现货统计与可匹配矩阵
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
export type ConsignmentStockSpecSummaryItem = {
|
||||
system: string
|
||||
series: string
|
||||
diamond_level: number
|
||||
quantity: number
|
||||
}
|
||||
|
||||
export type ConsignmentStockSpecMatrixCell = {
|
||||
diamond_level: number
|
||||
item?: ConsignmentStockSpecSummaryItem
|
||||
}
|
||||
|
||||
export type ConsignmentStockSpecMatrixRow = {
|
||||
system: string
|
||||
series: string
|
||||
cells: ConsignmentStockSpecMatrixCell[]
|
||||
}
|
||||
|
||||
export const CONSIGNMENT_STOCK_SERIES_ORDER = ['暗潮', '怒嚎', '血宴'] as const
|
||||
export const CONSIGNMENT_STOCK_SYSTEM_ORDER = ['苹果', '安卓'] as const
|
||||
export const CONSIGNMENT_STOCK_DIAMOND_LEVELS = [1, 2, 3, 4, 5, 6, 7] as const
|
||||
|
||||
const UNKNOWN_SPEC_LABEL = '未注明'
|
||||
|
||||
function normalizeText(value: unknown) {
|
||||
const text = String(value ?? '').trim()
|
||||
return text || UNKNOWN_SPEC_LABEL
|
||||
}
|
||||
|
||||
function formatDiamondLevel(value: unknown) {
|
||||
const level = Number(value)
|
||||
return Number.isFinite(level) && level > 0 ? `${Math.round(level)}钻` : '未知钻级'
|
||||
}
|
||||
|
||||
/** 现货汇总标签:系统 + 系列 + 钻级;璀璨数和区服不参与。 */
|
||||
export function formatConsignmentStockSpecLabel(item: ConsignmentStockSpecSummaryItem) {
|
||||
return `${normalizeText(item.system)}${normalizeText(item.series)}${formatDiamondLevel(
|
||||
item.diamond_level,
|
||||
)}`
|
||||
}
|
||||
|
||||
/** 页面展示格式,例如「安卓怒嚎3钻-2件」。 */
|
||||
export function formatConsignmentStockSpecSummary(item: ConsignmentStockSpecSummaryItem) {
|
||||
const quantity = Math.max(0, Math.round(Number(item.quantity) || 0))
|
||||
return `${formatConsignmentStockSpecLabel(item)}-${quantity}件`
|
||||
}
|
||||
|
||||
/** 汇总总件数,用于与顶部「在库数量」做同口径校验。 */
|
||||
export function sumConsignmentStockSpecQuantity(
|
||||
items: ConsignmentStockSpecSummaryItem[] | undefined,
|
||||
) {
|
||||
return (items || []).reduce(
|
||||
(total, item) => total + Math.max(0, Math.round(Number(item.quantity) || 0)),
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
export function isSameConsignmentStockSpec(
|
||||
left: ConsignmentStockSpecSummaryItem | null | undefined,
|
||||
right: ConsignmentStockSpecSummaryItem | null | undefined,
|
||||
) {
|
||||
if (!left || !right) return false
|
||||
return (
|
||||
normalizeText(left.system) === normalizeText(right.system) &&
|
||||
normalizeText(left.series) === normalizeText(right.series) &&
|
||||
Number(left.diamond_level) === Number(right.diamond_level)
|
||||
)
|
||||
}
|
||||
|
||||
/** 点击现货汇总后用于过滤下方列表;璀璨数和区服不进入筛选条件。 */
|
||||
export function buildConsignmentStockSpecFilter(item: ConsignmentStockSpecSummaryItem) {
|
||||
return {
|
||||
system: { value: normalizeText(item.system), match: 'exact' as const },
|
||||
series: { value: normalizeText(item.series), match: 'exact' as const },
|
||||
diamondLevel: {
|
||||
value: String(Math.round(Number(item.diamond_level) || 0)),
|
||||
match: 'exact' as const,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function sortByKnownOrder(values: string[], order: readonly string[]) {
|
||||
const rank = new Map(order.map((value, index) => [value, index]))
|
||||
return values.sort((left, right) => {
|
||||
const leftRank = rank.get(left)
|
||||
const rightRank = rank.get(right)
|
||||
if (leftRank !== undefined || rightRank !== undefined) {
|
||||
return (leftRank ?? Number.MAX_SAFE_INTEGER) - (rightRank ?? Number.MAX_SAFE_INTEGER)
|
||||
}
|
||||
return left.localeCompare(right, 'zh-CN')
|
||||
})
|
||||
}
|
||||
|
||||
/** 按图片结构生成矩阵:系统分组(苹果在前)→ 系列(暗潮/怒嚎/血宴)→ 1~7 钻。 */
|
||||
export function buildConsignmentStockSpecMatrix(
|
||||
items: ConsignmentStockSpecSummaryItem[] | undefined,
|
||||
): ConsignmentStockSpecMatrixRow[] {
|
||||
const normalizedItems = (items || []).map((item) => ({
|
||||
...item,
|
||||
system: normalizeText(item.system),
|
||||
series: normalizeText(item.series),
|
||||
diamond_level: Math.round(Number(item.diamond_level) || 0),
|
||||
}))
|
||||
const systems = sortByKnownOrder(
|
||||
[...new Set(normalizedItems.map((item) => item.system))],
|
||||
CONSIGNMENT_STOCK_SYSTEM_ORDER,
|
||||
)
|
||||
const seriesList = sortByKnownOrder(
|
||||
[...new Set(normalizedItems.map((item) => item.series))],
|
||||
CONSIGNMENT_STOCK_SERIES_ORDER,
|
||||
)
|
||||
const itemByKey = new Map(
|
||||
normalizedItems.map((item) => [
|
||||
`${item.system}\u0000${item.series}\u0000${item.diamond_level}`,
|
||||
item,
|
||||
]),
|
||||
)
|
||||
|
||||
return systems.flatMap((system) =>
|
||||
seriesList.map((series) => ({
|
||||
system,
|
||||
series,
|
||||
cells: CONSIGNMENT_STOCK_DIAMOND_LEVELS.map((diamondLevel) => ({
|
||||
diamond_level: diamondLevel,
|
||||
item: itemByKey.get(`${system}\u0000${series}\u0000${diamondLevel}`),
|
||||
})),
|
||||
})),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user