fix ระบบแจ้ง Error #2497
This commit is contained in:
parent
e33448508e
commit
b3839cb622
1 changed files with 47 additions and 3 deletions
|
|
@ -566,9 +566,48 @@ public class ImportBackgroundService : BackgroundService
|
||||||
|
|
||||||
if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null)
|
if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null)
|
||||||
{
|
{
|
||||||
|
// Store old ScoreImport ID and MinIO document ID for cleanup
|
||||||
|
var oldScoreImportId = rec_import.ScoreImport.Id;
|
||||||
|
var oldDocId = rec_import.ScoreImport.ImportFile?.Id;
|
||||||
|
|
||||||
|
// Delete old scores first
|
||||||
await _context.BulkDeleteAsync(rec_import.ScoreImport.Scores.ToList());
|
await _context.BulkDeleteAsync(rec_import.ScoreImport.Scores.ToList());
|
||||||
|
|
||||||
|
// Delete the old ScoreImport entity to prevent duplicate FK constraint
|
||||||
|
await _context.BulkDeleteAsync(new List<ScoreImport> { rec_import.ScoreImport });
|
||||||
|
|
||||||
|
// TODO: Implement retention policy for old MinIO documents
|
||||||
|
// For now, keep old files to allow recovery and avoid immediate deletion
|
||||||
|
// if (oldDocId.HasValue && oldDocId.Value != Guid.Empty)
|
||||||
|
// {
|
||||||
|
// try { await _minioService.DeleteFileAsync(oldDocId.Value); } catch { }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Clear ChangeTracker after bulk operations
|
||||||
|
_context.ChangeTracker.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reload to get fresh entity state after clearing
|
||||||
|
rec_import = await _context.RecruitImports.AsQueryable()
|
||||||
|
.Include(x => x.ImportHostories)
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == job.RecruitImportId)
|
||||||
|
?? throw new Exception("RecruitImport not found after reload");
|
||||||
|
|
||||||
|
// get doc from minio
|
||||||
|
var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile));
|
||||||
|
var docId = doc.Id; // Store the ID before clearing tracker
|
||||||
|
|
||||||
|
// Detach document entity to prevent duplicate INSERT on next SaveChangesAsync
|
||||||
|
_context.Entry(doc).State = EntityState.Detached;
|
||||||
|
_context.ChangeTracker.Clear();
|
||||||
|
|
||||||
|
// Reload RecruitImport after clearing tracker to get fresh entity
|
||||||
|
rec_import = await _context.RecruitImports.AsQueryable()
|
||||||
|
.Include(x => x.ImportHostories)
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == job.RecruitImportId)
|
||||||
|
?? throw new Exception("RecruitImport not found after MinIO upload");
|
||||||
|
|
||||||
|
// Add import history
|
||||||
rec_import.ImportHostories.Add(new RecruitImportHistory
|
rec_import.ImportHostories.Add(new RecruitImportHistory
|
||||||
{
|
{
|
||||||
Description = "นำเข้าข้อมูลผลคะแนนสอบ",
|
Description = "นำเข้าข้อมูลผลคะแนนสอบ",
|
||||||
|
|
@ -580,12 +619,14 @@ public class ImportBackgroundService : BackgroundService
|
||||||
LastUpdateFullName = job.FullName ?? "System Administrator",
|
LastUpdateFullName = job.FullName ?? "System Administrator",
|
||||||
});
|
});
|
||||||
|
|
||||||
// get doc from minio
|
// Load Document entity fresh from database to avoid tracking conflicts
|
||||||
var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile));
|
var freshDoc = await _context.Documents.FindAsync(docId)
|
||||||
|
?? throw new Exception("Failed to retrieve uploaded document. Please contact support.");
|
||||||
|
|
||||||
var imported = new ScoreImport
|
var imported = new ScoreImport
|
||||||
{
|
{
|
||||||
Year = rec_import.Year,
|
Year = rec_import.Year,
|
||||||
ImportFile = doc,
|
ImportFile = freshDoc,
|
||||||
CreatedAt = DateTime.Now,
|
CreatedAt = DateTime.Now,
|
||||||
CreatedUserId = job.UserId ?? "",
|
CreatedUserId = job.UserId ?? "",
|
||||||
CreatedFullName = job.FullName ?? "System Administrator",
|
CreatedFullName = job.FullName ?? "System Administrator",
|
||||||
|
|
@ -595,6 +636,9 @@ public class ImportBackgroundService : BackgroundService
|
||||||
Scores = new List<RecruitScore>()
|
Scores = new List<RecruitScore>()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add ScoreImport to context explicitly to ensure EF Core knows to INSERT it
|
||||||
|
_context.Add(imported);
|
||||||
|
|
||||||
// Save ScoreImport parent first to get its Id
|
// Save ScoreImport parent first to get its Id
|
||||||
rec_import.ScoreImport = imported;
|
rec_import.ScoreImport = imported;
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue