feat(phase-1): 京东数据库 UI 改造蓝图 Phase 1——类目计数聚合纯函数+首屏默认档 50

This commit is contained in:
2026-11-21 03:47:45 +08:00
parent 22771a7aea
commit 380792aa82
6 changed files with 242 additions and 11 deletions
@@ -153,3 +153,78 @@ export function jdCategoryNamesUnder(path: string[]): string[] | null {
walk([target])
return out
}
/** GET /categories 的计数行({name, productCount},name=京麦末级类目即落库
* category_path;同名末级在后端是同一等值桶,此处一行一名)。 */
export interface JdCategoryCountEntry {
name: string
productCount: number
}
/** 带计数的树节点:count=自身名+全部后代名(子树内去重)的 productCount 之和——
* 与「点节点 → jdCategoryNamesUnder 展开集合 IN 过滤」的命中集合同源(v1.6 蓝图
* Phase 1 备料):徽标数必须就是点它之后列表会命中的行数,两边各算各的就会出现
* 「徽标 5、点进去 8 行」。leaf/children 与 JdCategoryNode 同义(leaf=children 空)。 */
export interface JdCategoryCountedNode {
name: string
value: string
count: number
leaf: boolean
children: JdCategoryCountedNode[]
}
/** 计数聚合回执:roots=树形计数树(全部授权类目都在,无词条的节点 count=0);
* extra=树上任何节点名(含非叶)都对不上的历史行(树外兜底组——京麦改树后的
* 存量 category_path 从这里选,语义同旧版级联第三栏兜底)。 */
export interface JdCategoryCountTree {
roots: JdCategoryCountedNode[]
extra: JdCategoryCountEntry[]
/** 兜底组计数和(徽标直接用)。 */
extraTotal: number
}
/** /categories 计数行 → 树形计数聚合(v1.6 蓝图 Phase 1)。同名跨支叶(短裤/唐装/
* 吊带/背心/赠品)按过滤语义如实计入两个分支——点哪个分支都按同一名字等值命中
* 同一批行,不是双计;父层子树内按名字去重求和(与展开集合口径一致,父层徽标
* 不因同名跨支翻倍)。名字对不上树上任何节点的行归树外兜底组。脏值归一:空名
* 跳过、计数非有限数归 0、负数钳 0、同名多行求和。 */
export function jdCategoryCountTree(entries: JdCategoryCountEntry[]): JdCategoryCountTree {
const counts = new Map<string, number>()
for (const entry of Array.isArray(entries) ? entries : []) {
const name = entry && typeof entry.name === 'string' ? entry.name.trim() : ''
if (!name) continue
const raw = entry.productCount
const value = typeof raw === 'number' && Number.isFinite(raw) ? Math.max(0, Math.floor(raw)) : 0
counts.set(name, (counts.get(name) ?? 0) + value)
}
const treeNames = new Set<string>()
const build = (
nodes: JdCategoryNode[],
): { out: JdCategoryCountedNode[]; names: Set<string> } => {
const out: JdCategoryCountedNode[] = []
const names = new Set<string>()
for (const node of nodes) {
treeNames.add(node.name)
const child =
node.children && node.children.length
? build(node.children)
: { out: [] as JdCategoryCountedNode[], names: new Set<string>() }
const set = new Set(child.names)
set.add(node.name)
let count = 0
for (const name of set) count += counts.get(name) ?? 0
out.push({ name: node.name, value: node.value, count, leaf: child.out.length === 0, children: child.out })
for (const name of set) names.add(name)
}
return { out, names }
}
const roots = build(JD_CATEGORY_TREE).out
const extra: JdCategoryCountEntry[] = []
let extraTotal = 0
for (const [name, count] of counts) {
if (treeNames.has(name)) continue
extra.push({ name, productCount: count })
extraTotal += count
}
return { roots, extra, extraTotal }
}