/** 品牌数据库(不符合ASIN)列表模型(任务 68):解析 InvalidAsinDataPageVo(camel),纯逻辑。 */ import { unwrap } from '../../api/envelope.ts' import { ASIN_PAGE_DEFAULT_SIZE } from '../asin/asin-filter.ts' export type InvalidAsinSource = 'MANUAL' | 'AUTO' export interface InvalidAsinItem { id: number dataValue: string brand: string groupId: number | null groupName: string recordSource: string createdAt?: string updatedAt?: string } export interface InvalidAsinPageResult { items: InvalidAsinItem[] total: number page: number pageSize: number } export function emptyInvalidAsinPage(): InvalidAsinPageResult { 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 } /** 来源展示标签:MANUAL 手动新增,其余(含空)自动导入。 */ export function recordSourceLabel(source: string | null | undefined): string { return (source || '').trim().toUpperCase() === 'MANUAL' ? '手动新增' : '自动导入' } /** 删除二次确认文案(对齐 admin.js:3775 原文)。 */ export function invalidAsinDeleteText(item: Pick | null | undefined): string { const value = item?.dataValue || '该 ASIN' return `确定删除“${value}”吗?` } /** * 非超管分组锁定(对齐 admin.js getInvalidAsinDataLockedGroupId:超管不锁; * 非超管锁 lockedGroupId,缺失时退化为第一个分组;无分组返回 null)。 */ export function invalidAsinLockedGroupId( groups: Array<{ id: number }>, lockedGroupId: number | null | undefined, operatorRole: string, ): number | null { if ((operatorRole || '').toLowerCase() === 'super_admin') return null if (lockedGroupId != null && groups.some((group) => group.id === lockedGroupId)) return lockedGroupId return groups.length ? groups[0].id : null } /** 解析单条品牌数据库项;缺 id 视为无效。 */ export function toInvalidAsinItem(raw: unknown): InvalidAsinItem | null { if (!raw || typeof raw !== 'object') return null const record = raw as Record const id = numberOrNull(record.id) if (id === null) return null const item: InvalidAsinItem = { id, dataValue: text(record.dataValue ?? record.data_value), brand: text(record.brand), groupId: numberOrNull(record.groupId ?? record.group_id), groupName: text(record.groupName ?? record.group_name), recordSource: (text(record.recordSource ?? record.record_source)).toUpperCase(), } 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 parseInvalidAsinPage(payload: unknown): InvalidAsinPageResult { const out = emptyInvalidAsinPage() const core = unwrap(payload) if (!core || typeof core !== 'object') return out const record = core as Record if (Array.isArray(record.items)) { out.items = record.items .map((raw) => toInvalidAsinItem(raw)) .filter((item): item is InvalidAsinItem => 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 }