4 changed files with 161 additions and 47 deletions
@ -0,0 +1,89 @@ |
|||
// 检查特定用户的同步状态和jiandaoyun_record_id字段
|
|||
const mysql = require('mysql2/promise'); |
|||
const config = require('./src/config/config'); |
|||
|
|||
// 要检查的用户ID
|
|||
const targetUserId = 'user_1766039120078_lcmc7kom1'; |
|||
|
|||
async function checkSpecificUser() { |
|||
console.log(`===== 检查用户 ${targetUserId} 的同步状态 =====`); |
|||
|
|||
let connection = null; |
|||
|
|||
try { |
|||
// 1. 连接数据库
|
|||
console.log('1. 连接数据库...'); |
|||
connection = await mysql.createConnection({ |
|||
host: config.db.host, |
|||
port: config.db.port, |
|||
user: config.db.user, |
|||
password: config.db.password, |
|||
database: config.db.database |
|||
}); |
|||
console.log('数据库连接成功'); |
|||
|
|||
// 2. 查询该用户的详细信息
|
|||
console.log(`\n2. 查询用户 ${targetUserId} 的详细信息...`); |
|||
const checkSql = ` |
|||
SELECT userId, phoneNumber, nickName, type, authorized_region, sync_status, |
|||
last_sync_time, jiandaoyun_record_id, jiandao_record_id |
|||
FROM ${config.tables.users.name} |
|||
WHERE userId = ? |
|||
`;
|
|||
const [rows] = await connection.execute(checkSql, [targetUserId]); |
|||
|
|||
if (rows.length === 0) { |
|||
console.log(`未找到用户 ${targetUserId}`); |
|||
return; |
|||
} |
|||
|
|||
const userInfo = rows[0]; |
|||
console.log('用户信息:', JSON.stringify(userInfo, null, 2)); |
|||
|
|||
// 3. 检查是否有该用户的同步记录
|
|||
console.log(`\n3. 检查用户 ${targetUserId} 是否在同步队列中...`); |
|||
const syncSql = ` |
|||
SELECT userId, phoneNumber, sync_status, jiandaoyun_record_id |
|||
FROM ${config.tables.users.name} |
|||
WHERE (sync_status = 0 OR sync_status IS NULL) |
|||
AND (jiandaoyun_record_id IS NULL OR jiandaoyun_record_id = '') |
|||
LIMIT 10 |
|||
`;
|
|||
const [syncRows] = await connection.execute(syncSql); |
|||
|
|||
console.log(`当前需要同步的用户数量: ${syncRows.length}`); |
|||
const isInSyncQueue = syncRows.some(user => user.userId === targetUserId); |
|||
console.log(`用户 ${targetUserId} 是否在同步队列中: ${isInSyncQueue}`); |
|||
|
|||
if (syncRows.length > 0) { |
|||
console.log('同步队列中的用户ID:', syncRows.map(user => user.userId)); |
|||
} |
|||
|
|||
// 4. 检查是否有该用户的相关产品数据
|
|||
console.log(`\n4. 检查用户 ${targetUserId} 的产品数据...`); |
|||
const productSql = ` |
|||
SELECT * FROM ${config.tables.products.name} |
|||
WHERE userId = ? |
|||
LIMIT 5 |
|||
`;
|
|||
const [productRows] = await connection.execute(productSql, [targetUserId]); |
|||
|
|||
console.log(`用户 ${targetUserId} 的产品数量: ${productRows.length}`); |
|||
if (productRows.length > 0) { |
|||
console.log('产品示例:', JSON.stringify(productRows.slice(0, 2), null, 2)); |
|||
} |
|||
|
|||
} catch (error) { |
|||
console.error('检查过程中发生错误:', error); |
|||
console.error('错误堆栈:', error.stack); |
|||
} finally { |
|||
if (connection) { |
|||
await connection.end(); |
|||
console.log('\n数据库连接已关闭'); |
|||
} |
|||
console.log('\n===== 检查完成 ====='); |
|||
} |
|||
} |
|||
|
|||
// 运行检查
|
|||
checkSpecificUser(); |
|||
@ -0,0 +1,63 @@ |
|||
// 选择有收藏产品的用户并重置同步状态
|
|||
const mysql = require('mysql2/promise'); |
|||
const config = require('./src/config/config'); |
|||
|
|||
async function resetSyncStatus() { |
|||
console.log('===== 重置有收藏产品用户的同步状态 ====='); |
|||
|
|||
try { |
|||
// 连接数据库
|
|||
const connection = await mysql.createConnection(config.db); |
|||
console.log('数据库连接成功'); |
|||
|
|||
// 查询有收藏产品的用户
|
|||
const [usersWithFavorites] = await connection.execute( |
|||
'SELECT DISTINCT u.id, u.userId, u.phoneNumber FROM users u INNER JOIN favorites f ON u.phoneNumber = f.user_phone LIMIT 1' |
|||
); |
|||
|
|||
if (usersWithFavorites.length === 0) { |
|||
console.log('没有找到有收藏产品的用户'); |
|||
await connection.end(); |
|||
return; |
|||
} |
|||
|
|||
const testUser = usersWithFavorites[0]; |
|||
const phoneNumber = testUser.phoneNumber; |
|||
const userId = testUser.userId; |
|||
|
|||
console.log('选择测试用户:', JSON.stringify(testUser)); |
|||
|
|||
// 重置同步状态
|
|||
await connection.execute( |
|||
'UPDATE users SET sync_status = 0 WHERE phoneNumber = ?', |
|||
[phoneNumber] |
|||
); |
|||
|
|||
console.log(`已将用户 ${phoneNumber} 的同步状态重置为未同步`); |
|||
|
|||
// 验证重置结果
|
|||
const [updatedUser] = await connection.execute( |
|||
'SELECT id, userId, phoneNumber, sync_status FROM users WHERE phoneNumber = ?', |
|||
[phoneNumber] |
|||
); |
|||
|
|||
if (updatedUser.length > 0) { |
|||
console.log('重置后的用户状态:', updatedUser[0]); |
|||
} |
|||
|
|||
await connection.end(); |
|||
console.log('\n✅ 操作完成!'); |
|||
|
|||
} catch (error) { |
|||
console.error('操作过程中发生错误:', error.message); |
|||
if (error.sql) { |
|||
console.error('SQL语句:', error.sql); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 执行操作
|
|||
resetSyncStatus().catch(error => { |
|||
console.error('操作执行失败:', error.message); |
|||
process.exit(1); |
|||
}); |
|||
@ -1,46 +0,0 @@ |
|||
// 测试增量同步功能
|
|||
const DatabaseService = require('./src/services/databaseService'); |
|||
const config = require('./src/config/config'); |
|||
|
|||
async function testIncrementalSync() { |
|||
console.log('===== 测试增量同步功能 ====='); |
|||
|
|||
try { |
|||
// 连接数据库
|
|||
await DatabaseService.connect(); |
|||
|
|||
// 检查配置
|
|||
console.log('当前同步配置:', JSON.stringify(config.sync, null, 2)); |
|||
|
|||
// 查询需要同步的数据
|
|||
console.log('查询需要同步的数据...'); |
|||
const syncData = await DatabaseService.getAllSyncData(); |
|||
|
|||
console.log(`\n测试结果:`); |
|||
console.log(`增量同步模式: ${config.sync.incremental ? '开启' : '关闭'}`); |
|||
console.log(`查询到需要同步的数据条数: ${syncData.length}`); |
|||
|
|||
if (syncData.length > 0) { |
|||
console.log('\n前3条数据示例:'); |
|||
for (let i = 0; i < Math.min(3, syncData.length); i++) { |
|||
console.log(`\n用户ID: ${syncData[i].userId}`); |
|||
console.log(`公司: ${syncData[i].user.company}`); |
|||
console.log(`联系人: ${syncData[i].user.nickName}`); |
|||
console.log(`购买商品数量: ${syncData[i].cartItems.length}`); |
|||
console.log(`销售商品数量: ${syncData[i].products.length}`); |
|||
} |
|||
} |
|||
|
|||
} catch (error) { |
|||
console.error('测试过程中发生错误:', error.message); |
|||
} finally { |
|||
// 断开数据库连接
|
|||
await DatabaseService.disconnect(); |
|||
} |
|||
} |
|||
|
|||
// 执行测试
|
|||
testIncrementalSync().catch(error => { |
|||
console.error('测试执行失败:', error.message); |
|||
process.exit(1); |
|||
}); |
|||
Loading…
Reference in new issue