158 lines
4.5 KiB
JavaScript
158 lines
4.5 KiB
JavaScript
// 价格同步工具 - 将SKU文件中的价格信息同步到产品价格汇总表
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// 产品目录路径
|
|
const PRODUCTS_DIR = path.join(__dirname, '../../../../产品');
|
|
const PRICE_SUMMARY_FILE = path.join(PRODUCTS_DIR, '产品价格汇总表.md');
|
|
|
|
// 读取价格汇总表内容
|
|
function readPriceSummary() {
|
|
return fs.readFileSync(PRICE_SUMMARY_FILE, 'utf8');
|
|
}
|
|
|
|
// 写入价格汇总表内容
|
|
function writePriceSummary(content) {
|
|
fs.writeFileSync(PRICE_SUMMARY_FILE, content, 'utf8');
|
|
}
|
|
|
|
// 递归读取所有SKU文件
|
|
function findSkuFiles(dir) {
|
|
const skuFiles = [];
|
|
|
|
function traverse(currentDir) {
|
|
const files = fs.readdirSync(currentDir);
|
|
|
|
for (const file of files) {
|
|
const fullPath = path.join(currentDir, file);
|
|
const stat = fs.statSync(fullPath);
|
|
|
|
if (stat.isDirectory()) {
|
|
traverse(fullPath);
|
|
} else if (file.endsWith('.md') && (file.includes('-') || file.includes('openclaw'))) {
|
|
skuFiles.push(fullPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
traverse(dir);
|
|
return skuFiles;
|
|
}
|
|
|
|
// 解析SKU文件内容
|
|
function parseSkuFile(filePath) {
|
|
try {
|
|
const content = fs.readFileSync(filePath, 'utf8');
|
|
|
|
const skuData = {
|
|
model: '',
|
|
cost: 0,
|
|
price: 0
|
|
};
|
|
|
|
// 解析产品型号
|
|
const modelMatch = content.match(/\*\*产品型号\*\*\s*\|\s*([^|]+)\s*\|/);
|
|
if (modelMatch) {
|
|
skuData.model = modelMatch[1].trim();
|
|
} else {
|
|
console.log(`未找到产品型号: ${filePath}`);
|
|
}
|
|
|
|
// 解析总成本
|
|
const costMatch = content.match(/\*\*总成本\*\*\s*\|\s*-\s*\|\s*¥(\d+)\s*\|/);
|
|
if (costMatch) {
|
|
skuData.cost = parseInt(costMatch[1]);
|
|
} else {
|
|
console.log(`未找到总成本: ${filePath}`);
|
|
}
|
|
|
|
// 解析建议零售价
|
|
const priceMatch = content.match(/\*\*建议零售价\*\*\s*\|\s*-\s*\|\s*¥(\d+)\s*\|/);
|
|
if (priceMatch) {
|
|
skuData.price = parseInt(priceMatch[1]);
|
|
} else {
|
|
console.log(`未找到建议零售价: ${filePath}`);
|
|
}
|
|
|
|
if (skuData.model && skuData.cost && skuData.price) {
|
|
console.log(`解析成功: ${skuData.model} - 成本: ¥${skuData.cost}, 售价: ¥${skuData.price}`);
|
|
}
|
|
|
|
return skuData;
|
|
} catch (error) {
|
|
console.log(`解析文件失败: ${filePath}, 错误: ${error.message}`);
|
|
return { model: '', cost: 0, price: 0 };
|
|
}
|
|
}
|
|
|
|
// 同步价格信息到汇总表
|
|
function syncPrices() {
|
|
console.log('开始同步SKU价格到汇总表...');
|
|
|
|
// 读取所有SKU文件
|
|
const skuFiles = findSkuFiles(PRODUCTS_DIR);
|
|
console.log(`找到 ${skuFiles.length} 个SKU文件`);
|
|
|
|
// 解析所有SKU文件
|
|
const skuDataMap = new Map();
|
|
let parsedCount = 0;
|
|
|
|
for (const file of skuFiles) {
|
|
const skuData = parseSkuFile(file);
|
|
if (skuData.model && skuData.cost && skuData.price) {
|
|
skuDataMap.set(skuData.model, skuData);
|
|
parsedCount++;
|
|
}
|
|
}
|
|
|
|
console.log(`解析完成 ${parsedCount} 个SKU数据`);
|
|
|
|
// 读取汇总表内容
|
|
let summaryContent = readPriceSummary();
|
|
|
|
// 更新日期
|
|
const today = new Date().toISOString().split('T')[0];
|
|
summaryContent = summaryContent.replace(/更新日期:\d{4}-\d{2}-\d{2}/, `更新日期:${today}`);
|
|
|
|
// 逐行处理汇总表,避免一次性处理大文件
|
|
const lines = summaryContent.split('\n');
|
|
const updatedLines = [];
|
|
let updatedCount = 0;
|
|
|
|
for (const line of lines) {
|
|
let updatedLine = line;
|
|
|
|
// 检查是否是产品行
|
|
const modelMatch = line.match(/\|\s*\d+\s*\|\s*([^|]+)\s*\|\s*¥\d+\s*\|\s*¥\d+\s*\|\s*¥\d+\s*\|\s*\d+%\s*\|/);
|
|
if (modelMatch) {
|
|
const model = modelMatch[1].trim();
|
|
const skuData = skuDataMap.get(model);
|
|
|
|
if (skuData) {
|
|
const profit = skuData.price - skuData.cost;
|
|
const profitRate = Math.round((profit / skuData.cost) * 100);
|
|
|
|
// 找到行中的序号
|
|
const indexMatch = line.match(/\|\s*(\d+)\s*\|/);
|
|
const index = indexMatch ? indexMatch[1] : '1';
|
|
|
|
updatedLine = `| ${index} | ${model} | ¥${skuData.cost} | ¥${skuData.price} | ¥${profit} | ${profitRate}% |`;
|
|
updatedCount++;
|
|
}
|
|
}
|
|
|
|
updatedLines.push(updatedLine);
|
|
}
|
|
|
|
// 重新组合内容
|
|
const updatedContent = updatedLines.join('\n');
|
|
|
|
// 写入更新后的汇总表
|
|
writePriceSummary(updatedContent);
|
|
|
|
console.log(`同步完成!更新了 ${updatedCount} 个产品的价格信息`);
|
|
console.log(`价格汇总表已更新,更新日期:${today}`);
|
|
}
|
|
|
|
// 执行同步
|
|
syncPrices(); |