/** 批量导入文本解析(照抄桌面版 gating_web_admin.py parse_batch 的行格式与跳过规则)。 * 纯逻辑、零依赖(tests/gating-import.test.ts 用 node --test 直连本文件)。 * * 口径(与桌面版 L208-223 逐条对齐): * - 按行拆分;每行去首尾空白与 BOM(\uFEFF); * - 空行与以 # 开头的注释行跳过; * - 行内按中英文逗号/分号/制表符([,\t;,;])切分,各段去空白后丢掉空段; * - 字段序:品牌(必留)、国家(缺省「全部」)、Gating类型(缺省「品牌gating」)、ASIN(缺省空)、备注(缺省空); * 第 5 列之后的段照桌面版丢弃。 * * 前端只用它做导入前的行数预览;真正提交仍把原始文本整段交给后端 * (POST /api/admin/brand-gating/import { text },去重与入库由后端按同一份规则解析)。 */ export interface GatingImportItem { brand: string country: string gatingType: string asin: string remark: string } export interface GatingImportParseResult { items: GatingImportItem[] /** 因空行/注释行/纯分隔符行被跳过的行数。 */ skippedLines: number } export const GATING_IMPORT_DEFAULT_COUNTRY = '全部' export const GATING_IMPORT_DEFAULT_TYPE = '品牌gating' /** 与桌面版一致的列分隔符:中英文逗号、中英文分号、制表符。 */ const FIELD_SPLIT_RE = /[,\t;,;]/ /** 解析批量导入文本;永不抛错(坏行计入 skippedLines)。 */ export function parseGatingImportText(text: string): GatingImportParseResult { const items: GatingImportItem[] = [] let skippedLines = 0 const lines = String(text ?? '').split(/\r?\n/) for (const rawLine of lines) { const line = rawLine.trim().replace(/^\uFEFF/, '').trim() if (!line || line.startsWith('#')) { if (rawLine.trim()) skippedLines += 1 continue } const parts = line .split(FIELD_SPLIT_RE) .map((part) => part.trim()) .filter((part) => part.length > 0) if (!parts.length) { skippedLines += 1 continue } items.push({ brand: parts[0], country: parts[1] ?? GATING_IMPORT_DEFAULT_COUNTRY, gatingType: parts[2] ?? GATING_IMPORT_DEFAULT_TYPE, asin: parts[3] ?? '', remark: parts[4] ?? '', }) } return { items, skippedLines } } /** 导入完成反馈文案:新增 X 条,跳过重复 Y 条。 */ export function gatingImportOutcomeText(added: number, skipped: number): string { return `批量导入完成:新增 ${added} 条,跳过重复 ${skipped} 条` }