實現得獎資訊與資料庫整合

This commit is contained in:
2025-09-26 01:42:52 +08:00
parent 9bed168238
commit 0675fe63b0
8 changed files with 1531 additions and 409 deletions

View File

@@ -1733,11 +1733,21 @@ export class CompetitionService extends DatabaseServiceBase {
// 獲取競賽的團隊列表
static async getCompetitionTeams(competitionId: string): Promise<any[]> {
const sql = `
SELECT t.*, ct.registered_at, u.name as leader_name, u.phone as leader_phone
SELECT
t.*,
ct.registered_at,
u.name as leader_name,
u.phone as leader_phone,
COUNT(tm.id) as actual_member_count,
a.id as app_id,
a.name as app_name
FROM competition_teams ct
JOIN teams t ON ct.team_id = t.id
LEFT JOIN users u ON t.leader_id = u.id
LEFT JOIN team_members tm ON t.id = tm.team_id
LEFT JOIN apps a ON t.id = a.team_id AND a.is_active = 1
WHERE ct.competition_id = ? AND t.is_active = 1
GROUP BY t.id, ct.registered_at, u.name, u.phone, a.id, a.name
ORDER BY ct.registered_at ASC
`;
return await DatabaseServiceBase.safeQuery(sql, [competitionId]);
@@ -1757,7 +1767,7 @@ export class CompetitionService extends DatabaseServiceBase {
if (teams.length > 0) {
// 過濾掉 undefined 或 null 的 team_id 值
const teamIds = teams
.map(t => t.team_id)
.map(t => t.id)
.filter(id => id !== undefined && id !== null);
if (teamIds.length > 0) {
@@ -4459,10 +4469,10 @@ export class ScoringService extends DatabaseServiceBase {
// =====================================================
export class AwardService extends DatabaseServiceBase {
// 創建獎項
static async createAward(awardData: Omit<Award, 'id' | 'created_at'>): Promise<Award> {
static async createAward(awardData: any): Promise<Award> {
const sql = `
INSERT INTO awards (id, competition_id, app_id, team_id, proposal_id, app_name, team_name, proposal_title, creator, award_type, award_name, score, year, month, icon, custom_award_type_id, competition_type, rank, category)
VALUES (UUID(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INSERT INTO awards (id, competition_id, app_id, team_id, proposal_id, app_name, team_name, proposal_title, creator, award_type, award_name, score, year, month, icon, custom_award_type_id, competition_type, \`rank\`, category, description, judge_comments, application_links, documents, photos)
VALUES (UUID(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
const params = [
awardData.competition_id,
@@ -4482,11 +4492,19 @@ export class AwardService extends DatabaseServiceBase {
awardData.custom_award_type_id || null,
awardData.competition_type,
awardData.rank,
awardData.category
awardData.category,
awardData.description || null,
awardData.judge_comments || null,
awardData.application_links ? JSON.stringify(awardData.application_links) : null,
awardData.documents ? JSON.stringify(awardData.documents) : null,
awardData.photos ? JSON.stringify(awardData.photos) : null
];
await DatabaseServiceBase.safeInsert(sql, params);
return await this.getAwardByCompetitionAndCreator(awardData.competition_id, awardData.creator) as Award;
const result = await DatabaseServiceBase.safeInsert(sql, params);
// 獲取創建的獎項(使用最後創建的獎項)
const createdAward = await this.getAwardByCompetitionAndCreator(awardData.competition_id, awardData.creator);
return createdAward as Award;
}
// 根據競賽和創作者獲取獎項
@@ -4495,6 +4513,149 @@ export class AwardService extends DatabaseServiceBase {
return await db.queryOne<Award>(sql, [competitionId, creator]);
}
// 獲取所有獎項
static async getAllAwards(): Promise<Award[]> {
try {
console.log('🔍 開始查詢所有獎項...');
// 先測試簡單查詢
const simpleSql = 'SELECT * FROM awards ORDER BY created_at DESC LIMIT 5';
console.log('📝 執行簡單查詢:', simpleSql);
const simpleResult = await db.query<Award>(simpleSql);
console.log('✅ 簡單查詢結果:', simpleResult?.length || 0, '個獎項');
// 再執行 JOIN 查詢
const sql = `
SELECT
a.*,
c.name as competition_name,
c.type as competition_type
FROM awards a
LEFT JOIN competitions c ON a.competition_id = c.id
ORDER BY a.created_at DESC
`;
console.log('📝 執行JOIN查詢:', sql);
const result = await db.query<Award>(sql);
console.log('✅ JOIN查詢結果:', result?.length || 0, '個獎項');
// 檢查第一個獎項的數據
if (result && result.length > 0) {
console.log('🔍 第一個獎項數據:', {
id: result[0].id,
competition_name: (result[0] as any).competition_name,
competition_type: (result[0] as any).competition_type,
competition_id: result[0].competition_id
});
}
return result;
} catch (error) {
console.error('❌ 查詢獎項失敗:', error);
throw error;
}
}
// 根據競賽獲取獎項
static async getAwardsByCompetition(competitionId: string): Promise<Award[]> {
const sql = 'SELECT * FROM awards WHERE competition_id = ? ORDER BY created_at DESC';
return await db.query<Award>(sql, [competitionId]);
}
// 根據ID獲取獎項
static async getAwardById(id: string): Promise<Award | null> {
const sql = 'SELECT * FROM awards WHERE id = ?';
return await db.queryOne<Award>(sql, [id]);
}
// 根據競賽ID獲取評審信息
static async getCompetitionJudges(competitionId: string): Promise<any[]> {
try {
console.log('🔍 查詢競賽評審:', competitionId);
const sql = `
SELECT
j.id,
j.name,
j.title,
j.department,
j.expertise,
j.avatar
FROM competition_judges cj
LEFT JOIN judges j ON cj.judge_id = j.id
WHERE cj.competition_id = ? AND j.is_active = TRUE
ORDER BY j.name
`;
console.log('📝 執行評審查詢SQL:', sql);
const result = await db.query<any>(sql, [competitionId]);
console.log('✅ 查詢評審結果:', result?.length || 0, '位評審');
return result;
} catch (error) {
console.error('❌ 查詢評審失敗:', error);
throw error;
}
}
// 更新獎項
static async updateAward(id: string, updates: Partial<Award>): Promise<boolean> {
const fields = Object.keys(updates).filter(key =>
key !== 'id' &&
key !== 'created_at'
);
if (fields.length === 0) return true;
const setClause = fields.map(field => `${field} = ?`).join(', ');
const values = fields.map(field => (updates as any)[field]);
const sql = `UPDATE awards SET ${setClause} WHERE id = ?`;
const result = await DatabaseServiceBase.safeUpdate(sql, [...values, id]);
return result.affectedRows > 0;
}
// 刪除獎項
static async deleteAward(id: string): Promise<boolean> {
const sql = 'DELETE FROM awards WHERE id = ?';
const result = await DatabaseServiceBase.safeDelete(sql, [id]);
return result.affectedRows > 0;
}
// 獲取獎項統計
static async getAwardStats(competitionId?: string): Promise<{
total: number;
byType: Record<string, number>;
byCategory: Record<string, number>;
byYear: Record<number, number>;
}> {
let sql = 'SELECT award_type, category, year FROM awards';
const params: any[] = [];
if (competitionId) {
sql += ' WHERE competition_id = ?';
params.push(competitionId);
}
const awards = await DatabaseServiceBase.safeQuery(sql, params);
const stats = {
total: awards.length,
byType: {} as Record<string, number>,
byCategory: {} as Record<string, number>,
byYear: {} as Record<number, number>,
};
awards.forEach((award: any) => {
// 統計獎項類型
stats.byType[award.award_type] = (stats.byType[award.award_type] || 0) + 1;
// 統計獎項類別
stats.byCategory[award.category] = (stats.byCategory[award.category] || 0) + 1;
// 統計年份
stats.byYear[award.year] = (stats.byYear[award.year] || 0) + 1;
});
return stats;
}
// 根據年份獲取獎項
static async getAwardsByYear(year: number): Promise<Award[]> {
const sql = 'SELECT * FROM awards WHERE year = ? ORDER BY month DESC, rank ASC';