完成競賽編輯功能
This commit is contained in:
@@ -161,6 +161,7 @@ export class DatabaseSyncFixed {
|
||||
async smartDualInsertRelation(
|
||||
relationTable: string,
|
||||
competitionId: string,
|
||||
slaveCompetitionId: string,
|
||||
relationData: any[],
|
||||
relationIdField: string
|
||||
): Promise<WriteResult> {
|
||||
@@ -171,14 +172,27 @@ export class DatabaseSyncFixed {
|
||||
};
|
||||
|
||||
try {
|
||||
// 先獲取主機和備機的競賽 ID 對應關係
|
||||
console.log(`🔍 smartDualInsertRelation 開始執行`);
|
||||
console.log(` 表名: ${relationTable}`);
|
||||
console.log(` 主機競賽 ID: ${competitionId}`);
|
||||
console.log(` 備機競賽 ID: ${slaveCompetitionId}`);
|
||||
console.log(` 關聯數據:`, relationData);
|
||||
console.log(` 關聯字段: ${relationIdField}`);
|
||||
|
||||
// 先獲取主機的競賽 ID
|
||||
const masterCompetitionId = await this.getMasterCompetitionId(competitionId);
|
||||
const slaveCompetitionId = await this.getSlaveCompetitionId(competitionId);
|
||||
|
||||
if (!masterCompetitionId || !slaveCompetitionId) {
|
||||
throw new Error('找不到對應的競賽 ID');
|
||||
}
|
||||
|
||||
console.log(`🔍 關聯雙寫開始: ${relationTable}`);
|
||||
console.log(` 主機競賽 ID: ${masterCompetitionId}`);
|
||||
console.log(` 備機競賽 ID: ${slaveCompetitionId}`);
|
||||
console.log(` 關聯數據數量: ${relationData.length}`);
|
||||
console.log(` 主機競賽存在: ${!!masterCompetitionId}`);
|
||||
console.log(` 備機競賽存在: ${!!slaveCompetitionId}`);
|
||||
|
||||
// 同時寫入關聯數據
|
||||
const masterPromise = this.insertRelationsToMaster(relationTable, masterCompetitionId, relationData, relationIdField);
|
||||
const slavePromise = this.insertRelationsToSlave(relationTable, slaveCompetitionId, relationData, relationIdField);
|
||||
@@ -191,9 +205,11 @@ export class DatabaseSyncFixed {
|
||||
|
||||
if (masterResult.status === 'rejected') {
|
||||
result.masterError = masterResult.reason instanceof Error ? masterResult.reason.message : '主機關聯寫入失敗';
|
||||
console.error(`❌ 主機關聯寫入失敗:`, masterResult.reason);
|
||||
}
|
||||
if (slaveResult.status === 'rejected') {
|
||||
result.slaveError = slaveResult.reason instanceof Error ? slaveResult.reason.message : '備機關聯寫入失敗';
|
||||
console.error(`❌ 備機關聯寫入失敗:`, slaveResult.reason);
|
||||
}
|
||||
|
||||
console.log(`📝 關聯雙寫結果: 主機${result.masterSuccess ? '✅' : '❌'} 備機${result.slaveSuccess ? '✅' : '❌'}`);
|
||||
@@ -231,6 +247,44 @@ export class DatabaseSyncFixed {
|
||||
}
|
||||
}
|
||||
|
||||
// 獲取競賽信息
|
||||
private async getCompetitionById(competitionId: string): Promise<any> {
|
||||
if (!this.masterPool) return null;
|
||||
|
||||
const connection = await this.masterPool.getConnection();
|
||||
try {
|
||||
const [rows] = await connection.execute('SELECT * FROM competitions WHERE id = ?', [competitionId]);
|
||||
return (rows as any[])[0] || null;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
// 根據名稱獲取備機競賽 ID
|
||||
private async getSlaveCompetitionIdByName(name: string): Promise<string | null> {
|
||||
if (!this.slavePool) return null;
|
||||
|
||||
console.log(`🔍 getSlaveCompetitionIdByName 調用,名稱:`, name);
|
||||
|
||||
const connection = await this.slavePool.getConnection();
|
||||
try {
|
||||
const [rows] = await connection.execute('SELECT id FROM competitions WHERE name = ? ORDER BY created_at DESC LIMIT 1', [name]);
|
||||
console.log(`🔍 備機查詢結果:`, rows);
|
||||
const result = (rows as any[])[0]?.id || null;
|
||||
console.log(`🔍 備機競賽 ID 結果:`, result, typeof result);
|
||||
|
||||
// 確保返回的是字符串
|
||||
if (result && typeof result !== 'string') {
|
||||
console.log(`⚠️ 備機競賽 ID 不是字符串,轉換為字符串:`, String(result));
|
||||
return String(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
// 寫入主機關聯表
|
||||
private async insertRelationsToMaster(
|
||||
relationTable: string,
|
||||
@@ -263,15 +317,34 @@ export class DatabaseSyncFixed {
|
||||
): Promise<void> {
|
||||
if (!this.slavePool) return;
|
||||
|
||||
console.log(`🔍 備機關聯寫入開始: ${relationTable}`);
|
||||
console.log(` 備機競賽 ID: ${competitionId}`);
|
||||
console.log(` 關聯數據:`, relationData);
|
||||
console.log(` 關聯字段: ${relationIdField}`);
|
||||
|
||||
const connection = await this.slavePool.getConnection();
|
||||
try {
|
||||
for (const data of relationData) {
|
||||
const [uuidResult] = await connection.execute('SELECT UUID() as id');
|
||||
const relationId = (uuidResult as any)[0].id;
|
||||
|
||||
console.log(`🔍 準備插入關聯數據:`, {
|
||||
relationId,
|
||||
competitionId,
|
||||
relationField: relationIdField,
|
||||
relationValue: data[relationIdField]
|
||||
});
|
||||
|
||||
const sql = `INSERT INTO ${relationTable} (id, competition_id, ${relationIdField}) VALUES (?, ?, ?)`;
|
||||
console.log(`🔍 執行 SQL:`, sql);
|
||||
console.log(`🔍 參數:`, [relationId, competitionId, data[relationIdField]]);
|
||||
|
||||
await connection.execute(sql, [relationId, competitionId, data[relationIdField]]);
|
||||
console.log(`✅ 備機關聯數據插入成功`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ 備機關聯寫入失敗:`, error);
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
|
Reference in New Issue
Block a user