// 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, '"'], ]; // 电池/电源规格里常见的中文量词——这些是实际踩过的坑(比如"16/20节"这种电池 // 节数写法被MIC拒收),先只覆盖确认遇到过的几个,不是穷举翻译表。新遇到别的 // 中文量词报错,把字符发我,这里加一行就行,不要自己瞎猜翻译。 const UNIT_WORD_REPLACEMENTS = [ [/节/g, ' Cells'], // 电池节数,如"16/20节" -> "16/20 Cells" [/个/g, ' pcs'], [/只/g, ' pcs'], [/组/g, ' Set'], [/台/g, ' Unit'], [/根/g, ' pcs'], [/套/g, ' Set'], ]; 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); } for (const [pattern, replacement] of UNIT_WORD_REPLACEMENTS) { s = s.replace(pattern, replacement); } s = s.replace(/\s+/g, ' ').trim(); // 替换量词时前面加了空格,防止连续空格 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 -> 字段表。key必须是 product.type 里实际存的值——CRM这边品类的"细分类型" // 字段存的是中文短名(跟产品表单下拉框文字一致,如"离线式UPS"/"机架式UPS"), // 不是英文id。这张表按 crm:productCategories 默认生成的中文短名来对,如果你在 // "产品分组"里重命名过某个品类,这里的key要跟着改,不然会静默走extraSpecs兜底 // (不会报错,但字段映射不生效——一开始我就是把这里写成英文id导致一直没生效)。 const CATEGORY_FIELD_MAP = { '迷你UPS': MINI_UPS_FIELDS, '离线式UPS': OFFLINE_LINE_INTERACTIVE_FIELDS, '在线互动式UPS': OFFLINE_LINE_INTERACTIVE_FIELDS, '机架式UPS': RACK_FIELDS, '在线式高频UPS': ONLINE_HIGH_FREQ_FIELDS, '高频塔式UPS': GENERIC_THREE_FIELD_FIELDS, '工频UPS': GENERIC_THREE_FIELD_FIELDS, '模块化UPS': GENERIC_THREE_FIELD_FIELDS, '太阳能混网逆变器': HYBRID_INVERTER_FIELDS, '并网/离网逆变器': HYBRID_INVERTER_FIELDS, // 字段基本重叠,先共用;如有出入自行调整 'UPS配套锂电池系统': UPS_LITHIUM_SYSTEM_FIELDS, // 铅酸电池 / 磷酸铁锂电池 / 家庭储能系统 等:数据模型文档里没给出这些品类的专属 // 字段清单(目前主要靠 extraSpecs 记录参数),所以这里不建字段表,直接走 // extraSpecs 兜底。如果实际有专属字段,告诉我字段名再补。 }; // ---------------------------- 构建属性列表 ---------------------------- /** * 注意:CRM产品数据model里没有独立的"型号"字段——`name` 本身既是型号也是产品 * 标识(App.jsx里"按型号名称去重合并"用的就是这个字段),所以这里 model 就是 * product.name。MIC上的"产品名称"不再用这个当标题——改成micUploadRoutes.js里 * 单独调DeepSeek按官方规则生成(型号只是标题生成时的一个输入项)。 * * @param {object} product CRM产品对象(crm:products 里的一条) * @returns {{ 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 里的参数`); } return { model: sanitizeValue(product.name), attrList, warnings, }; } // ---------------------------- 包裹尺寸(mm→cm) ---------------------------- // CRM里"尺寸(mm)"是自由文本,约定俗成按"长x宽x高"顺序输入(没有强制校验), // 这里按这个假设拆分成三个数字、从mm换算成cm(MIC那边包裹尺寸的输入框单位是cm)。 // 顺序是CRM里人工填写时的约定,不保证100%准确——生成后建议人工核对一遍。 function parsePackageDimensionsCm(dimensionsMmStr) { if (!dimensionsMmStr) return { depthCm: '', widthCm: '', heightCm: '' }; const nums = String(dimensionsMmStr).match(/[\d.]+/g); if (!nums || nums.length < 3) return { depthCm: '', widthCm: '', heightCm: '' }; const toCm = (mm) => { const n = parseFloat(mm); if (Number.isNaN(n)) return ''; const cm = n / 10; return Number.isInteger(cm) ? String(cm) : cm.toFixed(1); }; return { depthCm: toCm(nums[0]), widthCm: toCm(nums[1]), heightCm: toCm(nums[2]) }; } module.exports = { buildAttributeList, sanitizeValue, sanitizeLabel, parsePackageDimensionsCm, CATEGORY_FIELD_MAP, };