diff --git a/Controllers/DisableController.cs b/Controllers/DisableController.cs index a4ee784..f690778 100644 --- a/Controllers/DisableController.cs +++ b/Controllers/DisableController.cs @@ -1152,7 +1152,8 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers 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.DateOfBirth = Convert.ToDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.DateOfBirth)]?.GetValue().ToDateTime(DateTimeFormat.Ymd, "-"));k + r.DateOfBirth = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.DateOfBirth)]?.GetValue()) ? _disableService.CheckDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.DateOfBirth)]?.GetValue() ?? "", "yyyy-MM-dd") : DateTime.MinValue; r.Marry = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Marry)]?.GetValue(); r.Isspecial = "N"; r.CitizenCardIssuer = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.PersonalCardIssue)]?.GetValue(); @@ -1239,8 +1240,8 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers { 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, "-")), + IssueDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateIssueDate)]?.GetValue()) ? _disableService.CheckDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateIssueDate)]?.GetValue() ?? "", "yyyy-MM-dd") : DateTime.MinValue, + ExpiredDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateExpireDate)]?.GetValue()) ? _disableService.CheckDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CertificateExpireDate)]?.GetValue() ?? "", "yyyy-MM-dd") : DateTime.MinValue, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", CreatedFullName = FullName ?? "System Administrator", @@ -1259,7 +1260,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers 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, "-")), + BachelorDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BachelorDate)]?.GetValue()) ? _disableService.CheckDateTime(workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.BachelorDate)]?.GetValue() ?? "", "yyyy-MM-dd") : DateTime.MinValue, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", CreatedFullName = FullName ?? "System Administrator", diff --git a/Services/DisableService.cs b/Services/DisableService.cs index 3212dab..2b99e0d 100644 --- a/Services/DisableService.cs +++ b/Services/DisableService.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using BMA.EHR.Recurit.Exam.Service.Data; using System.Data; +using System.Globalization; namespace BMA.EHR.Recurit.Exam.Service.Services { @@ -63,5 +64,79 @@ namespace BMA.EHR.Recurit.Exam.Service.Services return valid; } + public DateTime CheckDateTime(string Date, string Formate) + { + // ตอนนี้ทำไว้ให้รองรับแค่ "dd/MM/yyyy", "yyyy-MM-dd" + + if (string.IsNullOrWhiteSpace(Date)) + return DateTime.MinValue; + + string[] parts = Date.Trim().Replace("-", "/").Split("/"); + + if (parts.Length != 3) + return DateTime.MinValue; + + int year; + int month; + int day; + switch (Formate) + { + case "dd/MM/yyyy": + if (int.TryParse(parts[2], out year) && year > 2500) + { + year -= 543; + } + else if (!int.TryParse(parts[2], out year)) + { + return DateTime.MinValue; + } + + if (!int.TryParse(parts[1], out month)) + return DateTime.MinValue; + + if (!int.TryParse(parts[0], out day)) + return DateTime.MinValue; + + break; + + case "yyyy-MM-dd": + if (int.TryParse(parts[0], out year) && year > 2500) + { + year -= 543; + } + else if (!int.TryParse(parts[0], out year)) + { + return DateTime.MinValue; + } + + if (!int.TryParse(parts[1], out month)) + return DateTime.MinValue; + + if (!int.TryParse(parts[2], out day)) + return DateTime.MinValue; + break; + + default: + return DateTime.MinValue; + } + + if (month < 1 || month > 12) + month = 1; + + int maxDay = DateTime.DaysInMonth(year, month); + + if (day < 1) + day = 1; + else if (day > maxDay) + day = maxDay; + + var normalDate = $"{day}/{(month >= 1 && month <= 9 ? $"0{month}" : month)}/{year}"; + if (DateTime.TryParseExact(normalDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedDate)) + { + return parsedDate; + } + + return DateTime.MinValue; + } } } \ No newline at end of file