From e5e24d110898b9d1c7e97540542236a25dff7336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B3=E4=BD=A9=E5=BA=AD?= Date: Mon, 29 Sep 2025 20:53:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=95=B4=E5=AF=A6=E7=8F=BE=E8=A9=B3?= =?UTF-8?q?=E7=B4=B0=E7=AD=94=E9=A1=8C=E7=B5=90=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/results/page.tsx | 68 --------- app/api/admin/test-results/detail/route.ts | 166 +++++++++++++-------- app/api/test-db-info/route.ts | 46 ++++++ scripts/check-logic-answers.js | 38 +++++ scripts/check-single-test-answers.js | 66 ++++++++ scripts/test-db-connection.js | 53 +++++++ 6 files changed, 305 insertions(+), 132 deletions(-) create mode 100644 app/api/test-db-info/route.ts create mode 100644 scripts/check-logic-answers.js create mode 100644 scripts/check-single-test-answers.js create mode 100644 scripts/test-db-connection.js diff --git a/app/admin/results/page.tsx b/app/admin/results/page.tsx index 8825e87..4e3e192 100644 --- a/app/admin/results/page.tsx +++ b/app/admin/results/page.tsx @@ -803,74 +803,6 @@ function AdminResultsContent() { )} - {/* 單一測試類型的題目(邏輯或創意) */} - {detailData.result.type !== 'combined' && detailData.questions.map((question: any, index: number) => ( -
-
-

第 {index + 1} 題

- {question.isCorrect !== undefined && ( - - {question.isCorrect ? "正確" : "錯誤"} - - )} - {question.score !== undefined && ( - - {question.score} 分 - - )} -
- -
-
- -

{question.question || question.statement}

-
- - {question.type === 'logic' && ( - <> -
-
- -
- {question.option_a &&

A. {question.option_a}

} - {question.option_b &&

B. {question.option_b}

} - {question.option_c &&

C. {question.option_c}

} - {question.option_d &&

D. {question.option_d}

} - {question.option_e &&

E. {question.option_e}

} -
-
-
- -
-

用戶答案: {question.userAnswer}

-

正確答案: {question.correctAnswer}

-
-
-
- {question.explanation && ( -
- -

{question.explanation}

-
- )} - - )} - - {question.type === 'creative' && ( -
-
- -

{question.userAnswer}

-
-
- -

{question.score} 分

-
-
- )} -
-
- ))} diff --git a/app/api/admin/test-results/detail/route.ts b/app/api/admin/test-results/detail/route.ts index 7c1ec98..d66df47 100644 --- a/app/api/admin/test-results/detail/route.ts +++ b/app/api/admin/test-results/detail/route.ts @@ -111,41 +111,7 @@ export async function GET(request: NextRequest) { console.log('Debug: 總共找到題目數量:', questions.length) - // 如果 breakdown 中沒有詳細答案,嘗試從單獨的答案表獲取 - if (questions.length === 0) { - console.log('從 breakdown 中沒有找到答案,嘗試從答案表獲取...') - const logicAnswers = await getLogicTestAnswersByTestResultId(testResultId) - const creativeAnswers = await getCreativeTestAnswersByTestResultId(testResultId) - - // 處理邏輯題答案 - for (const answer of logicAnswers) { - const question = await findLogicQuestionById(answer.question_id) - if (question) { - questions.push({ - ...question, - type: 'logic', - userAnswer: answer.user_answer, - isCorrect: answer.is_correct, - correctAnswer: answer.correct_answer, - explanation: answer.explanation - }) - } - } - - // 處理創意題答案 - for (const answer of creativeAnswers) { - const question = await findCreativeQuestionById(answer.question_id) - if (question) { - questions.push({ - ...question, - type: 'creative', - userAnswer: answer.user_answer, - score: answer.score, - isReverse: answer.is_reverse - }) - } - } - } + // 綜合測試只從 breakdown 中獲取題目,不重複從答案表獲取 } } else { const testResult = await getTestResultById(testResultId) @@ -160,37 +126,109 @@ export async function GET(request: NextRequest) { } // 獲取詳細答案 - if (testType === "logic") { - answers = await getLogicTestAnswersByTestResultId(testResultId) - console.log('Debug: 邏輯測試答案數量:', answers.length) - // 獲取對應的題目 - for (const answer of answers) { - const question = await findLogicQuestionById(answer.question_id) - if (question) { - questions.push({ - ...question, - userAnswer: answer.user_answer, - isCorrect: answer.is_correct, - correctAnswer: answer.correct_answer, - explanation: answer.explanation - }) - } - } - } else if (testType === "creative") { - answers = await getCreativeTestAnswersByTestResultId(testResultId) - console.log('Debug: 創意測試答案數量:', answers.length) - // 獲取對應的題目 - for (const answer of answers) { - const question = await findCreativeQuestionById(answer.question_id) - if (question) { - questions.push({ - ...question, - userAnswer: answer.user_answer, - score: answer.score, - isReverse: answer.is_reverse - }) + try { + if (testType === "logic") { + console.log('Debug: 查詢邏輯測試答案,testResultId:', testResultId) + + + // 先嘗試從 logic_test_answers 表獲取 + const logicAnswersQuery = ` + SELECT lta.*, lq.question, lq.option_a, lq.option_b, lq.option_c, lq.option_d, lq.option_e, + lq.correct_answer, lq.explanation + FROM logic_test_answers lta + LEFT JOIN logic_questions lq ON lta.question_id = lq.id + WHERE lta.test_result_id = ? + ORDER BY lta.created_at ASC + ` + const logicAnswers = await executeQuery(logicAnswersQuery, [testResultId]) + console.log('Debug: 邏輯測試答案數量:', logicAnswers.length) + + if (logicAnswers.length > 0) { + // 處理邏輯題答案 + for (const answer of logicAnswers) { + if (answer.question) { + questions.push({ + id: answer.question_id, + question: answer.question, + option_a: answer.option_a, + option_b: answer.option_b, + option_c: answer.option_c, + option_d: answer.option_d, + option_e: answer.option_e, + correct_answer: answer.correct_answer, + explanation: answer.explanation, + type: 'logic', + userAnswer: answer.user_answer, + isCorrect: answer.is_correct, + correctAnswer: answer.correct_answer + }) + } + } + } else { + // 如果沒有找到答案,只顯示題目不顯示假答案 + console.log('Debug: 沒有找到邏輯答案,只顯示題目') + const allLogicQuestions = await executeQuery('SELECT * FROM logic_questions ORDER BY id') + + for (let i = 0; i < allLogicQuestions.length; i++) { + const question = allLogicQuestions[i] + questions.push({ + ...question, + type: 'logic', + userAnswer: null, // 不顯示假答案 + isCorrect: null, // 不顯示假結果 + correctAnswer: question.correct_answer + }) + } + } + } else if (testType === "creative") { + console.log('Debug: 查詢創意測試答案,testResultId:', testResultId) + + // 先嘗試從 creative_test_answers 表獲取 + const creativeAnswersQuery = ` + SELECT cta.*, cq.statement, cq.is_reverse + FROM creative_test_answers cta + LEFT JOIN creative_questions cq ON cta.question_id = cq.id + WHERE cta.test_result_id = ? + ORDER BY cta.created_at ASC + ` + const creativeAnswers = await executeQuery(creativeAnswersQuery, [testResultId]) + console.log('Debug: 創意測試答案數量:', creativeAnswers.length) + + if (creativeAnswers.length > 0) { + // 處理創意題答案 + for (const answer of creativeAnswers) { + if (answer.statement) { + questions.push({ + id: answer.question_id, + statement: answer.statement, + is_reverse: answer.is_reverse, + type: 'creative', + userAnswer: answer.user_answer, + score: answer.score, + isReverse: answer.is_reverse + }) + } + } + } else { + // 如果沒有找到答案,只顯示題目不顯示假答案 + console.log('Debug: 沒有找到創意答案,只顯示題目') + const allCreativeQuestions = await executeQuery('SELECT * FROM creative_questions ORDER BY id') + + for (let i = 0; i < allCreativeQuestions.length; i++) { + const question = allCreativeQuestions[i] + questions.push({ + ...question, + type: 'creative', + userAnswer: null, // 不顯示假答案 + score: null, // 不顯示假分數 + isReverse: question.is_reverse + }) + } } } + } catch (error) { + console.error('獲取詳細答案失敗:', error) + // 如果獲取答案失敗,至少返回基本結果 } console.log('Debug: 單一測試類型題目數量:', questions.length) diff --git a/app/api/test-db-info/route.ts b/app/api/test-db-info/route.ts new file mode 100644 index 0000000..f80bb22 --- /dev/null +++ b/app/api/test-db-info/route.ts @@ -0,0 +1,46 @@ +import { NextRequest, NextResponse } from "next/server" +import { executeQuery } from "@/lib/database/connection" + +export async function GET(request: NextRequest) { + try { + // 獲取資料庫信息 + const dbInfo = await executeQuery('SELECT DATABASE() as current_db, USER() as current_user, VERSION() as version') + + // 檢查所有表 + const tables = await executeQuery('SHOW TABLES') + + // 檢查 logic_test_answers 表 + let logicAnswersCount = 0 + let logicAnswersSample = [] + + try { + const countResult = await executeQuery('SELECT COUNT(*) as count FROM logic_test_answers') + logicAnswersCount = countResult[0].count + + if (logicAnswersCount > 0) { + logicAnswersSample = await executeQuery('SELECT * FROM logic_test_answers LIMIT 3') + } + } catch (error) { + console.error('查詢 logic_test_answers 失敗:', error) + } + + return NextResponse.json({ + success: true, + data: { + database: dbInfo[0], + tables: tables.map(t => Object.values(t)[0]), + logicTestAnswers: { + count: logicAnswersCount, + sample: logicAnswersSample + } + } + }) + + } catch (error) { + console.error('獲取資料庫信息失敗:', error) + return NextResponse.json( + { success: false, message: "獲取資料庫信息失敗", error: error instanceof Error ? error.message : String(error) }, + { status: 500 } + ) + } +} diff --git a/scripts/check-logic-answers.js b/scripts/check-logic-answers.js new file mode 100644 index 0000000..ce6b5d6 --- /dev/null +++ b/scripts/check-logic-answers.js @@ -0,0 +1,38 @@ +const { executeQuery } = require('../lib/database/connection'); + +async function checkLogicAnswers() { + try { + console.log('=== 檢查 logic_test_answers 表 ==='); + const answers = await executeQuery('SELECT * FROM logic_test_answers'); + console.log('logic_test_answers 資料總數:', answers.length); + if (answers.length > 0) { + console.log('前3筆資料:', answers.slice(0, 3)); + } + + console.log('\n=== 檢查 test_results 表 ==='); + const results = await executeQuery('SELECT * FROM test_results WHERE type = "logic" ORDER BY created_at DESC LIMIT 5'); + console.log('logic test_results 資料總數:', results.length); + if (results.length > 0) { + console.log('前3筆資料:', results); + + console.log('\n=== 檢查關聯資料 ==='); + for (const result of results.slice(0, 2)) { + console.log(`\n檢查 test_result_id: ${result.id}`); + const relatedAnswers = await executeQuery('SELECT * FROM logic_test_answers WHERE test_result_id = ?', [result.id]); + console.log(`關聯的答案數量: ${relatedAnswers.length}`); + if (relatedAnswers.length > 0) { + console.log('答案資料:', relatedAnswers); + } + } + } + + console.log('\n=== 檢查所有 test_results 類型 ==='); + const allResults = await executeQuery('SELECT type, COUNT(*) as count FROM test_results GROUP BY type'); + console.log('各類型測試結果數量:', allResults); + + } catch (error) { + console.error('錯誤:', error.message); + } +} + +checkLogicAnswers(); diff --git a/scripts/check-single-test-answers.js b/scripts/check-single-test-answers.js new file mode 100644 index 0000000..06abf79 --- /dev/null +++ b/scripts/check-single-test-answers.js @@ -0,0 +1,66 @@ +const { executeQuery } = require('../lib/database/connection'); + +async function checkSingleTestAnswers() { + console.log('🔍 檢查單一測試類型的答案資料'); + console.log('=============================='); + + try { + // 檢查 test_results 表 + console.log('\n📋 Test Results:'); + const testResults = await executeQuery('SELECT id, user_id, test_type, score, completed_at FROM test_results ORDER BY completed_at DESC LIMIT 5'); + testResults.forEach((result, index) => { + console.log(`測試 ${index + 1}:`, { + id: result.id, + user_id: result.user_id, + test_type: result.test_type, + score: result.score, + completed_at: result.completed_at + }); + }); + + // 檢查 logic_test_answers 表 + console.log('\n📋 Logic Test Answers:'); + const logicAnswers = await executeQuery('SELECT * FROM logic_test_answers ORDER BY created_at DESC LIMIT 5'); + logicAnswers.forEach((answer, index) => { + console.log(`邏輯答案 ${index + 1}:`, { + id: answer.id, + test_result_id: answer.test_result_id, + question_id: answer.question_id, + user_answer: answer.user_answer, + is_correct: answer.is_correct + }); + }); + + // 檢查 creative_test_answers 表 + console.log('\n📋 Creative Test Answers:'); + const creativeAnswers = await executeQuery('SELECT * FROM creative_test_answers ORDER BY created_at DESC LIMIT 5'); + creativeAnswers.forEach((answer, index) => { + console.log(`創意答案 ${index + 1}:`, { + id: answer.id, + test_result_id: answer.test_result_id, + question_id: answer.question_id, + user_answer: answer.user_answer, + score: answer.score + }); + }); + + // 檢查是否有匹配的答案 + if (testResults.length > 0) { + const firstTest = testResults[0]; + console.log(`\n🔍 檢查測試 ${firstTest.id} 的答案:`); + + const matchingLogicAnswers = await executeQuery('SELECT * FROM logic_test_answers WHERE test_result_id = ?', [firstTest.id]); + console.log(`邏輯答案匹配數量: ${matchingLogicAnswers.length}`); + + const matchingCreativeAnswers = await executeQuery('SELECT * FROM creative_test_answers WHERE test_result_id = ?', [firstTest.id]); + console.log(`創意答案匹配數量: ${matchingCreativeAnswers.length}`); + } + + } catch (error) { + console.error('❌ 檢查失敗:', error.message); + } + + console.log('==============================\n'); +} + +checkSingleTestAnswers(); diff --git a/scripts/test-db-connection.js b/scripts/test-db-connection.js new file mode 100644 index 0000000..55a4fec --- /dev/null +++ b/scripts/test-db-connection.js @@ -0,0 +1,53 @@ +const { executeQuery } = require('../lib/database/connection'); + +async function testDbConnection() { + console.log('🔍 測試資料庫連接和表結構'); + console.log('=============================='); + + try { + // 測試基本連接 + console.log('1. 測試基本連接...'); + const testQuery = await executeQuery('SELECT 1 as test'); + console.log('✅ 資料庫連接成功:', testQuery); + + // 檢查所有表 + console.log('\n2. 檢查所有表...'); + const tables = await executeQuery('SHOW TABLES'); + console.log('📋 所有表:', tables.map(t => Object.values(t)[0])); + + // 檢查 logic_test_answers 表是否存在 + console.log('\n3. 檢查 logic_test_answers 表...'); + const tableExists = await executeQuery(` + SELECT COUNT(*) as count + FROM information_schema.tables + WHERE table_schema = DATABASE() + AND table_name = 'logic_test_answers' + `); + console.log('logic_test_answers 表存在:', tableExists[0].count > 0); + + // 檢查表結構 + console.log('\n4. 檢查 logic_test_answers 表結構...'); + const tableStructure = await executeQuery('DESCRIBE logic_test_answers'); + console.log('📋 表結構:', tableStructure); + + // 檢查資料數量 + console.log('\n5. 檢查資料數量...'); + const count = await executeQuery('SELECT COUNT(*) as count FROM logic_test_answers'); + console.log('📊 logic_test_answers 資料數量:', count[0].count); + + // 檢查前幾筆資料 + if (count[0].count > 0) { + console.log('\n6. 檢查前 3 筆資料...'); + const sampleData = await executeQuery('SELECT * FROM logic_test_answers LIMIT 3'); + console.log('📋 範例資料:', sampleData); + } + + } catch (error) { + console.error('❌ 測試失敗:', error.message); + console.error('錯誤詳情:', error); + } + + console.log('==============================\n'); +} + +testDbConnection();