fix
This commit is contained in:
parent
4dc8849b31
commit
cc251f7129
146 changed files with 2465 additions and 4785 deletions
|
|
@ -1,11 +1,12 @@
|
|||
// using BMA.EHR.Core;
|
||||
using BMA.EHR.Recruit.Service.Responses;
|
||||
using BMA.EHR.Recruit.Service.Services;
|
||||
|
||||
using BMA.EHR.Recruit.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using BMA.EHR.Recruit.Responses;
|
||||
|
||||
namespace BMA.EHR.Recruit.Service.Controllers
|
||||
namespace BMA.EHR.Recruit.Controllers
|
||||
{
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
using Amazon.S3.Model;
|
||||
using BMA.EHR.MetaData.Service.Models;
|
||||
using BMA.EHR.Profile.Service.Models.HR;
|
||||
using BMA.EHR.Recruit.Service.Core;
|
||||
using BMA.EHR.Recruit.Service.Data;
|
||||
using BMA.EHR.Recruit.Service.Extensions;
|
||||
using BMA.EHR.Recruit.Service.Models.Recruits;
|
||||
using BMA.EHR.Recruit.Service.Requests.Recruits;
|
||||
using BMA.EHR.Recruit.Service.Responses;
|
||||
using BMA.EHR.Recruit.Service.Services;
|
||||
using BMA.EHR.Recurit.Service.Data;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
|
@ -31,8 +24,15 @@ using System.Globalization;
|
|||
using OfficeOpenXml.Style;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using BMA.EHR.Recruit.Core;
|
||||
using BMA.EHR.Recruit.Data;
|
||||
using BMA.EHR.Recruit.Extensions;
|
||||
using BMA.EHR.Recruit.Models.Recruits;
|
||||
using BMA.EHR.Recruit.Requests.Recruits;
|
||||
using BMA.EHR.Recruit.Responses;
|
||||
using BMA.EHR.Recruit.Services;
|
||||
|
||||
namespace BMA.EHR.Recruit.Service.Controllers
|
||||
namespace BMA.EHR.Recruit.Controllers
|
||||
{
|
||||
[Route("api/v{version:apiVersion}/recruit")]
|
||||
[ApiVersion("1.0")]
|
||||
|
|
@ -53,6 +53,8 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ILogger<RecruitController> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ImportJobQueue _importJobQueue;
|
||||
private readonly ImportJobTracker _importJobTracker;
|
||||
//private readonly DateTimeExtension;
|
||||
|
||||
#endregion
|
||||
|
|
@ -67,7 +69,9 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
IHttpContextAccessor httpContextAccessor,
|
||||
ILogger<RecruitController> logger,
|
||||
IConfiguration configuration,
|
||||
PermissionRepository permission
|
||||
PermissionRepository permission,
|
||||
ImportJobQueue importJobQueue,
|
||||
ImportJobTracker importJobTracker
|
||||
//DateTimeExtension extensions
|
||||
)
|
||||
{
|
||||
|
|
@ -80,6 +84,8 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_permission = permission;
|
||||
_importJobQueue = importJobQueue;
|
||||
_importJobTracker = importJobTracker;
|
||||
//_extensions = extensions;
|
||||
}
|
||||
|
||||
|
|
@ -699,7 +705,6 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
Directory.CreateDirectory(tmpDir);
|
||||
|
||||
var importFile = Path.Combine(tmpDir, $"c_{DateTime.Now.ToString("ddMMyyyyHHmmss")}.xlsx");
|
||||
var import_doc_id = "";
|
||||
|
||||
try
|
||||
{
|
||||
|
|
@ -710,11 +715,13 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
|
||||
var file = Request.Form.Files[0];
|
||||
var doc = await _minioService.UploadFileAsync(file);
|
||||
import_doc_id = doc.Id.ToString("D");
|
||||
var import_doc_id = doc.Id.ToString("D");
|
||||
|
||||
var fileContent = (await _minioService.DownloadFileAsync(doc.Id)).FileContent;
|
||||
System.IO.File.WriteAllBytes(importFile, fileContent);
|
||||
fileContent = null;
|
||||
|
||||
// สร้างรอบการบรรจุ โดยเอาเข้ามูลมาใส่จาก Request
|
||||
// สร้างรอบการบรรจุ
|
||||
var imported = new RecruitImport
|
||||
{
|
||||
Year = req.Year,
|
||||
|
|
@ -737,164 +744,27 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
};
|
||||
|
||||
await _context.RecruitImports.AddAsync(imported);
|
||||
await _context.SaveChangesAsync(); // Save import first to get Id
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// import datafile
|
||||
System.IO.File.WriteAllBytes(importFile, fileContent);
|
||||
|
||||
using (var c_package = new ExcelPackage(new FileInfo(importFile)))
|
||||
// Enqueue background job
|
||||
var job = _importJobTracker.CreateJob(new ImportJobInfo
|
||||
{
|
||||
// loop from sheet2 to end
|
||||
|
||||
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 = 500;
|
||||
|
||||
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);
|
||||
|
||||
//imported.Recruits.Add(r);
|
||||
row++;
|
||||
batchCount++;
|
||||
|
||||
// Batch save to prevent OutOfMemoryException on large imports
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
_context.SaveChanges();
|
||||
_context.ChangeTracker.Clear();
|
||||
// Re-attach the import entity as Unchanged after clearing the tracker
|
||||
_context.Entry(imported).State = EntityState.Unchanged;
|
||||
batchCount = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save remaining records in the last batch
|
||||
_context.SaveChanges();
|
||||
|
||||
return Success();
|
||||
JobType = ImportJobType.CandidateFile,
|
||||
ImportFile = importFile,
|
||||
RecruitImportId = imported.Id,
|
||||
ImportDocId = import_doc_id,
|
||||
UserId = UserId,
|
||||
FullName = FullName,
|
||||
Request = req,
|
||||
});
|
||||
await _importJobQueue.EnqueueAsync(job);
|
||||
|
||||
return Success(new { jobId = job.JobId, status = "queued" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _minioService.DeleteFileAsync(Guid.Parse(import_doc_id));
|
||||
return Error(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (System.IO.File.Exists(importFile))
|
||||
System.IO.File.Delete(importFile);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -1033,7 +903,6 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
Directory.CreateDirectory(tmpDir);
|
||||
|
||||
var importFile = Path.Combine(tmpDir, $"c_{DateTime.Now.ToString("ddMMyyyyHHmmss")}.xlsx");
|
||||
var import_doc_id = "";
|
||||
|
||||
try
|
||||
{
|
||||
|
|
@ -1046,16 +915,6 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
.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)
|
||||
.Include(x => x.Recruits)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (imported == null)
|
||||
|
|
@ -1063,225 +922,36 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
|
||||
if (imported.Recruits != null)
|
||||
{
|
||||
// remove old score data
|
||||
_context.Recruits.RemoveRange(imported.Recruits);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
var file = Request.Form.Files[0];
|
||||
var doc = await _minioService.UploadFileAsync(file);
|
||||
import_doc_id = doc.Id.ToString("D");
|
||||
var import_doc_id = doc.Id.ToString("D");
|
||||
|
||||
var fileContent = (await _minioService.DownloadFileAsync(doc.Id)).FileContent;
|
||||
|
||||
// สร้างรอบการบรรจุ โดยเอาเข้ามูลมาใส่จาก Request
|
||||
imported.ImportHostories.Add(new RecruitImportHistory
|
||||
{
|
||||
Description = "นำเข้าข้อมูลผู้สมัครสอบแข่งขัน",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = FullName ?? "System Administrator",
|
||||
});
|
||||
|
||||
//await _context.RecruitImports.AddAsync(imported);
|
||||
|
||||
// import datafile
|
||||
System.IO.File.WriteAllBytes(importFile, fileContent);
|
||||
fileContent = null;
|
||||
|
||||
using (var c_package = new ExcelPackage(new FileInfo(importFile)))
|
||||
// Enqueue background job
|
||||
var job = _importJobTracker.CreateJob(new ImportJobInfo
|
||||
{
|
||||
// loop from sheet2 to end
|
||||
|
||||
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 = 500;
|
||||
|
||||
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 = UserId ?? "";
|
||||
r.CreatedFullName = FullName ?? "System Administrator";
|
||||
r.LastUpdatedAt = DateTime.Now;
|
||||
r.LastUpdateUserId = UserId ?? "";
|
||||
r.LastUpdateFullName = 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 = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = 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 = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = 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 = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = 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 = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = FullName ?? "System Administrator"
|
||||
});
|
||||
/*Comment ข้อมูลใบประกอบวิชาชีพ เพราะในไฟล์นำเข้ายังไม่มีคอลัมน์ที่ระบุข้อมูลส่วนนี้*/
|
||||
//// certificate
|
||||
//r.Certificates.Add(new RecruitCertificate()
|
||||
//{
|
||||
// CertificateNo = workSheet?.Cells[row, 9999]?.GetValue<string>() ?? "",
|
||||
// Description = workSheet?.Cells[row, 9999]?.GetValue<string>() ?? "",
|
||||
// IssueDate = Convert.ToDateTime(workSheet?.Cells[row, 9999]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-")),
|
||||
// ExpiredDate = Convert.ToDateTime(workSheet?.Cells[row, 9999]?.GetValue<string>().ToDateTime(DateTimeFormat.Ymd, "-")),
|
||||
// CreatedAt = DateTime.Now,
|
||||
// CreatedUserId = UserId ?? "",
|
||||
// CreatedFullName = FullName ?? "System Administrator",
|
||||
// LastUpdatedAt = DateTime.Now,
|
||||
// LastUpdateUserId = UserId ?? "",
|
||||
// LastUpdateFullName = FullName ?? "System Administrator"
|
||||
//});
|
||||
|
||||
r.RecruitImport = imported;
|
||||
_context.Recruits.Add(r);
|
||||
|
||||
//imported.Recruits.Add(r);
|
||||
row++;
|
||||
batchCount++;
|
||||
|
||||
// Batch save to prevent OutOfMemoryException on large imports
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
_context.SaveChanges();
|
||||
_context.ChangeTracker.Clear();
|
||||
// Re-attach the import entity as Unchanged after clearing the tracker
|
||||
_context.Entry(imported).State = EntityState.Unchanged;
|
||||
batchCount = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save remaining records in the last batch
|
||||
_context.SaveChanges();
|
||||
|
||||
return Success();
|
||||
JobType = ImportJobType.CandidateFileById,
|
||||
ImportFile = importFile,
|
||||
RecruitImportId = id,
|
||||
ImportDocId = import_doc_id,
|
||||
UserId = UserId,
|
||||
FullName = FullName,
|
||||
});
|
||||
await _importJobQueue.EnqueueAsync(job);
|
||||
|
||||
return Success(new { jobId = job.JobId, status = "queued" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _minioService.DeleteFileAsync(Guid.Parse(import_doc_id));
|
||||
return Error(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (System.IO.File.Exists(importFile))
|
||||
System.IO.File.Delete(importFile);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
@ -1324,209 +994,34 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
}
|
||||
|
||||
var rec_import = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.ThenInclude(x => x.Scores)
|
||||
.Include(x => x.ImportHostories)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (rec_import == null)
|
||||
return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound);
|
||||
|
||||
if (rec_import.ScoreImport != null)
|
||||
{
|
||||
// remove old score data
|
||||
if (rec_import.ScoreImport.Scores != null)
|
||||
{
|
||||
_context.RecruitScores.RemoveRange(rec_import.ScoreImport.Scores);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
var file = Request.Form.Files[0];
|
||||
var doc = await _minioService.UploadFileAsync(file);
|
||||
import_doc_id = doc.Id.ToString("D");
|
||||
|
||||
Console.WriteLine($"file id = {import_doc_id}");
|
||||
|
||||
var fileContent = (await _minioService.DownloadFileAsync(doc.Id)).FileContent;
|
||||
|
||||
// create import history
|
||||
rec_import.ImportHostories.Add(new RecruitImportHistory
|
||||
using (var stream = new FileStream(importFile, FileMode.Create))
|
||||
{
|
||||
Description = "นำเข้าข้อมูลผลคะแนนสอบ",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = FullName ?? "System Administrator",
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
var job = _importJobTracker.CreateJob(new ImportJobInfo
|
||||
{
|
||||
JobType = ImportJobType.ScoreFile,
|
||||
ImportFile = importFile,
|
||||
RecruitImportId = id,
|
||||
ImportDocId = import_doc_id,
|
||||
UserId = UserId,
|
||||
FullName = FullName,
|
||||
});
|
||||
await _importJobQueue.EnqueueAsync(job);
|
||||
|
||||
// create new file import
|
||||
var imported = new ScoreImport
|
||||
{
|
||||
Year = rec_import.Year,
|
||||
ImportFile = doc,
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = FullName ?? "System Administrator",
|
||||
Scores = new List<RecruitScore>()
|
||||
};
|
||||
|
||||
// preload recruits ทั้งหมดของครั้งเดียว และ Group ExamId เอาเฉพาะ key ที่ไม่ซ้ำ
|
||||
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);
|
||||
|
||||
// import datafile
|
||||
System.IO.File.WriteAllBytes(importFile, fileContent);
|
||||
|
||||
using (var c_package = new ExcelPackage(new FileInfo(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 = 8;
|
||||
int batchCount = 0;
|
||||
const int batchSize = 500;
|
||||
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();
|
||||
|
||||
#region นำเข้าผลคะแนนสอบแข่งขัน old
|
||||
|
||||
//r.ExamId = workSheet?.Cells[row, 2]?.GetValue<string>();
|
||||
|
||||
//r.FullA = (int)workSheet?.Cells[7, 7]?.GetValue<string>().Replace("(", "").Replace(")", "").Replace("คะแนน", "").Trim().ToInteger();
|
||||
//r.SumA = workSheet?.Cells[row, 7]?.GetValue<string>() == "ขส." ? 0.00 : (double)workSheet?.Cells[row, 7]?.GetValue<double>();
|
||||
//r.PercentageA = workSheet?.Cells[row, 8]?.GetValue<string>() == "ขส." ? 0.0 : (double)workSheet?.Cells[row, 8]?.GetValue<double>();
|
||||
//r.AStatus = workSheet?.Cells[row, 9]?.GetValue<string>();
|
||||
|
||||
//r.FullB = (int)workSheet?.Cells[7, 12]?.GetValue<string>().Replace("(", "").Replace(")", "").Replace("คะแนน", "").Trim().ToInteger();
|
||||
//r.SumB = workSheet?.Cells[row, 12]?.GetValue<string>() == "ขส." ? 0.00 : (double)workSheet?.Cells[row, 12]?.GetValue<double>();
|
||||
//r.PercentageB = workSheet?.Cells[row, 13]?.GetValue<string>() == "ขส." ? 0.0 : (double)workSheet?.Cells[row, 13]?.GetValue<double>();
|
||||
//r.BStatus = workSheet?.Cells[row, 14]?.GetValue<string>();
|
||||
|
||||
//r.SumAB = workSheet?.Cells[row, 15]?.GetValue<string>() == "ขส." ? 0.00 : (double)workSheet?.Cells[row, 15]?.GetValue<double>();
|
||||
//r.ABStatus = workSheet?.Cells[row, 17]?.GetValue<string>();
|
||||
|
||||
//r.FullC = (int)workSheet?.Cells[7, 20]?.GetValue<string>().Replace("(", "").Replace(")", "").Replace("คะแนน", "").Trim().ToInteger();
|
||||
//r.SumC = workSheet?.Cells[row, 20]?.GetValue<string>() == "ขส." ? 0.00 : (double)workSheet?.Cells[row, 20]?.GetValue<double>();
|
||||
//r.PercentageC = workSheet?.Cells[row, 21]?.GetValue<string>() == "ขส." ? 0.0 : (double)workSheet?.Cells[row, 21]?.GetValue<double>();
|
||||
//r.CStatus = workSheet?.Cells[row, 22]?.GetValue<string>();
|
||||
|
||||
//r.ExamStatus = workSheet?.Cells[row, 24]?.GetValue<string>();
|
||||
//r.Number = workSheet?.Cells[row, 25]?.GetValue<string>() ?? ""; // ตัดนำเข้า ลำดับที่สอบได้ เพราะจะแยกเป็นอีกส่วน #1787
|
||||
|
||||
//r.Major = workSheet.Name;
|
||||
|
||||
#endregion
|
||||
|
||||
r.ExamId = workSheet?.Cells[row, 2]?.GetValue<string>();
|
||||
|
||||
// ใช้ dictionary lookup แทน query DB ทีละรอบ
|
||||
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 = UserId ?? "";
|
||||
r.CreatedFullName = FullName ?? "System Administrator";
|
||||
r.LastUpdatedAt = DateTime.Now;
|
||||
r.LastUpdateUserId = UserId ?? "";
|
||||
r.LastUpdateFullName = FullName ?? "System Administrator";
|
||||
|
||||
imported.Scores.Add(r);
|
||||
}
|
||||
|
||||
row++;
|
||||
batchCount++;
|
||||
|
||||
// Batch save to prevent OutOfMemoryException on large imports
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
rec_import.ScoreImport = imported;
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
// Re-attach entities after clearing the tracker
|
||||
_context.Attach(rec_import);
|
||||
_context.Attach(imported);
|
||||
imported.Scores.Clear();
|
||||
batchCount = 0;
|
||||
}
|
||||
|
||||
} // end of sheet loop
|
||||
} // end of all file loop
|
||||
}
|
||||
|
||||
// Save remaining records in the last batch
|
||||
if (imported.Scores.Count > 0)
|
||||
{
|
||||
rec_import.ScoreImport = imported;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return Success();
|
||||
|
||||
return Success(new { jobId = job.JobId, status = "queued" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//await _minioService.DeleteFileAsync(Guid.Parse(import_doc_id));
|
||||
return Error(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (System.IO.File.Exists(importFile))
|
||||
System.IO.File.Delete(importFile);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
@ -1555,6 +1050,12 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
||||
}
|
||||
|
||||
var tmpDir = Path.Combine(_webHostEnvironment.ContentRootPath, "tmp");
|
||||
if (!Directory.Exists(tmpDir))
|
||||
Directory.CreateDirectory(tmpDir);
|
||||
|
||||
var importFile = Path.Combine(tmpDir, $"r_{DateTime.Now.ToString("ddMMyyyyHHmmss")}.xlsx");
|
||||
|
||||
try
|
||||
{
|
||||
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
|
||||
|
|
@ -1563,103 +1064,28 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
}
|
||||
|
||||
var rec_import = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.ThenInclude(x => x.Scores)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (rec_import == null)
|
||||
return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound);
|
||||
|
||||
// update ฟิลด์ลำดับที่สอบได้และหมายเหตุจากลำดับที่สอบได้อันเก่าก่อน
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// create import history
|
||||
rec_import.ImportHostories.Add(new RecruitImportHistory
|
||||
{
|
||||
Description = "นำเข้าข้อมูลผลการสอบ",
|
||||
CreatedAt = DateTime.Now,
|
||||
CreatedUserId = UserId ?? "",
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
LastUpdatedAt = DateTime.Now,
|
||||
LastUpdateUserId = UserId ?? "",
|
||||
LastUpdateFullName = FullName ?? "System Administrator",
|
||||
});
|
||||
|
||||
// preload score
|
||||
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);
|
||||
|
||||
// ถ้าไม่มีผลคะแนนสอบแข่งขันให้จบการทำงาน
|
||||
if (score.Count == 0) return Success();
|
||||
|
||||
var file = Request.Form.Files[0];
|
||||
using (var stream = file.OpenReadStream())
|
||||
using (var c_package = new ExcelPackage(stream))
|
||||
using (var stream = new FileStream(importFile, FileMode.Create))
|
||||
{
|
||||
foreach (var workSheet in c_package.Workbook.Worksheets)
|
||||
{
|
||||
//var totalRows = workSheet.Dimension.Rows;
|
||||
int row = 7; // เริ่มที่ row 7 ตามตัวอย่างไฟล์
|
||||
int batchCount = 0;
|
||||
const int batchSize = 500;
|
||||
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 = UserId ?? "";
|
||||
existingScore.LastUpdateFullName = FullName ?? "System Administrator";
|
||||
batchCount++;
|
||||
}
|
||||
|
||||
row++;
|
||||
|
||||
// Batch save to prevent OutOfMemoryException on large imports
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
// Re-attach the import entity as Unchanged after clearing the tracker
|
||||
_context.Entry(rec_import).State = EntityState.Unchanged;
|
||||
batchCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
// Save remaining records in the last batch
|
||||
await _context.SaveChangesAsync();
|
||||
return Success();
|
||||
var job = _importJobTracker.CreateJob(new ImportJobInfo
|
||||
{
|
||||
JobType = ImportJobType.ResultFile,
|
||||
ImportFile = importFile,
|
||||
RecruitImportId = id,
|
||||
UserId = UserId,
|
||||
FullName = FullName,
|
||||
});
|
||||
await _importJobQueue.EnqueueAsync(job);
|
||||
|
||||
return Success(new { jobId = job.JobId, status = "queued" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -1669,6 +1095,37 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
|
||||
#endregion
|
||||
|
||||
#region " Import Status "
|
||||
|
||||
/// <summary>
|
||||
/// ตรวจสอบสถานะการนำเข้าข้อมูล
|
||||
/// </summary>
|
||||
/// <param name="jobId">รหัสงานนำเข้า</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("import/status/{jobId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public ActionResult<ResponseObject> GetImportStatus(string jobId)
|
||||
{
|
||||
var job = _importJobTracker.GetJob(jobId);
|
||||
if (job == null)
|
||||
return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound);
|
||||
|
||||
return Success(new
|
||||
{
|
||||
jobId = job.JobId,
|
||||
jobType = job.JobType.ToString(),
|
||||
status = job.Status.ToString(),
|
||||
processedCount = job.ProcessedCount,
|
||||
totalCount = job.TotalCount,
|
||||
errorMessage = job.ErrorMessage,
|
||||
createdAt = job.CreatedAt,
|
||||
completedAt = job.CompletedAt,
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Exam Information "
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue