diff --git a/micFieldMapping.js b/micFieldMapping.js new file mode 100644 index 0000000..f759c29 --- /dev/null +++ b/micFieldMapping.js @@ -0,0 +1,277 @@ +// MIC产品上传 — 字段映射与净化 +// +// 说明(按你的确认): +// - 不迁移原脚本里 product_attributes_*.py 的"品类级固定文案"(Design Life / +// Self-discharge Rate 之类),本版本只上传 CRM 产品档案里已有的变量字段。 +// - 标签文字是我按03-数据模型.md里的字段含义拟的英文名,供MIC属性名使用, +// 不是从某个官方渠道抄来的——你后续可以按实际MIC类目要求自行改这张表, +// 改了之后数据本身(来自CRM)不受影响。 +// - extraSpecs(产品表单里"更多参数"那个动态列表)总是追加在品类专属字段之后。 + +// ---------------------------- ASCII 净化 ---------------------------- + +const SYMBOL_REPLACEMENTS = [ + [/\u00B0C/g, 'Deg C'], // °C + [/\u2212/g, '-'], // − (Unicode减号) + [/\u2264/g, '<='], // ≤ + [/\u2265/g, '>='], // ≥ + [/\u00D7/g, 'x'], // × + [/\u2013|\u2014/g, '-'], // – / — 长短破折号 + [/\u2018|\u2019/g, "'"], // 弯引号 + [/\u201C|\u201D/g, '"'], +]; + +function sanitizeValue(raw) { + if (raw === null || raw === undefined) return ''; + let s = String(raw).trim(); + for (const [pattern, replacement] of SYMBOL_REPLACEMENTS) { + s = s.replace(pattern, replacement); + } + return s; +} + +// 字段名(属性名)额外去掉括号里的单位标注,如 "Dimensions(mm)" -> "Dimensions" +function sanitizeLabel(label) { + let s = sanitizeValue(label); + s = s.replace(/\s*\([^)]*\)\s*$/, '').trim(); + return s; +} + +// 净化后仍残留非ASCII字符(多半是中文),说明该值本来就没翻译成英文—— +// 这里不做机器翻译,只在日志里标出来,交给填表前的人工确认环节处理 +function hasNonAscii(s) { + return /[^\x00-\x7F]/.test(s); +} + +// ---------------------------- 品类字段表 ---------------------------- +// key: 产品分类树里的 type(细分品类)id,见 03-数据模型.md LEGACY_PRODUCT_TAXONOMY +// value: [[crmFieldKey, micLabel], ...],按此顺序生成属性列表 + +const OFFLINE_LINE_INTERACTIVE_FIELDS = [ + ['powerOrCapacity', 'Rated Capacity'], + ['inputVoltage', 'Input Voltage'], + ['inputVoltageRange', 'Input Voltage Range'], + ['inputFrequency', 'Input Frequency'], + ['outputVoltage', 'Output Voltage'], + ['outputVoltageRange', 'Output Voltage Range'], + ['transferTime', 'Transfer Time'], + ['waveform', 'Output Waveform'], + ['batteryVoltage', 'Battery Voltage'], + ['chargeCurrent', 'Charge Current'], + ['frequencyRangeBatteryMode', 'Frequency Range Battery Mode'], + ['chargeTime', 'Charge Time'], + ['lcdDisplay', 'LCD Display'], + ['ledDisplay', 'LED Display'], + ['fullProtection', 'Full Protection'], + ['alarmInfo', 'Alarm Info'], + ['interfacePort', 'Interface Port'], + ['humidity', 'Relative Humidity'], + ['noiseLevel', 'Noise Level'], + ['batterySpec', 'Battery Spec'], + ['dimensions', 'Dim L x W x H mm'], + ['netWeight', 'Net Weight'], +]; + +const RACK_FIELDS = [ + ['phase', 'Phase Config'], // 平台禁用 "Phase" 这个名字 + ['powerOrCapacity', 'Rated Capacity'], + ['inputVoltage', 'Input Voltage'], + ['inputVoltageRange', 'Input Voltage Range'], + ['inputFrequency', 'Input Frequency'], + ['inputPowerFactor', 'Input Power Factor'], + ['outputVoltage', 'Output Voltage'], + ['outputVoltageRange', 'Output Voltage Range'], + ['frequencyRangeBatteryMode', 'Frequency Range Battery Mode'], + ['transferTimeAcToBattery', 'Transfer Time AC to Battery'], + ['transferTime', 'Transfer Time'], + ['waveform', 'Output Waveform'], + ['efficiencyAcMode', 'Efficiency AC Mode'], + ['efficiencyBatteryMode', 'Efficiency Battery Mode'], + ['batterySpec', 'Battery Spec'], + ['chargeCurrent', 'Charge Current'], + ['chargeTime', 'Charge Time'], + ['chargingVoltage', 'Charging Voltage'], + ['lcdDisplay', 'LCD Display'], + ['alarmInfo', 'Alarm Info'], + ['dimensions', 'Dim L x W x H mm'], + ['netWeight', 'Net Weight'], + ['humidity', 'Relative Humidity'], + ['noiseLevel', 'Noise Level'], + ['interfacePort', 'Interface Port'], + ['optionalSnmp', 'Optional SNMP'], +]; + +const ONLINE_HIGH_FREQ_FIELDS = [ + ...RACK_FIELDS, + ['outputPowerFactor', 'Output Power Factor'], + ['parallelUnits', 'Parallel Units'], + ['overload', 'Overload Capacity'], +]; + +const HYBRID_INVERTER_FIELDS = [ + ['batteryType', 'Battery Type'], + ['batteryVoltage', 'Battery Voltage'], + ['maxChargingVoltage', 'Max Charging Voltage'], + ['maxChargeDischargeCurrent', 'Max Charge Discharge Current'], + ['maxDcInputPower', 'Max DC Input Power'], + ['maxDcInputVoltage', 'Max DC Input Voltage'], + ['mpptVoltageRange', 'MPPT Voltage Range'], + ['startingVoltage', 'Starting Voltage'], + ['maxInputCurrent', 'Max Input Current'], + ['mpptNumber', 'Number of MPPT'], + ['onGridMaxApparentPower', 'On-grid Max Apparent Power'], + ['outputVoltage', 'Output Voltage'], + ['ratedOutputFrequency', 'Rated Output Frequency'], + ['onGridMaxOutputCurrent', 'On-grid Max Output Current'], + ['outputPowerFactor', 'Output Power Factor'], + ['offGridRatedApparentPower', 'Off-grid Rated Apparent Power'], + ['offGridMaxApparentPower', 'Off-grid Max Apparent Power'], + ['offGridMaxOutputCurrent', 'Off-grid Max Output Current'], + ['efficiencyMppt', 'MPPT Efficiency'], + ['efficiencyMax', 'Max Efficiency'], + ['efficiencyEurope', 'Euro Efficiency'], + ['protectionFunctions', 'Protection Functions'], + ['operatingTemp', 'Operating Temperature'], + ['storageTemp', 'Storage Temperature'], + ['humidity', 'Relative Humidity'], + ['altitude', 'Altitude'], + ['cooling', 'Cooling Method'], + ['noiseLevel', 'Noise Level'], + ['netWeight', 'Net Weight'], + ['dimensions', 'Dim L x W x H mm'], + ['protectionClass', 'Protection Class'], + ['topology', 'Topology'], + ['safetyStandard', 'Safety Standard'], + ['gridStandard', 'Grid Standard'], + ['otherStandard', 'Other Standard'], +]; + +const UPS_LITHIUM_SYSTEM_FIELDS = [ + ['moduleType', 'Module Type'], + ['moduleCapacity', 'Module Capacity'], + ['cellType', 'Cell Type'], + ['moduleVoltage', 'Module Voltage'], + ['dischargeRate', 'Discharge Rate'], + ['chargeRate', 'Charge Rate'], + ['moduleMaxEnergy', 'Module Max Energy'], + ['moduleOutputPower', 'Module Output Power'], + ['moduleDimensions', 'Module Dim L x W x H mm'], + ['moduleWeight', 'Module Weight'], + ['cabinetMaxEnergy', 'Cabinet Max Energy'], + ['cabinetRatedVoltage', 'Cabinet Rated Voltage'], + ['cycleLife', 'Cycle Life'], + ['commInterface', 'Communication Interface'], + ['protectionFunctions', 'Protection Functions'], + ['operatingTemp', 'Operating Temperature'], + ['humidity', 'Relative Humidity'], + ['color', 'Color'], + ['cabinetDimensions', 'Cabinet Dim L x W x H mm'], + ['cabinetWeight', 'Cabinet Weight'], + ['altitude', 'Altitude'], + ['selfDischargeRate', 'Self-discharge Rate'], +]; + +const GENERIC_THREE_FIELD_FIELDS = [ + ['ratedPower', 'Rated Power'], + ['ratedPowerUnit', 'Rated Power Unit'], + ['powerFactor', 'Power Factor'], + ['voltage', 'Voltage'], +]; + +const MINI_UPS_FIELDS = [ + ['powerOrCapacity', 'Rated Capacity'], + ['voltage', 'Voltage'], + ['outputPortCount', 'Output Port Count'], + ['batterySpec', 'Battery Spec'], + ['dimensions', 'Dim L x W x H mm'], + ['netWeight', 'Net Weight'], +]; + +// type -> 字段表。没列出的 type 会退回到只用 extraSpecs(见 buildAttributeList) +const CATEGORY_FIELD_MAP = { + mini: MINI_UPS_FIELDS, + offline: OFFLINE_LINE_INTERACTIVE_FIELDS, + line_interactive: OFFLINE_LINE_INTERACTIVE_FIELDS, + rack: RACK_FIELDS, + online_high_freq: ONLINE_HIGH_FREQ_FIELDS, + high_freq_tower: GENERIC_THREE_FIELD_FIELDS, + low_freq: GENERIC_THREE_FIELD_FIELDS, + modular: GENERIC_THREE_FIELD_FIELDS, + hybrid: HYBRID_INVERTER_FIELDS, + grid_off_grid: HYBRID_INVERTER_FIELDS, // 字段基本重叠,先共用;如有出入自行调整 + ups_lithium_system: UPS_LITHIUM_SYSTEM_FIELDS, + // lead_acid / lifepo4:CRM数据模型文档里没有给出专属字段清单(这两个品类目前 + // 主要靠 extraSpecs 记录参数),所以这里不建字段表,直接走 extraSpecs 兜底。 +}; + +// ---------------------------- 构建属性列表 ---------------------------- + +/** + * 注意:CRM产品数据model里没有独立的"型号"字段——`name` 本身既是型号也是产品 + * 标识(App.jsx里"按型号名称去重合并"用的就是这个字段),所以这里 model 和 + * title 都用 product.name。如果你想让MIC上的标题更像营销标题(比如带上品牌名/ + * 品类描述),改这里的 title 拼接方式即可,不影响 model 字段。 + * + * @param {object} product CRM产品对象(crm:products 里的一条) + * @returns {{ title: string, model: string, attrList: {name:string, value:string}[], warnings: string[] }} + */ +function buildAttributeList(product) { + const warnings = []; + const fieldTable = CATEGORY_FIELD_MAP[product.type] || []; + const attrList = []; + const seenLabels = new Set(); + + for (const [fieldKey, rawLabel] of fieldTable) { + const value = product[fieldKey]; + if (value === undefined || value === null || value === '') continue; + if (Array.isArray(value) && value.length === 0) continue; + + const label = sanitizeLabel(rawLabel); + const valueStr = sanitizeValue(Array.isArray(value) ? value.join(', ') : value); + if (!valueStr) continue; + + if (hasNonAscii(label) || hasNonAscii(valueStr)) { + warnings.push(`字段 "${rawLabel}" 净化后仍含非ASCII字符,需人工检查: ${valueStr}`); + } + seenLabels.add(label); + attrList.push({ name: label, value: valueStr }); + } + + // extraSpecs:产品表单里的动态附加参数,格式假定为 [{label, value}, ...] + // (与PDF导出用的结构一致),追加在品类专属字段之后,标签冲突时跳过 + if (Array.isArray(product.extraSpecs)) { + for (const item of product.extraSpecs) { + const rawLabel = item && (item.label || item.name); + const rawValue = item && item.value; + if (!rawLabel || rawValue === undefined || rawValue === null || rawValue === '') continue; + const label = sanitizeLabel(rawLabel); + if (seenLabels.has(label)) continue; + const valueStr = sanitizeValue(rawValue); + if (!valueStr) continue; + if (hasNonAscii(label) || hasNonAscii(valueStr)) { + warnings.push(`附加参数 "${rawLabel}" 净化后仍含非ASCII字符,需人工检查: ${valueStr}`); + } + seenLabels.add(label); + attrList.push({ name: label, value: valueStr }); + } + } + + if (!fieldTable.length) { + warnings.push(`品类 "${product.type}" 还没配置专属字段表,本次只上传了 extraSpecs 里的参数`); + } + + const modelAndTitle = sanitizeValue(product.name); + return { + title: modelAndTitle, + model: modelAndTitle, + attrList, + warnings, + }; +} + +module.exports = { + buildAttributeList, + sanitizeValue, + sanitizeLabel, + CATEGORY_FIELD_MAP, +};