diff --git a/.gitignore b/.gitignore index a479b56..3b4a194 100644 --- a/.gitignore +++ b/.gitignore @@ -475,3 +475,5 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk + +.claude \ No newline at end of file diff --git a/Controllers/DisableController.cs b/Controllers/DisableController.cs index c982f03..1bdf8f5 100644 --- a/Controllers/DisableController.cs +++ b/Controllers/DisableController.cs @@ -2178,90 +2178,177 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers { try { - var data = await _context.Disables.AsQueryable() + #region old + //var data = await _context.Disables.AsQueryable() + // .Include(x => x.PeriodExam) + // .Include(x => x.Documents) + // .ThenInclude(x => x.DocumentFile) + // .Where(x => x.PeriodExam.Id == id) + // .Where(x => x.PeriodExam.CheckDisability == true) + // .Where(x => x.ExamId == examId) + // .Join(_context.DisableScores.AsQueryable() + // .Include(x => x.ScoreImport), + // rc => new { PeriodExamId = rc.PeriodExam.Id, rc.ExamId }, + // sc => new { PeriodExamId = sc.ScoreImport.PeriodExamId, sc.ExamId }, + // (p, sr) => new + // { + // ExamID = p.ExamId, + // ProfileID = p.CitizenId, + // p.Prefix, + // FullName = $"{p.FirstName} {p.LastName}", + // DateOfBirth = p.DateOfBirth.HasValue && p.DateOfBirth.Value != DateTime.MinValue + // ? p.DateOfBirth.Value.ToThaiShortDate() + // : "", + // Gender = p.Gendor, + // Degree = p.Educations.First().Degree, + // Major = p.Educations.First().Major, + // CertificateNo = p.Certificates.Count > 0 + // ? p.Certificates.First().CertificateNo ?? "" + // : "", + // CertificateIssueDate = p.Certificates.Count > 0 + // ? p.Certificates.First().IssueDate != DateTime.MinValue + // ? p.Certificates.First().IssueDate.ToThaiShortDate() + // : "" + // : "", + // ExamResult = sr == null ? "" : sr.ExamStatus, + // ExamAttribute = sr != null && !string.IsNullOrEmpty(sr.ExamAttribute) && + // (sr.ExamAttribute == "มีคุณสมบัติ" || sr.ExamAttribute == "ไม่มีคุณสมบัติ") + // ? sr.ExamAttribute : "", + // IsSpecial = p.Isspecial, + // Remark = p.Remark, + // University = p.Educations.First().University, + // PositionName = p.PositionName, + // PositionType = p.PositionType, + // PositionLevel = p.PositionLevel, + // ExamName = p.PeriodExam!.Name, + // ExamOrder = p.PeriodExam.Round, + // ExamYear = p.PeriodExam.Year == null ? 0 : p.PeriodExam.Year.Value.ToThaiYear(), + // Score = sr == null ? 0.0 : sr.TotalScore, + // Number = sr == null ? "" : sr.Number, + // ExamCount = _disableService.GetExamCount(p.CitizenId), + // ScoreExpire = p.PeriodExam.AnnouncementDate == null + // ? "" + // : p.PeriodExam.AnnouncementDate != DateTime.MinValue + // ? p.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate() + // : "", + // typeTest = p.typeTest, + // ScoreResult = sr == null ? null : new + // { + // ScoreAFull = sr.FullA, + // ScoreA = sr.SumA, + // ScoreBFull = sr.FullB, + // ScoreB = sr.SumB, + // ScoreAB = sr.SumAB, + // ScoreCFull = sr.FullC, + // ScoreC = sr.SumC, + // ScoreDFull = sr.FullD, + // ScoreD = sr.SumD, + // ScoreCD = sr.SumCD, + // ScoreSumFull = sr.FullScore, + // ScoreSum = sr.TotalScore, + // ExamResult = sr.ExamStatus + // }, + // Attachments = p.Documents.Select(a => new + // { + // FileName = a.DocumentFile.FileName, + // DocumentId = a.DocumentFile.Id + // }) + // }) + // .FirstOrDefaultAsync(); + #endregion + + // 1. Query Disable (ข้อมูลผู้สอบ) + var disable = await _context.Disables.AsQueryable() .Include(x => x.PeriodExam) .Include(x => x.Documents) .ThenInclude(x => x.DocumentFile) + .Include(x => x.Educations) + .Include(x => x.Certificates) + .Where(x => x.PeriodExam != null) .Where(x => x.PeriodExam.Id == id) .Where(x => x.PeriodExam.CheckDisability == true) .Where(x => x.ExamId == examId) - .Join(_context.DisableScores.AsQueryable() - .Include(x => x.ScoreImport), - rc => new { PeriodExamId = rc.PeriodExam.Id, rc.ExamId }, - sc => new { PeriodExamId = sc.ScoreImport.PeriodExamId, sc.ExamId }, - (p, sr) => new - { - ExamID = p.ExamId, - ProfileID = p.CitizenId, - p.Prefix, - FullName = $"{p.FirstName} {p.LastName}", - DateOfBirth = p.DateOfBirth.HasValue && p.DateOfBirth.Value != DateTime.MinValue - ? p.DateOfBirth.Value.ToThaiShortDate() - : "", - Gender = p.Gendor, - Degree = p.Educations.First().Degree, - Major = p.Educations.First().Major, - CertificateNo = p.Certificates.Count > 0 - ? p.Certificates.First().CertificateNo ?? "" - : "", - CertificateIssueDate = p.Certificates.Count > 0 - ? p.Certificates.First().IssueDate != DateTime.MinValue - ? p.Certificates.First().IssueDate.ToThaiShortDate() - : "" - : "", - ExamResult = sr == null ? "" : sr.ExamStatus, - // ExamAttribute = p.Certificates.Count > 0 ? - // _disableService.CheckValidCertificate(p.Certificates.First().IssueDate, 5) - // ? "มีคุณสมบัติ" - // : "ไม่มีคุณสมบัติ" - // : "ไม่มีคุณสมบัติ", - - ExamAttribute = sr != null && !string.IsNullOrEmpty(sr.ExamAttribute) && - (sr.ExamAttribute == "มีคุณสมบัติ" || sr.ExamAttribute == "ไม่มีคุณสมบัติ") - ? sr.ExamAttribute : "", - - IsSpecial = p.Isspecial, - Remark = p.Remark, - University = p.Educations.First().University, - PositionName = p.PositionName, - PositionType = p.PositionType, - PositionLevel = p.PositionLevel, - ExamName = p.PeriodExam!.Name, - ExamOrder = p.PeriodExam.Round, - ExamYear = p.PeriodExam.Year == null ? 0 : p.PeriodExam.Year.Value.ToThaiYear(), - Score = sr == null ? 0.0 : sr.TotalScore, - Number = sr == null ? "" : sr.Number, - ExamCount = _disableService.GetExamCount(p.CitizenId), - ScoreExpire = p.PeriodExam.AnnouncementDate == null - ? "" - : p.PeriodExam.AnnouncementDate != DateTime.MinValue - ? p.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate() - : "", - typeTest = p.typeTest, - ScoreResult = sr == null ? null : new - { - ScoreAFull = sr.FullA, - ScoreA = sr.SumA, - ScoreBFull = sr.FullB, - ScoreB = sr.SumB, - ScoreAB = sr.SumAB, - ScoreCFull = sr.FullC, - ScoreC = sr.SumC, - ScoreDFull = sr.FullD, - ScoreD = sr.SumD, - ScoreCD = sr.SumCD, - ScoreSumFull = sr.FullScore, - ScoreSum = sr.TotalScore, - ExamResult = sr.ExamStatus - }, - Attachments = p.Documents.Select(a => new - { - FileName = a.DocumentFile.FileName, - DocumentId = a.DocumentFile.Id - }) - }) .FirstOrDefaultAsync(); + if (disable == null) + return Success(); + + // 2. Query DisableScore (ข้อมูลคะแนน) — แยกต่างหาก ไม่มีก็ผ่าน + var score = await _context.DisableScores.AsQueryable() + .Include(x => x.ScoreImport) + .Where(x => x.ScoreImport != null) + .Where(x => x.ScoreImport.PeriodExamId == id) + .Where(x => x.ExamId == examId) + .FirstOrDefaultAsync(); + + // 3. รวมผลลัพธ์ก่อน return + var data = new + { + ExamID = disable.ExamId, + ProfileID = disable.CitizenId, + disable.Prefix, + FullName = $"{disable.FirstName} {disable.LastName}", + DateOfBirth = disable.DateOfBirth.HasValue && disable.DateOfBirth.Value != DateTime.MinValue + ? disable.DateOfBirth.Value.ToThaiShortDate() + : "", + Gender = disable.Gendor, + Degree = disable.Educations.First().Degree, + Major = disable.Educations.First().Major, + CertificateNo = disable.Certificates.Count > 0 + ? disable.Certificates.First().CertificateNo ?? "" + : "", + CertificateIssueDate = disable.Certificates.Count > 0 + ? disable.Certificates.First().IssueDate != DateTime.MinValue + ? disable.Certificates.First().IssueDate.ToThaiShortDate() + : "" + : "", + ExamResult = score == null ? "" : score.ExamStatus, + + ExamAttribute = score != null && !string.IsNullOrEmpty(score.ExamAttribute) && + (score.ExamAttribute == "มีคุณสมบัติ" || score.ExamAttribute == "ไม่มีคุณสมบัติ") + ? score.ExamAttribute : "", + + IsSpecial = disable.Isspecial, + Remark = disable.Remark, + University = disable.Educations.First().University, + PositionName = disable.PositionName, + PositionType = disable.PositionType, + PositionLevel = disable.PositionLevel, + ExamName = disable.PeriodExam!.Name, + ExamOrder = disable.PeriodExam.Round, + ExamYear = disable.PeriodExam.Year == null ? 0 : disable.PeriodExam.Year.Value.ToThaiYear(), + Score = score == null ? 0.0 : score.TotalScore, + Number = score == null ? "" : score.Number, + ExamCount = _disableService.GetExamCount(disable.CitizenId), + ScoreExpire = disable.PeriodExam.AnnouncementDate == null + ? "" + : disable.PeriodExam.AnnouncementDate != DateTime.MinValue + ? disable.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate() + : "", + typeTest = disable.typeTest, + ScoreResult = score == null ? null : new + { + ScoreAFull = score.FullA, + ScoreA = score.SumA, + ScoreBFull = score.FullB, + ScoreB = score.SumB, + ScoreAB = score.SumAB, + ScoreCFull = score.FullC, + ScoreC = score.SumC, + ScoreDFull = score.FullD, + ScoreD = score.SumD, + ScoreCD = score.SumCD, + ScoreSumFull = score.FullScore, + ScoreSum = score.TotalScore, + ExamResult = score.ExamStatus + }, + Attachments = disable.Documents.Select(a => new + { + FileName = a.DocumentFile.FileName, + DocumentId = a.DocumentFile.Id + }) + }; + return Success(data); } catch (Exception ex) @@ -2496,49 +2583,106 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers { try { - var data = await _context.Disables.AsQueryable() - .Include(x => x.PeriodExam) - .Where(x => x.PeriodExam!.Id == id) - .Where(x => x.ExamId == examId) - .Join(_context.DisableScores.AsQueryable() - .Include(x => x.ScoreImport), - rc => new { rc.PeriodExam!.Id, rc.ExamId }, - sc => new { Id = sc.ScoreImport!.PeriodExamId, sc.ExamId }, - (p, sr) => new - { - ExamID = p.ExamId != null ? p.ExamId.ToThaiNumber() : "", - CitizenId = p.CitizenId != null ? p.CitizenId.ToThaiNumber() : "", - FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", - DateOfBirth = p.DateOfBirth.HasValue && p.DateOfBirth.Value != DateTime.MinValue - ? p.DateOfBirth.Value.ToThaiShortDate().ToThaiNumber() - : "", - Gender = p.Gendor, - Degree = p.Educations.First().Degree, - Major = p.Educations.First().Major, - University = p.Educations.First().University, - PositionName = p.PositionName, - ExamName = $"{p.PeriodExam!.Name} ครั้งที่ {p.PeriodExam.Round}/{(p.PeriodExam.Year > 2500 ? p.PeriodExam.Year : (p.PeriodExam.Year + 543))}".ToThaiNumber(), + #region old + //var data = await _context.Disables.AsQueryable() + // .Include(x => x.PeriodExam) + // .Where(x => x.PeriodExam!.Id == id) + // .Where(x => x.ExamId == examId) + // .Join(_context.DisableScores.AsQueryable() + // .Include(x => x.ScoreImport), + // rc => new { rc.PeriodExam!.Id, rc.ExamId }, + // sc => new { Id = sc.ScoreImport!.PeriodExamId, sc.ExamId }, + // (p, sr) => new + // { + // ExamID = p.ExamId != null ? p.ExamId.ToThaiNumber() : "", + // CitizenId = p.CitizenId != null ? p.CitizenId.ToThaiNumber() : "", + // FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", + // DateOfBirth = p.DateOfBirth.HasValue && p.DateOfBirth.Value != DateTime.MinValue + // ? p.DateOfBirth.Value.ToThaiShortDate().ToThaiNumber() + // : "", + // Gender = p.Gendor, + // Degree = p.Educations.First().Degree, + // Major = p.Educations.First().Major, + // University = p.Educations.First().University, + // PositionName = p.PositionName, + // ExamName = $"{p.PeriodExam!.Name} ครั้งที่ {p.PeriodExam.Round}/{(p.PeriodExam.Year > 2500 ? p.PeriodExam.Year : (p.PeriodExam.Year + 543))}".ToThaiNumber(), + // + // ExamCount = _disableService.GetExamCount(p.CitizenId).ToString().ToThaiNumber(), + // type = p.typeTest == "bangkok" ? "กทม." : p.typeTest == "ocsc" ? "ก.พ." : "", + // FA = sr.FullA != null ? sr.FullA.ToString().ToThaiNumber() : "", + // SA = sr.SumA != null ? sr.SumA.ToString().ToThaiNumber() : "", + // FC = sr.FullC != null ? sr.FullC.ToString().ToThaiNumber() : "", + // SC = sr.SumC != null ? sr.SumC.ToString().ToThaiNumber() : "", + // FD = sr.FullD != null ? sr.FullD.ToString().ToThaiNumber() : "", + // SD = sr.SumD != null ? sr.SumD.ToString().ToThaiNumber() : "", + // F = sr.FullScore != null ? sr.FullScore.ToString().ToThaiNumber() : "", + // T = sr.TotalScore != null ? sr.TotalScore.ToString().ToThaiNumber() : "", + // Result = sr.ExamStatus, + // Number = sr.Number != null ? sr.Number.ToString().ToThaiNumber() : "", + // Expire = p.PeriodExam.AnnouncementDate == null + // ? "" + // : p.PeriodExam.AnnouncementDate != DateTime.MinValue + // ? p.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate().ToString().ToThaiNumber() + // : "", + // }) + // .FirstOrDefaultAsync(); + #endregion - ExamCount = _disableService.GetExamCount(p.CitizenId).ToString().ToThaiNumber(), - type = p.typeTest == "bangkok" ? "กทม." : p.typeTest == "ocsc" ? "ก.พ." : "", - FA = sr.FullA != null ? sr.FullA.ToString().ToThaiNumber() : "", - SA = sr.SumA != null ? sr.SumA.ToString().ToThaiNumber() : "", - FC = sr.FullC != null ? sr.FullC.ToString().ToThaiNumber() : "", - SC = sr.SumC != null ? sr.SumC.ToString().ToThaiNumber() : "", - FD = sr.FullD != null ? sr.FullD.ToString().ToThaiNumber() : "", - SD = sr.SumD != null ? sr.SumD.ToString().ToThaiNumber() : "", - F = sr.FullScore != null ? sr.FullScore.ToString().ToThaiNumber() : "", - T = sr.TotalScore != null ? sr.TotalScore.ToString().ToThaiNumber() : "", - Result = sr.ExamStatus, - Number = sr.Number != null ? sr.Number.ToString().ToThaiNumber() : "", - Expire = p.PeriodExam.AnnouncementDate == null - ? "" - : p.PeriodExam.AnnouncementDate != DateTime.MinValue - ? p.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate().ToString().ToThaiNumber() - : "", - }) + // 1. Query Disable (ข้อมูลผู้สอบ) + var disable = await _context.Disables.AsQueryable() + .Include(x => x.PeriodExam) + .Include(x => x.Educations) + .Where(x => x.PeriodExam != null) + .Where(x => x.PeriodExam.Id == id) + .Where(x => x.ExamId == examId) .FirstOrDefaultAsync(); + if (disable == null) + return Success(); + + // 2. Query DisableScore (ข้อมูลคะแนน) — แยกต่างหาก ไม่มีก็ผ่าน + var score = await _context.DisableScores.AsQueryable() + .Include(x => x.ScoreImport) + .Where(x => x.ScoreImport != null) + .Where(x => x.ScoreImport.PeriodExamId == id) + .Where(x => x.ExamId == examId) + .FirstOrDefaultAsync(); + + // 3. รวมผลลัพธ์ + var data = new + { + ExamID = disable.ExamId != null ? disable.ExamId.ToThaiNumber() : "", + CitizenId = disable.CitizenId != null ? disable.CitizenId.ToThaiNumber() : "", + FullName = $"{disable.Prefix}{disable.FirstName} {disable.LastName}", + DateOfBirth = disable.DateOfBirth.HasValue && disable.DateOfBirth.Value != DateTime.MinValue + ? disable.DateOfBirth.Value.ToThaiShortDate().ToThaiNumber() + : "", + Gender = disable.Gendor, + Degree = disable.Educations.First().Degree, + Major = disable.Educations.First().Major, + University = disable.Educations.First().University, + PositionName = disable.PositionName, + ExamName = $"{disable.PeriodExam!.Name} ครั้งที่ {disable.PeriodExam.Round}/{(disable.PeriodExam.Year > 2500 ? disable.PeriodExam.Year : (disable.PeriodExam.Year + 543))}".ToThaiNumber(), + + ExamCount = _disableService.GetExamCount(disable.CitizenId!).ToString().ToThaiNumber(), + type = disable.typeTest == "bangkok" ? "กทม." : disable.typeTest == "ocsc" ? "ก.พ." : "", + FA = score != null ? score.FullA.ToString().ToThaiNumber() : "", + SA = score != null ? score.SumA.ToString().ToThaiNumber() : "", + FC = score != null ? score.FullC.ToString().ToThaiNumber() : "", + SC = score != null ? score.SumC.ToString().ToThaiNumber() : "", + FD = score != null ? score.FullD.ToString().ToThaiNumber() : "", + SD = score != null ? score.SumD.ToString().ToThaiNumber() : "", + F = score != null ? score.FullScore.ToString().ToThaiNumber() : "", + T = score != null ? score.TotalScore.ToString().ToThaiNumber() : "", + Result = score != null ? score.ExamStatus : "", + Number = score != null && score.Number != null ? score.Number.ToString().ToThaiNumber() : "", + Expire = disable.PeriodExam.AnnouncementDate == null + ? "" + : disable.PeriodExam.AnnouncementDate != DateTime.MinValue + ? disable.PeriodExam.AnnouncementDate.Value.AddYears(2).ToThaiShortDate().ToString().ToThaiNumber() + : "", + }; + var result = new { template = "recruit-exam",