diff --git a/BMA.EHR.Recruit.csproj b/BMA.EHR.Recruit.csproj
index c148fa1..bba397f 100644
--- a/BMA.EHR.Recruit.csproj
+++ b/BMA.EHR.Recruit.csproj
@@ -24,8 +24,6 @@
-
-
diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs
index 7d246ff..a673934 100644
--- a/Controllers/RecruitController.cs
+++ b/Controllers/RecruitController.cs
@@ -377,7 +377,6 @@ namespace BMA.EHR.Recruit.Controllers
try
{
var data = await _context.RecruitImports.AsQueryable()
- .AsNoTracking()
.Include(x => x.RecruitImages)
.ThenInclude(x => x.Document)
.Include(x => x.RecruitDocuments)
@@ -2010,7 +2009,6 @@ namespace BMA.EHR.Recruit.Controllers
public async Task> ExportExamAsync(Guid id)
{
var data = await _context.RecruitImports.AsQueryable()
- .AsNoTracking()
.Include(x => x.Recruits)
.FirstOrDefaultAsync(x => x.Id == id);
@@ -2519,7 +2517,6 @@ namespace BMA.EHR.Recruit.Controllers
public async Task GetCandidateNewListReportAsync(Guid id)
{
var data = await _context.Recruits.AsQueryable()
- .AsNoTracking()
.Include(x => x.RecruitImport)
.Where(x => x.RecruitImport.Id == id)
.OrderBy(x => x.ExamId)
diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs
index 0f827fb..ddc4036 100644
--- a/Services/ImportBackgroundService.cs
+++ b/Services/ImportBackgroundService.cs
@@ -4,13 +4,13 @@ using BMA.EHR.Recruit.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
+using OfficeOpenXml;
using System.Net.Http.Headers;
using BMA.EHR.Recruit.Data;
-using BMA.EHR.Recruit.Extensions;
using EFCore.BulkExtensions;
+using BMA.EHR.Recruit.Extensions;
using BMA.EHR.Recruit.Models.Recruits;
using BMA.EHR.Recruit.Requests.Recruits;
-using ExcelDataReader;
namespace BMA.EHR.Recruit.Services;
@@ -108,18 +108,14 @@ public class ImportBackgroundService : BackgroundService
var imported = await _context.RecruitImports.FindAsync(job.RecruitImportId);
if (imported == null) throw new Exception("RecruitImport not found");
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
- using var stream = System.IO.File.OpenRead(job.ImportFile);
- using var reader = ExcelReaderFactory.CreateReader(stream);
-
- do
+ using var c_package = new ExcelPackage(new FileInfo(job.ImportFile));
+ for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++)
{
- // Read header row (row 1) to build column index map
- if (!reader.Read()) continue;
- var cols = new string[reader.FieldCount];
- for (int c = 0; c < reader.FieldCount; c++)
- cols[c] = reader.GetValue(c)?.ToString() ?? "";
+ 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 = 500;
int totalProcessed = 0;
@@ -131,105 +127,105 @@ public class ImportBackgroundService : BackgroundService
var batchPayments = new List();
var batchCertificates = new List();
- while (reader.Read())
+ while (row <= totalRows)
{
- var cell1 = reader.GetValue(0)?.ToString();
- if (string.IsNullOrEmpty(cell1)) break;
+ var cell1 = workSheet?.Cells[row, 1]?.GetValue();
+ if (cell1 == "" || cell1 == null) break;
var r = new Models.Recruits.Recruit();
- r.ExamId = GetCellValue(reader, cols, CandidateFileHeader.ExamID);
- r.CitizenId = GetCellValue(reader, cols, CandidateFileHeader.PersonalID);
- r.Prefix = GetCellValue(reader, cols, CandidateFileHeader.Prefix);
- r.FirstName = GetCellValue(reader, cols, CandidateFileHeader.FirstName);
- r.LastName = GetCellValue(reader, cols, CandidateFileHeader.LastName);
- r.Gendor = GetCellValue(reader, cols, CandidateFileHeader.Gender);
- r.National = GetCellValue(reader, cols, CandidateFileHeader.National).IsNull("");
- r.Race = GetCellValue(reader, cols, CandidateFileHeader.Race).IsNull("");
- r.Religion = GetCellValue(reader, cols, CandidateFileHeader.Religion).IsNull("");
- r.DateOfBirth = Convert.ToDateTime(GetCellValue(reader, cols, CandidateFileHeader.DateOfBirth).ToDateTime(DateTimeFormat.Ymd, "-"));
- r.Marry = GetCellValue(reader, cols, CandidateFileHeader.Marry);
+ r.ExamId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ExamID)]?.GetValue();
+ r.CitizenId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalID)]?.GetValue();
+ r.Prefix = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Prefix)]?.GetValue();
+ r.FirstName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.FirstName)]?.GetValue();
+ r.LastName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.LastName)]?.GetValue();
+ r.Gendor = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Gender)]?.GetValue();
+ r.National = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.National)]?.GetValue().IsNull("");
+ r.Race = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Race)]?.GetValue().IsNull("");
+ r.Religion = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Religion)]?.GetValue().IsNull("");
+ r.DateOfBirth = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.DateOfBirth)]?.GetValue().ToDateTime(DateTimeFormat.Ymd, "-"));
+ r.Marry = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Marry)]?.GetValue();
r.Isspecial = "N";
- r.CitizenCardIssuer = GetCellValue(reader, cols, CandidateFileHeader.PersonalCardIssue);
- r.CitizenCardExpireDate = Convert.ToDateTime(GetCellValue(reader, cols, CandidateFileHeader.PersonalCardExpireDate).ToDateTime(DateTimeFormat.Ymd, "-"));
- r.ApplyDate = GetCellDateTime(reader, cols, CandidateFileHeader.ApplyDate) ?? DateTime.MinValue;
- r.PositionName = GetCellValue(reader, cols, CandidateFileHeader.PositionName).IsNull("");
- r.PositionType = GetCellValue(reader, cols, CandidateFileHeader.PositionType).IsNull("");
- r.PositionLevel = GetCellValue(reader, cols, CandidateFileHeader.PositionLevel).IsNull("");
+ r.CitizenCardIssuer = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalCardIssue)]?.GetValue();
+ r.CitizenCardExpireDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalCardExpireDate)]?.GetValue().ToDateTime(DateTimeFormat.Ymd, "-"));
+ r.ApplyDate = (DateTime)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ApplyDate)]?.GetValue();
+ r.PositionName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionName)]?.GetValue().IsNull("");
+ r.PositionType = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionType)]?.GetValue().IsNull("");
+ r.PositionLevel = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionLevel)]?.GetValue().IsNull("");
// address
var address = new RecruitAddress()
{
- Address = GetCellValue(reader, cols, CandidateFileHeader.Address),
- Moo = GetCellValue(reader, cols, CandidateFileHeader.Moo),
- Soi = GetCellValue(reader, cols, CandidateFileHeader.Soi),
- Road = GetCellValue(reader, cols, CandidateFileHeader.Road),
- District = GetCellValue(reader, cols, CandidateFileHeader.District),
- Amphur = GetCellValue(reader, cols, CandidateFileHeader.Amphur),
- Province = GetCellValue(reader, cols, CandidateFileHeader.Province),
- ZipCode = GetCellValue(reader, cols, CandidateFileHeader.ZipCode),
- Telephone = GetCellValue(reader, cols, CandidateFileHeader.Telephone),
- Mobile = GetCellValue(reader, cols, CandidateFileHeader.Mobile),
- Address1 = GetCellValue(reader, cols, CandidateFileHeader.Address1),
- Moo1 = GetCellValue(reader, cols, CandidateFileHeader.Moo1),
- Soi1 = GetCellValue(reader, cols, CandidateFileHeader.Soi1),
- Road1 = GetCellValue(reader, cols, CandidateFileHeader.Road1),
- District1 = GetCellValue(reader, cols, CandidateFileHeader.District1),
- Amphur1 = GetCellValue(reader, cols, CandidateFileHeader.Amphur1),
- Province1 = GetCellValue(reader, cols, CandidateFileHeader.Province1),
- ZipCode1 = GetCellValue(reader, cols, CandidateFileHeader.ZipCode1),
+ Address = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address)]?.GetValue() ?? "",
+ Moo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Moo)]?.GetValue() ?? "",
+ Soi = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Soi)]?.GetValue() ?? "",
+ Road = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Road)]?.GetValue() ?? "",
+ District = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.District)]?.GetValue() ?? "",
+ Amphur = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amphur)]?.GetValue() ?? "",
+ Province = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Province)]?.GetValue() ?? "",
+ ZipCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ZipCode)]?.GetValue() ?? "",
+ Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Telephone)]?.GetValue() ?? "",
+ Mobile = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Mobile)]?.GetValue() ?? "",
+ Address1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address1)]?.GetValue() ?? "",
+ Moo1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Moo1)]?.GetValue() ?? "",
+ Soi1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Soi1)]?.GetValue() ?? "",
+ Road1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Road1)]?.GetValue() ?? "",
+ District1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.District1)]?.GetValue() ?? "",
+ Amphur1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amphur1)]?.GetValue() ?? "",
+ Province1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Province1)]?.GetValue() ?? "",
+ ZipCode1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ZipCode1)]?.GetValue() ?? "",
};
// payment
var payment = new RecruitPayment()
{
- PaymentId = GetCellValue(reader, cols, CandidateFileHeader.PaymentID),
- CompanyCode = GetCellValue(reader, cols, CandidateFileHeader.CompanyCode),
- TextFile = GetCellValue(reader, cols, CandidateFileHeader.TextFile),
- BankCode = GetCellValue(reader, cols, CandidateFileHeader.BankCode),
- AccountNumber = GetCellValue(reader, cols, CandidateFileHeader.AccouontNumer),
- TransDate = GetCellValue(reader, cols, CandidateFileHeader.TransDate),
- TransTime = GetCellValue(reader, cols, CandidateFileHeader.TransTime),
- CustomerName = GetCellValue(reader, cols, CandidateFileHeader.CustomerName),
- RefNo1 = GetCellValue(reader, cols, CandidateFileHeader.RefNo1),
- TermBranch = GetCellValue(reader, cols, CandidateFileHeader.TermBranch),
- TellerId = GetCellValue(reader, cols, CandidateFileHeader.TellerID),
- CreditDebit = GetCellValue(reader, cols, CandidateFileHeader.CreditDebit),
- PaymentType = GetCellValue(reader, cols, CandidateFileHeader.Type),
- ChequeNo = GetCellValue(reader, cols, CandidateFileHeader.ChequeNo),
- Amount = GetCellDecimal(reader, cols, CandidateFileHeader.Amount),
- ChqueBankCode = GetCellValue(reader, cols, CandidateFileHeader.ChqBankCode)
+ PaymentId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PaymentID)]?.GetValue() ?? "",
+ CompanyCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CompanyCode)]?.GetValue() ?? "",
+ TextFile = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TextFile)]?.GetValue() ?? "",
+ BankCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BankCode)]?.GetValue() ?? "",
+ AccountNumber = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.AccouontNumer)]?.GetValue() ?? "",
+ TransDate = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TransDate)]?.GetValue() ?? "",
+ TransTime = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TransTime)]?.GetValue() ?? "",
+ CustomerName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CustomerName)]?.GetValue() ?? "",
+ RefNo1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.RefNo1)]?.GetValue() ?? "",
+ TermBranch = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TermBranch)]?.GetValue() ?? "",
+ TellerId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TellerID)]?.GetValue() ?? "",
+ CreditDebit = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CreditDebit)]?.GetValue() ?? "",
+ PaymentType = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Type)]?.GetValue(),
+ ChequeNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChequeNo)]?.GetValue() ?? "",
+ Amount = (decimal)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amount)]?.GetValue(),
+ ChqueBankCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChqBankCode)]?.GetValue() ?? ""
};
// occupation
var occupation = new RecruitOccupation()
{
- Occupation = GetCellValue(reader, cols, CandidateFileHeader.Occupation),
- Position = GetCellValue(reader, cols, CandidateFileHeader.Position),
- Workplace = GetCellValue(reader, cols, CandidateFileHeader.Workplace),
- Telephone = GetCellValue(reader, cols, CandidateFileHeader.WorkplaceTelephone),
- WorkAge = GetCellValue(reader, cols, CandidateFileHeader.WorkAge),
+ Occupation = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Occupation)]?.GetValue() ?? "",
+ Position = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Position)]?.GetValue() ?? "",
+ Workplace = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Workplace)]?.GetValue() ?? "",
+ Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkplaceTelephone)]?.GetValue() ?? "",
+ WorkAge = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkAge)]?.GetValue() ?? "",
};
// certificate
var certificate = new RecruitCertificate()
{
- CertificateNo = GetCellValue(reader, cols, CandidateFileHeader.CertificateNo),
- Description = GetCellValue(reader, cols, CandidateFileHeader.CertificateDesc),
- IssueDate = Convert.ToDateTime(GetCellValue(reader, cols, CandidateFileHeader.CertificateIssueDate).ToDateTime(DateTimeFormat.Ymd, "-")),
- ExpiredDate = Convert.ToDateTime(GetCellValue(reader, cols, CandidateFileHeader.CertificateExpireDate).ToDateTime(DateTimeFormat.Ymd, "-"))
+ CertificateNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateNo)]?.GetValue() ?? "",
+ Description = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateDesc)]?.GetValue() ?? "",
+ IssueDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateIssueDate)]?.GetValue().ToDateTime(DateTimeFormat.Ymd, "-")),
+ ExpiredDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateExpireDate)]?.GetValue().ToDateTime(DateTimeFormat.Ymd, "-"))
};
var education = new RecruitEducation()
{
- Degree = GetCellValue(reader, cols, CandidateFileHeader.Degree),
- Major = GetCellValue(reader, cols, CandidateFileHeader.Major),
- MajorGroupId = GetCellValue(reader, cols, CandidateFileHeader.MajorGroupID),
- MajorGroupName = GetCellValue(reader, cols, CandidateFileHeader.MajorGroupName),
- University = GetCellValue(reader, cols, CandidateFileHeader.University),
- GPA = GetCellDouble(reader, cols, CandidateFileHeader.GPA),
- Specialist = GetCellValue(reader, cols, CandidateFileHeader.SpecialList),
- HighDegree = GetCellValue(reader, cols, CandidateFileHeader.HighDegree),
- BachelorDate = Convert.ToDateTime(GetCellValue(reader, cols, CandidateFileHeader.BachelorDate).ToDateTime(DateTimeFormat.Ymd, "-"))
+ Degree = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Degree)]?.GetValue() ?? "",
+ Major = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Major)]?.GetValue() ?? "",
+ MajorGroupId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.MajorGroupID)]?.GetValue() ?? "",
+ MajorGroupName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.MajorGroupName)]?.GetValue() ?? "",
+ University = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.University)]?.GetValue() ?? "",
+ GPA = (double)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.GPA)]?.GetValue(),
+ Specialist = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.SpecialList)]?.GetValue() ?? "",
+ HighDegree = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.HighDegree)]?.GetValue() ?? "",
+ BachelorDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BachelorDate)]?.GetValue().ToDateTime(DateTimeFormat.Ymd, "-"))
};
r.Addresses.Add(address);
@@ -246,6 +242,7 @@ public class ImportBackgroundService : BackgroundService
batchCertificates.Add(certificate);
batchEducations.Add(education);
+ row++;
batchCount++;
totalProcessed++;
@@ -303,7 +300,7 @@ public class ImportBackgroundService : BackgroundService
_context.ChangeTracker.Clear();
}
- } while (reader.NextResult());
+ }
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
}
@@ -334,14 +331,11 @@ public class ImportBackgroundService : BackgroundService
var importId = imported.Id;
var importRef = _context.Attach(new RecruitImport { Id = importId }).Entity;
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
- using var stream = System.IO.File.OpenRead(job.ImportFile);
- using var reader = ExcelReaderFactory.CreateReader(stream);
-
- do
+ using var c_package = new ExcelPackage(new FileInfo(job.ImportFile));
+ for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++)
{
- // Skip header row
- if (!reader.Read()) continue;
+ var workSheet = c_package.Workbook.Worksheets[i];
+ var totalRows = workSheet.Dimension.Rows;
int row = 2;
int batchCount = 0;
@@ -354,33 +348,33 @@ public class ImportBackgroundService : BackgroundService
var batchAddresses = new List();
var batchPayments = new List();
- while (reader.Read())
+ while (row <= totalRows)
{
- var cell1 = reader.GetValue(0)?.ToString();
- if (string.IsNullOrEmpty(cell1)) break;
+ var cell1 = workSheet?.Cells[row, 1]?.GetValue();
+ if (cell1 == "" || cell1 == null) break;
try
{
var r = new Models.Recruits.Recruit();
r.Id = Guid.NewGuid();
- r.ExamId = reader.GetValue(0)?.ToString() ?? "";
- r.PositionName = reader.GetValue(2)?.ToString() ?? "";
- r.HddPosition = reader.GetValue(3)?.ToString() ?? "";
- r.Prefix = reader.GetValue(4)?.ToString() == "อื่น ๆ" ? reader.GetValue(5)?.ToString() ?? "" : reader.GetValue(4)?.ToString() ?? "";
- r.FirstName = reader.GetValue(6)?.ToString() ?? "";
- r.LastName = reader.GetValue(7)?.ToString() ?? "";
- r.Gendor = reader.GetValue(97)?.ToString() ?? "";
- r.National = reader.GetValue(8)?.ToString() ?? "";
+ r.ExamId = workSheet?.Cells[row, 1]?.GetValue() ?? "";
+ r.PositionName = workSheet?.Cells[row, 3]?.GetValue() ?? "";
+ r.HddPosition = workSheet?.Cells[row, 4]?.GetValue() ?? "";
+ r.Prefix = workSheet?.Cells[row, 5]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 6]?.GetValue() ?? "" : workSheet?.Cells[row, 5]?.GetValue() ?? "";
+ r.FirstName = workSheet?.Cells[row, 7]?.GetValue() ?? "";
+ r.LastName = workSheet?.Cells[row, 8]?.GetValue() ?? "";
+ r.Gendor = workSheet?.Cells[row, 98]?.GetValue() ?? "";
+ r.National = workSheet?.Cells[row, 9]?.GetValue() ?? "";
r.Race = "";
- r.Religion = reader.GetValue(9)?.ToString() ?? "";
- r.DateOfBirth = !string.IsNullOrWhiteSpace(reader.GetValue(10)?.ToString()) ? _recruitService.CheckDateTime(reader.GetValue(10)?.ToString() ?? "", "dd/MM/yyyy") : null;
- r.CitizenId = reader.GetValue(11)?.ToString() ?? "";
- r.typeTest = reader.GetValue(12)?.ToString() ?? "";
+ r.Religion = workSheet?.Cells[row, 10]?.GetValue() ?? "";
+ r.DateOfBirth = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 11]?.GetValue() ?? "", "dd/MM/yyyy") : null;
+ r.CitizenId = workSheet?.Cells[row, 12]?.GetValue() ?? "";
+ r.typeTest = workSheet?.Cells[row, 13]?.GetValue() ?? "";
r.Marry = "";
r.Isspecial = "N";
r.CitizenCardExpireDate = null;
r.ModifiedDate = null;
- r.ApplyDate = !string.IsNullOrWhiteSpace(reader.GetValue(86)?.ToString()) ? _recruitService.CheckDateTime(reader.GetValue(86)?.ToString() ?? "", "dd/MM/yyyy") : null;
+ r.ApplyDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 87]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue() ?? "", "dd/MM/yyyy") : null;
r.PositionType = "";
r.PositionLevel = "";
r.CreatedAt = DateTime.Now;
@@ -395,15 +389,15 @@ public class ImportBackgroundService : BackgroundService
var education = new RecruitEducation()
{
Id = Guid.NewGuid(),
- Degree = reader.GetValue(17)?.ToString() ?? "",
- Major = reader.GetValue(18)?.ToString() == "อื่น ๆ" ? reader.GetValue(19)?.ToString() ?? "" : reader.GetValue(18)?.ToString() ?? "",
+ Degree = workSheet?.Cells[row, 18]?.GetValue() ?? "",
+ Major = workSheet?.Cells[row, 19]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 20]?.GetValue() ?? "" : workSheet?.Cells[row, 19]?.GetValue() ?? "",
MajorGroupId = "",
MajorGroupName = "",
- University = reader.GetValue(20)?.ToString() == "อื่น ๆ" ? reader.GetValue(21)?.ToString() ?? "" : reader.GetValue(20)?.ToString() ?? "",
- GPA = GetReaderDouble(reader, 25),
+ University = workSheet?.Cells[row, 21]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue() ?? "" : workSheet?.Cells[row, 21]?.GetValue() ?? "",
+ GPA = (double)workSheet?.Cells[row, 26]?.GetValue(),
Specialist = "",
- HighDegree = reader.GetValue(26)?.ToString() ?? "",
- BachelorDate = !string.IsNullOrWhiteSpace(reader.GetValue(24)?.ToString()) ? _recruitService.CheckDateTime(reader.GetValue(24)?.ToString() ?? "", "dd/MM/yyyy") : null,
+ HighDegree = workSheet?.Cells[row, 27]?.GetValue() ?? "",
+ BachelorDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 25]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 25]?.GetValue() ?? "", "dd/MM/yyyy") : null,
Recruit = r,
CreatedAt = DateTime.Now,
CreatedUserId = job.UserId ?? "",
@@ -416,11 +410,11 @@ public class ImportBackgroundService : BackgroundService
var occupation = new RecruitOccupation()
{
Id = Guid.NewGuid(),
- Occupation = reader.GetValue(32)?.ToString() == "อื่น ๆ" ? reader.GetValue(33)?.ToString() ?? "" : reader.GetValue(32)?.ToString() ?? "",
- Position = reader.GetValue(36)?.ToString() ?? "",
- Workplace = $"{(reader.GetValue(35)?.ToString() ?? "")} {(reader.GetValue(34)?.ToString() ?? "")}",
- Telephone = "",
- WorkAge = "",
+ Occupation = workSheet?.Cells[row, 33]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 34]?.GetValue() ?? "" : workSheet?.Cells[row, 33]?.GetValue() ?? "",
+ Position = workSheet?.Cells[row, 37]?.GetValue() ?? "",
+ Workplace = $"{(workSheet?.Cells[row, 36]?.GetValue() ?? "")} {(workSheet?.Cells[row, 35]?.GetValue() ?? "")}",
+ Telephone = workSheet?.Cells[row, 9999]?.GetValue() ?? "",
+ WorkAge = workSheet?.Cells[row, 9999]?.GetValue() ?? "",
Recruit = r,
CreatedAt = DateTime.Now,
CreatedUserId = job.UserId ?? "",
@@ -433,24 +427,24 @@ public class ImportBackgroundService : BackgroundService
var address = new RecruitAddress()
{
Id = Guid.NewGuid(),
- Address = $"{(reader.GetValue(48)?.ToString() ?? "")} {(reader.GetValue(49)?.ToString() ?? "")}",
- Moo = reader.GetValue(50)?.ToString() ?? "",
- Soi = reader.GetValue(51)?.ToString() ?? "",
- Road = reader.GetValue(52)?.ToString() ?? "",
- District = reader.GetValue(53)?.ToString() ?? "",
- Amphur = reader.GetValue(54)?.ToString() ?? "",
- Province = reader.GetValue(55)?.ToString() ?? "",
- ZipCode = (reader.GetValue(56)?.ToString() ?? "").Trim(),
- Telephone = reader.GetValue(57)?.ToString() ?? "",
+ Address = $"{(workSheet?.Cells[row, 49]?.GetValue() ?? "")} {(workSheet?.Cells[row, 50]?.GetValue() ?? "")}",
+ Moo = workSheet?.Cells[row, 51]?.GetValue() ?? "",
+ Soi = workSheet?.Cells[row, 52]?.GetValue() ?? "",
+ Road = workSheet?.Cells[row, 53]?.GetValue() ?? "",
+ District = workSheet?.Cells[row, 54]?.GetValue() ?? "",
+ Amphur = workSheet?.Cells[row, 55]?.GetValue() ?? "",
+ Province = workSheet?.Cells[row, 56]?.GetValue() ?? "",
+ ZipCode = (workSheet?.Cells[row, 57]?.GetValue() ?? "").Trim(),
+ Telephone = workSheet?.Cells[row, 58]?.GetValue() ?? "",
Mobile = "",
- Address1 = $"{(reader.GetValue(60)?.ToString() ?? "")} {(reader.GetValue(61)?.ToString() ?? "")}",
- Moo1 = reader.GetValue(62)?.ToString() ?? "",
- Soi1 = reader.GetValue(63)?.ToString() ?? "",
- Road1 = reader.GetValue(64)?.ToString() ?? "",
- District1 = reader.GetValue(65)?.ToString() ?? "",
- Amphur1 = reader.GetValue(66)?.ToString() ?? "",
- Province1 = reader.GetValue(67)?.ToString() ?? "",
- ZipCode1 = (reader.GetValue(68)?.ToString() ?? "").Trim(),
+ Address1 = $"{(workSheet?.Cells[row, 61]?.GetValue() ?? "")} {(workSheet?.Cells[row, 62]?.GetValue() ?? "")}",
+ Moo1 = workSheet?.Cells[row, 63]?.GetValue() ?? "",
+ Soi1 = workSheet?.Cells[row, 64]?.GetValue() ?? "",
+ Road1 = workSheet?.Cells[row, 65]?.GetValue() ?? "",
+ District1 = workSheet?.Cells[row, 66]?.GetValue() ?? "",
+ Amphur1 = workSheet?.Cells[row, 67]?.GetValue() ?? "",
+ Province1 = workSheet?.Cells[row, 68]?.GetValue() ?? "",
+ ZipCode1 = (workSheet?.Cells[row, 69]?.GetValue() ?? "").Trim(),
Recruit = r,
CreatedAt = DateTime.Now,
CreatedUserId = job.UserId ?? "",
@@ -463,22 +457,22 @@ public class ImportBackgroundService : BackgroundService
var payment = new RecruitPayment()
{
Id = Guid.NewGuid(),
- PaymentId = reader.GetValue(103)?.ToString() ?? "",
- CompanyCode = reader.GetValue(104)?.ToString() ?? "",
- TextFile = reader.GetValue(105)?.ToString() ?? "",
- BankCode = reader.GetValue(106)?.ToString() ?? "",
- AccountNumber = reader.GetValue(107)?.ToString() ?? "",
- TransDate = reader.GetValue(108)?.ToString() ?? "",
- TransTime = reader.GetValue(109)?.ToString() ?? "",
- CustomerName = reader.GetValue(110)?.ToString() ?? "",
- RefNo1 = reader.GetValue(111)?.ToString() ?? "",
- TermBranch = reader.GetValue(112)?.ToString() ?? "",
- TellerId = reader.GetValue(113)?.ToString() ?? "",
- CreditDebit = reader.GetValue(114)?.ToString() ?? "",
- PaymentType = reader.GetValue(115)?.ToString() ?? "",
- ChequeNo = reader.GetValue(116)?.ToString() ?? "",
- Amount = GetReaderDecimal(reader, 117),
- ChqueBankCode = reader.GetValue(118)?.ToString() ?? "",
+ PaymentId = workSheet?.Cells[row, 104]?.GetValue() ?? "",
+ CompanyCode = workSheet?.Cells[row, 105]?.GetValue() ?? "",
+ TextFile = workSheet?.Cells[row, 106]?.GetValue() ?? "",
+ BankCode = workSheet?.Cells[row, 107]?.GetValue() ?? "",
+ AccountNumber = workSheet?.Cells[row, 108]?.GetValue() ?? "",
+ TransDate = workSheet?.Cells[row, 109]?.GetValue() ?? "",
+ TransTime = workSheet?.Cells[row, 110]?.GetValue() ?? "",
+ CustomerName = workSheet?.Cells[row, 111]?.GetValue() ?? "",
+ RefNo1 = workSheet?.Cells[row, 112]?.GetValue() ?? "",
+ TermBranch = workSheet?.Cells[row, 113]?.GetValue() ?? "",
+ TellerId = workSheet?.Cells[row, 114]?.GetValue() ?? "",
+ CreditDebit = workSheet?.Cells[row, 115]?.GetValue() ?? "",
+ PaymentType = workSheet?.Cells[row, 116]?.GetValue() ?? "",
+ ChequeNo = workSheet?.Cells[row, 117]?.GetValue() ?? "",
+ Amount = (decimal)workSheet?.Cells[row, 118]?.GetValue(),
+ ChqueBankCode = workSheet?.Cells[row, 119]?.GetValue() ?? "",
Recruit = r,
CreatedAt = DateTime.Now,
CreatedUserId = job.UserId ?? "",
@@ -551,7 +545,7 @@ public class ImportBackgroundService : BackgroundService
_context.ChangeTracker.Clear();
}
- } while (reader.NextResult());
+ }
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
}
@@ -572,9 +566,48 @@ public class ImportBackgroundService : BackgroundService
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());
+
+ // Delete the old ScoreImport entity to prevent duplicate FK constraint
+ await _context.BulkDeleteAsync(new List { 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
{
Description = "นำเข้าข้อมูลผลคะแนนสอบ",
@@ -586,12 +619,14 @@ public class ImportBackgroundService : BackgroundService
LastUpdateFullName = job.FullName ?? "System Administrator",
});
- // get doc from minio
- var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile));
+ // Load Document entity fresh from database to avoid tracking conflicts
+ var freshDoc = await _context.Documents.FindAsync(docId)
+ ?? throw new Exception("Failed to retrieve uploaded document. Please contact support.");
+
var imported = new ScoreImport
{
Year = rec_import.Year,
- ImportFile = doc,
+ ImportFile = freshDoc,
CreatedAt = DateTime.Now,
CreatedUserId = job.UserId ?? "",
CreatedFullName = job.FullName ?? "System Administrator",
@@ -601,96 +636,94 @@ public class ImportBackgroundService : BackgroundService
Scores = new List()
};
+ // Add ScoreImport to context explicitly to ensure EF Core knows to INSERT it
+ _context.Add(imported);
+
// Save ScoreImport parent first to get its Id
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
+ var scoreImportId = imported.Id;
_context.ChangeTracker.Clear();
- // preload recruits using AsNoTracking to avoid EF tracking overhead
+ // Re-attach ScoreImport reference to avoid FK issues during bulk insert
+ var importRef = _context.Attach(new ScoreImport { Id = scoreImportId }).Entity;
+
+ // preload recruits (lightweight - only ExamId)
var recruitsDict = await _context.Recruits
- .AsNoTracking()
.Where(x => x.RecruitImport.Id == rec_import.Id)
.GroupBy(x => x.ExamId)
.Where(g => g.Count() == 1)
.Select(g => g.First())
.ToDictionaryAsync(x => x.ExamId, x => x);
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
- using var stream = System.IO.File.OpenRead(job.ImportFile);
- using var reader = ExcelReaderFactory.CreateReader(stream);
-
- do
+ using var c_package = new ExcelPackage(new FileInfo(job.ImportFile));
+ for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++)
{
- // Read header rows (rows 1-7), then data starts at row 8
- // Skip 7 rows: first 7 are header/metadata
- for (int skip = 0; skip < 7; skip++)
- {
- if (!reader.Read()) break;
- }
-
- var cols = new string[reader.FieldCount];
- // Use current row (row 7) as header reference — not actually used for ScoreFile column mapping
- // ScoreFile uses hardcoded column indices
-
+ var workSheet = c_package.Workbook.Worksheets[i];
+ var cols = workSheet.GetHeaderColumns();
+ int row = 8;
int batchCount = 0;
const int batchSize = 500;
int totalProcessed = 0;
+ var endRow = workSheet.Dimension.End.Row;
var batchScores = new List();
- while (reader.Read())
+ while (row <= endRow)
{
- var cell1 = reader.GetValue(0)?.ToString();
- if (string.IsNullOrEmpty(cell1)) break;
+ var cell1 = workSheet?.Cells[row, 1]?.GetValue();
+ if (cell1 == "" || cell1 == null) break;
var r = new RecruitScore();
- r.ExamId = reader.GetValue(1)?.ToString();
+ r.ExamId = workSheet?.Cells[row, 2]?.GetValue();
if (!string.IsNullOrEmpty(r.ExamId) && recruitsDict.TryGetValue(r.ExamId, out var recruit))
{
- r.CitizenId = reader.GetValue(2)?.ToString()?.Trim();
+ r.Id = Guid.NewGuid(); // Generate unique ID for each record
+ r.CitizenId = workSheet?.Cells[row, 3]?.GetValue()?.Trim();
r.FullA = 200;
- r.SumA = string.IsNullOrWhiteSpace(reader.GetValue(4)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(4)), 2);
- r.PercentageA = string.IsNullOrWhiteSpace(reader.GetValue(5)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(5)), 2);
- r.AStatus = string.IsNullOrWhiteSpace(reader.GetValue(6)?.ToString()) ? "" : reader.GetValue(6)?.ToString();
- r.SumAB = string.IsNullOrWhiteSpace(reader.GetValue(4)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(4)), 2);
- r.ABStatus = string.IsNullOrWhiteSpace(reader.GetValue(6)?.ToString()) ? "" : reader.GetValue(6)?.ToString();
+ r.SumA = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 5]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 5].GetValue(), 2);
+ r.PercentageA = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 6]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 6].GetValue(), 2);
+ r.AStatus = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 7]?.GetValue()) ? "" : workSheet?.Cells[row, 7]?.GetValue();
+ r.SumAB = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 5]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 5].GetValue(), 2);
+ r.ABStatus = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 7]?.GetValue()) ? "" : workSheet?.Cells[row, 7]?.GetValue();
r.FullC = 50;
- r.SumC = string.IsNullOrWhiteSpace(reader.GetValue(7)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(7)), 2);
+ r.SumC = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 8]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 8].GetValue(), 2);
r.FullD = 50;
- r.SumD = string.IsNullOrWhiteSpace(reader.GetValue(8)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(8)), 2);
- r.SumCD = string.IsNullOrWhiteSpace(reader.GetValue(9)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(9)), 2);
- r.PercentageC = string.IsNullOrWhiteSpace(reader.GetValue(10)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(10)), 2);
- r.CStatus = string.IsNullOrWhiteSpace(reader.GetValue(11)?.ToString()) ? "" : reader.GetValue(11)?.ToString();
+ r.SumD = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 9]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 9].GetValue(), 2);
+ r.SumCD = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 10]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 10].GetValue(), 2);
+ r.PercentageC = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 11].GetValue(), 2);
+ r.CStatus = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 12]?.GetValue()) ? "" : workSheet?.Cells[row, 12]?.GetValue();
r.FullScore = 300;
- r.TotalScore = string.IsNullOrWhiteSpace(reader.GetValue(12)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(12)), 2);
+ r.TotalScore = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 13]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 13].GetValue(), 2);
- var examStatusCol7 = reader.GetValue(6)?.ToString()?.Trim();
- var examStatusCol14 = reader.GetValue(13)?.ToString()?.Trim();
+ var examStatusCol7 = workSheet?.Cells[row, 7]?.GetValue()?.Trim();
+ var examStatusCol14 = workSheet?.Cells[row, 14]?.GetValue()?.Trim();
r.ExamStatus =
examStatusCol7 == "ขาดสอบ" ? "ขส." :
examStatusCol14 == "ได้" ? "ผ่าน" :
examStatusCol14 == "ตก" ? "ไม่ผ่าน" : "-";
- r.RemarkScore = string.IsNullOrWhiteSpace(reader.GetValue(14)?.ToString()) ? string.Empty : reader.GetValue(14)?.ToString();
+ r.RemarkScore = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 15]?.GetValue()) ? string.Empty : workSheet?.Cells[row, 15]?.GetValue();
- var examAttr = reader.GetValue(15)?.ToString()?.Trim();
+ var examAttr = workSheet?.Cells[row, 16]?.GetValue()?.Trim();
r.ExamAttribute =
examAttr == "ผ่าน" ? "มีคุณสมบัติ" :
examAttr == "ไม่ผ่าน" ? "ไม่มีคุณสมบัติ" : "";
- r.Major = reader.Name; // worksheet name
+ r.Major = workSheet.Name;
r.CreatedAt = DateTime.Now;
r.CreatedUserId = job.UserId ?? "";
r.CreatedFullName = job.FullName ?? "System Administrator";
r.LastUpdatedAt = DateTime.Now;
r.LastUpdateUserId = job.UserId ?? "";
r.LastUpdateFullName = job.FullName ?? "System Administrator";
- r.ScoreImport = imported;
+ r.ScoreImport = importRef;
batchScores.Add(r);
}
+ row++;
batchCount++;
totalProcessed++;
@@ -708,7 +741,7 @@ public class ImportBackgroundService : BackgroundService
{
await _context.BulkInsertAsync(batchScores);
}
- } while (reader.NextResult());
+ }
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
}
@@ -757,65 +790,75 @@ public class ImportBackgroundService : BackgroundService
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
- // preload scores using AsNoTracking to avoid EF tracking overhead
+ // หา ScoreImportId ก่อน
+ var scoreImportId = await _context.ScoreImports
+ .Where(x => x.RecruitImportId == rec_import.Id)
+ .Select(x => x.Id)
+ .FirstOrDefaultAsync();
+
+ if (scoreImportId == Guid.Empty) return;
+
+ // preload scores - query all entities that will be updated (with tracking)
var scoreList = await _context.RecruitScores
- .AsNoTracking()
- .Where(s => s.ScoreImport.RecruitImportId == rec_import.Id && !string.IsNullOrEmpty(s.ExamId))
+ .Where(s => EF.Property(s, "ScoreImportId") == scoreImportId && !string.IsNullOrEmpty(s.ExamId))
.GroupBy(x => x.ExamId)
.Where(g => g.Count() == 1)
.Select(g => g.First())
.ToListAsync();
+
+ // Group by ExamId for easy lookup
var score = scoreList.ToDictionary(s => s.ExamId!, s => s);
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
+ // ถ้าไม่มีผลคะแนนสอบคัดเลือกผู้พิการให้จบการทำงาน
+ if (score.Count == 0) return;
+
+ // Read from saved file (ResultFile uses stream from Form, but we saved to disk)
using var stream = System.IO.File.OpenRead(job.ImportFile);
- using var reader = ExcelReaderFactory.CreateReader(stream);
+ using var c_package = new ExcelPackage(stream);
- do
+ foreach (var workSheet in c_package.Workbook.Worksheets)
{
- // Skip 6 header rows, data starts at row 7
- for (int skip = 0; skip < 6; skip++)
- {
- if (!reader.Read()) break;
- }
-
+ int row = 7;
int batchCount = 0;
const int batchSize = 500;
- var batchUpdates = new List();
+ var endRow = workSheet.Dimension.End.Row;
- while (reader.Read())
+ while (row <= endRow)
{
- var examId = reader.GetValue(1)?.ToString();
+ var examId = workSheet?.Cells[row, 2]?.GetValue();
if (string.IsNullOrWhiteSpace(examId))
{
+ row++;
continue;
}
if (score.TryGetValue(examId, out var existingScore))
{
- existingScore.Number = reader.GetValue(0)?.ToString();
- existingScore.RemarkExamOrder = reader.GetValue(3)?.ToString() ?? string.Empty;
+ existingScore.Number = workSheet?.Cells[row, 1]?.GetValue();
+ existingScore.RemarkExamOrder = workSheet?.Cells[row, 4]?.GetValue() ?? string.Empty;
existingScore.LastUpdatedAt = DateTime.Now;
existingScore.LastUpdateUserId = job.UserId ?? "";
existingScore.LastUpdateFullName = job.FullName ?? "System Administrator";
- batchUpdates.Add(existingScore);
batchCount++;
}
+ row++;
+
if (batchCount >= batchSize)
{
- await _context.BulkUpdateAsync(batchUpdates);
- batchUpdates.Clear();
+ await _context.SaveChangesAsync();
+ _context.ChangeTracker.Clear();
batchCount = 0;
}
}
// Process remaining records
- if (batchUpdates.Count > 0)
+ if (batchCount > 0)
{
- await _context.BulkUpdateAsync(batchUpdates);
+ await _context.SaveChangesAsync();
+ _context.ChangeTracker.Clear();
}
- } while (reader.NextResult());
+ }
}
#endregion
@@ -837,81 +880,6 @@ public class ImportBackgroundService : BackgroundService
}
}
- ///
- /// Get string value from ExcelDataReader by header column name
- ///
- private static string GetCellValue(IExcelDataReader reader, string[] cols, string headerName)
- {
- var idx = GetColumnIndex(cols, headerName);
- if (idx <= 0 || idx > reader.FieldCount) return "";
- return reader.GetValue(idx - 1)?.ToString() ?? "";
- }
-
- ///
- /// Get DateTime value from ExcelDataReader by header column name
- ///
- private static DateTime? GetCellDateTime(IExcelDataReader reader, string[] cols, string headerName)
- {
- var idx = GetColumnIndex(cols, headerName);
- if (idx <= 0 || idx > reader.FieldCount) return null;
- var val = reader.GetValue(idx - 1);
- if (val is DateTime dt) return dt;
- if (val != null && DateTime.TryParse(val.ToString(), out var parsed)) return parsed;
- return null;
- }
-
- ///
- /// Get double value from ExcelDataReader by header column name
- ///
- private static double GetCellDouble(IExcelDataReader reader, string[] cols, string headerName)
- {
- var idx = GetColumnIndex(cols, headerName);
- if (idx <= 0 || idx > reader.FieldCount) return 0.0;
- var val = reader.GetValue(idx - 1);
- if (val is double d) return d;
- if (val != null && double.TryParse(val.ToString(), out var parsed)) return parsed;
- return 0.0;
- }
-
- ///
- /// Get decimal value from ExcelDataReader by header column name
- ///
- private static decimal GetCellDecimal(IExcelDataReader reader, string[] cols, string headerName)
- {
- var idx = GetColumnIndex(cols, headerName);
- if (idx <= 0 || idx > reader.FieldCount) return 0m;
- var val = reader.GetValue(idx - 1);
- if (val is decimal dec) return dec;
- if (val is double dbl) return (decimal)dbl;
- if (val != null && decimal.TryParse(val.ToString(), out var parsed)) return parsed;
- return 0m;
- }
-
- ///
- /// Get double value from ExcelDataReader by 0-based column index
- ///
- private static double GetReaderDouble(IExcelDataReader reader, int index)
- {
- if (index < 0 || index >= reader.FieldCount) return 0.0;
- var val = reader.GetValue(index);
- if (val is double d) return d;
- if (val != null && double.TryParse(val.ToString(), out var parsed)) return parsed;
- return 0.0;
- }
-
- ///
- /// Get decimal value from ExcelDataReader by 0-based column index
- ///
- private static decimal GetReaderDecimal(IExcelDataReader reader, int index)
- {
- if (index < 0 || index >= reader.FieldCount) return 0m;
- var val = reader.GetValue(index);
- if (val is decimal dec) return dec;
- if (val is double dbl) return (decimal)dbl;
- if (val != null && decimal.TryParse(val.ToString(), out var parsed)) return parsed;
- return 0m;
- }
-
#endregion
}
diff --git a/obj/project.assets.json b/obj/project.assets.json
index 82551d3..818122a 100644
--- a/obj/project.assets.json
+++ b/obj/project.assets.json
@@ -289,35 +289,6 @@
"lib/net7.0/EPPlus.System.Drawing.dll": {}
}
},
- "ExcelDataReader/3.8.0": {
- "type": "package",
- "compile": {
- "lib/netstandard2.1/ExcelDataReader.dll": {
- "related": ".pdb;.xml"
- }
- },
- "runtime": {
- "lib/netstandard2.1/ExcelDataReader.dll": {
- "related": ".pdb;.xml"
- }
- }
- },
- "ExcelDataReader.DataSet/3.8.0": {
- "type": "package",
- "dependencies": {
- "ExcelDataReader": "3.8.0"
- },
- "compile": {
- "lib/netstandard2.1/ExcelDataReader.DataSet.dll": {
- "related": ".pdb;.xml"
- }
- },
- "runtime": {
- "lib/netstandard2.1/ExcelDataReader.DataSet.dll": {
- "related": ".pdb;.xml"
- }
- }
- },
"Google.Protobuf/3.19.4": {
"type": "package",
"compile": {
@@ -5699,56 +5670,6 @@
"readme.md"
]
},
- "ExcelDataReader/3.8.0": {
- "sha512": "kbUsldc5Fn9IKgzL2nr4VvN/mKqPqn8zGXUZpA7uL6svCA4psF+qMK519EhMvderpU4pAJoqk9DWpiSIkiZtXA==",
- "type": "package",
- "path": "exceldatareader/3.8.0",
- "files": [
- ".nupkg.metadata",
- ".signature.p7s",
- "ExcelDataReader.png",
- "README.md",
- "exceldatareader.3.8.0.nupkg.sha512",
- "exceldatareader.nuspec",
- "lib/net462/ExcelDataReader.dll",
- "lib/net462/ExcelDataReader.pdb",
- "lib/net462/ExcelDataReader.xml",
- "lib/net8.0/ExcelDataReader.dll",
- "lib/net8.0/ExcelDataReader.pdb",
- "lib/net8.0/ExcelDataReader.xml",
- "lib/netstandard2.0/ExcelDataReader.dll",
- "lib/netstandard2.0/ExcelDataReader.pdb",
- "lib/netstandard2.0/ExcelDataReader.xml",
- "lib/netstandard2.1/ExcelDataReader.dll",
- "lib/netstandard2.1/ExcelDataReader.pdb",
- "lib/netstandard2.1/ExcelDataReader.xml"
- ]
- },
- "ExcelDataReader.DataSet/3.8.0": {
- "sha512": "+eQg5oinHir7ayE/rF4dedvy8J6FBDG8RDyKFQsS/VZG9ygrnNgW6U8JSlrfGfe3DxYgbVoVwYV9Hbk63JQJFQ==",
- "type": "package",
- "path": "exceldatareader.dataset/3.8.0",
- "files": [
- ".nupkg.metadata",
- ".signature.p7s",
- "ExcelDataReader.png",
- "README.md",
- "exceldatareader.dataset.3.8.0.nupkg.sha512",
- "exceldatareader.dataset.nuspec",
- "lib/net462/ExcelDataReader.DataSet.dll",
- "lib/net462/ExcelDataReader.DataSet.pdb",
- "lib/net462/ExcelDataReader.DataSet.xml",
- "lib/net8.0/ExcelDataReader.DataSet.dll",
- "lib/net8.0/ExcelDataReader.DataSet.pdb",
- "lib/net8.0/ExcelDataReader.DataSet.xml",
- "lib/netstandard2.0/ExcelDataReader.DataSet.dll",
- "lib/netstandard2.0/ExcelDataReader.DataSet.pdb",
- "lib/netstandard2.0/ExcelDataReader.DataSet.xml",
- "lib/netstandard2.1/ExcelDataReader.DataSet.dll",
- "lib/netstandard2.1/ExcelDataReader.DataSet.pdb",
- "lib/netstandard2.1/ExcelDataReader.DataSet.xml"
- ]
- },
"Google.Protobuf/3.19.4": {
"sha512": "fd07/ykL4O4FhqrZIELm5lmiyOHfdPg9+o+hWr6tcfRdS7tHXnImg/2wtogLzlW2eEmr0J7j6ZrZvaWOLiJbxQ==",
"type": "package",
@@ -14075,8 +13996,6 @@
"CoreAdmin >= 2.7.0",
"EFCore.BulkExtensions.MySql >= 6.7.16",
"EPPlus >= 6.1.3",
- "ExcelDataReader >= 3.8.0",
- "ExcelDataReader.DataSet >= 3.8.0",
"Microsoft.AspNetCore.Authentication.JwtBearer >= 7.0.20",
"Microsoft.AspNetCore.Mvc.NewtonsoftJson >= 7.0.3",
"Microsoft.AspNetCore.Mvc.Versioning >= 5.0.0",
@@ -14170,14 +14089,6 @@
"target": "Package",
"version": "[6.1.3, )"
},
- "ExcelDataReader": {
- "target": "Package",
- "version": "[3.8.0, )"
- },
- "ExcelDataReader.DataSet": {
- "target": "Package",
- "version": "[3.8.0, )"
- },
"Microsoft.AspNetCore.Authentication.JwtBearer": {
"target": "Package",
"version": "[7.0.20, )"
diff --git a/obj/project.nuget.cache b/obj/project.nuget.cache
index 4529909..e443564 100644
--- a/obj/project.nuget.cache
+++ b/obj/project.nuget.cache
@@ -1,6 +1,6 @@
{
"version": 2,
- "dgSpecHash": "++tptrWNc3M=",
+ "dgSpecHash": "XR3lYvVwNcQ=",
"success": true,
"projectFilePath": "/Users/suphonchaip/Develop/hrms/hrms-api-recruit/BMA.EHR.Recruit.csproj",
"expectedPackageFiles": [
@@ -21,8 +21,6 @@
"/Users/suphonchaip/.nuget/packages/epplus/6.1.3/epplus.6.1.3.nupkg.sha512",
"/Users/suphonchaip/.nuget/packages/epplus.interfaces/6.1.1/epplus.interfaces.6.1.1.nupkg.sha512",
"/Users/suphonchaip/.nuget/packages/epplus.system.drawing/6.1.1/epplus.system.drawing.6.1.1.nupkg.sha512",
- "/Users/suphonchaip/.nuget/packages/exceldatareader/3.8.0/exceldatareader.3.8.0.nupkg.sha512",
- "/Users/suphonchaip/.nuget/packages/exceldatareader.dataset/3.8.0/exceldatareader.dataset.3.8.0.nupkg.sha512",
"/Users/suphonchaip/.nuget/packages/google.protobuf/3.19.4/google.protobuf.3.19.4.nupkg.sha512",
"/Users/suphonchaip/.nuget/packages/humanizer.core/2.14.1/humanizer.core.2.14.1.nupkg.sha512",
"/Users/suphonchaip/.nuget/packages/k4os.compression.lz4/1.2.6/k4os.compression.lz4.1.2.6.nupkg.sha512",