// 详细检查chat_online_status表的数据 const { Sequelize } = require('sequelize'); console.log('=== 详细检查chat_online_status表数据 ===\n'); // 创建Sequelize实例(使用与项目相同的配置) const sequelize = new Sequelize('wechat_app', 'root', '', { host: '1.95.162.61', port: 3306, dialect: 'mysql', logging: false }); async function checkChatOnlineStatus() { try { // 测试数据库连接 console.log('正在连接数据库...'); await sequelize.authenticate(); console.log('✅ 数据库连接成功\n'); // 1. 查询chat_online_status表中的所有客服记录 console.log('1. 查询所有客服在线状态记录:'); const [allStatuses] = await sequelize.query( 'SELECT * FROM chat_online_status WHERE type = 2 ORDER BY updated_at DESC', { type: Sequelize.QueryTypes.SELECT } ); console.log(`找到 ${allStatuses.length} 条客服在线状态记录\n`); // 打印详细信息 allStatuses.forEach((status, index) => { console.log(`记录 ${index + 1}:`); console.log(` userId: ${status.userId}`); console.log(` type: ${status.type === 2 ? '客服' : status.type}`); console.log(` socket_id: ${status.socket_id}`); console.log(` is_online: ${status.is_online === 1 ? '✅ 在线' : '❌ 离线'}`); console.log(` last_heartbeat: ${status.last_heartbeat}`); console.log(` updated_at: ${status.updated_at}`); console.log(` created_at: ${status.created_at}`); console.log(` device_info: ${status.device_info}`); console.log('---'); }); // 2. 特别查询managerId=22的客服记录(我们测试的客服) console.log('\n2. 特别查询测试客服(managerId=22)的状态:'); const [targetManagerStatus] = await sequelize.query( 'SELECT * FROM chat_online_status WHERE userId = ? AND type = 2', { replacements: ['22'], type: Sequelize.QueryTypes.SELECT } ); if (targetManagerStatus) { console.log(`✅ 找到测试客服记录:`); console.log(` 在线状态: ${targetManagerStatus.is_online === 1 ? '✅ 在线' : '❌ 离线'}`); console.log(` 最后心跳: ${targetManagerStatus.last_heartbeat}`); console.log(` 最后更新: ${targetManagerStatus.updated_at}`); } else { console.log(`❌ 未找到managerId=22的客服记录`); } // 3. 查询personnel表中managerId与id的映射关系 console.log('\n3. 查询personnel表中的managerId与id关系:'); const [personnelData] = await sequelize.query( 'SELECT id, managerId, name, phoneNumber FROM userlogin.personnel WHERE projectName = ? AND phoneNumber IS NOT NULL LIMIT 10', { replacements: ['采购员'], type: Sequelize.QueryTypes.SELECT } ); console.log(`找到 ${personnelData.length} 条采购员记录,查看id与managerId的关系:`); personnelData.forEach(person => { console.log(` ${person.name}: id=${person.id}, managerId=${person.managerId || 'null'}`); }); // 4. 检查我们测试使用的managerId=22在personnel表中的对应关系 console.log('\n4. 检查测试客服(managerId=22)在personnel表中的记录:'); const [targetPersonnel] = await sequelize.query( 'SELECT id, managerId, name, phoneNumber FROM userlogin.personnel WHERE managerId = ? OR id = ?', { replacements: ['22', '22'], type: Sequelize.QueryTypes.SELECT } ); if (targetPersonnel) { console.log(`✅ 找到记录:`); console.log(` 姓名: ${targetPersonnel.name}`); console.log(` id: ${targetPersonnel.id}`); console.log(` managerId: ${targetPersonnel.managerId || 'null'}`); console.log(` 手机号: ${targetPersonnel.phoneNumber}`); } else { console.log(`❌ 未找到managerId=22或id=22的记录`); } } catch (error) { console.error('❌ 查询数据库时出错:', error); } finally { // 关闭连接 await sequelize.close(); console.log('\n✅ 数据库连接已关闭'); } } // 执行查询 checkChatOnlineStatus();