Bladeren bron

数据库

S0025136190 1 jaar geleden
bovenliggende
commit
02ae4e67fa
2 gewijzigde bestanden met toevoegingen van 8 en 20 verwijderingen
  1. 5 6
      app/api/bigModel/route.ts
  2. 3 14
      app/components/Record.tsx

+ 5 - 6
app/api/bigModel/route.ts

@@ -31,9 +31,9 @@ export async function POST(req: any) {
 
     const [rows, fields] = await connection.query('SELECT * FROM dialog where id = ?', [id]);
 
-    if (!rows) {
+    if (rows.length === 0) {
       // 执行 MySQL 添加头表
-      await connection.execute('INSERT INTO dialog (id, create_time) VALUES (?,?)', [data, new Date])
+      connection.execute('INSERT INTO dialog (id, create_time) VALUES (?,?)', [id, new Date])
     }
 
     messages.forEach(async function (message) {
@@ -41,11 +41,10 @@ export async function POST(req: any) {
       const role = message.role;
       const createDate = message.date;
       const content = message.content;
-      console.log(message);
       const [detailRows, fields] = await connection.query('SELECT * FROM dialog_detail where id = ?', [detailId]);
-      if (!detailRows) {
+      if (detailRows.length === 0) {
         // 执行 MySQL 添加头表
-        await connection.execute('INSERT INTO dialog_detail (id,dialog_id,type,content,create_by, create_time) VALUES (?, ?, ?)', [detailId, id, role, content, role, createDate])
+        connection.execute('INSERT INTO dialog_detail (id,dialog_id,type,content,create_by, create_time) VALUES (?, ?, ?,?,?,?)', [detailId, id, role, content, role, createDate])
       }
     });
 
@@ -64,7 +63,7 @@ export async function GET() {
     const connection = await pool.getConnection()
 
     // 执行 MySQL 查询
-    const [rows, fields] = await connection.query('SELECT he.id,he.dialog_name,de.id did,de.dialog_id,de.type,de.content FROM dialog he left join dialog_detail de on he.id = de.dialog_id')
+    const [rows, fields] = await connection.query('SELECT he.id,he.dialog_name,de.id did,de.dialog_id,de.type,de.content,de.create_time FROM dialog he left join dialog_detail de on he.id = de.dialog_id')
 
     // 释放连接回连接池
     connection.release()

+ 3 - 14
app/components/Record.tsx

@@ -8,28 +8,17 @@ const RecordApp: React.FC = () => {
     const [account, setAccount] = React.useState('');
     const [password, setPassword] = React.useState('');
 
-    // 模拟获取聊天记录数据
-    const getChatRecords = () => {
-        const records = [
-            { id: 1, timestamp: '2024-08-15 10:00:00', message: '你好,欢迎来到建科招聘!' },
-            { id: 2, timestamp: '2024-08-15 10:01:00', message: '请问有什么可以帮助您的?' },
-            { id: 3, timestamp: '2024-08-15 10:02:00', message: '我想了解关于软件工程师的职位详情。' },
-        ];
-        return records;
-    };
-
     // 点击导出
     const onClickExport = async (data: { account: string, password: string }) => {
         if (data.account && data.password) {
             if (data.account === 'root' && password === 'root@2024') {
-                // const res = await fetch('/api/bigModel');
-                // 模拟获取数据
-                const res = getChatRecords();
+                const res = await fetch('/api/bigModel');
+                const {data} = await res.json()
                 // 导出数据到Excel
                 const option = {
                     fileName: '聊天记录',
                     datas: [{
-                        sheetData: res,
+                        sheetData: data,
                         sheetName: '聊天记录',
                     }],
                 };