feat(consignment): 支持复制寄卖模板配置与一二级分类折叠展示
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
export type ConsignmentCategoryGroup<T> = {
|
||||
/** 一级分类 ID;只有二级配置时这里是一级分类 ID(来自子项的 parentId) */
|
||||
groupCategoryId: number
|
||||
/** 一级分类名称 */
|
||||
groupName: string
|
||||
/** 一级分类自己的配置/筛选项;没有则为 null */
|
||||
root: T | null
|
||||
/** 该一级下的二级项 */
|
||||
children: T[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 把「一级/二级」平铺数据聚合成可折叠树。保持输入顺序:
|
||||
* 一级先出现,二级按输入顺序挂在对应一级下面;只有二级、没有一级的数据
|
||||
* 会创建一个无 root 的分组,避免历史配置丢失。
|
||||
*/
|
||||
export function buildConsignmentCategoryGroups<T>(input: {
|
||||
items: T[]
|
||||
getCategoryId: (item: T) => number
|
||||
getParentId: (item: T) => number | null | undefined
|
||||
getParentName?: (item: T) => string | undefined
|
||||
getFallbackName: (item: T) => string
|
||||
}): ConsignmentCategoryGroup<T>[] {
|
||||
const groups = new Map<number, ConsignmentCategoryGroup<T>>()
|
||||
const order: number[] = []
|
||||
|
||||
for (const item of input.items) {
|
||||
const parentId = input.getParentId(item)
|
||||
if (parentId) continue
|
||||
const groupCategoryId = input.getCategoryId(item)
|
||||
if (groups.has(groupCategoryId)) {
|
||||
groups.get(groupCategoryId)!.root = item
|
||||
continue
|
||||
}
|
||||
groups.set(groupCategoryId, {
|
||||
groupCategoryId,
|
||||
groupName: input.getFallbackName(item),
|
||||
root: item,
|
||||
children: [],
|
||||
})
|
||||
order.push(groupCategoryId)
|
||||
}
|
||||
|
||||
for (const item of input.items) {
|
||||
const parentId = input.getParentId(item)
|
||||
if (!parentId) continue
|
||||
let group = groups.get(parentId)
|
||||
if (!group) {
|
||||
group = {
|
||||
groupCategoryId: parentId,
|
||||
groupName: input.getParentName?.(item) || input.getFallbackName(item),
|
||||
root: null,
|
||||
children: [],
|
||||
}
|
||||
groups.set(parentId, group)
|
||||
order.push(parentId)
|
||||
}
|
||||
group.children.push(item)
|
||||
}
|
||||
|
||||
return order.map((groupCategoryId) => groups.get(groupCategoryId)!)
|
||||
}
|
||||
Reference in New Issue
Block a user