From 17d48ebbf8e1b5ab6f28d7bf5e9bba20d40b5ec7 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 12 May 2026 13:21:38 +0700 Subject: [PATCH 01/18] fix --- .../BMA.EHR.Recruit.Service.AssemblyInfo.cs | 2 +- ...R.Recruit.Service.AssemblyInfoInputs.cache | 2 +- ....GeneratedMSBuildEditorConfig.editorconfig | 2 ++ .../BMA.EHR.Recruit.Service.GlobalUsings.g.cs | 32 +++++++++--------- .../BMA.EHR.Recruit.Service.assets.cache | Bin 177121 -> 177121 bytes 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfo.cs b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfo.cs index 945f009..b0967b3 100644 --- a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfo.cs +++ b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("BMA.EHR.Recruit.Service")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d245236a144a9ba61661418db9ef1a8a2225d063")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+e1d869c9329ffd1d9e4fdb73f077eee60b67e09c")] [assembly: System.Reflection.AssemblyProductAttribute("BMA.EHR.Recruit.Service")] [assembly: System.Reflection.AssemblyTitleAttribute("BMA.EHR.Recruit.Service")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfoInputs.cache b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfoInputs.cache index 7935fe8..0aa2ab4 100644 --- a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfoInputs.cache +++ b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.AssemblyInfoInputs.cache @@ -1 +1 @@ -157a45ee45981ce605f03881ba3934018f157203e24d6fc6a8c2d7a6fcd5987d +115668deb371dd5b1a7e9691f5117e5e4acdd7bef73f049871879b64ab58463e diff --git a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GeneratedMSBuildEditorConfig.editorconfig b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GeneratedMSBuildEditorConfig.editorconfig index c970989..9c7d6d3 100644 --- a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GeneratedMSBuildEditorConfig.editorconfig +++ b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GeneratedMSBuildEditorConfig.editorconfig @@ -1,5 +1,7 @@ is_global = true build_property.TargetFramework = net7.0 +build_property.TargetFrameworkIdentifier = .NETCoreApp +build_property.TargetFrameworkVersion = v7.0 build_property.TargetPlatformMinVersion = build_property.UsingMicrosoftNETSdkWeb = true build_property.ProjectTypeGuids = diff --git a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GlobalUsings.g.cs b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GlobalUsings.g.cs index 025530a..5e6145d 100644 --- a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GlobalUsings.g.cs +++ b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.GlobalUsings.g.cs @@ -1,17 +1,17 @@ // -global using global::Microsoft.AspNetCore.Builder; -global using global::Microsoft.AspNetCore.Hosting; -global using global::Microsoft.AspNetCore.Http; -global using global::Microsoft.AspNetCore.Routing; -global using global::Microsoft.Extensions.Configuration; -global using global::Microsoft.Extensions.DependencyInjection; -global using global::Microsoft.Extensions.Hosting; -global using global::Microsoft.Extensions.Logging; -global using global::System; -global using global::System.Collections.Generic; -global using global::System.IO; -global using global::System.Linq; -global using global::System.Net.Http; -global using global::System.Net.Http.Json; -global using global::System.Threading; -global using global::System.Threading.Tasks; +global using Microsoft.AspNetCore.Builder; +global using Microsoft.AspNetCore.Hosting; +global using Microsoft.AspNetCore.Http; +global using Microsoft.AspNetCore.Routing; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Hosting; +global using Microsoft.Extensions.Logging; +global using System; +global using System.Collections.Generic; +global using System.IO; +global using System.Linq; +global using System.Net.Http; +global using System.Net.Http.Json; +global using System.Threading; +global using System.Threading.Tasks; diff --git a/obj/Debug/net7.0/BMA.EHR.Recruit.Service.assets.cache b/obj/Debug/net7.0/BMA.EHR.Recruit.Service.assets.cache index f27eddc0e4f03a706e8917aed87bef9f39b2bfa8..43caf71f56e85771bfc3d81f98c0cb8bd90eb9b0 100644 GIT binary patch delta 57 zcmV-90LK5}=L+HH3Mf!ZM?nk#002S;&km`3dV1&zfjt31>r#Z*_+e!T^(O{=ov|TA P&Z&VYg(v~FC<3{&jQ Date: Thu, 14 May 2026 16:29:16 +0700 Subject: [PATCH 02/18] add send noti and bulk insert (not complete) --- Controllers/RecruitController.cs | 4 + Program.cs | 1 + Services/ImportBackgroundService.cs | 323 ++++++++++++++-------------- Services/ImportJobTracker.cs | 1 + Services/NotificationService.cs | 59 +++++ appsettings.json | 2 +- bin/Debug/net7.0/appsettings.json | 2 +- 7 files changed, 232 insertions(+), 160 deletions(-) create mode 100644 Services/NotificationService.cs diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs index bbf38c3..8c1fbdd 100644 --- a/Controllers/RecruitController.cs +++ b/Controllers/RecruitController.cs @@ -755,6 +755,7 @@ namespace BMA.EHR.Recruit.Controllers ImportDocId = import_doc_id, UserId = UserId, FullName = FullName, + Token = token, Request = req, }); await _importJobQueue.EnqueueAsync(job); @@ -943,6 +944,7 @@ namespace BMA.EHR.Recruit.Controllers ImportDocId = import_doc_id, UserId = UserId, FullName = FullName, + Token = token, }); await _importJobQueue.EnqueueAsync(job); @@ -1013,6 +1015,7 @@ namespace BMA.EHR.Recruit.Controllers ImportDocId = import_doc_id, UserId = UserId, FullName = FullName, + Token = token, }); await _importJobQueue.EnqueueAsync(job); @@ -1082,6 +1085,7 @@ namespace BMA.EHR.Recruit.Controllers RecruitImportId = id, UserId = UserId, FullName = FullName, + Token = token, }); await _importJobQueue.EnqueueAsync(job); diff --git a/Program.cs b/Program.cs index f39a56c..3475b74 100644 --- a/Program.cs +++ b/Program.cs @@ -69,6 +69,7 @@ builder.Services.AddAuthorization(); // Register Services builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 230a37c..f041390 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -47,6 +47,7 @@ public class ImportBackgroundService : BackgroundService var context = scope.ServiceProvider.GetRequiredService(); var minioService = scope.ServiceProvider.GetRequiredService(); var recruitService = scope.ServiceProvider.GetRequiredService(); + var notificationService = scope.ServiceProvider.GetRequiredService(); var webHostEnv = scope.ServiceProvider.GetRequiredService(); var logger = scope.ServiceProvider.GetRequiredService>(); @@ -71,11 +72,15 @@ public class ImportBackgroundService : BackgroundService } _tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount); + + await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ"); } catch (Exception ex) { logger.LogError(ex, "Import job {JobId} failed: {Message}", job.JobId, ex.Message); - _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.InnerException?.Message ?? ex.Message); + _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message); + + await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message); // cleanup minio file on failure if (!string.IsNullOrEmpty(job.ImportDocId)) @@ -321,6 +326,7 @@ public class ImportBackgroundService : BackgroundService _context.ChangeTracker.Clear(); var importId = imported.Id; + var importRef = _context.Attach(new RecruitImport { Id = importId }).Entity; using var c_package = new ExcelPackage(new FileInfo(job.ImportFile)); for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++) @@ -344,133 +350,145 @@ public class ImportBackgroundService : BackgroundService var cell1 = workSheet?.Cells[row, 1]?.GetValue(); if (cell1 == "" || cell1 == null) break; - var r = new Models.Recruits.Recruit(); - 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 = 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(workSheet?.Cells[row, 87]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue() ?? "", "dd/MM/yyyy") : null; - r.PositionType = ""; - r.PositionLevel = ""; - 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"; - - // Store child entities in separate lists for bulk insert - var education = new RecruitEducation() + try { - 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 = workSheet?.Cells[row, 21]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue() ?? "" : workSheet?.Cells[row, 21]?.GetValue() ?? "", - GPA = (double)workSheet?.Cells[row, 26]?.GetValue(), - Specialist = "", - 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, - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; + var r = new Models.Recruits.Recruit(); + r.Id = Guid.NewGuid(); + 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 = 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(workSheet?.Cells[row, 87]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue() ?? "", "dd/MM/yyyy") : null; + r.PositionType = ""; + r.PositionLevel = ""; + 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.RecruitImport = importRef; - var occupation = new RecruitOccupation() + // Store child entities in separate lists for bulk insert + var education = new RecruitEducation() + { + Id = Guid.NewGuid(), + 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 = workSheet?.Cells[row, 21]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue() ?? "" : workSheet?.Cells[row, 21]?.GetValue() ?? "", + GPA = (double)workSheet?.Cells[row, 26]?.GetValue(), + Specialist = "", + 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 ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + var occupation = new RecruitOccupation() + { + Id = Guid.NewGuid(), + 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 ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + var address = new RecruitAddress() + { + Id = Guid.NewGuid(), + 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 = $"{(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 ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + var payment = new RecruitPayment() + { + Id = Guid.NewGuid(), + 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 ?? "", + CreatedFullName = job.FullName ?? "System Administrator", + LastUpdatedAt = DateTime.Now, + LastUpdateUserId = job.UserId ?? "", + LastUpdateFullName = job.FullName ?? "System Administrator" + }; + + batchRecruits.Add(r); + batchEducations.Add(education); + batchOccupations.Add(occupation); + batchAddresses.Add(address); + batchPayments.Add(payment); + } + catch (Exception ex) { - 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() ?? "", - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; - - var address = new RecruitAddress() - { - 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 = $"{(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(), - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; - - var payment = new RecruitPayment() - { - 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() ?? "", - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator" - }; - - 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); + throw new Exception($"Row {row}: {ex.Message}", ex); + } row++; batchCount++; @@ -478,26 +496,19 @@ public class ImportBackgroundService : BackgroundService if (batchCount >= batchSize) { - // BulkInsert Recruits first (with SetOutputIdentity to get generated Ids) - await _context.BulkInsertAsync(batchRecruits, options => + try { - 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]; + await _context.BulkInsertAsync(batchRecruits); + await _context.BulkInsertAsync(batchEducations); + await _context.BulkInsertAsync(batchOccupations); + await _context.BulkInsertAsync(batchAddresses); + await _context.BulkInsertAsync(batchPayments); + } + catch (Exception ex) + { + var batchStartRow = row - batchCount + 1; + throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex); } - - // 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(); @@ -513,23 +524,19 @@ public class ImportBackgroundService : BackgroundService // Process remaining records if (batchRecruits.Count > 0) { - await _context.BulkInsertAsync(batchRecruits, options => + try { - 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(batchRecruits); + await _context.BulkInsertAsync(batchEducations); + await _context.BulkInsertAsync(batchOccupations); + await _context.BulkInsertAsync(batchAddresses); + await _context.BulkInsertAsync(batchPayments); + } + catch (Exception ex) + { + var batchStartRow = row - batchCount + 1; + throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex); } - - await _context.BulkInsertAsync(batchEducations); - await _context.BulkInsertAsync(batchOccupations); - await _context.BulkInsertAsync(batchAddresses); - await _context.BulkInsertAsync(batchPayments); } } diff --git a/Services/ImportJobTracker.cs b/Services/ImportJobTracker.cs index fcefd8b..ebd795b 100644 --- a/Services/ImportJobTracker.cs +++ b/Services/ImportJobTracker.cs @@ -36,6 +36,7 @@ public class ImportJobInfo public string ImportDocId { get; set; } = ""; public string? UserId { get; set; } public string? FullName { get; set; } + public string? Token { get; set; } // For CandidateFile public PostRecruitImportRequest? Request { get; set; } diff --git a/Services/NotificationService.cs b/Services/NotificationService.cs new file mode 100644 index 0000000..9f142e7 --- /dev/null +++ b/Services/NotificationService.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; + +namespace BMA.EHR.Recruit.Services; + +public class NotificationService +{ + private readonly IHttpClientFactory _httpClientFactory; + private readonly ILogger _logger; + private readonly IConfiguration _configuration; + + private string NotifyEndpoint = "https://hrmsbkk.case-collection.com/api/v1/org/through-socket/notify-from-token"; + + public NotificationService(IHttpClientFactory httpClientFactory, ILogger logger, IConfiguration configuration) + { + _httpClientFactory = httpClientFactory; + _logger = logger; + _configuration = configuration; + NotifyEndpoint = $"{_configuration["API"]}/org/through-socket/notify-from-token"; + } + + public async Task SendImportNotificationAsync(string? token, bool error, string message) + { + if (string.IsNullOrEmpty(token)) + { + _logger.LogWarning("Cannot send import notification: token is null or empty."); + return; + } + + try + { + var client = _httpClientFactory.CreateClient("default"); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); + + var payload = new + { + error, + message + }; + + var json = JsonConvert.SerializeObject(payload); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await client.PostAsync(NotifyEndpoint, content); + + if (!response.IsSuccessStatusCode) + { + var responseBody = await response.Content.ReadAsStringAsync(); + _logger.LogWarning("Import notification failed with status {StatusCode}: {Body}", response.StatusCode, responseBody); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to send import notification: {Message}", ex.Message); + } + } +} diff --git a/appsettings.json b/appsettings.json index 869e914..4585fbe 100644 --- a/appsettings.json +++ b/appsettings.json @@ -18,7 +18,7 @@ "MongoConnection": "mongodb://admin:adminVM123@127.0.0.1:27017", "DefaultConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", "OrgConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_organization;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", - "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None" + "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None;AllowLoadLocalInfile=true;" }, "Jwt": { "Key": "j7C9RO_p4nRtuwCH4z9Db_A_6We42tkD_p4lZtDrezc", diff --git a/bin/Debug/net7.0/appsettings.json b/bin/Debug/net7.0/appsettings.json index 869e914..4585fbe 100644 --- a/bin/Debug/net7.0/appsettings.json +++ b/bin/Debug/net7.0/appsettings.json @@ -18,7 +18,7 @@ "MongoConnection": "mongodb://admin:adminVM123@127.0.0.1:27017", "DefaultConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", "OrgConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_organization;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None", - "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None" + "RecruitConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;database=hrms_recruit;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;SslMode=None;AllowLoadLocalInfile=true;" }, "Jwt": { "Key": "j7C9RO_p4nRtuwCH4z9Db_A_6We42tkD_p4lZtDrezc", From 2b000cbd69497357e2565eeb6bc324f704595f19 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Fri, 15 May 2026 16:37:14 +0700 Subject: [PATCH 03/18] fix OOM Error --- Controllers/RecruitController.cs | 16 ++-- Dockerfile | 5 ++ Services/ImportBackgroundService.cs | 113 +++++++++++++++------------- Services/ImportJobTracker.cs | 19 +++++ Services/MinIOService.cs | 5 +- 5 files changed, 95 insertions(+), 63 deletions(-) diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs index 8c1fbdd..a673934 100644 --- a/Controllers/RecruitController.cs +++ b/Controllers/RecruitController.cs @@ -717,9 +717,11 @@ namespace BMA.EHR.Recruit.Controllers var doc = await _minioService.UploadFileAsync(file); var import_doc_id = doc.Id.ToString("D"); - var fileContent = (await _minioService.DownloadFileAsync(doc.Id)).FileContent; - System.IO.File.WriteAllBytes(importFile, fileContent); - fileContent = null; + // Write file to disk directly from IFormFile instead of downloading back from MinIO + using (var stream = new FileStream(importFile, FileMode.Create)) + { + await file.CopyToAsync(stream); + } // สร้างรอบการบรรจุ var imported = new RecruitImport @@ -931,9 +933,11 @@ namespace BMA.EHR.Recruit.Controllers var doc = await _minioService.UploadFileAsync(file); var import_doc_id = doc.Id.ToString("D"); - var fileContent = (await _minioService.DownloadFileAsync(doc.Id)).FileContent; - System.IO.File.WriteAllBytes(importFile, fileContent); - fileContent = null; + // Write file to disk directly from IFormFile instead of downloading back from MinIO + using (var stream = new FileStream(importFile, FileMode.Create)) + { + await file.CopyToAsync(stream); + } // Enqueue background job var job = _importJobTracker.CreateJob(new ImportJobInfo diff --git a/Dockerfile b/Dockerfile index ea318e6..0ee996f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,4 +20,9 @@ RUN dotnet publish "BMA.EHR.Recruit.csproj" -c Release -o /app/publish /p:UseApp FROM base AS final WORKDIR /app COPY --from=publish /app/publish . + +# GC configuration for better memory management in containers +#ENV DOTNET_GCHeapHardLimit=1073741824 +#ENV DOTNET_GCConserveMemory=9 + ENTRYPOINT ["dotnet", "BMA.EHR.Recruit.dll"] \ No newline at end of file diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index f041390..cf33fd5 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -41,64 +41,61 @@ public class ImportBackgroundService : BackgroundService { var job = await _queue.DequeueAsync(stoppingToken); - _ = Task.Run(async () => - { - using var scope = _scopeFactory.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - var minioService = scope.ServiceProvider.GetRequiredService(); - var recruitService = scope.ServiceProvider.GetRequiredService(); - var notificationService = scope.ServiceProvider.GetRequiredService(); - var webHostEnv = scope.ServiceProvider.GetRequiredService(); - var logger = scope.ServiceProvider.GetRequiredService>(); + using var scope = _scopeFactory.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + var minioService = scope.ServiceProvider.GetRequiredService(); + var recruitService = scope.ServiceProvider.GetRequiredService(); + var notificationService = scope.ServiceProvider.GetRequiredService(); + var webHostEnv = scope.ServiceProvider.GetRequiredService(); + var logger = scope.ServiceProvider.GetRequiredService>(); + try + { + _tracker.UpdateStatus(job.JobId, ImportJobStatus.Running); + + switch (job.JobType) + { + case ImportJobType.CandidateFile: + await ProcessCandidateFileAsync(context, minioService, webHostEnv, job); + break; + case ImportJobType.CandidateFileById: + await ProcessCandidateFileByIdAsync(context, minioService, recruitService, webHostEnv, job); + break; + case ImportJobType.ScoreFile: + await ProcessScoreFileAsync(context, minioService, recruitService, job); + break; + case ImportJobType.ResultFile: + await ProcessResultFileAsync(context, recruitService, job); + break; + } + + _tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount); + + await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ"); + } + catch (Exception ex) + { + logger.LogError(ex, "Import job {JobId} failed: {Message}", job.JobId, ex.Message); + _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message); + + try { await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message); } catch { } + + // cleanup minio file on failure + if (!string.IsNullOrEmpty(job.ImportDocId)) + { + try { await minioService.DeleteFileAsync(Guid.Parse(job.ImportDocId)); } catch { } + } + } + finally + { + // cleanup temp file try { - _tracker.UpdateStatus(job.JobId, ImportJobStatus.Running); - - switch (job.JobType) - { - case ImportJobType.CandidateFile: - await ProcessCandidateFileAsync(context, minioService, webHostEnv, job); - break; - case ImportJobType.CandidateFileById: - await ProcessCandidateFileByIdAsync(context, minioService, recruitService, webHostEnv, job); - break; - case ImportJobType.ScoreFile: - await ProcessScoreFileAsync(context, minioService, recruitService, job); - break; - case ImportJobType.ResultFile: - await ProcessResultFileAsync(context, recruitService, job); - break; - } - - _tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount); - - await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ"); + if (System.IO.File.Exists(job.ImportFile)) + System.IO.File.Delete(job.ImportFile); } - catch (Exception ex) - { - logger.LogError(ex, "Import job {JobId} failed: {Message}", job.JobId, ex.Message); - _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message); - - await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message); - - // cleanup minio file on failure - if (!string.IsNullOrEmpty(job.ImportDocId)) - { - try { await minioService.DeleteFileAsync(Guid.Parse(job.ImportDocId)); } catch { } - } - } - finally - { - // cleanup temp file - try - { - if (System.IO.File.Exists(job.ImportFile)) - System.IO.File.Delete(job.ImportFile); - } - catch { } - } - }, stoppingToken); + catch { } + } } } @@ -266,6 +263,8 @@ public class ImportBackgroundService : BackgroundService await _context.BulkInsertAsync(batchCertificates); await _context.BulkInsertAsync(batchEducations); + _context.ChangeTracker.Clear(); + batchRecruits.Clear(); batchAddresses.Clear(); batchPayments.Clear(); @@ -296,6 +295,8 @@ public class ImportBackgroundService : BackgroundService await _context.BulkInsertAsync(batchOccupations); await _context.BulkInsertAsync(batchCertificates); await _context.BulkInsertAsync(batchEducations); + + _context.ChangeTracker.Clear(); } } @@ -510,6 +511,8 @@ public class ImportBackgroundService : BackgroundService throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex); } + _context.ChangeTracker.Clear(); + // Clear all lists for next batch batchRecruits.Clear(); batchEducations.Clear(); @@ -537,6 +540,8 @@ public class ImportBackgroundService : BackgroundService var batchStartRow = row - batchCount + 1; throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex); } + + _context.ChangeTracker.Clear(); } } diff --git a/Services/ImportJobTracker.cs b/Services/ImportJobTracker.cs index ebd795b..d58a37c 100644 --- a/Services/ImportJobTracker.cs +++ b/Services/ImportJobTracker.cs @@ -45,9 +45,11 @@ public class ImportJobInfo public class ImportJobTracker { private readonly ConcurrentDictionary _jobs = new(); + private readonly TimeSpan _evictionAge = TimeSpan.FromHours(1); public ImportJobInfo CreateJob(ImportJobInfo job) { + EvictOldJobs(); _jobs[job.JobId] = job; return job; } @@ -66,7 +68,24 @@ public class ImportJobTracker if (errorMessage != null) job.ErrorMessage = errorMessage; if (status == ImportJobStatus.Completed || status == ImportJobStatus.Failed) + { job.CompletedAt = DateTime.Now; + // Clear request data to free memory for completed/failed jobs + job.Request = null; + job.Token = null; + } + } + } + + private void EvictOldJobs() + { + var cutoff = DateTime.Now - _evictionAge; + foreach (var kvp in _jobs) + { + if (kvp.Value.CompletedAt.HasValue && kvp.Value.CompletedAt.Value < cutoff) + { + _jobs.TryRemove(kvp.Key, out _); + } } } } diff --git a/Services/MinIOService.cs b/Services/MinIOService.cs index a56eba2..cf231b2 100644 --- a/Services/MinIOService.cs +++ b/Services/MinIOService.cs @@ -72,14 +72,13 @@ namespace BMA.EHR.Recruit.Services { var id = Guid.NewGuid(); file.CopyTo(ms); - var fileBytes = ms.ToArray(); - System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileBytes); + ms.Position = 0; // Reset stream position for reading var request = new PutObjectRequest { BucketName = _bucketName, Key = id.ToString("D"), - InputStream = filestream, + InputStream = ms, ContentType = file.ContentType, CannedACL = S3CannedACL.PublicRead }; From c77035dbb21afebba0b653e3713a5a14d562a8c9 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Fri, 15 May 2026 21:53:40 +0700 Subject: [PATCH 04/18] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=9B?= =?UTF-8?q?=E0=B8=B1=E0=B8=8D=E0=B8=AB=E0=B8=B2=20token=20=E0=B8=96?= =?UTF-8?q?=E0=B8=B9=E0=B8=81=E0=B8=A5=E0=B9=89=E0=B8=B2=E0=B8=87=E0=B9=84?= =?UTF-8?q?=E0=B8=9B=E0=B8=81=E0=B9=88=E0=B8=AD=E0=B8=99=E0=B8=97=E0=B8=B5?= =?UTF-8?q?=E0=B9=88=E0=B8=88=E0=B8=B0=E0=B8=AA=E0=B9=88=E0=B8=87=20notifi?= =?UTF-8?q?cation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/ImportBackgroundService.cs | 2 ++ Services/ImportJobTracker.cs | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index cf33fd5..0d3de83 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -72,6 +72,7 @@ public class ImportBackgroundService : BackgroundService _tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount); await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ"); + job.Token = null; // Clear token after notification sent } catch (Exception ex) { @@ -79,6 +80,7 @@ public class ImportBackgroundService : BackgroundService _tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message); try { await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message); } catch { } + job.Token = null; // Clear token after notification sent // cleanup minio file on failure if (!string.IsNullOrEmpty(job.ImportDocId)) diff --git a/Services/ImportJobTracker.cs b/Services/ImportJobTracker.cs index d58a37c..eed3188 100644 --- a/Services/ImportJobTracker.cs +++ b/Services/ImportJobTracker.cs @@ -72,7 +72,6 @@ public class ImportJobTracker job.CompletedAt = DateTime.Now; // Clear request data to free memory for completed/failed jobs job.Request = null; - job.Token = null; } } } From 11d308ab51b50578b2a92f421ea00441050acf90 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Sat, 16 May 2026 09:51:26 +0700 Subject: [PATCH 05/18] remove file --- BMA.EHR.Recruit.Service.csproj.user | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 BMA.EHR.Recruit.Service.csproj.user diff --git a/BMA.EHR.Recruit.Service.csproj.user b/BMA.EHR.Recruit.Service.csproj.user deleted file mode 100644 index 96e63d6..0000000 --- a/BMA.EHR.Recruit.Service.csproj.user +++ /dev/null @@ -1,9 +0,0 @@ - - - - https - - - ProjectDebugger - - \ No newline at end of file From cf98121993aacc42f5794f597417e671d91e36a3 Mon Sep 17 00:00:00 2001 From: harid Date: Mon, 18 May 2026 13:57:48 +0700 Subject: [PATCH 06/18] =?UTF-8?q?fix=20=E0=B8=A3=E0=B8=B0=E0=B8=9A?= =?UTF-8?q?=E0=B8=9A=E0=B9=81=E0=B8=88=E0=B9=89=E0=B8=87=20Error=20#2497?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/ImportBackgroundService.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 0d3de83..6143171 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -598,8 +598,12 @@ public class ImportBackgroundService : BackgroundService // Save ScoreImport parent first to get its Id rec_import.ScoreImport = imported; await _context.SaveChangesAsync(); + var scoreImportId = imported.Id; _context.ChangeTracker.Clear(); + // 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 .Where(x => x.RecruitImport.Id == rec_import.Id) @@ -669,7 +673,7 @@ public class ImportBackgroundService : BackgroundService r.LastUpdatedAt = DateTime.Now; r.LastUpdateUserId = job.UserId ?? ""; r.LastUpdateFullName = job.FullName ?? "System Administrator"; - r.ScoreImport = imported; + r.ScoreImport = importRef; batchScores.Add(r); } From e33448508e5882380b4482d4fbe0eac66ce50a0a Mon Sep 17 00:00:00 2001 From: harid Date: Mon, 18 May 2026 14:45:27 +0700 Subject: [PATCH 07/18] =?UTF-8?q?fix=20=E0=B8=A3=E0=B8=B0=E0=B8=9A?= =?UTF-8?q?=E0=B8=9A=E0=B9=81=E0=B8=88=E0=B9=89=E0=B8=87=20Error=20#2497?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/ImportBackgroundService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 6143171..2a0d7f2 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -635,6 +635,7 @@ public class ImportBackgroundService : BackgroundService if (!string.IsNullOrEmpty(r.ExamId) && recruitsDict.TryGetValue(r.ExamId, out var recruit)) { + 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(workSheet?.Cells[row, 5]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 5].GetValue(), 2); From 4ea7c0010b0e65a44d8dd4477767fc354d3a0c9b Mon Sep 17 00:00:00 2001 From: harid Date: Mon, 18 May 2026 17:48:51 +0700 Subject: [PATCH 08/18] =?UTF-8?q?fix=20=E0=B8=A3=E0=B8=B0=E0=B8=9A?= =?UTF-8?q?=E0=B8=9A=E0=B9=81=E0=B8=88=E0=B9=89=E0=B8=87=20Error=20#2497?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/ImportBackgroundService.cs | 50 +++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 2a0d7f2..7b56e9c 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -566,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 = "นำเข้าข้อมูลผลคะแนนสอบ", @@ -580,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", @@ -595,6 +636,9 @@ 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(); From eea7fbcfa181e0bb427ea27b9e29734c152105ba Mon Sep 17 00:00:00 2001 From: harid Date: Tue, 19 May 2026 13:40:50 +0700 Subject: [PATCH 09/18] =?UTF-8?q?fix=20Update=20FK=20=E0=B8=95=E0=B8=B4?= =?UTF-8?q?=E0=B8=94=20Error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/ImportBackgroundService.cs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 7b56e9c..ddc4036 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -790,15 +790,28 @@ public class ImportBackgroundService : BackgroundService await _context.SaveChangesAsync(); _context.ChangeTracker.Clear(); - // preload scores - re-query from DB to avoid tracking issues + // หา 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 - .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); + // ถ้าไม่มีผลคะแนนสอบคัดเลือกผู้พิการให้จบการทำงาน + 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 c_package = new ExcelPackage(stream); @@ -809,7 +822,6 @@ public class ImportBackgroundService : BackgroundService int batchCount = 0; const int batchSize = 500; var endRow = workSheet.Dimension.End.Row; - var batchUpdates = new List(); while (row <= endRow) { @@ -827,7 +839,6 @@ public class ImportBackgroundService : BackgroundService existingScore.LastUpdatedAt = DateTime.Now; existingScore.LastUpdateUserId = job.UserId ?? ""; existingScore.LastUpdateFullName = job.FullName ?? "System Administrator"; - batchUpdates.Add(existingScore); batchCount++; } @@ -835,16 +846,17 @@ public class ImportBackgroundService : BackgroundService 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(); } } } From 902faeb7f46e6d13a58ec391228886c638a204af Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 16:34:03 +0700 Subject: [PATCH 10/18] fix OOM --- BMA.EHR.Recruit.csproj | 2 + Controllers/RecruitController.cs | 3 + Services/ImportBackgroundService.cs | 477 +++++++++++++++++----------- obj/project.assets.json | 89 ++++++ obj/project.nuget.cache | 4 +- 5 files changed, 382 insertions(+), 193 deletions(-) diff --git a/BMA.EHR.Recruit.csproj b/BMA.EHR.Recruit.csproj index bba397f..c148fa1 100644 --- a/BMA.EHR.Recruit.csproj +++ b/BMA.EHR.Recruit.csproj @@ -24,6 +24,8 @@ + + diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs index a673934..7d246ff 100644 --- a/Controllers/RecruitController.cs +++ b/Controllers/RecruitController.cs @@ -377,6 +377,7 @@ 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) @@ -2009,6 +2010,7 @@ 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); @@ -2517,6 +2519,7 @@ 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 0d3de83..0f827fb 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 EFCore.BulkExtensions; using BMA.EHR.Recruit.Extensions; +using EFCore.BulkExtensions; using BMA.EHR.Recruit.Models.Recruits; using BMA.EHR.Recruit.Requests.Recruits; +using ExcelDataReader; namespace BMA.EHR.Recruit.Services; @@ -108,14 +108,18 @@ public class ImportBackgroundService : BackgroundService var imported = await _context.RecruitImports.FindAsync(job.RecruitImportId); if (imported == null) throw new Exception("RecruitImport not found"); - 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(); + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + using var stream = System.IO.File.OpenRead(job.ImportFile); + using var reader = ExcelReaderFactory.CreateReader(stream); + + do + { + // 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() ?? ""; - int row = 2; int batchCount = 0; const int batchSize = 500; int totalProcessed = 0; @@ -127,105 +131,105 @@ public class ImportBackgroundService : BackgroundService var batchPayments = new List(); var batchCertificates = new List(); - while (row <= totalRows) + while (reader.Read()) { - var cell1 = workSheet?.Cells[row, 1]?.GetValue(); - if (cell1 == "" || cell1 == null) break; + var cell1 = reader.GetValue(0)?.ToString(); + if (string.IsNullOrEmpty(cell1)) break; var r = new Models.Recruits.Recruit(); - 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.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.Isspecial = "N"; - 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(""); + 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(""); // address var address = new RecruitAddress() { - 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() ?? "", + 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), }; // payment var payment = new RecruitPayment() { - 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() ?? "" + 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) }; // occupation var occupation = new RecruitOccupation() { - 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() ?? "", + 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), }; // certificate var certificate = new RecruitCertificate() { - 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, "-")) + 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, "-")) }; var education = new RecruitEducation() { - 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, "-")) + 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, "-")) }; r.Addresses.Add(address); @@ -242,7 +246,6 @@ public class ImportBackgroundService : BackgroundService batchCertificates.Add(certificate); batchEducations.Add(education); - row++; batchCount++; totalProcessed++; @@ -300,7 +303,7 @@ public class ImportBackgroundService : BackgroundService _context.ChangeTracker.Clear(); } - } + } while (reader.NextResult()); job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0; } @@ -331,11 +334,14 @@ public class ImportBackgroundService : BackgroundService var importId = imported.Id; var importRef = _context.Attach(new RecruitImport { Id = importId }).Entity; - using var c_package = new ExcelPackage(new FileInfo(job.ImportFile)); - for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++) + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + using var stream = System.IO.File.OpenRead(job.ImportFile); + using var reader = ExcelReaderFactory.CreateReader(stream); + + do { - var workSheet = c_package.Workbook.Worksheets[i]; - var totalRows = workSheet.Dimension.Rows; + // Skip header row + if (!reader.Read()) continue; int row = 2; int batchCount = 0; @@ -348,33 +354,33 @@ public class ImportBackgroundService : BackgroundService var batchAddresses = new List(); var batchPayments = new List(); - while (row <= totalRows) + while (reader.Read()) { - var cell1 = workSheet?.Cells[row, 1]?.GetValue(); - if (cell1 == "" || cell1 == null) break; + var cell1 = reader.GetValue(0)?.ToString(); + if (string.IsNullOrEmpty(cell1)) break; try { var r = new Models.Recruits.Recruit(); r.Id = Guid.NewGuid(); - 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.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.Race = ""; - 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.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.Marry = ""; r.Isspecial = "N"; r.CitizenCardExpireDate = null; r.ModifiedDate = null; - r.ApplyDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 87]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue() ?? "", "dd/MM/yyyy") : null; + r.ApplyDate = !string.IsNullOrWhiteSpace(reader.GetValue(86)?.ToString()) ? _recruitService.CheckDateTime(reader.GetValue(86)?.ToString() ?? "", "dd/MM/yyyy") : null; r.PositionType = ""; r.PositionLevel = ""; r.CreatedAt = DateTime.Now; @@ -389,15 +395,15 @@ public class ImportBackgroundService : BackgroundService var education = new RecruitEducation() { Id = Guid.NewGuid(), - Degree = workSheet?.Cells[row, 18]?.GetValue() ?? "", - Major = workSheet?.Cells[row, 19]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 20]?.GetValue() ?? "" : workSheet?.Cells[row, 19]?.GetValue() ?? "", + Degree = reader.GetValue(17)?.ToString() ?? "", + Major = reader.GetValue(18)?.ToString() == "อื่น ๆ" ? reader.GetValue(19)?.ToString() ?? "" : reader.GetValue(18)?.ToString() ?? "", MajorGroupId = "", MajorGroupName = "", - University = workSheet?.Cells[row, 21]?.GetValue() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue() ?? "" : workSheet?.Cells[row, 21]?.GetValue() ?? "", - GPA = (double)workSheet?.Cells[row, 26]?.GetValue(), + University = reader.GetValue(20)?.ToString() == "อื่น ๆ" ? reader.GetValue(21)?.ToString() ?? "" : reader.GetValue(20)?.ToString() ?? "", + GPA = GetReaderDouble(reader, 25), Specialist = "", - 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, + HighDegree = reader.GetValue(26)?.ToString() ?? "", + BachelorDate = !string.IsNullOrWhiteSpace(reader.GetValue(24)?.ToString()) ? _recruitService.CheckDateTime(reader.GetValue(24)?.ToString() ?? "", "dd/MM/yyyy") : null, Recruit = r, CreatedAt = DateTime.Now, CreatedUserId = job.UserId ?? "", @@ -410,11 +416,11 @@ public class ImportBackgroundService : BackgroundService var occupation = new RecruitOccupation() { Id = Guid.NewGuid(), - 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() ?? "", + 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 = "", Recruit = r, CreatedAt = DateTime.Now, CreatedUserId = job.UserId ?? "", @@ -427,24 +433,24 @@ public class ImportBackgroundService : BackgroundService var address = new RecruitAddress() { Id = Guid.NewGuid(), - 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() ?? "", + 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() ?? "", Mobile = "", - 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(), + 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(), Recruit = r, CreatedAt = DateTime.Now, CreatedUserId = job.UserId ?? "", @@ -457,22 +463,22 @@ public class ImportBackgroundService : BackgroundService var payment = new RecruitPayment() { Id = Guid.NewGuid(), - 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() ?? "", + 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() ?? "", Recruit = r, CreatedAt = DateTime.Now, CreatedUserId = job.UserId ?? "", @@ -545,7 +551,7 @@ public class ImportBackgroundService : BackgroundService _context.ChangeTracker.Clear(); } - } + } while (reader.NextResult()); job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0; } @@ -600,69 +606,80 @@ public class ImportBackgroundService : BackgroundService await _context.SaveChangesAsync(); _context.ChangeTracker.Clear(); - // preload recruits (lightweight - only ExamId) + // preload recruits using AsNoTracking to avoid EF tracking overhead 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); - using var c_package = new ExcelPackage(new FileInfo(job.ImportFile)); - for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++) + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + using var stream = System.IO.File.OpenRead(job.ImportFile); + using var reader = ExcelReaderFactory.CreateReader(stream); + + do { - var workSheet = c_package.Workbook.Worksheets[i]; - var cols = workSheet.GetHeaderColumns(); - int row = 8; + // 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 + int batchCount = 0; const int batchSize = 500; int totalProcessed = 0; - var endRow = workSheet.Dimension.End.Row; var batchScores = new List(); - while (row <= endRow) + while (reader.Read()) { - var cell1 = workSheet?.Cells[row, 1]?.GetValue(); - if (cell1 == "" || cell1 == null) break; + var cell1 = reader.GetValue(0)?.ToString(); + if (string.IsNullOrEmpty(cell1)) break; var r = new RecruitScore(); - r.ExamId = workSheet?.Cells[row, 2]?.GetValue(); + r.ExamId = reader.GetValue(1)?.ToString(); if (!string.IsNullOrEmpty(r.ExamId) && recruitsDict.TryGetValue(r.ExamId, out var recruit)) { - r.CitizenId = workSheet?.Cells[row, 3]?.GetValue()?.Trim(); + r.CitizenId = reader.GetValue(2)?.ToString()?.Trim(); r.FullA = 200; - 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.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.FullC = 50; - r.SumC = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 8]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 8].GetValue(), 2); + r.SumC = string.IsNullOrWhiteSpace(reader.GetValue(7)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(7)), 2); r.FullD = 50; - 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.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.FullScore = 300; - r.TotalScore = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 13]?.GetValue()) ? 0.00 : Math.Round(workSheet.Cells[row, 13].GetValue(), 2); + r.TotalScore = string.IsNullOrWhiteSpace(reader.GetValue(12)?.ToString()) ? 0.00 : Math.Round(Convert.ToDouble(reader.GetValue(12)), 2); - var examStatusCol7 = workSheet?.Cells[row, 7]?.GetValue()?.Trim(); - var examStatusCol14 = workSheet?.Cells[row, 14]?.GetValue()?.Trim(); + var examStatusCol7 = reader.GetValue(6)?.ToString()?.Trim(); + var examStatusCol14 = reader.GetValue(13)?.ToString()?.Trim(); r.ExamStatus = examStatusCol7 == "ขาดสอบ" ? "ขส." : examStatusCol14 == "ได้" ? "ผ่าน" : examStatusCol14 == "ตก" ? "ไม่ผ่าน" : "-"; - r.RemarkScore = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 15]?.GetValue()) ? string.Empty : workSheet?.Cells[row, 15]?.GetValue(); + r.RemarkScore = string.IsNullOrWhiteSpace(reader.GetValue(14)?.ToString()) ? string.Empty : reader.GetValue(14)?.ToString(); - var examAttr = workSheet?.Cells[row, 16]?.GetValue()?.Trim(); + var examAttr = reader.GetValue(15)?.ToString()?.Trim(); r.ExamAttribute = examAttr == "ผ่าน" ? "มีคุณสมบัติ" : examAttr == "ไม่ผ่าน" ? "ไม่มีคุณสมบัติ" : ""; - r.Major = workSheet.Name; + r.Major = reader.Name; // worksheet name r.CreatedAt = DateTime.Now; r.CreatedUserId = job.UserId ?? ""; r.CreatedFullName = job.FullName ?? "System Administrator"; @@ -674,7 +691,6 @@ public class ImportBackgroundService : BackgroundService batchScores.Add(r); } - row++; batchCount++; totalProcessed++; @@ -692,7 +708,7 @@ public class ImportBackgroundService : BackgroundService { await _context.BulkInsertAsync(batchScores); } - } + } while (reader.NextResult()); job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0; } @@ -741,8 +757,9 @@ public class ImportBackgroundService : BackgroundService await _context.SaveChangesAsync(); _context.ChangeTracker.Clear(); - // preload scores - re-query from DB to avoid tracking issues + // preload scores using AsNoTracking to avoid EF tracking overhead var scoreList = await _context.RecruitScores + .AsNoTracking() .Where(s => s.ScoreImport.RecruitImportId == rec_import.Id && !string.IsNullOrEmpty(s.ExamId)) .GroupBy(x => x.ExamId) .Where(g => g.Count() == 1) @@ -750,31 +767,34 @@ public class ImportBackgroundService : BackgroundService .ToListAsync(); var score = scoreList.ToDictionary(s => s.ExamId!, s => s); - // Read from saved file (ResultFile uses stream from Form, but we saved to disk) + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); using var stream = System.IO.File.OpenRead(job.ImportFile); - using var c_package = new ExcelPackage(stream); + using var reader = ExcelReaderFactory.CreateReader(stream); - foreach (var workSheet in c_package.Workbook.Worksheets) + do { - int row = 7; + // Skip 6 header rows, data starts at row 7 + for (int skip = 0; skip < 6; skip++) + { + if (!reader.Read()) break; + } + int batchCount = 0; const int batchSize = 500; - var endRow = workSheet.Dimension.End.Row; var batchUpdates = new List(); - while (row <= endRow) + while (reader.Read()) { - var examId = workSheet?.Cells[row, 2]?.GetValue(); + var examId = reader.GetValue(1)?.ToString(); if (string.IsNullOrWhiteSpace(examId)) { - row++; continue; } if (score.TryGetValue(examId, out var existingScore)) { - existingScore.Number = workSheet?.Cells[row, 1]?.GetValue(); - existingScore.RemarkExamOrder = workSheet?.Cells[row, 4]?.GetValue() ?? string.Empty; + existingScore.Number = reader.GetValue(0)?.ToString(); + existingScore.RemarkExamOrder = reader.GetValue(3)?.ToString() ?? string.Empty; existingScore.LastUpdatedAt = DateTime.Now; existingScore.LastUpdateUserId = job.UserId ?? ""; existingScore.LastUpdateFullName = job.FullName ?? "System Administrator"; @@ -782,8 +802,6 @@ public class ImportBackgroundService : BackgroundService batchCount++; } - row++; - if (batchCount >= batchSize) { await _context.BulkUpdateAsync(batchUpdates); @@ -797,7 +815,7 @@ public class ImportBackgroundService : BackgroundService { await _context.BulkUpdateAsync(batchUpdates); } - } + } while (reader.NextResult()); } #endregion @@ -819,6 +837,81 @@ 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 818122a..82551d3 100644 --- a/obj/project.assets.json +++ b/obj/project.assets.json @@ -289,6 +289,35 @@ "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": { @@ -5670,6 +5699,56 @@ "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", @@ -13996,6 +14075,8 @@ "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", @@ -14089,6 +14170,14 @@ "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 e443564..4529909 100644 --- a/obj/project.nuget.cache +++ b/obj/project.nuget.cache @@ -1,6 +1,6 @@ { "version": 2, - "dgSpecHash": "XR3lYvVwNcQ=", + "dgSpecHash": "++tptrWNc3M=", "success": true, "projectFilePath": "/Users/suphonchaip/Develop/hrms/hrms-api-recruit/BMA.EHR.Recruit.csproj", "expectedPackageFiles": [ @@ -21,6 +21,8 @@ "/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", From e0adbb0b711dbcc260446db03c4df4d362854188 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 16:47:12 +0700 Subject: [PATCH 11/18] fix Score Import --- Services/ImportBackgroundService.cs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 0f827fb..350c5cd 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -570,12 +570,23 @@ public class ImportBackgroundService : BackgroundService if (rec_import == null) throw new Exception("RecruitImport not found"); - if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null) + var rec_import_id = rec_import.Id; + var rec_import_year = rec_import.Year; + var hasExistingScores = rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null; + + if (hasExistingScores) { - await _context.BulkDeleteAsync(rec_import.ScoreImport.Scores.ToList()); + var existingScores = rec_import.ScoreImport.Scores.ToList(); + await _context.BulkDeleteAsync(existingScores); } - rec_import.ImportHostories.Add(new RecruitImportHistory + // Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking) + _context.ChangeTracker.Clear(); + + // Add history record using Attach stub for navigation (no explicit FK property on model) + var importStub = new RecruitImport { Id = rec_import_id }; + _context.Attach(importStub); + _context.RecruitImportHistories.Add(new RecruitImportHistory { Description = "นำเข้าข้อมูลผลคะแนนสอบ", CreatedAt = DateTime.Now, @@ -584,13 +595,15 @@ public class ImportBackgroundService : BackgroundService LastUpdatedAt = DateTime.Now, LastUpdateUserId = job.UserId ?? "", LastUpdateFullName = job.FullName ?? "System Administrator", + RecruitImport = importStub, }); // get doc from minio var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile)); var imported = new ScoreImport { - Year = rec_import.Year, + Year = rec_import_year, + RecruitImportId = rec_import_id, ImportFile = doc, CreatedAt = DateTime.Now, CreatedUserId = job.UserId ?? "", @@ -602,7 +615,7 @@ public class ImportBackgroundService : BackgroundService }; // Save ScoreImport parent first to get its Id - rec_import.ScoreImport = imported; + _context.ScoreImports.Add(imported); await _context.SaveChangesAsync(); _context.ChangeTracker.Clear(); From e8681834f256eee6c72e63284da6313fc7c31d43 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 16:53:27 +0700 Subject: [PATCH 12/18] fix again --- Services/ImportBackgroundService.cs | 32 +++++++++++++---------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 350c5cd..acf058f 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -583,10 +583,8 @@ public class ImportBackgroundService : BackgroundService // Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking) _context.ChangeTracker.Clear(); - // Add history record using Attach stub for navigation (no explicit FK property on model) - var importStub = new RecruitImport { Id = rec_import_id }; - _context.Attach(importStub); - _context.RecruitImportHistories.Add(new RecruitImportHistory + // Add history record — set FK shadow property directly to avoid Attach side-effects + var historyEntry = _context.RecruitImportHistories.Add(new RecruitImportHistory { Description = "นำเข้าข้อมูลผลคะแนนสอบ", CreatedAt = DateTime.Now, @@ -595,24 +593,22 @@ public class ImportBackgroundService : BackgroundService LastUpdatedAt = DateTime.Now, LastUpdateUserId = job.UserId ?? "", LastUpdateFullName = job.FullName ?? "System Administrator", - RecruitImport = importStub, }); + historyEntry.Property("RecruitImportId").CurrentValue = rec_import_id; // get doc from minio var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile)); - var imported = new ScoreImport - { - Year = rec_import_year, - RecruitImportId = rec_import_id, - ImportFile = doc, - CreatedAt = DateTime.Now, - CreatedUserId = job.UserId ?? "", - CreatedFullName = job.FullName ?? "System Administrator", - LastUpdatedAt = DateTime.Now, - LastUpdateUserId = job.UserId ?? "", - LastUpdateFullName = job.FullName ?? "System Administrator", - Scores = new List() - }; + var imported = new ScoreImport(); + imported.Year = rec_import_year; + imported.RecruitImportId = rec_import_id; + imported.ImportFile = doc; + imported.CreatedAt = DateTime.Now; + imported.CreatedUserId = job.UserId ?? ""; + imported.CreatedFullName = job.FullName ?? "System Administrator"; + imported.LastUpdatedAt = DateTime.Now; + imported.LastUpdateUserId = job.UserId ?? ""; + imported.LastUpdateFullName = job.FullName ?? "System Administrator"; + imported.Scores = new List(); // Save ScoreImport parent first to get its Id _context.ScoreImports.Add(imported); From 13a8e309e76666fe91df5f6f6a240df999adbc0e Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 16:57:33 +0700 Subject: [PATCH 13/18] fix minio file --- Services/ImportBackgroundService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index acf058f..3c54528 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -596,12 +596,11 @@ public class ImportBackgroundService : BackgroundService }); historyEntry.Property("RecruitImportId").CurrentValue = rec_import_id; - // get doc from minio + // get doc from minio (MinIOService already saves Document to its own context) var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile)); var imported = new ScoreImport(); imported.Year = rec_import_year; imported.RecruitImportId = rec_import_id; - imported.ImportFile = doc; imported.CreatedAt = DateTime.Now; imported.CreatedUserId = job.UserId ?? ""; imported.CreatedFullName = job.FullName ?? "System Administrator"; @@ -610,8 +609,9 @@ public class ImportBackgroundService : BackgroundService imported.LastUpdateFullName = job.FullName ?? "System Administrator"; imported.Scores = new List(); - // Save ScoreImport parent first to get its Id - _context.ScoreImports.Add(imported); + // Save ScoreImport — use shadow FK for ImportFile to avoid re-inserting Document + var scoreEntry = _context.ScoreImports.Add(imported); + scoreEntry.Property("ImportFileId").CurrentValue = doc.Id; await _context.SaveChangesAsync(); _context.ChangeTracker.Clear(); From cba16f25b9e20ed0d540541a27c999e8df01b094 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 17:02:53 +0700 Subject: [PATCH 14/18] fix scoreimport --- Services/ImportBackgroundService.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 3c54528..4d4d4bc 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -572,12 +572,16 @@ public class ImportBackgroundService : BackgroundService var rec_import_id = rec_import.Id; var rec_import_year = rec_import.Year; - var hasExistingScores = rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null; + var existingScoreImport = rec_import.ScoreImport; - if (hasExistingScores) + if (existingScoreImport != null) { - var existingScores = rec_import.ScoreImport.Scores.ToList(); - await _context.BulkDeleteAsync(existingScores); + var existingScores = existingScoreImport.Scores?.ToList() ?? new List(); + if (existingScores.Count > 0) + await _context.BulkDeleteAsync(existingScores); + // Also delete the ScoreImport row itself (has unique index on RecruitImportId) + _context.ChangeTracker.Clear(); + await _context.BulkDeleteAsync(new List { existingScoreImport }); } // Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking) From 9c353f40c6c0080e9f70fd7a8a74fd1ec02835fb Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 17:07:27 +0700 Subject: [PATCH 15/18] fix fk --- Services/ImportBackgroundService.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 4d4d4bc..6841bc5 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -602,7 +602,9 @@ public class ImportBackgroundService : BackgroundService // get doc from minio (MinIOService already saves Document to its own context) var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile)); + var scoreImport_id = Guid.NewGuid(); // Pre-generate Id to use as FK var imported = new ScoreImport(); + imported.Id = scoreImport_id; imported.Year = rec_import_year; imported.RecruitImportId = rec_import_id; imported.CreatedAt = DateTime.Now; @@ -699,7 +701,6 @@ public class ImportBackgroundService : BackgroundService r.LastUpdatedAt = DateTime.Now; r.LastUpdateUserId = job.UserId ?? ""; r.LastUpdateFullName = job.FullName ?? "System Administrator"; - r.ScoreImport = imported; batchScores.Add(r); } @@ -709,6 +710,11 @@ public class ImportBackgroundService : BackgroundService if (batchCount >= batchSize) { + // Set ScoreImportId FK for all scores in batch + foreach (var score in batchScores) + { + _context.Entry(score).Property("ScoreImportId").CurrentValue = scoreImport_id; + } await _context.BulkInsertAsync(batchScores); batchScores.Clear(); batchCount = 0; @@ -719,6 +725,10 @@ public class ImportBackgroundService : BackgroundService // Process remaining records if (batchScores.Count > 0) { + foreach (var score in batchScores) + { + _context.Entry(score).Property("ScoreImportId").CurrentValue = scoreImport_id; + } await _context.BulkInsertAsync(batchScores); } } while (reader.NextResult()); From 054ef81c6366b91e987c807dc1c478ded91a75f7 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 17:14:39 +0700 Subject: [PATCH 16/18] update model and fix code --- Models/Recruits/RecruitScore.cs | 2 ++ Models/Recruits/ScoreImport.cs | 4 +++- Services/ImportBackgroundService.cs | 10 +++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Models/Recruits/RecruitScore.cs b/Models/Recruits/RecruitScore.cs index 9698b3f..283bfd9 100644 --- a/Models/Recruits/RecruitScore.cs +++ b/Models/Recruits/RecruitScore.cs @@ -83,6 +83,8 @@ namespace BMA.EHR.Recruit.Models.Recruits [MaxLength(50), Comment("สถานะคัดกรองคุณสมบัติ")] public string ExamAttribute { get; set; } = string.Empty; + public Guid ScoreImportId { get; set; } + public ScoreImport ScoreImport { get; set; } } } diff --git a/Models/Recruits/ScoreImport.cs b/Models/Recruits/ScoreImport.cs index badee62..413f3f2 100644 --- a/Models/Recruits/ScoreImport.cs +++ b/Models/Recruits/ScoreImport.cs @@ -7,7 +7,9 @@ namespace BMA.EHR.Recruit.Models.Recruits { public int Year { get; set; } - public Document ImportFile { get; set; } = new Document(); + public Guid? ImportFileId { get; set; } + + public Document ImportFile { get; set; } public virtual List Scores { get; set; } = new List(); diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 6841bc5..64b1af8 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -615,9 +615,9 @@ public class ImportBackgroundService : BackgroundService imported.LastUpdateFullName = job.FullName ?? "System Administrator"; imported.Scores = new List(); - // Save ScoreImport — use shadow FK for ImportFile to avoid re-inserting Document - var scoreEntry = _context.ScoreImports.Add(imported); - scoreEntry.Property("ImportFileId").CurrentValue = doc.Id; + // Save ScoreImport — set ImportFileId FK directly (explicit property, not shadow) + imported.ImportFileId = doc.Id; + _context.ScoreImports.Add(imported); await _context.SaveChangesAsync(); _context.ChangeTracker.Clear(); @@ -713,7 +713,7 @@ public class ImportBackgroundService : BackgroundService // Set ScoreImportId FK for all scores in batch foreach (var score in batchScores) { - _context.Entry(score).Property("ScoreImportId").CurrentValue = scoreImport_id; + score.ScoreImportId = scoreImport_id; } await _context.BulkInsertAsync(batchScores); batchScores.Clear(); @@ -727,7 +727,7 @@ public class ImportBackgroundService : BackgroundService { foreach (var score in batchScores) { - _context.Entry(score).Property("ScoreImportId").CurrentValue = scoreImport_id; + score.ScoreImportId = scoreImport_id; } await _context.BulkInsertAsync(batchScores); } From d019ed588b7cf87652cd7e81d998aa2cd147d5e6 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 17:23:36 +0700 Subject: [PATCH 17/18] fix relation --- Data/ApplicationDbContext.cs | 1 + .../20260519102256_fix relation.Designer.cs | 1605 +++++++++++++++++ Migrations/20260519102256_fix relation.cs | 64 + .../ApplicationDbContextModelSnapshot.cs | 6 +- Models/Recruits/RecruitScore.cs | 2 + Services/ImportBackgroundService.cs | 7 +- 6 files changed, 1680 insertions(+), 5 deletions(-) create mode 100644 Migrations/20260519102256_fix relation.Designer.cs create mode 100644 Migrations/20260519102256_fix relation.cs diff --git a/Data/ApplicationDbContext.cs b/Data/ApplicationDbContext.cs index 81e4fd6..d386d19 100644 --- a/Data/ApplicationDbContext.cs +++ b/Data/ApplicationDbContext.cs @@ -20,6 +20,7 @@ namespace BMA.EHR.Recruit.Data modelBuilder.Entity().HasMany(x => x.Addresses).WithOne(x => x.Recruit).OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity().HasMany(x => x.Certificates).WithOne(x => x.Recruit).OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity().HasMany(x => x.Payments).WithOne(x => x.Recruit).OnDelete(DeleteBehavior.Cascade); + modelBuilder.Entity().HasMany(x => x.Scores).WithOne(x => x.ScoreImport).HasForeignKey(x => x.ScoreImportId).OnDelete(DeleteBehavior.Cascade); } public DbSet Documents { get; set; } diff --git a/Migrations/20260519102256_fix relation.Designer.cs b/Migrations/20260519102256_fix relation.Designer.cs new file mode 100644 index 0000000..770f155 --- /dev/null +++ b/Migrations/20260519102256_fix relation.Designer.cs @@ -0,0 +1,1605 @@ +// +using System; +using BMA.EHR.Recruit.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BMA.EHR.Recruit.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20260519102256_fix relation")] + partial class fixrelation + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Documents.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("Detail") + .IsRequired() + .HasColumnType("text"); + + b.Property("FileName") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("varchar(255)"); + + b.Property("FileSize") + .HasColumnType("int"); + + b.Property("FileType") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ObjectRefId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.Recruit", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("ApplyDate") + .HasColumnType("datetime(6)"); + + b.Property("CitizenCardExpireDate") + .HasColumnType("datetime(6)"); + + b.Property("CitizenCardIssuer") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("CitizenId") + .IsRequired() + .HasMaxLength(13) + .HasColumnType("varchar(13)") + .HasComment("เลขประจำตัวประชาชน"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("DateOfBirth") + .HasColumnType("datetime(6)"); + + b.Property("ExamId") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("FirstName") + .IsRequired() + .HasMaxLength(150) + .HasColumnType("varchar(150)"); + + b.Property("Gendor") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("HddPosition") + .HasColumnType("longtext") + .HasComment("บัญชีสอบ"); + + b.Property("Isspecial") + .IsRequired() + .HasMaxLength(1) + .HasColumnType("varchar(1)"); + + b.Property("LastName") + .IsRequired() + .HasMaxLength(150) + .HasColumnType("varchar(150)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("Marry") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("ModifiedDate") + .HasColumnType("datetime(6)"); + + b.Property("National") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("PositionLevel") + .HasColumnType("longtext"); + + b.Property("PositionName") + .HasColumnType("longtext"); + + b.Property("PositionType") + .HasColumnType("longtext"); + + b.Property("Prefix") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("Qualified") + .IsRequired() + .HasMaxLength(1) + .HasColumnType("varchar(1)"); + + b.Property("Race") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("RecruitImportId") + .HasColumnType("char(36)"); + + b.Property("RefNo") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("Religion") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Remark") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("typeTest") + .HasColumnType("longtext") + .HasComment("ประเภทการสอบภาค ก."); + + b.HasKey("Id"); + + b.HasIndex("RecruitImportId"); + + b.ToTable("Recruits"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitAddress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("Address") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Address1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Amphur") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Amphur1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("District") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("District1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("Mobile") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Moo") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Moo1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Province") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Province1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("RecruitId") + .HasColumnType("char(36)"); + + b.Property("Road") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Road1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Soi") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Soi1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Telephone") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("ZipCode") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("ZipCode1") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.HasKey("Id"); + + b.HasIndex("RecruitId"); + + b.ToTable("RecruitAddresses"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitCertificate", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CertificateNo") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("Description") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("ExpiredDate") + .HasColumnType("datetime(6)"); + + b.Property("IssueDate") + .HasColumnType("datetime(6)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("RecruitId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("RecruitId"); + + b.ToTable("RecruitCertificates"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("DocumentFileId") + .HasColumnType("char(36)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("RecruitId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("DocumentFileId"); + + b.HasIndex("RecruitId"); + + b.ToTable("RecruitDocuments"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitEducation", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("BachelorDate") + .HasColumnType("datetime(6)"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("Degree") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("GPA") + .HasColumnType("double"); + + b.Property("HighDegree") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("Major") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("MajorGroupId") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("MajorGroupName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("RecruitId") + .HasColumnType("char(36)"); + + b.Property("Specialist") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("varchar(1000)"); + + b.Property("University") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.HasKey("Id"); + + b.HasIndex("RecruitId"); + + b.ToTable("RecruitEducations"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImport", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("AnnouncementDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(14) + .HasComment("วันที่ประกาศผลสอบ"); + + b.Property("AnnouncementEndDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(7) + .HasComment("วันสิ้นสุดประกาศ"); + + b.Property("AnnouncementStartDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(6) + .HasComment("วันเริ่มประกาศ"); + + b.Property("AuthName") + .HasColumnType("longtext"); + + b.Property("AuthPosition") + .HasColumnType("longtext"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("Detail") + .HasColumnType("longtext") + .HasColumnOrder(4) + .HasComment("รายละเอียด"); + + b.Property("ExamDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(12) + .HasComment("วันที่สอบ"); + + b.Property("Fee") + .HasColumnType("int") + .HasColumnOrder(5) + .HasComment("ค่าธรรมเนียม"); + + b.Property("ImportFileId") + .HasColumnType("char(36)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(250) + .HasColumnType("varchar(250)") + .HasColumnOrder(2) + .HasComment("รอบการสอบ"); + + b.Property("Note") + .HasColumnType("text") + .HasColumnOrder(13) + .HasComment("หมายเหตุ"); + + b.Property("Order") + .HasColumnType("int") + .HasColumnOrder(3) + .HasComment("ครั้งที่"); + + b.Property("PaymentEndDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(9) + .HasComment("วันสิ้นสุดชำระเงิน"); + + b.Property("PaymentStartDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(8) + .HasComment("วันเริ่มชำระเงิน"); + + b.Property("RegisterEndDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(11) + .HasComment("วันสิ้นสุดสมัครสอบ"); + + b.Property("RegisterStartDate") + .HasColumnType("datetime(6)") + .HasColumnOrder(10) + .HasComment("วันเริ่มสมัครสอบ"); + + b.Property("Year") + .HasColumnType("int") + .HasColumnOrder(1) + .HasComment("ปีงบประมาณที่จัดสอบ"); + + b.HasKey("Id"); + + b.HasIndex("ImportFileId"); + + b.ToTable("RecruitImports"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImportDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("DocumentId") + .HasColumnType("char(36)") + .HasComment("Id เอกสาร"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("RecruitImportId") + .HasColumnType("char(36)") + .HasComment("Id รอบสมัครสอบ"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("RecruitImportId"); + + b.ToTable("RecruitImportDocuments"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImportHistory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("Description") + .IsRequired() + .HasColumnType("longtext") + .HasColumnOrder(1) + .HasComment("รายละเอียดการนำเข้า"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("RecruitImportId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("RecruitImportId"); + + b.ToTable("RecruitImportHistories"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImportImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("DocumentId") + .HasColumnType("char(36)") + .HasComment("Id ไฟล์รูป"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("RecruitImportId") + .HasColumnType("char(36)") + .HasComment("Id รอบสมัครสอบ"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("RecruitImportId"); + + b.ToTable("RecruitImportImages"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitOccupation", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("Occupation") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Position") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("RecruitId") + .HasColumnType("char(36)"); + + b.Property("Telephone") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("WorkAge") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Workplace") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.HasKey("Id"); + + b.HasIndex("RecruitId"); + + b.ToTable("RecruitOccupations"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitPayment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("AccountNumber") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("Amount") + .HasColumnType("decimal(65,30)"); + + b.Property("BankCode") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("ChequeNo") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("ChqueBankCode") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("CompanyCode") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("CreditDebit") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("CustomerName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("PaymentId") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("PaymentType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("RecruitId") + .HasColumnType("char(36)"); + + b.Property("RefNo1") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("TellerId") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("TermBranch") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("TextFile") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("TransDate") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("TransTime") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.HasKey("Id"); + + b.HasIndex("RecruitId"); + + b.ToTable("RecruitPayments"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitScore", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("ABStatus") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)") + .HasComment("ภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่ง ผลประเมิน"); + + b.Property("AStatus") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)") + .HasComment("ภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่ง ผลประเมิน"); + + b.Property("BStatus") + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.Property("CStatus") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง ผลประเมิน"); + + b.Property("CitizenId") + .IsRequired() + .HasMaxLength(13) + .HasColumnType("varchar(13)") + .HasComment("เลขประจำตัวประชาชน"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("ExamAttribute") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)") + .HasComment("สถานะคัดกรองคุณสมบัติ"); + + b.Property("ExamId") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)") + .HasComment("เลขประจำตัวสอบ"); + + b.Property("ExamStatus") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)") + .HasComment("สอบได้ / ตก / ขาดสอบ"); + + b.Property("FullA") + .HasColumnType("int") + .HasComment("ภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่ง คะแนนเต็ม"); + + b.Property("FullB") + .HasColumnType("int"); + + b.Property("FullC") + .HasColumnType("int") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง ทดสอบสมรรถนะ+ทดสอบจิตวิทยาฯ คะแนนเต็ม"); + + b.Property("FullD") + .HasColumnType("int") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง สัมภาษณ์ คะแนนเต็ม"); + + b.Property("FullScore") + .HasColumnType("int") + .HasComment("คะแนนเต็ม"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("Major") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Number") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasComment("ลำดับที่สอบได้"); + + b.Property("PercentageA") + .HasColumnType("double") + .HasComment("ภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่ง ร้อยละ"); + + b.Property("PercentageB") + .HasColumnType("double"); + + b.Property("PercentageC") + .HasColumnType("double") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง ร้อยละ"); + + b.Property("RemarkExamOrder") + .IsRequired() + .HasColumnType("longtext") + .HasComment("หมายเหตุจากลำดับที่สอบได้"); + + b.Property("RemarkScore") + .IsRequired() + .HasColumnType("longtext") + .HasComment("หมายเหตุจากบัญชีรวมคะแนน"); + + b.Property("ScoreImportId") + .HasColumnType("char(36)"); + + b.Property("SumA") + .HasColumnType("double") + .HasComment("ภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่ง คะแนนรวม"); + + b.Property("SumAB") + .HasColumnType("double") + .HasComment("ภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่ง คะแนนรวม"); + + b.Property("SumB") + .HasColumnType("double"); + + b.Property("SumC") + .HasColumnType("double") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง ทดสอบสมรรถนะ+ทดสอบจิตวิทยาฯ คะแนนรวม"); + + b.Property("SumCD") + .HasColumnType("double") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง คะแนนรวมทดสอบสมรรถนะ+ทดสอบจิตวิทยาฯ และสัมภาษณ์"); + + b.Property("SumD") + .HasColumnType("double") + .HasComment("ภาคความเหมาะสมกับตำแหน่ง สัมภาษณ์ คะแนนรวม"); + + b.Property("TotalScore") + .HasColumnType("double") + .HasComment("คะแนนรวม"); + + b.HasKey("Id"); + + b.HasIndex("ScoreImportId"); + + b.ToTable("RecruitScores"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.ScoreImport", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)") + .HasColumnOrder(0) + .HasComment("PrimaryKey") + .HasAnnotation("Relational:JsonPropertyName", "id"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(100) + .HasComment("สร้างข้อมูลเมื่อ"); + + b.Property("CreatedFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(104) + .HasComment("ชื่อ User ที่สร้างข้อมูล"); + + b.Property("CreatedUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(101) + .HasComment("User Id ที่สร้างข้อมูล"); + + b.Property("ImportFileId") + .HasColumnType("char(36)"); + + b.Property("LastUpdateFullName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)") + .HasColumnOrder(105) + .HasComment("ชื่อ User ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdateUserId") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("varchar(40)") + .HasColumnOrder(103) + .HasComment("User Id ที่แก้ไขข้อมูลล่าสุด"); + + b.Property("LastUpdatedAt") + .HasColumnType("datetime(6)") + .HasColumnOrder(102) + .HasComment("แก้ไขข้อมูลล่าสุดเมื่อ"); + + b.Property("RecruitImportId") + .HasColumnType("char(36)"); + + b.Property("Year") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ImportFileId"); + + b.HasIndex("RecruitImportId") + .IsUnique(); + + b.ToTable("ScoreImports"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.Recruit", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport") + .WithMany("Recruits") + .HasForeignKey("RecruitImportId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("RecruitImport"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitAddress", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.Recruit", "Recruit") + .WithMany("Addresses") + .HasForeignKey("RecruitId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Recruit"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitCertificate", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.Recruit", "Recruit") + .WithMany("Certificates") + .HasForeignKey("RecruitId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Recruit"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitDocument", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "DocumentFile") + .WithMany() + .HasForeignKey("DocumentFileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BMA.EHR.Recruit.Models.Recruits.Recruit", "Recruit") + .WithMany("Documents") + .HasForeignKey("RecruitId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("DocumentFile"); + + b.Navigation("Recruit"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitEducation", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.Recruit", "Recruit") + .WithMany("Educations") + .HasForeignKey("RecruitId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Recruit"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImport", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "ImportFile") + .WithMany() + .HasForeignKey("ImportFileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ImportFile"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImportDocument", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport") + .WithMany("RecruitDocuments") + .HasForeignKey("RecruitImportId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("RecruitImport"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImportHistory", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport") + .WithMany("ImportHostories") + .HasForeignKey("RecruitImportId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("RecruitImport"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImportImage", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport") + .WithMany("RecruitImages") + .HasForeignKey("RecruitImportId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("RecruitImport"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitOccupation", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.Recruit", "Recruit") + .WithMany("Occupations") + .HasForeignKey("RecruitId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Recruit"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitPayment", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.Recruit", "Recruit") + .WithMany("Payments") + .HasForeignKey("RecruitId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Recruit"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitScore", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Recruits.ScoreImport", "ScoreImport") + .WithMany("Scores") + .HasForeignKey("ScoreImportId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ScoreImport"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.ScoreImport", b => + { + b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "ImportFile") + .WithMany() + .HasForeignKey("ImportFileId"); + + b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport") + .WithOne("ScoreImport") + .HasForeignKey("BMA.EHR.Recruit.Models.Recruits.ScoreImport", "RecruitImportId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ImportFile"); + + b.Navigation("RecruitImport"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.Recruit", b => + { + b.Navigation("Addresses"); + + b.Navigation("Certificates"); + + b.Navigation("Documents"); + + b.Navigation("Educations"); + + b.Navigation("Occupations"); + + b.Navigation("Payments"); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.RecruitImport", b => + { + b.Navigation("ImportHostories"); + + b.Navigation("RecruitDocuments"); + + b.Navigation("RecruitImages"); + + b.Navigation("Recruits"); + + b.Navigation("ScoreImport") + .IsRequired(); + }); + + modelBuilder.Entity("BMA.EHR.Recruit.Models.Recruits.ScoreImport", b => + { + b.Navigation("Scores"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20260519102256_fix relation.cs b/Migrations/20260519102256_fix relation.cs new file mode 100644 index 0000000..bf91e02 --- /dev/null +++ b/Migrations/20260519102256_fix relation.cs @@ -0,0 +1,64 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BMA.EHR.Recruit.Migrations +{ + /// + public partial class fixrelation : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ScoreImports_Documents_ImportFileId", + table: "ScoreImports"); + + migrationBuilder.AlterColumn( + name: "ImportFileId", + table: "ScoreImports", + type: "char(36)", + nullable: true, + collation: "ascii_general_ci", + oldClrType: typeof(Guid), + oldType: "char(36)") + .OldAnnotation("Relational:Collation", "ascii_general_ci"); + + migrationBuilder.AddForeignKey( + name: "FK_ScoreImports_Documents_ImportFileId", + table: "ScoreImports", + column: "ImportFileId", + principalTable: "Documents", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ScoreImports_Documents_ImportFileId", + table: "ScoreImports"); + + migrationBuilder.AlterColumn( + name: "ImportFileId", + table: "ScoreImports", + type: "char(36)", + nullable: false, + defaultValue: new Guid("00000000-0000-0000-0000-000000000000"), + collation: "ascii_general_ci", + oldClrType: typeof(Guid), + oldType: "char(36)", + oldNullable: true) + .OldAnnotation("Relational:Collation", "ascii_general_ci"); + + migrationBuilder.AddForeignKey( + name: "FK_ScoreImports_Documents_ImportFileId", + table: "ScoreImports", + column: "ImportFileId", + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/Migrations/ApplicationDbContextModelSnapshot.cs b/Migrations/ApplicationDbContextModelSnapshot.cs index d849857..94e7f07 100644 --- a/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Migrations/ApplicationDbContextModelSnapshot.cs @@ -1353,7 +1353,7 @@ namespace BMA.EHR.Recruit.Migrations .HasColumnOrder(101) .HasComment("User Id ที่สร้างข้อมูล"); - b.Property("ImportFileId") + b.Property("ImportFileId") .HasColumnType("char(36)"); b.Property("LastUpdateFullName") @@ -1550,9 +1550,7 @@ namespace BMA.EHR.Recruit.Migrations { b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "ImportFile") .WithMany() - .HasForeignKey("ImportFileId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("ImportFileId"); b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport") .WithOne("ScoreImport") diff --git a/Models/Recruits/RecruitScore.cs b/Models/Recruits/RecruitScore.cs index 283bfd9..93024f1 100644 --- a/Models/Recruits/RecruitScore.cs +++ b/Models/Recruits/RecruitScore.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace BMA.EHR.Recruit.Models.Recruits { @@ -83,6 +84,7 @@ namespace BMA.EHR.Recruit.Models.Recruits [MaxLength(50), Comment("สถานะคัดกรองคุณสมบัติ")] public string ExamAttribute { get; set; } = string.Empty; + [ForeignKey("ScoreImport")] public Guid ScoreImportId { get; set; } public ScoreImport ScoreImport { get; set; } diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 64b1af8..5f8119f 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -580,8 +580,13 @@ public class ImportBackgroundService : BackgroundService if (existingScores.Count > 0) await _context.BulkDeleteAsync(existingScores); // Also delete the ScoreImport row itself (has unique index on RecruitImportId) + // Use Remove+SaveChanges instead of BulkDelete since BulkDelete fails with detached entities + var scoreImportToDelete = existingScoreImport; _context.ChangeTracker.Clear(); - await _context.BulkDeleteAsync(new List { existingScoreImport }); + var deleteStub = new ScoreImport { Id = scoreImportToDelete.Id }; + _context.ScoreImports.Attach(deleteStub); + _context.ScoreImports.Remove(deleteStub); + await _context.SaveChangesAsync(); } // Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking) From 44c56c4a0a1c231ea98a6c85116ef651ca8f9c18 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 19 May 2026 17:37:42 +0700 Subject: [PATCH 18/18] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20error=20ins?= =?UTF-8?q?ert=20score?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/ImportBackgroundService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 5f8119f..7628ad0 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -664,6 +664,7 @@ public class ImportBackgroundService : BackgroundService if (string.IsNullOrEmpty(cell1)) break; var r = new RecruitScore(); + r.Id = Guid.NewGuid(); r.ExamId = reader.GetValue(1)?.ToString(); if (!string.IsNullOrEmpty(r.ExamId) && recruitsDict.TryGetValue(r.ExamId, out var recruit))