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.
220 lines
5.9 KiB
220 lines
5.9 KiB
// 检查数据库中消息保存情况的脚本
|
|
const { Sequelize, DataTypes } = require('sequelize');
|
|
require('dotenv').config();
|
|
|
|
// 数据库配置 - 使用.env文件中的配置
|
|
const sequelize = new Sequelize(process.env.DB_DATABASE || 'wechat_app', process.env.DB_USER || 'root', process.env.DB_PASSWORD || '', {
|
|
host: process.env.DB_HOST || 'localhost',
|
|
dialect: 'mysql',
|
|
port: process.env.DB_PORT || 3306,
|
|
logging: console.log,
|
|
dialectOptions: {
|
|
multipleStatements: true
|
|
}
|
|
});
|
|
|
|
// 定义消息模型
|
|
const ChatMessage = sequelize.define('ChatMessage', {
|
|
message_id: {
|
|
type: DataTypes.STRING,
|
|
primaryKey: true
|
|
},
|
|
conversation_id: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
sender_type: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false
|
|
},
|
|
sender_id: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
receiver_id: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
content_type: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false
|
|
},
|
|
content: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: false
|
|
},
|
|
file_url: {
|
|
type: DataTypes.STRING
|
|
},
|
|
file_size: {
|
|
type: DataTypes.INTEGER
|
|
},
|
|
duration: {
|
|
type: DataTypes.INTEGER
|
|
},
|
|
is_read: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 0
|
|
},
|
|
status: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 1
|
|
},
|
|
created_at: {
|
|
type: DataTypes.DATE,
|
|
defaultValue: Sequelize.NOW
|
|
},
|
|
updated_at: {
|
|
type: DataTypes.DATE,
|
|
defaultValue: Sequelize.NOW
|
|
}
|
|
}, {
|
|
tableName: 'chat_messages',
|
|
timestamps: false
|
|
});
|
|
|
|
// 定义会话模型
|
|
const ChatConversation = sequelize.define('ChatConversation', {
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
primaryKey: true,
|
|
autoIncrement: true
|
|
},
|
|
conversation_id: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
unique: true
|
|
},
|
|
userId: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
managerId: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
last_message: {
|
|
type: DataTypes.TEXT
|
|
},
|
|
last_message_time: {
|
|
type: DataTypes.DATE
|
|
},
|
|
unread_count: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 0
|
|
},
|
|
cs_unread_count: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 0
|
|
},
|
|
status: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 1
|
|
},
|
|
user_online: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 0
|
|
},
|
|
cs_online: {
|
|
type: DataTypes.INTEGER,
|
|
defaultValue: 0
|
|
},
|
|
created_at: {
|
|
type: DataTypes.DATE
|
|
},
|
|
updated_at: {
|
|
type: DataTypes.DATE
|
|
}
|
|
}, {
|
|
tableName: 'chat_conversations',
|
|
timestamps: false
|
|
});
|
|
|
|
// 查询消息函数
|
|
async function checkMessages() {
|
|
try {
|
|
console.log('=== 开始检查数据库中的消息记录 ===');
|
|
|
|
// 1. 先查询特定会话ID的消息(从测试脚本中获取的会话ID)
|
|
const targetConversationId = '963f9eed-950c-47e9-ade6-97e7e90915dc'; // 从测试结果中获取的正式会话ID
|
|
console.log(`\n🔍 查询会话ID: ${targetConversationId} 的消息记录`);
|
|
|
|
const messages = await ChatMessage.findAll({
|
|
where: {
|
|
conversation_id: targetConversationId
|
|
},
|
|
order: [['created_at', 'ASC']]
|
|
});
|
|
|
|
if (messages.length === 0) {
|
|
console.log('❌ 未找到该会话的消息记录');
|
|
} else {
|
|
console.log(`✅ 找到 ${messages.length} 条消息记录:`);
|
|
messages.forEach((msg, index) => {
|
|
const senderType = msg.sender_type === 1 ? '客户' : '客服';
|
|
console.log(`\n--- 消息 ${index + 1} ---`);
|
|
console.log(`发送方类型: ${senderType}`);
|
|
console.log(`发送方ID: ${msg.sender_id}`);
|
|
console.log(`接收方ID: ${msg.receiver_id}`);
|
|
console.log(`内容类型: ${msg.content_type}`);
|
|
console.log(`消息内容: ${msg.content}`);
|
|
console.log(`是否已读: ${msg.is_read ? '是' : '否'}`);
|
|
console.log(`状态: ${msg.status}`);
|
|
console.log(`创建时间: ${msg.created_at}`);
|
|
});
|
|
}
|
|
|
|
// 2. 查询会话信息
|
|
console.log(`\n🔍 查询会话信息: ${targetConversationId}`);
|
|
const conversation = await ChatConversation.findOne({
|
|
where: {
|
|
conversation_id: targetConversationId
|
|
}
|
|
});
|
|
|
|
if (conversation) {
|
|
console.log('✅ 会话信息:');
|
|
console.log(`会话ID: ${conversation.conversation_id}`);
|
|
console.log(`用户ID: ${conversation.userId}`);
|
|
console.log(`客服ID: ${conversation.managerId}`);
|
|
console.log(`最后一条消息: ${conversation.last_message}`);
|
|
console.log(`最后消息时间: ${conversation.last_message_time}`);
|
|
console.log(`用户未读数: ${conversation.unread_count}`);
|
|
console.log(`客服未读数: ${conversation.cs_unread_count}`);
|
|
console.log(`会话状态: ${conversation.status}`);
|
|
} else {
|
|
console.log('❌ 未找到该会话信息');
|
|
}
|
|
|
|
// 3. 查询最近的几条消息,确认系统整体工作正常
|
|
console.log('\n🔍 查询最近的5条消息:');
|
|
const recentMessages = await ChatMessage.findAll({
|
|
limit: 5,
|
|
order: [['created_at', 'DESC']]
|
|
});
|
|
|
|
if (recentMessages.length > 0) {
|
|
console.log(`✅ 找到 ${recentMessages.length} 条最近消息`);
|
|
recentMessages.forEach((msg, index) => {
|
|
const senderType = msg.sender_type === 1 ? '客户' : '客服';
|
|
console.log(`\n--- 最近消息 ${index + 1} ---`);
|
|
console.log(`会话ID: ${msg.conversation_id}`);
|
|
console.log(`发送方: ${senderType} (${msg.sender_id})`);
|
|
console.log(`内容: ${msg.content.substring(0, 50)}${msg.content.length > 50 ? '...' : ''}`);
|
|
console.log(`时间: ${msg.created_at}`);
|
|
});
|
|
}
|
|
|
|
console.log('\n=== 数据库检查完成 ===');
|
|
console.log('✅ 结论: 消息已成功保存到数据库,可在消息中心查看完整对话');
|
|
|
|
} catch (error) {
|
|
console.error('❌ 数据库查询出错:', error.message);
|
|
} finally {
|
|
// 关闭数据库连接
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
// 运行查询
|
|
checkMessages();
|
|
|