import fs from 'fs/promises'; import path from 'path'; import { main as projectMain } from './Project/index.js'; import { main as licenseMain } from './PreSaleLicense/index.js'; // 检查数据文件是否存在 async function checkDataFileExists(dataPath) { try { await fs.access(dataPath); return true; } catch { return false; } } // 执行脚本函数并等待完成 async function runScript(scriptFunction, description, cwd, dataFilePath) { console.log(`开始执行: ${description}`); // 检查数据文件是否已存在 const dataExists = await checkDataFileExists(dataFilePath); if (dataExists) { console.log(`📁 发现现有数据文件: ${path.basename(dataFilePath)},跳过抓取脚本\n`); return true; } try { // 临时改变工作目录 const originalCwd = process.cwd(); if (cwd) { process.chdir(cwd); } await scriptFunction(); // 恢复原始工作目录 if (cwd) { process.chdir(originalCwd); } console.log(`✅ ${description} 执行完成\n`); return true; } catch (error) { console.error(`❌ ${description} 执行失败:`, error.message); return false; } } // 合并许可证数据到项目数据中 async function mergeLicenseData() { console.log('开始合并许可证数据...'); try { // 读取项目数据 const projectDataPath = path.join(process.cwd(), 'Project', 'data.json'); const projectData = JSON.parse(await fs.readFile(projectDataPath, 'utf-8')); // 读取许可证数据 const licenseDataPath = path.join(process.cwd(), 'PreSaleLicense', 'data.json'); const licenseData = JSON.parse(await fs.readFile(licenseDataPath, 'utf-8')); // 创建许可证号到许可证数据的映射 const licenseMap = new Map(); licenseData.forEach(license => { if (license['许可证号']) { licenseMap.set(license['许可证号'], license); } }); console.log(`找到 ${licenseMap.size} 个许可证记录`); // 为每个项目合并许可证详细信息 let mergedCount = 0; projectData.forEach(project => { if (project['预售许可证'] && Array.isArray(project['预售许可证'])) { project['预售许可证'].forEach(license => { const licenseNumber = license['许可证号']; const fullLicenseData = licenseMap.get(licenseNumber); if (fullLicenseData) { // 将完整的许可证信息合并到现有许可证对象中 Object.assign(license, fullLicenseData); mergedCount++; } else { console.warn(`⚠️ 未找到许可证详情: ${licenseNumber}`); // 添加一个标记表示未找到详情 license['详情获取状态'] = '未找到'; } }); } }); console.log(`✅ 成功合并 ${mergedCount} 个许可证详情`); // 保存合并后的数据 const outputPath = path.join(process.cwd(), 'merged_data.json'); await fs.writeFile(outputPath, JSON.stringify(projectData, null, 4), 'utf-8'); console.log(`📁 合并后的数据已保存至: ${outputPath}`); console.log(`📊 总项目数: ${projectData.length}`); return projectData; } catch (error) { console.error('❌ 数据合并失败:', error.message); throw error; } } // 主函数 async function main() { console.log('🚀 开始普宁房地产数据整合流程\n'); try { // 步骤1: 获取项目数据 const projectCwd = path.join(process.cwd(), 'Project'); const projectDataPath = path.join(projectCwd, 'data.json'); const projectSuccess = await runScript(projectMain, '项目信息抓取脚本', projectCwd, projectDataPath); if (!projectSuccess) { throw new Error('项目信息抓取失败'); } // 步骤2: 获取许可证数据 const licenseCwd = path.join(process.cwd(), 'PreSaleLicense'); const licenseDataPath = path.join(licenseCwd, 'data.json'); const licenseSuccess = await runScript(licenseMain, '预售许可证抓取脚本', licenseCwd, licenseDataPath); if (!licenseSuccess) { throw new Error('预售许可证抓取失败'); } // 步骤3: 合并数据 await mergeLicenseData(); console.log('\n🎉 所有数据处理完成!'); console.log('📁 输出文件: merged_data.json'); } catch (error) { console.error('\n💥 处理过程中发生错误:', error.message); process.exit(1); } } // 运行主函数 main();