fix
This commit is contained in:
parent
4dc8849b31
commit
cc251f7129
146 changed files with 2465 additions and 4785 deletions
729
Services/ImportBackgroundService.cs
Normal file
729
Services/ImportBackgroundService.cs
Normal file
|
|
@ -0,0 +1,729 @@
|
|||
using System.Threading.Channels;
|
||||
using BMA.EHR.Recruit.Core;
|
||||
using BMA.EHR.Recruit.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OfficeOpenXml;
|
||||
using System.Net.Http.Headers;
|
||||
using BMA.EHR.Recruit.Data;
|
||||
using BMA.EHR.Recruit.Extensions;
|
||||
using BMA.EHR.Recruit.Models.Recruits;
|
||||
using BMA.EHR.Recruit.Requests.Recruits;
|
||||
|
||||
namespace BMA.EHR.Recruit.Services;
|
||||
|
||||
public class ImportBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly ImportJobQueue _queue;
|
||||
private readonly ImportJobTracker _tracker;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ILogger<ImportBackgroundService> _logger;
|
||||
|
||||
public ImportBackgroundService(
|
||||
ImportJobQueue queue,
|
||||
ImportJobTracker tracker,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
ILogger<ImportBackgroundService> logger)
|
||||
{
|
||||
_queue = queue;
|
||||
_tracker = tracker;
|
||||
_scopeFactory = scopeFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("ImportBackgroundService started.");
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var job = await _queue.DequeueAsync(stoppingToken);
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var minioService = scope.ServiceProvider.GetRequiredService<MinIOService>();
|
||||
var recruitService = scope.ServiceProvider.GetRequiredService<RecruitService>();
|
||||
var webHostEnv = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<ImportBackgroundService>>();
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
#region CandidateFile
|
||||
|
||||
private async Task ProcessCandidateFileAsync(ApplicationDbContext _context, MinIOService _minioService, IWebHostEnvironment _webHostEnv, ImportJobInfo job)
|
||||
{
|
||||
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();
|
||||
|
||||
int row = 2;
|
||||
int batchCount = 0;
|
||||
const int batchSize = 100;
|
||||
int totalProcessed = 0;
|
||||
|
||||
while (row <= totalRows)
|
||||
{
|
||||
var cell1 = workSheet?.Cells[row, 1]?.GetValue<string>();
|
||||
if (cell1 == "" || cell1 == null) break;
|
||||
|
||||
var r = new Models.Recruits.Recruit();
|
||||
r.ExamId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ExamID)]?.GetValue<string>();
|
||||
r.CitizenId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalID)]?.GetValue<string>();
|
||||
r.Prefix = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Prefix)]?.GetValue<string>();
|
||||
r.FirstName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.FirstName)]?.GetValue<string>();
|
||||
r.LastName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.LastName)]?.GetValue<string>();
|
||||
r.Gendor = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Gender)]?.GetValue<string>();
|
||||
r.National = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.National)]?.GetValue<string>().IsNull("");
|
||||
r.Race = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Race)]?.GetValue<string>().IsNull("");
|
||||
r.Religion = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Religion)]?.GetValue<string>().IsNull("");
|
||||
r.DateOfBirth = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.DateOfBirth)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-"));
|
||||
r.Marry = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Marry)]?.GetValue<string>();
|
||||
r.Isspecial = "N";
|
||||
r.CitizenCardIssuer = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalCardIssue)]?.GetValue<string>();
|
||||
r.CitizenCardExpireDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalCardExpireDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-"));
|
||||
r.ApplyDate = (DateTime)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ApplyDate)]?.GetValue<DateTime>();
|
||||
r.PositionName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionName)]?.GetValue<string>().IsNull("");
|
||||
r.PositionType = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionType)]?.GetValue<string>().IsNull("");
|
||||
r.PositionLevel = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PositionLevel)]?.GetValue<string>().IsNull("");
|
||||
|
||||
// address
|
||||
r.Addresses.Add(new RecruitAddress()
|
||||
{
|
||||
Address = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address)]?.GetValue<string>() ?? "",
|
||||
Moo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Moo)]?.GetValue<string>() ?? "",
|
||||
Soi = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Soi)]?.GetValue<string>() ?? "",
|
||||
Road = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Road)]?.GetValue<string>() ?? "",
|
||||
District = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.District)]?.GetValue<string>() ?? "",
|
||||
Amphur = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amphur)]?.GetValue<string>() ?? "",
|
||||
Province = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Province)]?.GetValue<string>() ?? "",
|
||||
ZipCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ZipCode)]?.GetValue<string>() ?? "",
|
||||
Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Telephone)]?.GetValue<string>() ?? "",
|
||||
Mobile = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Mobile)]?.GetValue<string>() ?? "",
|
||||
Address1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address1)]?.GetValue<string>() ?? "",
|
||||
Moo1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Moo1)]?.GetValue<string>() ?? "",
|
||||
Soi1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Soi1)]?.GetValue<string>() ?? "",
|
||||
Road1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Road1)]?.GetValue<string>() ?? "",
|
||||
District1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.District1)]?.GetValue<string>() ?? "",
|
||||
Amphur1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amphur1)]?.GetValue<string>() ?? "",
|
||||
Province1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Province1)]?.GetValue<string>() ?? "",
|
||||
ZipCode1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ZipCode1)]?.GetValue<string>() ?? "",
|
||||
});
|
||||
|
||||
// payment
|
||||
r.Payments.Add(new RecruitPayment()
|
||||
{
|
||||
PaymentId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PaymentID)]?.GetValue<string>() ?? "",
|
||||
CompanyCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CompanyCode)]?.GetValue<string>() ?? "",
|
||||
TextFile = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TextFile)]?.GetValue<string>() ?? "",
|
||||
BankCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BankCode)]?.GetValue<string>() ?? "",
|
||||
AccountNumber = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.AccouontNumer)]?.GetValue<string>() ?? "",
|
||||
TransDate = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TransDate)]?.GetValue<string>() ?? "",
|
||||
TransTime = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TransTime)]?.GetValue<string>() ?? "",
|
||||
CustomerName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CustomerName)]?.GetValue<string>() ?? "",
|
||||
RefNo1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.RefNo1)]?.GetValue<string>() ?? "",
|
||||
TermBranch = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TermBranch)]?.GetValue<string>() ?? "",
|
||||
TellerId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TellerID)]?.GetValue<string>() ?? "",
|
||||
CreditDebit = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CreditDebit)]?.GetValue<string>() ?? "",
|
||||
PaymentType = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Type)]?.GetValue<string>(),
|
||||
ChequeNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChequeNo)]?.GetValue<string>() ?? "",
|
||||
Amount = (decimal)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amount)]?.GetValue<decimal>(),
|
||||
ChqueBankCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChqBankCode)]?.GetValue<string>() ?? ""
|
||||
});
|
||||
|
||||
// occupation
|
||||
r.Occupations.Add(new RecruitOccupation()
|
||||
{
|
||||
Occupation = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Occupation)]?.GetValue<string>() ?? "",
|
||||
Position = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Position)]?.GetValue<string>() ?? "",
|
||||
Workplace = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Workplace)]?.GetValue<string>() ?? "",
|
||||
Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkplaceTelephone)]?.GetValue<string>() ?? "",
|
||||
WorkAge = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkAge)]?.GetValue<string>() ?? "",
|
||||
});
|
||||
|
||||
// certificate
|
||||
r.Certificates.Add(new RecruitCertificate()
|
||||
{
|
||||
CertificateNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateNo)]?.GetValue<string>() ?? "",
|
||||
Description = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateDesc)]?.GetValue<string>() ?? "",
|
||||
IssueDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateIssueDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-")),
|
||||
ExpiredDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateExpireDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-"))
|
||||
});
|
||||
|
||||
r.Educations.Add(new RecruitEducation()
|
||||
{
|
||||
Degree = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Degree)]?.GetValue<string>() ?? "",
|
||||
Major = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Major)]?.GetValue<string>() ?? "",
|
||||
MajorGroupId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.MajorGroupID)]?.GetValue<string>() ?? "",
|
||||
MajorGroupName = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.MajorGroupName)]?.GetValue<string>() ?? "",
|
||||
University = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.University)]?.GetValue<string>() ?? "",
|
||||
GPA = (double)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.GPA)]?.GetValue<double>(),
|
||||
Specialist = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.SpecialList)]?.GetValue<string>() ?? "",
|
||||
HighDegree = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.HighDegree)]?.GetValue<string>() ?? "",
|
||||
BachelorDate = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BachelorDate)]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-"))
|
||||
});
|
||||
|
||||
r.RecruitImport = imported;
|
||||
_context.Recruits.Add(r);
|
||||
|
||||
row++;
|
||||
batchCount++;
|
||||
totalProcessed++;
|
||||
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
_context.SaveChanges();
|
||||
_context.ChangeTracker.Clear();
|
||||
_context.Entry(imported).State = EntityState.Unchanged;
|
||||
batchCount = 0;
|
||||
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CandidateFileById
|
||||
|
||||
private async Task ProcessCandidateFileByIdAsync(ApplicationDbContext _context, MinIOService _minioService, RecruitService _recruitService, IWebHostEnvironment _webHostEnv, ImportJobInfo job)
|
||||
{
|
||||
var imported = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ImportHostories)
|
||||
.Include(x => x.ImportFile)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Addresses)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Occupations)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Certificates)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Educations)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Payments)
|
||||
.FirstOrDefaultAsync(x => x.Id == job.RecruitImportId);
|
||||
|
||||
if (imported == null) throw new Exception("RecruitImport not found");
|
||||
|
||||
imported.ImportHostories.Add(new RecruitImportHistory
|
||||
{
|
||||
Description = "นำเข้าข้อมูลผู้สมัครสอบแข่งขัน",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = job.UserId ?? "",
|
||||
CreatedFullName = job.FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = job.UserId ?? "",
|
||||
LastUpdateFullName = job.FullName ?? "System Administrator",
|
||||
});
|
||||
|
||||
using var c_package = new ExcelPackage(new FileInfo(job.ImportFile));
|
||||
for (int i = 0; i < c_package.Workbook.Worksheets.Count; i++)
|
||||
{
|
||||
var workSheet = c_package.Workbook.Worksheets[i];
|
||||
var totalRows = workSheet.Dimension.Rows;
|
||||
var cols = workSheet.GetHeaderColumns();
|
||||
|
||||
int row = 2;
|
||||
int batchCount = 0;
|
||||
const int batchSize = 100;
|
||||
int totalProcessed = 0;
|
||||
|
||||
while (row <= totalRows)
|
||||
{
|
||||
var cell1 = workSheet?.Cells[row, 1]?.GetValue<string>();
|
||||
if (cell1 == "" || cell1 == null) break;
|
||||
|
||||
var r = new Models.Recruits.Recruit();
|
||||
r.ExamId = workSheet?.Cells[row, 1]?.GetValue<string>() ?? "";
|
||||
r.PositionName = workSheet?.Cells[row, 3]?.GetValue<string>() ?? "";
|
||||
r.HddPosition = workSheet?.Cells[row, 4]?.GetValue<string>() ?? "";
|
||||
r.Prefix = workSheet?.Cells[row, 5]?.GetValue<string>() == "อื่น ๆ" ? workSheet?.Cells[row, 6]?.GetValue<string>() ?? "" : workSheet?.Cells[row, 5]?.GetValue<string>() ?? "";
|
||||
r.FirstName = workSheet?.Cells[row, 7]?.GetValue<string>() ?? "";
|
||||
r.LastName = workSheet?.Cells[row, 8]?.GetValue<string>() ?? "";
|
||||
r.Gendor = workSheet?.Cells[row, 98]?.GetValue<string>() ?? "";
|
||||
r.National = workSheet?.Cells[row, 9]?.GetValue<string>() ?? "";
|
||||
r.Race = "";
|
||||
r.Religion = workSheet?.Cells[row, 10]?.GetValue<string>() ?? "";
|
||||
r.DateOfBirth = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue<string>()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 11]?.GetValue<string>() ?? "", "dd/MM/yyyy") : null;
|
||||
r.CitizenId = workSheet?.Cells[row, 12]?.GetValue<string>() ?? "";
|
||||
r.typeTest = workSheet?.Cells[row, 13]?.GetValue<string>() ?? "";
|
||||
r.Marry = "";
|
||||
r.Isspecial = "N";
|
||||
r.CitizenCardExpireDate = null;
|
||||
r.ModifiedDate = null;
|
||||
r.ApplyDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 87]?.GetValue<string>()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 87]?.GetValue<string>() ?? "", "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";
|
||||
|
||||
// education
|
||||
r.Educations.Add(new RecruitEducation()
|
||||
{
|
||||
Degree = workSheet?.Cells[row, 18]?.GetValue<string>() ?? "",
|
||||
Major = workSheet?.Cells[row, 19]?.GetValue<string>() == "อื่น ๆ" ? workSheet?.Cells[row, 20]?.GetValue<string>() ?? "" : workSheet?.Cells[row, 19]?.GetValue<string>() ?? "",
|
||||
MajorGroupId = "",
|
||||
MajorGroupName = "",
|
||||
University = workSheet?.Cells[row, 21]?.GetValue<string>() == "อื่น ๆ" ? workSheet?.Cells[row, 22]?.GetValue<string>() ?? "" : workSheet?.Cells[row, 21]?.GetValue<string>() ?? "",
|
||||
GPA = (double)workSheet?.Cells[row, 26]?.GetValue<double>(),
|
||||
Specialist = "",
|
||||
HighDegree = workSheet?.Cells[row, 27]?.GetValue<string>() ?? "",
|
||||
BachelorDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 25]?.GetValue<string>()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 25]?.GetValue<string>() ?? "", "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"
|
||||
});
|
||||
|
||||
// occupation
|
||||
r.Occupations.Add(new RecruitOccupation()
|
||||
{
|
||||
Occupation = workSheet?.Cells[row, 33]?.GetValue<string>() == "อื่น ๆ" ? workSheet?.Cells[row, 34]?.GetValue<string>() ?? "" : workSheet?.Cells[row, 33]?.GetValue<string>() ?? "",
|
||||
Position = workSheet?.Cells[row, 37]?.GetValue<string>() ?? "",
|
||||
Workplace = $"{(workSheet?.Cells[row, 36]?.GetValue<string>() ?? "")} {(workSheet?.Cells[row, 35]?.GetValue<string>() ?? "")}",
|
||||
Telephone = workSheet?.Cells[row, 9999]?.GetValue<string>() ?? "",
|
||||
WorkAge = workSheet?.Cells[row, 9999]?.GetValue<string>() ?? "",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = job.UserId ?? "",
|
||||
CreatedFullName = job.FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = job.UserId ?? "",
|
||||
LastUpdateFullName = job.FullName ?? "System Administrator"
|
||||
});
|
||||
|
||||
// address
|
||||
r.Addresses.Add(new RecruitAddress()
|
||||
{
|
||||
Address = $"{(workSheet?.Cells[row, 49]?.GetValue<string>() ?? "")} {(workSheet?.Cells[row, 50]?.GetValue<string>() ?? "")}",
|
||||
Moo = workSheet?.Cells[row, 51]?.GetValue<string>() ?? "",
|
||||
Soi = workSheet?.Cells[row, 52]?.GetValue<string>() ?? "",
|
||||
Road = workSheet?.Cells[row, 53]?.GetValue<string>() ?? "",
|
||||
District = workSheet?.Cells[row, 54]?.GetValue<string>() ?? "",
|
||||
Amphur = workSheet?.Cells[row, 55]?.GetValue<string>() ?? "",
|
||||
Province = workSheet?.Cells[row, 56]?.GetValue<string>() ?? "",
|
||||
ZipCode = (workSheet?.Cells[row, 57]?.GetValue<string>() ?? "").Trim(),
|
||||
Telephone = workSheet?.Cells[row, 58]?.GetValue<string>() ?? "",
|
||||
Mobile = "",
|
||||
Address1 = $"{(workSheet?.Cells[row, 61]?.GetValue<string>() ?? "")} {(workSheet?.Cells[row, 62]?.GetValue<string>() ?? "")}",
|
||||
Moo1 = workSheet?.Cells[row, 63]?.GetValue<string>() ?? "",
|
||||
Soi1 = workSheet?.Cells[row, 64]?.GetValue<string>() ?? "",
|
||||
Road1 = workSheet?.Cells[row, 65]?.GetValue<string>() ?? "",
|
||||
District1 = workSheet?.Cells[row, 66]?.GetValue<string>() ?? "",
|
||||
Amphur1 = workSheet?.Cells[row, 67]?.GetValue<string>() ?? "",
|
||||
Province1 = workSheet?.Cells[row, 68]?.GetValue<string>() ?? "",
|
||||
ZipCode1 = (workSheet?.Cells[row, 69]?.GetValue<string>() ?? "").Trim(),
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = job.UserId ?? "",
|
||||
CreatedFullName = job.FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = job.UserId ?? "",
|
||||
LastUpdateFullName = job.FullName ?? "System Administrator"
|
||||
});
|
||||
|
||||
// payment
|
||||
r.Payments.Add(new RecruitPayment()
|
||||
{
|
||||
PaymentId = workSheet?.Cells[row, 104]?.GetValue<string>() ?? "",
|
||||
CompanyCode = workSheet?.Cells[row, 105]?.GetValue<string>() ?? "",
|
||||
TextFile = workSheet?.Cells[row, 106]?.GetValue<string>() ?? "",
|
||||
BankCode = workSheet?.Cells[row, 107]?.GetValue<string>() ?? "",
|
||||
AccountNumber = workSheet?.Cells[row, 108]?.GetValue<string>() ?? "",
|
||||
TransDate = workSheet?.Cells[row, 109]?.GetValue<string>() ?? "",
|
||||
TransTime = workSheet?.Cells[row, 110]?.GetValue<string>() ?? "",
|
||||
CustomerName = workSheet?.Cells[row, 111]?.GetValue<string>() ?? "",
|
||||
RefNo1 = workSheet?.Cells[row, 112]?.GetValue<string>() ?? "",
|
||||
TermBranch = workSheet?.Cells[row, 113]?.GetValue<string>() ?? "",
|
||||
TellerId = workSheet?.Cells[row, 114]?.GetValue<string>() ?? "",
|
||||
CreditDebit = workSheet?.Cells[row, 115]?.GetValue<string>() ?? "",
|
||||
PaymentType = workSheet?.Cells[row, 116]?.GetValue<string>() ?? "",
|
||||
ChequeNo = workSheet?.Cells[row, 117]?.GetValue<string>() ?? "",
|
||||
Amount = (decimal)workSheet?.Cells[row, 118]?.GetValue<decimal>(),
|
||||
ChqueBankCode = workSheet?.Cells[row, 119]?.GetValue<string>() ?? "",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = job.UserId ?? "",
|
||||
CreatedFullName = job.FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = job.UserId ?? "",
|
||||
LastUpdateFullName = job.FullName ?? "System Administrator"
|
||||
});
|
||||
|
||||
r.RecruitImport = imported;
|
||||
_context.Recruits.Add(r);
|
||||
|
||||
row++;
|
||||
batchCount++;
|
||||
totalProcessed++;
|
||||
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
_context.SaveChanges();
|
||||
_context.ChangeTracker.Clear();
|
||||
_context.Entry(imported).State = EntityState.Unchanged;
|
||||
batchCount = 0;
|
||||
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ScoreFile
|
||||
|
||||
private async Task ProcessScoreFileAsync(ApplicationDbContext _context, MinIOService _minioService, RecruitService _recruitService, ImportJobInfo job)
|
||||
{
|
||||
var rec_import = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.ThenInclude(x => x.Scores)
|
||||
.Include(x => x.ImportHostories)
|
||||
.FirstOrDefaultAsync(x => x.Id == job.RecruitImportId);
|
||||
|
||||
if (rec_import == null) throw new Exception("RecruitImport not found");
|
||||
|
||||
if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null)
|
||||
{
|
||||
_context.RecruitScores.RemoveRange(rec_import.ScoreImport.Scores);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
rec_import.ImportHostories.Add(new RecruitImportHistory
|
||||
{
|
||||
Description = "นำเข้าข้อมูลผลคะแนนสอบ",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = job.UserId ?? "",
|
||||
CreatedFullName = job.FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = job.UserId ?? "",
|
||||
LastUpdateFullName = job.FullName ?? "System Administrator",
|
||||
});
|
||||
|
||||
// get doc from minio
|
||||
var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile));
|
||||
var imported = new ScoreImport
|
||||
{
|
||||
Year = rec_import.Year,
|
||||
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<RecruitScore>()
|
||||
};
|
||||
|
||||
// preload recruits
|
||||
var recruitsDict = await _context.Recruits
|
||||
.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++)
|
||||
{
|
||||
var workSheet = c_package.Workbook.Worksheets[i];
|
||||
var cols = workSheet.GetHeaderColumns();
|
||||
int row = 8;
|
||||
int batchCount = 0;
|
||||
const int batchSize = 100;
|
||||
int totalProcessed = 0;
|
||||
var endRow = workSheet.Dimension.End.Row;
|
||||
|
||||
while (row <= endRow)
|
||||
{
|
||||
var cell1 = workSheet?.Cells[row, 1]?.GetValue<string>();
|
||||
if (cell1 == "" || cell1 == null) break;
|
||||
|
||||
var r = new RecruitScore();
|
||||
r.ExamId = workSheet?.Cells[row, 2]?.GetValue<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(r.ExamId) && recruitsDict.TryGetValue(r.ExamId, out var recruit))
|
||||
{
|
||||
r.CitizenId = workSheet?.Cells[row, 3]?.GetValue<string>()?.Trim();
|
||||
r.FullA = 200;
|
||||
r.SumA = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 5]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 5].GetValue<double>(), 2);
|
||||
r.PercentageA = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 6]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 6].GetValue<double>(), 2);
|
||||
r.AStatus = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 7]?.GetValue<string>()) ? "" : workSheet?.Cells[row, 7]?.GetValue<string>();
|
||||
r.SumAB = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 5]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 5].GetValue<double>(), 2);
|
||||
r.ABStatus = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 7]?.GetValue<string>()) ? "" : workSheet?.Cells[row, 7]?.GetValue<string>();
|
||||
r.FullC = 50;
|
||||
r.SumC = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 8]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 8].GetValue<double>(), 2);
|
||||
r.FullD = 50;
|
||||
r.SumD = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 9]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 9].GetValue<double>(), 2);
|
||||
r.SumCD = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 10]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 10].GetValue<double>(), 2);
|
||||
r.PercentageC = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 11].GetValue<double>(), 2);
|
||||
r.CStatus = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 12]?.GetValue<string>()) ? "" : workSheet?.Cells[row, 12]?.GetValue<string>();
|
||||
r.FullScore = 300;
|
||||
r.TotalScore = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 13]?.GetValue<string>()) ? 0.00 : Math.Round(workSheet.Cells[row, 13].GetValue<double>(), 2);
|
||||
|
||||
var examStatusCol7 = workSheet?.Cells[row, 7]?.GetValue<string>()?.Trim();
|
||||
var examStatusCol14 = workSheet?.Cells[row, 14]?.GetValue<string>()?.Trim();
|
||||
r.ExamStatus =
|
||||
examStatusCol7 == "ขาดสอบ" ? "ขส." :
|
||||
examStatusCol14 == "ได้" ? "ผ่าน" :
|
||||
examStatusCol14 == "ตก" ? "ไม่ผ่าน" : "-";
|
||||
|
||||
r.RemarkScore = string.IsNullOrWhiteSpace(workSheet?.Cells[row, 15]?.GetValue<string>()) ? string.Empty : workSheet?.Cells[row, 15]?.GetValue<string>();
|
||||
|
||||
var examAttr = workSheet?.Cells[row, 16]?.GetValue<string>()?.Trim();
|
||||
r.ExamAttribute =
|
||||
examAttr == "ผ่าน" ? "มีคุณสมบัติ" :
|
||||
examAttr == "ไม่ผ่าน" ? "ไม่มีคุณสมบัติ" : "";
|
||||
|
||||
r.Major = workSheet.Name;
|
||||
r.CreatedAt = DateTime.Now;
|
||||
r.CreatedUserId = job.UserId ?? "";
|
||||
r.CreatedFullName = job.FullName ?? "System Administrator";
|
||||
r.LastUpdatedAt = DateTime.Now;
|
||||
r.LastUpdateUserId = job.UserId ?? "";
|
||||
r.LastUpdateFullName = job.FullName ?? "System Administrator";
|
||||
|
||||
imported.Scores.Add(r);
|
||||
}
|
||||
|
||||
row++;
|
||||
batchCount++;
|
||||
totalProcessed++;
|
||||
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
rec_import.ScoreImport = imported;
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
_context.Attach(rec_import);
|
||||
_context.Attach(imported);
|
||||
imported.Scores.Clear();
|
||||
batchCount = 0;
|
||||
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (imported.Scores.Count > 0)
|
||||
{
|
||||
rec_import.ScoreImport = imported;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
job.TotalCount = _tracker.GetJob(job.JobId)?.ProcessedCount ?? 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResultFile
|
||||
|
||||
private async Task ProcessResultFileAsync(ApplicationDbContext _context, RecruitService _recruitService, ImportJobInfo job)
|
||||
{
|
||||
var rec_import = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.ThenInclude(x => x.Scores)
|
||||
.Include(x => x.ImportHostories)
|
||||
.FirstOrDefaultAsync(x => x.Id == job.RecruitImportId);
|
||||
|
||||
if (rec_import == null) throw new Exception("RecruitImport not found");
|
||||
|
||||
// update old scores
|
||||
if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null)
|
||||
{
|
||||
var oldScores = rec_import.ScoreImport.Scores
|
||||
.Where(x => !string.IsNullOrEmpty(x.Number))
|
||||
.ToList();
|
||||
if (oldScores.Count > 0)
|
||||
{
|
||||
foreach (var x in oldScores)
|
||||
{
|
||||
x.Number = string.Empty;
|
||||
x.RemarkExamOrder = string.Empty;
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
rec_import.ImportHostories.Add(new RecruitImportHistory
|
||||
{
|
||||
Description = "นำเข้าข้อมูลผลการสอบ",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = job.UserId ?? "",
|
||||
CreatedFullName = job.FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = job.UserId ?? "",
|
||||
LastUpdateFullName = job.FullName ?? "System Administrator",
|
||||
});
|
||||
|
||||
// preload scores
|
||||
var score = rec_import.ScoreImport.Scores
|
||||
.Where(s => !string.IsNullOrEmpty(s.ExamId))
|
||||
.GroupBy(x => x.ExamId)
|
||||
.Where(g => g.Count() == 1)
|
||||
.Select(g => g.First())
|
||||
.ToDictionary(s => s.ExamId, s => s);
|
||||
|
||||
// Read from saved file (ResultFile uses stream from Form, but we saved to disk)
|
||||
using var stream = System.IO.File.OpenRead(job.ImportFile);
|
||||
using var c_package = new ExcelPackage(stream);
|
||||
|
||||
foreach (var workSheet in c_package.Workbook.Worksheets)
|
||||
{
|
||||
int row = 7;
|
||||
int batchCount = 0;
|
||||
const int batchSize = 100;
|
||||
var endRow = workSheet.Dimension.End.Row;
|
||||
|
||||
while (row <= endRow)
|
||||
{
|
||||
var examId = workSheet?.Cells[row, 2]?.GetValue<string>();
|
||||
if (string.IsNullOrWhiteSpace(examId))
|
||||
{
|
||||
row++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (score.TryGetValue(examId, out var existingScore))
|
||||
{
|
||||
existingScore.Number = workSheet?.Cells[row, 1]?.GetValue<string>();
|
||||
existingScore.RemarkExamOrder = workSheet?.Cells[row, 4]?.GetValue<string>() ?? string.Empty;
|
||||
existingScore.LastUpdatedAt = DateTime.Now;
|
||||
existingScore.LastUpdateUserId = job.UserId ?? "";
|
||||
existingScore.LastUpdateFullName = job.FullName ?? "System Administrator";
|
||||
batchCount++;
|
||||
}
|
||||
|
||||
row++;
|
||||
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
_context.Entry(rec_import).State = EntityState.Unchanged;
|
||||
batchCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
|
||||
private static int GetColumnIndex(string[] columns, string name, bool partial = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (partial)
|
||||
return Array.FindIndex(columns, x => x.Contains(name)) + 1;
|
||||
else
|
||||
return Array.FindIndex(columns, x => x == name) + 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
// Helper class to wrap a file path as IFormFile for MinIO upload
|
||||
internal class DummyFormFile : IFormFile
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly FileInfo _fileInfo;
|
||||
|
||||
public DummyFormFile(string filePath)
|
||||
{
|
||||
_filePath = filePath;
|
||||
_fileInfo = new FileInfo(filePath);
|
||||
}
|
||||
|
||||
public string ContentType => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
public string ContentDisposition => $"form-data; name=\"file\"; filename=\"{_fileInfo.Name}\"";
|
||||
public IHeaderDictionary Headers => new HeaderDictionary();
|
||||
public long Length => _fileInfo.Length;
|
||||
public string Name => "file";
|
||||
public string FileName => _fileInfo.Name;
|
||||
|
||||
public void CopyTo(Stream target)
|
||||
{
|
||||
using var stream = _fileInfo.OpenRead();
|
||||
stream.CopyTo(target);
|
||||
}
|
||||
|
||||
public async Task CopyToAsync(Stream target, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var stream = _fileInfo.OpenRead();
|
||||
await stream.CopyToAsync(target, cancellationToken);
|
||||
}
|
||||
|
||||
public Stream OpenReadStream()
|
||||
{
|
||||
return _fileInfo.OpenRead();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue