149 lines
5.0 KiB
TypeScript
149 lines
5.0 KiB
TypeScript
/** 查询 ASIN 列表查询模型(任务 71):解析 QueryAsinPageVo(camel) 与每店铺 5 国家 ASIN 宽行,纯逻辑。 */
|
|
import { unwrap } from '../../api/envelope.ts'
|
|
import { ASIN_PAGE_DEFAULT_SIZE } from './asin-filter.ts'
|
|
|
|
export const QUERY_ASIN_COUNTRIES = ['DE', 'UK', 'FR', 'IT', 'ES'] as const
|
|
export type QueryAsinCountry = (typeof QUERY_ASIN_COUNTRIES)[number]
|
|
|
|
export interface QueryAsinItem {
|
|
id: number
|
|
groupId: number | null
|
|
groupName: string
|
|
shopName: string
|
|
asinDe: string
|
|
asinUk: string
|
|
asinFr: string
|
|
asinIt: string
|
|
asinEs: string
|
|
createdAt?: string
|
|
updatedAt?: string
|
|
}
|
|
|
|
export interface QueryAsinPageResult {
|
|
items: QueryAsinItem[]
|
|
total: number
|
|
page: number
|
|
pageSize: number
|
|
}
|
|
|
|
export function emptyQueryAsinPage(): QueryAsinPageResult {
|
|
return { items: [], total: 0, page: 1, pageSize: ASIN_PAGE_DEFAULT_SIZE }
|
|
}
|
|
|
|
function text(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
function numberOrNull(value: unknown): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
|
}
|
|
|
|
const COUNTRY_COLUMN: Record<string, keyof Pick<QueryAsinItem, 'asinDe' | 'asinUk' | 'asinFr' | 'asinIt' | 'asinEs'>> = {
|
|
DE: 'asinDe',
|
|
UK: 'asinUk',
|
|
FR: 'asinFr',
|
|
IT: 'asinIt',
|
|
ES: 'asinEs',
|
|
}
|
|
|
|
/** 解析单条查询 ASIN 店铺行;缺 id 视为无效。 */
|
|
export function toQueryAsinItem(raw: unknown): QueryAsinItem | null {
|
|
if (!raw || typeof raw !== 'object') return null
|
|
const record = raw as Record<string, unknown>
|
|
const id = numberOrNull(record.id)
|
|
if (id === null) return null
|
|
const item: QueryAsinItem = {
|
|
id,
|
|
groupId: numberOrNull(record.groupId ?? record.group_id),
|
|
groupName: text(record.groupName ?? record.group_name),
|
|
shopName: text(record.shopName ?? record.shop_name),
|
|
asinDe: text(record.asinDe ?? record.asin_de),
|
|
asinUk: text(record.asinUk ?? record.asin_uk),
|
|
asinFr: text(record.asinFr ?? record.asin_fr),
|
|
asinIt: text(record.asinIt ?? record.asin_it),
|
|
asinEs: text(record.asinEs ?? record.asin_es),
|
|
}
|
|
const createdAt = text(record.createdAt ?? record.created_at)
|
|
if (createdAt) item.createdAt = createdAt
|
|
const updatedAt = text(record.updatedAt ?? record.updated_at)
|
|
if (updatedAt) item.updatedAt = updatedAt
|
|
return item
|
|
}
|
|
|
|
/** 归一化分页负载(信封或已解包 VO)为前端结果;缺省字段回默认。 */
|
|
export function parseQueryAsinPage(payload: unknown): QueryAsinPageResult {
|
|
const out = emptyQueryAsinPage()
|
|
const core = unwrap<unknown>(payload)
|
|
if (!core || typeof core !== 'object') return out
|
|
const record = core as Record<string, unknown>
|
|
if (Array.isArray(record.items)) {
|
|
out.items = record.items
|
|
.map((raw) => toQueryAsinItem(raw))
|
|
.filter((item): item is QueryAsinItem => item !== null)
|
|
}
|
|
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
|
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
|
const rawSize = record.pageSize ?? record.page_size
|
|
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
|
return out
|
|
}
|
|
|
|
/** 取某国家列 ASIN;国家码不属白名单时返回空串。 */
|
|
export function asinForCountry(item: QueryAsinItem, country: string): string {
|
|
const column = COUNTRY_COLUMN[country]
|
|
return column ? item[column] : ''
|
|
}
|
|
|
|
/** 该店铺行是否存在任一国家 ASIN。 */
|
|
export function queryAsinItemHasAnyAsin(item: QueryAsinItem): boolean {
|
|
return QUERY_ASIN_COUNTRIES.some((code) => asinForCountry(item, code) !== '')
|
|
}
|
|
|
|
/** 纵向行条目(对齐 admin.js renderQueryAsinEntries):按 DE/UK/FR/IT/ES 顺序收集有值国家;全空补一条空条目。 */
|
|
export interface QueryAsinRowEntry {
|
|
country: QueryAsinCountry | null
|
|
asin: string
|
|
}
|
|
|
|
export function queryAsinRowEntries(item: QueryAsinItem): QueryAsinRowEntry[] {
|
|
const entries: QueryAsinRowEntry[] = []
|
|
for (const code of QUERY_ASIN_COUNTRIES) {
|
|
const asin = asinForCountry(item, code)
|
|
if (asin) entries.push({ country: code, asin })
|
|
}
|
|
if (!entries.length) entries.push({ country: null, asin: '' })
|
|
return entries
|
|
}
|
|
|
|
/** 纵向展示行(对齐 admin.js renderQueryAsinRows):序号/分组/店铺/操作 按 rowspan 合并,逐国一行。 */
|
|
export interface QueryAsinDisplayRow {
|
|
key: string
|
|
rowNo: number
|
|
rowspan: number
|
|
isFirst: boolean
|
|
country: QueryAsinCountry | null
|
|
asin: string
|
|
item: QueryAsinItem
|
|
}
|
|
|
|
export function queryAsinDisplayRows(items: QueryAsinItem[], startRowNo: number): QueryAsinDisplayRow[] {
|
|
const out: QueryAsinDisplayRow[] = []
|
|
let rowNo = Math.max(Math.floor(startRowNo || 1), 1)
|
|
for (const item of items) {
|
|
const entries = queryAsinRowEntries(item)
|
|
entries.forEach((entry, index) => {
|
|
out.push({
|
|
key: `${item.id}:${index}`,
|
|
rowNo,
|
|
rowspan: entries.length,
|
|
isFirst: index === 0,
|
|
country: entry.country,
|
|
asin: entry.asin,
|
|
item,
|
|
})
|
|
})
|
|
rowNo += 1
|
|
}
|
|
return out
|
|
}
|