diff --git a/app/api/admin/scoring/route.ts b/app/api/admin/scoring/route.ts index d6a2edd..44bf1d4 100644 --- a/app/api/admin/scoring/route.ts +++ b/app/api/admin/scoring/route.ts @@ -260,9 +260,15 @@ export async function POST(request: NextRequest) { } // 驗證評分格式(基於實際的競賽規則) + console.log('🔍 競賽規則:', rules); + console.log('🔍 提交的評分:', scores); + const providedScores = Object.keys(scores).filter(key => scores[key] > 0); const invalidScores = providedScores.filter(score => scores[score] < 1 || scores[score] > 10); + console.log('🔍 提供的評分項目:', providedScores); + console.log('🔍 無效的評分項目:', invalidScores); + if (invalidScores.length > 0) { return NextResponse.json({ success: false, @@ -278,6 +284,16 @@ export async function POST(request: NextRequest) { error: '至少需要提供一個評分項目' }, { status: 400 }); } + + // 驗證所有競賽規則都有對應的評分 + const ruleNames = rules.map((rule: any) => rule.name); + const missingRules = ruleNames.filter(ruleName => !(ruleName in scores) || scores[ruleName] <= 0); + + if (missingRules.length > 0) { + console.log('⚠️ 缺少評分的規則:', missingRules); + console.log('可用規則:', ruleNames); + console.log('提供的評分:', Object.keys(scores)); + } // 計算總分(基於權重,轉換為100分制) let totalScore = 0; diff --git a/components/admin/scoring-management.tsx b/components/admin/scoring-management.tsx index c3706eb..ce8b86d 100644 --- a/components/admin/scoring-management.tsx +++ b/components/admin/scoring-management.tsx @@ -195,11 +195,20 @@ export function ScoringManagement() { rules.forEach((rule: any) => { const score = scores[rule.name] || 0 const weight = parseFloat(rule.weight) || 1 - totalScore += score * weight - totalWeight += weight + if (score > 0) { + totalScore += score * (weight / 100) + totalWeight += weight + } }) - return totalWeight > 0 ? totalScore / totalWeight : 0 + // 如果總權重為0,使用平均分 + if (totalWeight === 0) { + const validScores = Object.values(scores).filter(score => score > 0) + totalScore = validScores.length > 0 ? validScores.reduce((sum, score) => sum + score, 0) / validScores.length : 0 + } + + // 轉換為100分制(10分制 * 10 = 100分制) + return totalScore } // 生成所有評審和APP的組合 @@ -408,7 +417,7 @@ export function ScoringManagement() { participantName: competitionParticipants.find(p => p.id === manualScoring.participantId)?.displayName || competitionParticipants.find(p => p.id === manualScoring.participantId)?.name || '未知參賽者', participantType: competitionParticipants.find(p => p.id === manualScoring.participantId)?.type as "individual" | "team" || "individual", scores: apiScores, - totalScore: calculateTotalScore(apiScores, rules), + totalScore: data.data?.total_score || calculateTotalScore(apiScores, rules), comments: manualScoring.comments.trim(), status: "completed", submittedAt: new Date().toISOString() diff --git a/lib/services/database-service.ts b/lib/services/database-service.ts index dd380c9..bbe9600 100644 --- a/lib/services/database-service.ts +++ b/lib/services/database-service.ts @@ -3872,7 +3872,12 @@ export class ScoringService extends DatabaseServiceBase { ); // 4. 插入新的評分詳情 + console.log('🔍 開始插入評分詳情,scores:', scores); + console.log('🔍 競賽規則數量:', rules.length); + for (const [ruleName, score] of Object.entries(scores)) { + console.log(`🔍 處理評分項目: ${ruleName} = ${score}`); + if (typeof score === 'number' && score > 0) { // 找到對應的規則 const rule = rules.find((r: any) => r.name === ruleName); @@ -3881,7 +3886,7 @@ export class ScoringService extends DatabaseServiceBase { if (rule) { const detailId = `jsd_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; - console.log('🔍 插入評分詳情:', { + console.log('🔍 準備插入評分詳情:', { detailId, finalJudgeScoreId, ruleId: rule.id, @@ -3890,23 +3895,48 @@ export class ScoringService extends DatabaseServiceBase { weight: rule.weight }); - await DatabaseServiceBase.safeInsert(` - INSERT INTO judge_score_details (id, judge_score_id, rule_id, rule_name, score, weight) - VALUES (?, ?, ?, ?, ?, ?) - `, [ - detailId, - finalJudgeScoreId, - rule.id, - rule.name, - score, - rule.weight - ]); + try { + await DatabaseServiceBase.safeInsert(` + INSERT INTO judge_score_details (id, judge_score_id, rule_id, rule_name, score, weight) + VALUES (?, ?, ?, ?, ?, ?) + `, [ + detailId, + finalJudgeScoreId, + rule.id, + rule.name, + score, + rule.weight + ]); + console.log('✅ 成功插入評分詳情:', ruleName); + } catch (insertError) { + console.error('❌ 插入評分詳情失敗:', insertError); + console.error('插入數據:', { + detailId, + finalJudgeScoreId, + ruleId: rule.id, + ruleName: rule.name, + score, + weight: rule.weight + }); + throw insertError; + } } else { console.log(`⚠️ 找不到規則: ${ruleName}`); + console.log('可用規則:', rules.map((r: any) => r.name)); } + } else { + console.log(`⚠️ 跳過無效評分: ${ruleName} = ${score}`); } } + // 驗證插入結果 + const insertedDetails = await DatabaseServiceBase.safeQuery( + 'SELECT * FROM judge_score_details WHERE judge_score_id = ?', + [finalJudgeScoreId] + ); + console.log('🔍 插入後的評分詳情數量:', insertedDetails.length); + console.log('🔍 插入後的評分詳情:', insertedDetails); + // 返回完整的評分記錄 return await this.getJudgeScoreById(finalJudgeScoreId);