change logic
All checks were successful
Build & Deploy on Dev / build (push) Successful in 52s

This commit is contained in:
Suphonchai Phoonsawat 2026-05-13 07:00:22 +07:00
parent 461790c1c1
commit 0f1ec072ad
6 changed files with 579 additions and 74 deletions

View file

@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging;
using OfficeOpenXml;
using System.Net.Http.Headers;
using BMA.EHR.Recruit.Data;
using EFCore.BulkExtensions;
using BMA.EHR.Recruit.Extensions;
using BMA.EHR.Recruit.Models.Recruits;
using BMA.EHR.Recruit.Requests.Recruits;
@ -112,9 +113,16 @@ public class ImportBackgroundService : BackgroundService
int row = 2;
int batchCount = 0;
const int batchSize = 100;
const int batchSize = 500;
int totalProcessed = 0;
var batchRecruits = new List<Models.Recruits.Recruit>();
var batchEducations = new List<RecruitEducation>();
var batchOccupations = new List<RecruitOccupation>();
var batchAddresses = new List<RecruitAddress>();
var batchPayments = new List<RecruitPayment>();
var batchCertificates = new List<RecruitCertificate>();
while (row <= totalRows)
{
var cell1 = workSheet?.Cells[row, 1]?.GetValue<string>();
@ -141,7 +149,7 @@ public class ImportBackgroundService : BackgroundService
r.PositionLevel = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionLevel)]?.GetValue<string>().IsNull("");
// address
r.Addresses.Add(new RecruitAddress()
var address = new RecruitAddress()
{
Address = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address)]?.GetValue<string>() ?? "",
Moo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Moo)]?.GetValue<string>() ?? "",
@ -161,10 +169,10 @@ public class ImportBackgroundService : BackgroundService
Amphur1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amphur1)]?.GetValue<string>() ?? "",
Province1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Province1)]?.GetValue<string>() ?? "",
ZipCode1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ZipCode1)]?.GetValue<string>() ?? "",
});
};
// payment
r.Payments.Add(new RecruitPayment()
var payment = new RecruitPayment()
{
PaymentId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PaymentID)]?.GetValue<string>() ?? "",
CompanyCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CompanyCode)]?.GetValue<string>() ?? "",
@ -182,28 +190,28 @@ public class ImportBackgroundService : BackgroundService
ChequeNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChequeNo)]?.GetValue<string>() ?? "",
Amount = (decimal)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amount)]?.GetValue<decimal>(),
ChqueBankCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChqBankCode)]?.GetValue<string>() ?? ""
});
};
// occupation
r.Occupations.Add(new RecruitOccupation()
var occupation = new RecruitOccupation()
{
Occupation = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Occupation)]?.GetValue<string>() ?? "",
Position = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Position)]?.GetValue<string>() ?? "",
Workplace = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Workplace)]?.GetValue<string>() ?? "",
Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkplaceTelephone)]?.GetValue<string>() ?? "",
WorkAge = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkAge)]?.GetValue<string>() ?? "",
});
};
// certificate
r.Certificates.Add(new RecruitCertificate()
var certificate = new RecruitCertificate()
{
CertificateNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateNo)]?.GetValue<string>() ?? "",
Description = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateDesc)]?.GetValue<string>() ?? "",
IssueDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateIssueDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-")),
ExpiredDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateExpireDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-"))
});
};
r.Educations.Add(new RecruitEducation()
var education = new RecruitEducation()
{
Degree = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Degree)]?.GetValue<string>() ?? "",
Major = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Major)]?.GetValue<string>() ?? "",
@ -214,10 +222,21 @@ public class ImportBackgroundService : BackgroundService
Specialist = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.SpecialList)]?.GetValue<string>() ?? "",
HighDegree = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.HighDegree)]?.GetValue<string>() ?? "",
BachelorDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BachelorDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-"))
});
};
r.Addresses.Add(address);
r.Payments.Add(payment);
r.Occupations.Add(occupation);
r.Certificates.Add(certificate);
r.Educations.Add(education);
r.RecruitImport = imported;
_context.Recruits.Add(r);
batchRecruits.Add(r);
batchAddresses.Add(address);
batchPayments.Add(payment);
batchOccupations.Add(occupation);
batchCertificates.Add(certificate);
batchEducations.Add(education);
row++;
batchCount++;
@ -225,16 +244,56 @@ public class ImportBackgroundService : BackgroundService
if (batchCount >= batchSize)
{
_context.SaveChanges();
_context.ChangeTracker.Clear();
_context.Entry(imported).State = EntityState.Unchanged;
await _context.BulkInsertAsync(batchRecruits, options => { options.SetOutputIdentity = true; });
for (int j = 0; j < batchRecruits.Count; j++)
{
batchAddresses[j].Recruit = batchRecruits[j];
batchPayments[j].Recruit = batchRecruits[j];
batchOccupations[j].Recruit = batchRecruits[j];
batchCertificates[j].Recruit = batchRecruits[j];
batchEducations[j].Recruit = batchRecruits[j];
}
await _context.BulkInsertAsync(batchAddresses);
await _context.BulkInsertAsync(batchPayments);
await _context.BulkInsertAsync(batchOccupations);
await _context.BulkInsertAsync(batchCertificates);
await _context.BulkInsertAsync(batchEducations);
batchRecruits.Clear();
batchAddresses.Clear();
batchPayments.Clear();
batchOccupations.Clear();
batchCertificates.Clear();
batchEducations.Clear();
batchCount = 0;
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed);
}
}
// Process remaining records
if (batchRecruits.Count > 0)
{
await _context.BulkInsertAsync(batchRecruits, options => { options.SetOutputIdentity = true; });
for (int j = 0; j < batchRecruits.Count; j++)
{
batchAddresses[j].Recruit = batchRecruits[j];
batchPayments[j].Recruit = batchRecruits[j];
batchOccupations[j].Recruit = batchRecruits[j];
batchCertificates[j].Recruit = batchRecruits[j];
batchEducations[j].Recruit = batchRecruits[j];
}
await _context.BulkInsertAsync(batchAddresses);
await _context.BulkInsertAsync(batchPayments);
await _context.BulkInsertAsync(batchOccupations);
await _context.BulkInsertAsync(batchCertificates);
await _context.BulkInsertAsync(batchEducations);
}
}
_context.SaveChanges();
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
}
@ -244,13 +303,10 @@ public class ImportBackgroundService : BackgroundService
private async Task ProcessCandidateFileByIdAsync(ApplicationDbContext _context, MinIOService _minioService, RecruitService _recruitService, IWebHostEnvironment _webHostEnv, ImportJobInfo job)
{
var imported = await _context.RecruitImports.AsQueryable()
.Include(x => x.ImportHostories)
.Include(x => x.ImportFile)
.FirstOrDefaultAsync(x => x.Id == job.RecruitImportId);
var imported = await _context.RecruitImports.FindAsync(job.RecruitImportId);
if (imported == null) throw new Exception("RecruitImport not found");
// Save import history using regular SaveChanges (small operation)
imported.ImportHostories.Add(new RecruitImportHistory
{
Description = "นำเข้าข้อมูลผู้สมัครสอบแข่งขัน",
@ -261,19 +317,27 @@ public class ImportBackgroundService : BackgroundService
LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator",
});
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
var importId = imported.Id;
using var c_package = new ExcelPackage(new FileInfo(job.ImportFile));
for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++)
{
var workSheet = c_package.Workbook.Worksheets[i];
var totalRows = workSheet.Dimension.Rows;
var cols = workSheet.GetHeaderColumns();
int row = 2;
int batchCount = 0;
const int batchSize = 50;
const int batchSize = 500;
int totalProcessed = 0;
var batchList = new List<Models.Recruits.Recruit>();
var batchRecruits = new List<Models.Recruits.Recruit>();
var batchEducations = new List<RecruitEducation>();
var batchOccupations = new List<RecruitOccupation>();
var batchAddresses = new List<RecruitAddress>();
var batchPayments = new List<RecruitPayment>();
while (row <= totalRows)
{
@ -308,8 +372,8 @@ public class ImportBackgroundService : BackgroundService
r.LastUpdateUserId = job.UserId ?? "";
r.LastUpdateFullName = job.FullName ?? "System Administrator";
// education
r.Educations.Add(new RecruitEducation()
// Store child entities in separate lists for bulk insert
var education = new RecruitEducation()
{
Degree = workSheet?.Cells[row, 18]?.GetValue<string>() ?? "",
Major = workSheet?.Cells[row, 19]?.GetValue<string>() == "อื่น ๆ" ? workSheet?.Cells[row, 20]?.GetValue<string>() ?? "" : workSheet?.Cells[row, 19]?.GetValue<string>() ?? "",
@ -326,10 +390,9 @@ public class ImportBackgroundService : BackgroundService
LastUpdatedAt = DateTime.Now,
LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator"
});
};
// occupation
r.Occupations.Add(new RecruitOccupation()
var occupation = new RecruitOccupation()
{
Occupation = workSheet?.Cells[row, 33]?.GetValue<string>() == "อื่น ๆ" ? workSheet?.Cells[row, 34]?.GetValue<string>() ?? "" : workSheet?.Cells[row, 33]?.GetValue<string>() ?? "",
Position = workSheet?.Cells[row, 37]?.GetValue<string>() ?? "",
@ -342,10 +405,9 @@ public class ImportBackgroundService : BackgroundService
LastUpdatedAt = DateTime.Now,
LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator"
});
};
// address
r.Addresses.Add(new RecruitAddress()
var address = new RecruitAddress()
{
Address = $"{(workSheet?.Cells[row, 49]?.GetValue<string>() ?? "")} {(workSheet?.Cells[row, 50]?.GetValue<string>() ?? "")}",
Moo = workSheet?.Cells[row, 51]?.GetValue<string>() ?? "",
@ -371,10 +433,9 @@ public class ImportBackgroundService : BackgroundService
LastUpdatedAt = DateTime.Now,
LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator"
});
};
// payment
r.Payments.Add(new RecruitPayment()
var payment = new RecruitPayment()
{
PaymentId = workSheet?.Cells[row, 104]?.GetValue<string>() ?? "",
CompanyCode = workSheet?.Cells[row, 105]?.GetValue<string>() ?? "",
@ -398,10 +459,18 @@ public class ImportBackgroundService : BackgroundService
LastUpdatedAt = DateTime.Now,
LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator"
});
};
r.RecruitImport = imported;
batchList.Add(r);
r.Educations.Add(education);
r.Occupations.Add(occupation);
r.Addresses.Add(address);
r.Payments.Add(payment);
batchRecruits.Add(r);
batchEducations.Add(education);
batchOccupations.Add(occupation);
batchAddresses.Add(address);
batchPayments.Add(payment);
row++;
batchCount++;
@ -409,23 +478,61 @@ public class ImportBackgroundService : BackgroundService
if (batchCount >= batchSize)
{
_context.Recruits.AddRange(batchList);
_context.SaveChanges();
_context.ChangeTracker.Clear();
_context.Entry(imported).State = EntityState.Unchanged;
batchList.Clear();
// BulkInsert Recruits first (with SetOutputIdentity to get generated Ids)
await _context.BulkInsertAsync(batchRecruits, options =>
{
options.SetOutputIdentity = true;
});
// Assign generated Recruit Id to child entities
for (int j = 0; j < batchRecruits.Count; j++)
{
batchEducations[j].Recruit = batchRecruits[j];
batchOccupations[j].Recruit = batchRecruits[j];
batchAddresses[j].Recruit = batchRecruits[j];
batchPayments[j].Recruit = batchRecruits[j];
}
// BulkInsert child entities (no identity output needed)
await _context.BulkInsertAsync(batchEducations);
await _context.BulkInsertAsync(batchOccupations);
await _context.BulkInsertAsync(batchAddresses);
await _context.BulkInsertAsync(batchPayments);
// Clear all lists for next batch
batchRecruits.Clear();
batchEducations.Clear();
batchOccupations.Clear();
batchAddresses.Clear();
batchPayments.Clear();
batchCount = 0;
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed);
}
}
if (batchList.Count > 0)
// Process remaining records
if (batchRecruits.Count > 0)
{
_context.Recruits.AddRange(batchList);
await _context.BulkInsertAsync(batchRecruits, options =>
{
options.SetOutputIdentity = true;
});
for (int j = 0; j < batchRecruits.Count; j++)
{
batchEducations[j].Recruit = batchRecruits[j];
batchOccupations[j].Recruit = batchRecruits[j];
batchAddresses[j].Recruit = batchRecruits[j];
batchPayments[j].Recruit = batchRecruits[j];
}
await _context.BulkInsertAsync(batchEducations);
await _context.BulkInsertAsync(batchOccupations);
await _context.BulkInsertAsync(batchAddresses);
await _context.BulkInsertAsync(batchPayments);
}
}
_context.SaveChanges();
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
}
@ -445,8 +552,7 @@ public class ImportBackgroundService : BackgroundService
if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null)
{
_context.RecruitScores.RemoveRange(rec_import.ScoreImport.Scores);
await _context.SaveChangesAsync();
await _context.BulkDeleteAsync(rec_import.ScoreImport.Scores.ToList());
}
rec_import.ImportHostories.Add(new RecruitImportHistory
@ -475,7 +581,12 @@ public class ImportBackgroundService : BackgroundService
Scores = new List<RecruitScore>()
};
// preload recruits
// Save ScoreImport parent first to get its Id
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
// preload recruits (lightweight - only ExamId)
var recruitsDict = await _context.Recruits
.Where(x => x.RecruitImport.Id == rec_import.Id)
.GroupBy(x => x.ExamId)
@ -490,10 +601,12 @@ public class ImportBackgroundService : BackgroundService
var cols = workSheet.GetHeaderColumns();
int row = 8;
int batchCount = 0;
const int batchSize = 100;
const int batchSize = 500;
int totalProcessed = 0;
var endRow = workSheet.Dimension.End.Row;
var batchScores = new List<RecruitScore>();
while (row <= endRow)
{
var cell1 = workSheet?.Cells[row, 1]?.GetValue<string>();
@ -542,8 +655,9 @@ public class ImportBackgroundService : BackgroundService
r.LastUpdatedAt = DateTime.Now;
r.LastUpdateUserId = job.UserId ?? "";
r.LastUpdateFullName = job.FullName ?? "System Administrator";
r.ScoreImport = imported;
imported.Scores.Add(r);
batchScores.Add(r);
}
row++;
@ -552,23 +666,20 @@ public class ImportBackgroundService : BackgroundService
if (batchCount >= batchSize)
{
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
_context.Attach(rec_import);
_context.Attach(imported);
imported.Scores.Clear();
await _context.BulkInsertAsync(batchScores);
batchScores.Clear();
batchCount = 0;
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed);
}
}
// Process remaining records
if (batchScores.Count > 0)
{
await _context.BulkInsertAsync(batchScores);
}
}
if (imported.Scores.Count > 0)
{
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
}
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
}
@ -599,7 +710,7 @@ public class ImportBackgroundService : BackgroundService
x.Number = string.Empty;
x.RemarkExamOrder = string.Empty;
}
await _context.SaveChangesAsync();
await _context.BulkUpdateAsync(oldScores);
}
}
@ -613,14 +724,17 @@ public class ImportBackgroundService : BackgroundService
LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator",
});
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
// preload scores
var score = rec_import.ScoreImport.Scores
.Where(s => !string.IsNullOrEmpty(s.ExamId))
// preload scores - re-query from DB to avoid tracking issues
var scoreList = await _context.RecruitScores
.Where(s => s.ScoreImport.RecruitImportId == rec_import.Id && !string.IsNullOrEmpty(s.ExamId))
.GroupBy(x => x.ExamId)
.Where(g => g.Count() == 1)
.Select(g => g.First())
.ToDictionary(s => s.ExamId, s => s);
.ToListAsync();
var score = scoreList.ToDictionary(s => s.ExamId!, s => s);
// Read from saved file (ResultFile uses stream from Form, but we saved to disk)
using var stream = System.IO.File.OpenRead(job.ImportFile);
@ -630,8 +744,9 @@ public class ImportBackgroundService : BackgroundService
{
int row = 7;
int batchCount = 0;
const int batchSize = 100;
const int batchSize = 500;
var endRow = workSheet.Dimension.End.Row;
var batchUpdates = new List<RecruitScore>();
while (row <= endRow)
{
@ -649,6 +764,7 @@ public class ImportBackgroundService : BackgroundService
existingScore.LastUpdatedAt = DateTime.Now;
existingScore.LastUpdateUserId = job.UserId ?? "";
existingScore.LastUpdateFullName = job.FullName ?? "System Administrator";
batchUpdates.Add(existingScore);
batchCount++;
}
@ -656,15 +772,18 @@ public class ImportBackgroundService : BackgroundService
if (batchCount >= batchSize)
{
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
_context.Entry(rec_import).State = EntityState.Unchanged;
await _context.BulkUpdateAsync(batchUpdates);
batchUpdates.Clear();
batchCount = 0;
}
}
}
await _context.SaveChangesAsync();
// Process remaining records
if (batchUpdates.Count > 0)
{
await _context.BulkUpdateAsync(batchUpdates);
}
}
}
#endregion