145 lines
5.1 KiB
TypeScript
145 lines
5.1 KiB
TypeScript
/** 撞款控制台视图派生(分组索引 / 组内撞款子集 / 店铺列并集):纯逻辑。 */
|
||
import type { DuplicateItem, DuplicateOccurrence, DuplicateShop } from './duplicate-model.ts'
|
||
|
||
export interface ShopGroups {
|
||
/** 分组名 → 成员店铺(展示名,保持首次出现顺序)。 */
|
||
members: Record<string, string[]>
|
||
/** 店铺展示名 → 所属分组名(含多分组拆分的每一组)。 */
|
||
shopGroupNames: Record<string, string[]>
|
||
}
|
||
|
||
const GROUP_SEPARATOR = /[、,,/;;]/
|
||
|
||
function shopKey(name: string): string {
|
||
return (name || '').trim().toLowerCase()
|
||
}
|
||
|
||
function splitTokens(value: string): string[] {
|
||
const out: string[] = []
|
||
for (const token of (value || '').split(GROUP_SEPARATOR)) {
|
||
const group = token.trim()
|
||
if (group && !out.includes(group)) out.push(group)
|
||
}
|
||
return out
|
||
}
|
||
|
||
/** 由店铺分布构建 分组→成员 与 店铺→分组 双向索引。 */
|
||
export function indexShopGroups(shops: DuplicateShop[]): ShopGroups {
|
||
const members: Record<string, string[]> = {}
|
||
const shopGroupNames: Record<string, string[]> = {}
|
||
for (const shop of shops || []) {
|
||
const display = (shop.shopName || '').trim()
|
||
if (!display) continue
|
||
const groups = splitTokens(shop.groupName || '')
|
||
for (const group of groups) {
|
||
const list = members[group] || (members[group] = [])
|
||
if (!list.includes(display)) list.push(display)
|
||
const owned = shopGroupNames[display] || (shopGroupNames[display] = [])
|
||
if (!owned.includes(group)) owned.push(group)
|
||
}
|
||
}
|
||
return { members, shopGroupNames }
|
||
}
|
||
|
||
/** 仅在指定成员店铺(展示名)内保留 occurrences;重算 shop_count,返回 ≥2 店的撞款子集。 */
|
||
export function dupWithinGroup(items: DuplicateItem[], memberShops: string[]): DuplicateItem[] {
|
||
const memberKeys = new Set<string>()
|
||
for (const shop of memberShops || []) memberKeys.add(shopKey(shop))
|
||
const result: DuplicateItem[] = []
|
||
for (const item of items || []) {
|
||
const kept: DuplicateOccurrence[] = []
|
||
const shops = new Set<string>()
|
||
for (const occ of item.occurrences || []) {
|
||
if (memberKeys.has(shopKey(occ.shopName))) {
|
||
kept.push(occ)
|
||
shops.add(occ.shopName || '')
|
||
}
|
||
}
|
||
if (kept.length > 0 && shops.size >= 2) {
|
||
result.push({ asin: item.asin, shopCount: shops.size, recordCount: kept.length, occurrences: kept })
|
||
}
|
||
}
|
||
return result
|
||
}
|
||
|
||
/** 撞款行涉及店铺的并集列(出现顺序去重);memberShops 非空时仅取交集成员的子集顺序。 */
|
||
export function storeColumnsOf(items: DuplicateItem[], memberShops: string[] = []): string[] {
|
||
const restricted = new Set(memberShops.map((shop) => shopKey(shop)))
|
||
const useRestricted = restricted.size > 0
|
||
const order: string[] = []
|
||
const seen = new Set<string>()
|
||
const push = (name: string): void => {
|
||
const key = shopKey(name)
|
||
if (!name || seen.has(key) || (useRestricted && !restricted.has(key))) return
|
||
seen.add(key)
|
||
order.push(name)
|
||
}
|
||
for (const item of items || []) {
|
||
for (const occ of item.occurrences || []) push(occ.shopName || '')
|
||
}
|
||
if (order.length === 0 && memberShops.length > 0) {
|
||
// 无撞款行但该组有成员店铺:矩阵列仍需展示组员店。
|
||
for (const shop of memberShops) {
|
||
const key = shopKey(shop)
|
||
if (!seen.has(key)) {
|
||
seen.add(key)
|
||
order.push(shop)
|
||
}
|
||
}
|
||
}
|
||
return order
|
||
}
|
||
|
||
/** 明细抽屉聚合行:店铺×站点聚合(对齐 reference 抽屉的 店铺/分组/站点/上架时间(升序堆叠)/次数)。 */
|
||
export interface DrawerAggRow {
|
||
shopName: string
|
||
groupName: string
|
||
country: string
|
||
times: string[]
|
||
count: number
|
||
}
|
||
|
||
export function aggregateDrawerRows(occurrences: DuplicateOccurrence[]): DrawerAggRow[] {
|
||
const rows = new Map<string, DrawerAggRow>()
|
||
for (const occ of occurrences || []) {
|
||
const key = `${shopKey(occ.shopName)}|${(occ.country || '').trim().toUpperCase()}`
|
||
let row = rows.get(key)
|
||
if (!row) {
|
||
row = { shopName: occ.shopName || '', groupName: occ.groupName || '', country: (occ.country || '').trim(), times: [], count: 0 }
|
||
rows.set(key, row)
|
||
}
|
||
row.times.push(occ.date || '')
|
||
row.count += 1
|
||
}
|
||
const list = [...rows.values()]
|
||
for (const row of list) row.times.sort()
|
||
return list
|
||
}
|
||
|
||
/** 明细抽屉汇总徽章:N店在售/N家店铺/国家chips/N次上架/最早上架。 */
|
||
export interface DrawerSummary {
|
||
shopCount: number
|
||
countries: string[]
|
||
totalRecords: number
|
||
earliest: string
|
||
}
|
||
|
||
export function drawerSummaryOf(occurrences: DuplicateOccurrence[]): DrawerSummary {
|
||
const shops = new Set<string>()
|
||
const countries: string[] = []
|
||
const seenCountry = new Set<string>()
|
||
let earliest = ''
|
||
let totalRecords = 0
|
||
for (const occ of occurrences || []) {
|
||
if (occ.shopName) shops.add(shopKey(occ.shopName))
|
||
const country = (occ.country || '').trim()
|
||
if (country && !seenCountry.has(country)) {
|
||
seenCountry.add(country)
|
||
countries.push(country)
|
||
}
|
||
if (occ.date && (!earliest || occ.date < earliest)) earliest = occ.date
|
||
totalRecords += 1
|
||
}
|
||
return { shopCount: shops.size, countries, totalRecords, earliest }
|
||
}
|