創意題與資料庫整合

This commit is contained in:
2025-09-29 00:50:47 +08:00
parent eb073c7c1d
commit 052eb0ea7b
10 changed files with 562 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import { testConnection } from './connection'
import { createUsersTable } from './models/user'
import { createLogicQuestionsTable } from './models/logic_question'
import { createCreativeQuestionsTable } from './models/creative_question'
// 初始化資料庫
export async function initializeDatabase(): Promise<boolean> {
@@ -16,10 +17,13 @@ export async function initializeDatabase(): Promise<boolean> {
// 建立用戶表
await createUsersTable()
// 建立邏輯思維題目表
await createLogicQuestionsTable()
// 建立創意能力測試題目表
await createCreativeQuestionsTable()
console.log('✅ 資料庫初始化完成')
return true
} catch (error) {

View File

@@ -0,0 +1,82 @@
import { executeQuery, executeQueryOne } from '../connection'
export interface CreativeQuestion {
id: number
statement: string
category: 'innovation' | 'imagination' | 'flexibility' | 'originality'
is_reverse: boolean
created_at: string
}
export interface CreateCreativeQuestionData {
statement: string
category: 'innovation' | 'imagination' | 'flexibility' | 'originality'
is_reverse: boolean
}
// 建立創意能力測試題目表(如果不存在)
export async function createCreativeQuestionsTable(): Promise<void> {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS creative_questions (
id INT AUTO_INCREMENT PRIMARY KEY,
statement TEXT NOT NULL,
category ENUM('innovation', 'imagination', 'flexibility', 'originality') NOT NULL,
is_reverse BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`
await executeQuery(createTableQuery)
console.log('✅ 創意能力測試題目表建立成功')
}
// 建立新題目
export async function createCreativeQuestion(questionData: CreateCreativeQuestionData): Promise<CreativeQuestion | null> {
const query = `
INSERT INTO creative_questions (statement, category, is_reverse)
VALUES (?, ?, ?)
`
const { statement, category, is_reverse } = questionData
try {
const result = await executeQuery(query, [statement, category, is_reverse])
// 獲取插入的 ID
const insertId = (result as any).insertId
return await findCreativeQuestionById(insertId)
} catch (error) {
console.error('建立創意能力測試題目失敗:', error)
return null
}
}
// 根據 ID 查找題目
export async function findCreativeQuestionById(id: number): Promise<CreativeQuestion | null> {
const query = 'SELECT * FROM creative_questions WHERE id = ?'
return await executeQueryOne<CreativeQuestion>(query, [id])
}
// 獲取所有題目
export async function getAllCreativeQuestions(): Promise<CreativeQuestion[]> {
const query = 'SELECT * FROM creative_questions ORDER BY created_at'
return await executeQuery<CreativeQuestion[]>(query)
}
// 清空所有題目
export async function clearCreativeQuestions(): Promise<void> {
await executeQuery('DELETE FROM creative_questions')
console.log('✅ 已清空所有創意能力測試題目')
}
// 建立多個題目
export async function createMultipleCreativeQuestions(questionsData: CreateCreativeQuestionData[]): Promise<CreativeQuestion[]> {
const createdQuestions: CreativeQuestion[] = []
for (const questionData of questionsData) {
const createdQuestion = await createCreativeQuestion(questionData)
if (createdQuestion) {
createdQuestions.push(createdQuestion)
}
}
return createdQuestions
}

View File

@@ -0,0 +1,136 @@
import { createMultipleCreativeQuestions, clearCreativeQuestions } from './models/creative_question'
import { initializeDatabase } from './init'
// 創意能力測試題目數據
const creativeQuestions = [
{
statement: "我常能從不同角度看事情,接受多元觀點。",
category: "flexibility" as const,
is_reverse: false
},
{
statement: "我有時會提出具挑戰性或爭議性的想法,促使他人表達不同觀點。",
category: "innovation" as const,
is_reverse: false
},
{
statement: "我習慣一次只做一件事,不輕易嘗試新方法。",
category: "flexibility" as const,
is_reverse: true
},
{
statement: "當靈感枯竭時,我仍能找到突破的方法。",
category: "imagination" as const,
is_reverse: false
},
{
statement: "我喜歡與不同背景的人合作,從差異中獲得新想法。",
category: "innovation" as const,
is_reverse: false
},
{
statement: "我通常笑得比別人多,並帶動正面氛圍。",
category: "originality" as const,
is_reverse: false
},
{
statement: "我會追根究柢思考,直到找到事件背後的原因。",
category: "imagination" as const,
is_reverse: false
},
{
statement: "我更喜歡看到整體格局,而不是專注在細節上。",
category: "originality" as const,
is_reverse: false
},
{
statement: "我認為規定和框架在組織中絕對必要。",
category: "flexibility" as const,
is_reverse: true
},
{
statement: "我通常會先做詳細規劃,然後按部就班執行。",
category: "flexibility" as const,
is_reverse: true
},
{
statement: "我能找到更快的方法或捷徑完成任務。",
category: "innovation" as const,
is_reverse: false
},
{
statement: "我喜歡解謎或挑戰看似難解的問題。",
category: "imagination" as const,
is_reverse: false
},
{
statement: "我能接受頻繁的改變,並調整自己因應。",
category: "flexibility" as const,
is_reverse: false
},
{
statement: "我通常不輕易說出心中想法,除非被問到。",
category: "originality" as const,
is_reverse: true
},
{
statement: "我經常追求穩定感,避免風險。",
category: "flexibility" as const,
is_reverse: true
},
{
statement: "當遇到一個陌生問題時,我會主動去探索,即使沒有明確指引。",
category: "innovation" as const,
is_reverse: false
},
{
statement: "當既有方法行不通時,我會刻意嘗試完全相反的方向。",
category: "originality" as const,
is_reverse: false
},
{
statement: "即使存在風險,我也願意嘗試新的解決方法。",
category: "innovation" as const,
is_reverse: false
}
]
// 種子創意能力測試題目
export async function seedCreativeQuestions(): Promise<void> {
try {
await initializeDatabase()
console.log('🔄 正在種子創意能力測試題目...')
// 清空現有題目數據
await clearCreativeQuestions()
// 建立題目
const createdQuestions = await createMultipleCreativeQuestions(creativeQuestions)
console.log(`✅ 成功建立 ${createdQuestions.length} 道創意能力測試題目`)
// 顯示題目摘要
console.log('\n📋 題目摘要:')
createdQuestions.forEach((question, index) => {
const reverseText = question.is_reverse ? ' (反向題)' : ''
console.log(`${index + 1}. ${question.statement.substring(0, 30)}...${reverseText}`)
})
console.log('\n📊 統計:')
const reverseCount = createdQuestions.filter(q => q.is_reverse).length
const normalCount = createdQuestions.length - reverseCount
console.log(`- 一般題目: ${normalCount}`)
console.log(`- 反向題目: ${reverseCount}`)
} catch (error) {
console.error('❌ 種子創意能力測試題目失敗:', error)
throw error
}
}
if (require.main === module) {
seedCreativeQuestions().catch(error => {
console.error('執行 seedCreativeQuestions 腳本失敗:', error)
process.exit(1)
})
}