// ===================================================== // 評審統計 API // ===================================================== import { NextResponse } from 'next/server'; import { JudgeService } from '@/lib/services/database-service'; // 獲取評審統計 export async function GET() { try { const judges = await JudgeService.getAllJudges(); // 計算統計數據 const totalJudges = judges.length; const activeJudges = judges.filter(judge => judge.is_active).length; const inactiveJudges = judges.filter(judge => !judge.is_active).length; // 按部門統計 const departmentStats = judges.reduce((acc, judge) => { const dept = judge.department || '未分類'; if (!acc[dept]) { acc[dept] = { total: 0, active: 0, inactive: 0 }; } acc[dept].total++; if (judge.is_active) { acc[dept].active++; } else { acc[dept].inactive++; } return acc; }, {} as Record); // 按專業領域統計 const expertiseStats = judges.reduce((acc, judge) => { judge.expertise.forEach(exp => { if (!acc[exp]) { acc[exp] = { total: 0, active: 0, inactive: 0 }; } acc[exp].total++; if (judge.is_active) { acc[exp].active++; } else { acc[exp].inactive++; } }); return acc; }, {} as Record); const stats = { totalJudges, activeJudges, inactiveJudges, departmentStats, expertiseStats, lastUpdated: new Date().toISOString() }; return NextResponse.json({ success: true, message: '評審統計獲取成功', data: stats }); } catch (error) { console.error('獲取評審統計失敗:', error); return NextResponse.json({ success: false, message: '獲取評審統計失敗', error: error instanceof Error ? error.message : '未知錯誤' }, { status: 500 }); } }