diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs index b0ce7d0..48bd6a7 100644 --- a/Controllers/RecruitController.cs +++ b/Controllers/RecruitController.cs @@ -1081,7 +1081,8 @@ namespace BMA.EHR.Recruit.Service.Controllers r.National = workSheet?.Cells[row, 9]?.GetValue() ?? ""; r.Race = workSheet?.Cells[row, 9999]?.GetValue() ?? ""; r.Religion = workSheet?.Cells[row, 10]?.GetValue() ?? ""; - r.DateOfBirth = DateTime.TryParseExact(workSheet?.Cells[row, 11]?.GetValue()?.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dob) && dob.Year > 2500 ? dob.AddYears(-543) : DateTime.MinValue; + //r.DateOfBirth = DateTime.TryParseExact(workSheet?.Cells[row, 11]?.GetValue()?.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dob) && dob.Year > 2500 ? dob.AddYears(-543) : DateTime.MinValue; + r.DateOfBirth = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 11]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 11]?.GetValue() ?? "", "dd/MM/yyyy") : DateTime.MinValue; r.Marry = workSheet?.Cells[row, 9999]?.GetValue() ?? ""; r.Isspecial = "N"; r.CitizenCardIssuer = workSheet?.Cells[row, 9999]?.GetValue() ?? ""; @@ -1108,7 +1109,7 @@ namespace BMA.EHR.Recruit.Service.Controllers GPA = (double)workSheet?.Cells[row, 26]?.GetValue(), Specialist = workSheet?.Cells[row, 9999]?.GetValue() ?? "", HighDegree = workSheet?.Cells[row, 27]?.GetValue() ?? "", - BachelorDate = DateTime.TryParseExact(workSheet?.Cells[row, 25]?.GetValue()?.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var bachelorDob) && bachelorDob.Year > 2500 ? bachelorDob.AddYears(-543) : DateTime.MinValue, + BachelorDate = !string.IsNullOrWhiteSpace(workSheet?.Cells[row, 25]?.GetValue()) ? _recruitService.CheckDateTime(workSheet?.Cells[row, 11]?.GetValue() ?? "", "dd/MM/yyyy") : DateTime.MinValue, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", CreatedFullName = FullName ?? "System Administrator", @@ -1494,7 +1495,8 @@ namespace BMA.EHR.Recruit.Service.Controllers majorgroup = dr["majorgroup"].ToString(), certificateNo = dr["certificateno"].ToString(), certificateIssueDate = dr["certificateIssueDate"] == null || Convert.ToDateTime(dr["certificateIssueDate"]) == DateTime.MinValue ? "" : Convert.ToDateTime(dr["certificateIssueDate"]).ToThaiShortDate(), - ExamScore = dr["score"] == null ? 0 : dr["score"].ToString().ToInteger(), + //ExamScore = dr["score"] == null ? 0 : dr["score"].ToString().ToInteger(), + ExamScore = string.IsNullOrWhiteSpace(dr["score"]?.ToString()) ? 0 : decimal.Parse(dr["score"]?.ToString()), ExamResult = dr["result"].ToString(), ExamAttribute = dr["examAttribute"].ToString(), Remark = dr["remark"].ToString(), diff --git a/Services/RecruitService.cs b/Services/RecruitService.cs index e66648a..76f3119 100644 --- a/Services/RecruitService.cs +++ b/Services/RecruitService.cs @@ -12,6 +12,7 @@ using BMA.EHR.Recurit.Service.Data; using System.Security.Claims; using System.Net.Http.Headers; using Newtonsoft.Json; +using System.Globalization; namespace BMA.EHR.Recruit.Service.Services { @@ -388,6 +389,79 @@ namespace BMA.EHR.Recruit.Service.Services throw; } } + 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