修正評分詳細沒有上傳問題
This commit is contained in:
@@ -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 providedScores = Object.keys(scores).filter(key => scores[key] > 0);
|
||||||
const invalidScores = providedScores.filter(score => scores[score] < 1 || scores[score] > 10);
|
const invalidScores = providedScores.filter(score => scores[score] < 1 || scores[score] > 10);
|
||||||
|
|
||||||
|
console.log('🔍 提供的評分項目:', providedScores);
|
||||||
|
console.log('🔍 無效的評分項目:', invalidScores);
|
||||||
|
|
||||||
if (invalidScores.length > 0) {
|
if (invalidScores.length > 0) {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: false,
|
success: false,
|
||||||
@@ -278,6 +284,16 @@ export async function POST(request: NextRequest) {
|
|||||||
error: '至少需要提供一個評分項目'
|
error: '至少需要提供一個評分項目'
|
||||||
}, { status: 400 });
|
}, { 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分制)
|
// 計算總分(基於權重,轉換為100分制)
|
||||||
let totalScore = 0;
|
let totalScore = 0;
|
||||||
|
@@ -195,11 +195,20 @@ export function ScoringManagement() {
|
|||||||
rules.forEach((rule: any) => {
|
rules.forEach((rule: any) => {
|
||||||
const score = scores[rule.name] || 0
|
const score = scores[rule.name] || 0
|
||||||
const weight = parseFloat(rule.weight) || 1
|
const weight = parseFloat(rule.weight) || 1
|
||||||
totalScore += score * weight
|
if (score > 0) {
|
||||||
totalWeight += weight
|
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的組合
|
// 生成所有評審和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 || '未知參賽者',
|
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",
|
participantType: competitionParticipants.find(p => p.id === manualScoring.participantId)?.type as "individual" | "team" || "individual",
|
||||||
scores: apiScores,
|
scores: apiScores,
|
||||||
totalScore: calculateTotalScore(apiScores, rules),
|
totalScore: data.data?.total_score || calculateTotalScore(apiScores, rules),
|
||||||
comments: manualScoring.comments.trim(),
|
comments: manualScoring.comments.trim(),
|
||||||
status: "completed",
|
status: "completed",
|
||||||
submittedAt: new Date().toISOString()
|
submittedAt: new Date().toISOString()
|
||||||
|
@@ -3872,7 +3872,12 @@ export class ScoringService extends DatabaseServiceBase {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// 4. 插入新的評分詳情
|
// 4. 插入新的評分詳情
|
||||||
|
console.log('🔍 開始插入評分詳情,scores:', scores);
|
||||||
|
console.log('🔍 競賽規則數量:', rules.length);
|
||||||
|
|
||||||
for (const [ruleName, score] of Object.entries(scores)) {
|
for (const [ruleName, score] of Object.entries(scores)) {
|
||||||
|
console.log(`🔍 處理評分項目: ${ruleName} = ${score}`);
|
||||||
|
|
||||||
if (typeof score === 'number' && score > 0) {
|
if (typeof score === 'number' && score > 0) {
|
||||||
// 找到對應的規則
|
// 找到對應的規則
|
||||||
const rule = rules.find((r: any) => r.name === ruleName);
|
const rule = rules.find((r: any) => r.name === ruleName);
|
||||||
@@ -3881,7 +3886,7 @@ export class ScoringService extends DatabaseServiceBase {
|
|||||||
if (rule) {
|
if (rule) {
|
||||||
const detailId = `jsd_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
const detailId = `jsd_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||||
|
|
||||||
console.log('🔍 插入評分詳情:', {
|
console.log('🔍 準備插入評分詳情:', {
|
||||||
detailId,
|
detailId,
|
||||||
finalJudgeScoreId,
|
finalJudgeScoreId,
|
||||||
ruleId: rule.id,
|
ruleId: rule.id,
|
||||||
@@ -3890,23 +3895,48 @@ export class ScoringService extends DatabaseServiceBase {
|
|||||||
weight: rule.weight
|
weight: rule.weight
|
||||||
});
|
});
|
||||||
|
|
||||||
await DatabaseServiceBase.safeInsert(`
|
try {
|
||||||
INSERT INTO judge_score_details (id, judge_score_id, rule_id, rule_name, score, weight)
|
await DatabaseServiceBase.safeInsert(`
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
INSERT INTO judge_score_details (id, judge_score_id, rule_id, rule_name, score, weight)
|
||||||
`, [
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
detailId,
|
`, [
|
||||||
finalJudgeScoreId,
|
detailId,
|
||||||
rule.id,
|
finalJudgeScoreId,
|
||||||
rule.name,
|
rule.id,
|
||||||
score,
|
rule.name,
|
||||||
rule.weight
|
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 {
|
} else {
|
||||||
console.log(`⚠️ 找不到規則: ${ruleName}`);
|
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);
|
return await this.getJudgeScoreById(finalJudgeScoreId);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user