You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
4.0 KiB
134 lines
4.0 KiB
// 主程序 - 数据同步服务
|
|
const databaseService = require('./src/services/databaseService');
|
|
const jiandaoyunService = require('./src/services/jiandaoyunService');
|
|
const config = require('./src/config/config');
|
|
|
|
async function syncData() {
|
|
console.log('===== 开始数据同步 =====');
|
|
|
|
try {
|
|
// 1. 连接数据库
|
|
const connection = await databaseService.connect();
|
|
|
|
// 2. 查询所有需要同步的数据
|
|
console.log('正在查询数据库数据...');
|
|
const syncData = await databaseService.getAllSyncData();
|
|
console.log(`共查询到 ${syncData.length} 条需要同步的数据`);
|
|
|
|
if (syncData.length === 0) {
|
|
console.log('没有需要同步的数据');
|
|
return;
|
|
}
|
|
|
|
// 3. 批量提交数据到简道云
|
|
console.log('开始向简道云提交数据...');
|
|
const results = await jiandaoyunService.batchSubmitData(syncData, connection);
|
|
|
|
// 4. 更新同步状态
|
|
console.log('\n更新同步状态...');
|
|
for (let i = 0; i < results.length; i++) {
|
|
const result = results[i];
|
|
const data = syncData[i];
|
|
|
|
if (result.success && !result.skipped) {
|
|
// 同步成功且未被跳过,更新状态为已同步
|
|
await databaseService.updateSyncStatus(data.userId, true);
|
|
}
|
|
}
|
|
|
|
// 5. 统计结果
|
|
const successCount = results.filter(result => result.success).length;
|
|
const failCount = results.filter(result => !result.success).length;
|
|
|
|
console.log(`\n===== 数据同步完成 =====`);
|
|
console.log(`成功提交: ${successCount} 条`);
|
|
console.log(`失败提交: ${failCount} 条`);
|
|
|
|
// 6. 输出失败详情
|
|
if (failCount > 0) {
|
|
console.log(`\n失败详情:`);
|
|
results.forEach((result, index) => {
|
|
if (!result.success) {
|
|
console.log(`第 ${index + 1} 条数据失败: ${result.error}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('数据同步过程中发生错误:', error.message);
|
|
} finally {
|
|
// 断开数据库连接
|
|
await databaseService.disconnect();
|
|
}
|
|
}
|
|
|
|
// 测试数据库连接
|
|
async function testDatabase() {
|
|
console.log('===== 测试数据库连接 =====');
|
|
return await databaseService.testDatabaseConnection();
|
|
}
|
|
|
|
// 测试简道云API连接
|
|
async function testJiandaoyun() {
|
|
console.log('\n===== 测试简道云API连接 =====');
|
|
return await jiandaoyunService.testApiConnection();
|
|
}
|
|
|
|
// 主函数
|
|
async function main() {
|
|
// 解析命令行参数
|
|
const args = process.argv.slice(2);
|
|
|
|
if (args.includes('--test-db')) {
|
|
// 仅测试数据库连接
|
|
await testDatabase();
|
|
} else if (args.includes('--test-jiandao')) {
|
|
// 仅测试简道云API连接
|
|
await testJiandaoyun();
|
|
} else if (args.includes('--test-all')) {
|
|
// 测试所有连接
|
|
await testDatabase();
|
|
await testJiandaoyun();
|
|
} else {
|
|
// 检查是否需要临时切换同步模式
|
|
if (args.includes('--full-sync')) {
|
|
console.log('临时切换到全量同步模式');
|
|
config.sync.incremental = false;
|
|
|
|
// 仅执行一次全量同步
|
|
await syncData();
|
|
} else if (args.includes('--incremental-sync')) {
|
|
console.log('临时切换到增量同步模式');
|
|
config.sync.incremental = true;
|
|
|
|
// 仅执行一次增量同步
|
|
await syncData();
|
|
} else {
|
|
// 启动自动同步服务
|
|
await startAutoSync();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 自动同步函数
|
|
async function startAutoSync() {
|
|
console.log('===== 启动自动同步服务 =====');
|
|
console.log(`自动同步间隔时间:${config.sync.autoSync.interval}分钟`);
|
|
|
|
// 立即执行一次同步
|
|
await syncData();
|
|
|
|
// 设置定时器,定期执行同步
|
|
const intervalMs = config.sync.autoSync.interval * 60 * 1000;
|
|
setInterval(async () => {
|
|
console.log(`\n===== 自动执行数据同步 =====`);
|
|
console.log(`当前时间:${new Date().toLocaleString()}`);
|
|
await syncData();
|
|
}, intervalMs);
|
|
}
|
|
|
|
// 执行主函数
|
|
main().catch(error => {
|
|
console.error('程序执行失败:', error.message);
|
|
process.exit(1);
|
|
});
|
|
|