require('dotenv').config(); const db = require('../src/config/database'); /** * 测试数据源标识功能 * 验证三个接口的数据是否有明显区分 */ async function testDataSourceIdentification() { console.log('🧪 测试数据源标识功能\n'); try { // 1. 测试查询所有数据源统计 console.log('📊 查询所有数据源统计...\n'); const [stats] = await db.execute(` SELECT price_source_code AS '数据源代码', price_source_desc AS '数据源描述', data_origin AS '数据来源', COUNT(*) AS '记录数', AVG(hang_price) AS '平均价格', MIN(hang_price) AS '最低价格', MAX(hang_price) AS '最高价格' FROM prices GROUP BY price_source_code, price_source_desc, data_origin ORDER BY price_source_code `); console.log('┌' + '─'.repeat(120) + '┐'); console.log('│' + ' '.repeat(40) + '数据源统计报告' + ' '.repeat(56) + '│'); console.log('└' + '─'.repeat(120) + '┘'); console.log(''); if (stats.length === 0) { console.log('⚠️ 当前数据库中没有数据'); console.log('💡 提示:运行以下命令导入数据:'); console.log(' npm run db:import:api'); console.log(' npm run db:import:local'); } else { // 表头 console.log('┌' + '─'.repeat(22) + '┬' + '─'.repeat(28) + '┬' + '─'.repeat(16) + '┬' + '─'.repeat(10) + '┬' + '─'.repeat(12) + '┬' + '─'.repeat(12) + '┬' + '─'.repeat(12) + '┐'); console.log('│ ' + '数据源代码'.padEnd(20) + ' │ ' + '数据源描述'.padEnd(26) + ' │ ' + '数据来源'.padEnd(14) + ' │ ' + '记录数'.padEnd(8) + ' │ ' + '平均价格'.padEnd(10) + ' │ ' + '最低价格'.padEnd(10) + ' │ ' + '最高价格'.padEnd(10) + ' │'); console.log('├' + '─'.repeat(22) + '┼' + '─'.repeat(28) + '┼' + '─'.repeat(16) + '┼' + '─'.repeat(10) + '┼' + '─'.repeat(12) + '┼' + '─'.repeat(12) + '┼' + '─'.repeat(12) + '┤'); // 数据行 const colorMap = { 'YUNNAN_STEEL_ASSOC': '🔴', 'MY_STEEL': '🔵', 'DE_STEEL_FACTORY': '🟢', 'UNKNOWN': '⚪' }; stats.forEach(row => { const emoji = colorMap[row['数据源代码']] || '⚪'; const avgPrice = row['平均价格'] ? parseFloat(row['平均价格']).toFixed(2) : 'N/A'; const minPrice = row['最低价格'] || 'N/A'; const maxPrice = row['最高价格'] || 'N/A'; console.log( '│ ' + `${emoji} ${row['数据源代码']}`.padEnd(20) + ' │ ' + String(row['数据源描述']).padEnd(26) + ' │ ' + String(row['数据来源']).padEnd(14) + ' │ ' + String(row['记录数']).padEnd(8) + ' │ ' + String(avgPrice).padEnd(10) + ' │ ' + String(minPrice).padEnd(10) + ' │ ' + String(maxPrice).padEnd(10) + ' │' ); }); console.log('└' + '─'.repeat(22) + '┴' + '─'.repeat(28) + '┴' + '─'.repeat(16) + '┴' + '─'.repeat(10) + '┴' + '─'.repeat(12) + '┴' + '─'.repeat(12) + '┴' + '─'.repeat(12) + '┘'); } // 2. 查看最新导入的数据样本 console.log('\n📋 最新数据样本(每个数据源取最新 3 条)...\n'); const samples = await db.execute(` SELECT price_source_code, price_source_desc, data_origin, goods_material, goods_spec, price_region, hang_price, price_date FROM prices WHERE (price_source_code, price_date) IN ( SELECT price_source_code, price_date FROM prices p2 WHERE p2.price_source_code = prices.price_source_code ORDER BY price_date DESC LIMIT 3 ) ORDER BY price_source_code, price_date DESC `); if (samples[0].length > 0) { samples[0].forEach((row, index) => { const emoji = colorMap[row.price_source_code] || '⚪'; console.log(`${index + 1}. ${emoji} [${row.price_source_code}] ${row.goods_material}/${row.goods_spec} - ${row.price_region} - ¥${row.hang_price} (${new Date(row.price_date).toLocaleDateString('zh-CN')})`); }); } // 3. 数据源配置信息 console.log('\n🎨 数据源配置信息\n'); console.log('┌─────────────────────────────────────────────────────────────────────────────────────────────────────┐'); console.log('│ ' + '数据源标识配置'.padEnd(119) + '│'); console.log('├─────────────────────────────────────────────────────────────────────────────────────────────────────┤'); console.log('│ 🔴 YUNNAN_STEEL_ASSOC → 云南钢协指导价(DEFAULT 接口) → Color: #FF6B6B' + ' '.repeat(44) + '│'); console.log('│ 🔵 MY_STEEL → 我的钢铁网价格(BACKUP 接口) → Color: #4ECDC4' + ' '.repeat(44) + '│'); console.log('│ 🟢 DE_STEEL_FACTORY → 德钢钢厂指导价(EXTENDED 接口) → Color: #95E1D3' + ' '.repeat(44) + '│'); console.log('└─────────────────────────────────────────────────────────────────────────────────────────────────────┘'); // 4. 字段说明 console.log('\n📝 字段说明\n'); console.log(' • price_source_code - 数据源唯一代码(用于查询和筛选)'); console.log(' • price_source_desc - 数据源描述(用于显示)'); console.log(' • data_origin - 数据来源标识(LOCAL_FILE 或 API:ENDPOINT)'); console.log('\n' + '='.repeat(120)); console.log('✅ 数据源标识测试完成!'); console.log('='.repeat(120) + '\n'); process.exit(0); } catch (error) { console.error('\n❌ 测试失败:', error.message); console.error(error.stack); process.exit(1); } } // 运行测试 testDataSourceIdentification();