新增歷史履歷

This commit is contained in:
2025-09-23 20:58:50 +08:00
parent 60358bca3e
commit d9fd0e4fbd
4 changed files with 351 additions and 144 deletions

99
app/api/history/route.ts Normal file
View File

@@ -0,0 +1,99 @@
import { NextRequest, NextResponse } from 'next/server';
import { ProjectService, EvaluationService, ProjectFileService } from '@/lib/services/database';
export async function GET(request: NextRequest) {
try {
console.log('📊 開始獲取歷史記錄數據...');
// 獲取所有專案及其評審結果
const projects = await ProjectService.getAll();
console.log(`📋 找到 ${projects.length} 個專案`);
const historyData = [];
for (const project of projects) {
// 獲取該專案的最新評審結果
const latestEvaluation = await EvaluationService.findByProjectId(project.id);
// 獲取該專案的文件信息
const projectFiles = await ProjectFileService.findByProjectId(project.id);
// 判斷文件類型
let fileType = 'Unknown';
if (projectFiles.length > 0) {
const fileTypeFromDB = projectFiles[0].file_type;
if (fileTypeFromDB) {
// 根據 file_type 判斷文件類型
if (fileTypeFromDB.toLowerCase().includes('ppt') || fileTypeFromDB.toLowerCase().includes('powerpoint')) {
fileType = 'PPT';
} else if (fileTypeFromDB.toLowerCase().includes('mp4') || fileTypeFromDB.toLowerCase().includes('avi') || fileTypeFromDB.toLowerCase().includes('mov')) {
fileType = 'Video';
} else if (fileTypeFromDB.toLowerCase().includes('pdf')) {
fileType = 'PDF';
} else if (fileTypeFromDB.toLowerCase().includes('html') || fileTypeFromDB.toLowerCase().includes('htm')) {
fileType = 'Website';
} else {
// 如果無法從 file_type 判斷,嘗試從檔名判斷
const fileName = projectFiles[0].original_name.toLowerCase();
if (fileName.includes('.ppt') || fileName.includes('.pptx')) {
fileType = 'PPT';
} else if (fileName.includes('.mp4') || fileName.includes('.avi') || fileName.includes('.mov')) {
fileType = 'Video';
} else if (fileName.includes('.pdf')) {
fileType = 'PDF';
} else if (fileName.includes('.html') || fileName.includes('.htm')) {
fileType = 'Website';
} else {
fileType = fileTypeFromDB.toUpperCase();
}
}
}
} else {
// 如果沒有文件記錄,嘗試從專案標題判斷
if (project.title.includes('PPT') || project.title.includes('簡報')) {
fileType = 'PPT';
} else if (project.title.includes('網站') || project.title.includes('Website')) {
fileType = 'Website';
} else if (project.title.includes('影片') || project.title.includes('Video')) {
fileType = 'Video';
}
}
// 格式化日期
const formatDate = (date: Date) => {
return new Date(date).toISOString().split('T')[0];
};
historyData.push({
id: project.id.toString(),
title: project.title,
type: fileType,
score: latestEvaluation?.overall_score || 0,
grade: latestEvaluation?.grade || '-',
date: formatDate(project.created_at),
status: project.status === 'completed' ? 'completed' : 'processing',
evaluation_id: latestEvaluation?.id || null,
description: project.description || '',
created_at: project.created_at,
updated_at: project.updated_at
});
}
// 按創建時間倒序排列
historyData.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
console.log(`✅ 成功獲取 ${historyData.length} 條歷史記錄`);
return NextResponse.json({
success: true,
data: historyData
});
} catch (error) {
console.error('❌ 獲取歷史記錄失敗:', error);
return NextResponse.json(
{ success: false, error: '獲取歷史記錄失敗' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from 'next/server';
import { ProjectService, EvaluationService } from '@/lib/services/database';
export async function GET(request: NextRequest) {
try {
console.log('📊 開始獲取統計數據...');
// 獲取所有專案
const projects = await ProjectService.getAll();
console.log(`📋 找到 ${projects.length} 個專案`);
// 計算統計數據
const totalProjects = projects.length;
const completedProjects = projects.filter(p => p.status === 'completed').length;
const processingProjects = projects.filter(p => p.status === 'analyzing' || p.status === 'pending').length;
// 計算平均分數
let totalScore = 0;
let scoredProjects = 0;
for (const project of projects) {
if (project.status === 'completed') {
const evaluation = await EvaluationService.findByProjectId(project.id);
console.log(`專案 ${project.id} (${project.title}): 狀態=${project.status}, 評審=${evaluation ? '有' : '無'}, 分數=${evaluation?.overall_score || '無'}`);
if (evaluation && evaluation.overall_score) {
totalScore += evaluation.overall_score;
scoredProjects++;
}
}
}
const averageScore = scoredProjects > 0 ? Math.round(totalScore / scoredProjects) : 0;
console.log(`平均分數計算: 總分=${totalScore}, 有分數的專案數=${scoredProjects}, 平均分數=${averageScore}`);
const stats = {
totalProjects,
completedProjects,
processingProjects,
averageScore
};
console.log('✅ 統計數據:', stats);
return NextResponse.json({
success: true,
data: stats
});
} catch (error) {
console.error('❌ 獲取統計數據失敗:', error);
return NextResponse.json(
{ success: false, error: '獲取統計數據失敗' },
{ status: 500 }
);
}
}