diff --git a/Controllers/RecruitController.cs b/Controllers/RecruitController.cs index aa146ac..3e093e6 100644 --- a/Controllers/RecruitController.cs +++ b/Controllers/RecruitController.cs @@ -30,6 +30,7 @@ using static System.Runtime.InteropServices.JavaScript.JSType; using System.Globalization; using OfficeOpenXml.Style; using System.Drawing; +using System.Linq; namespace BMA.EHR.Recruit.Service.Controllers { @@ -1962,7 +1963,7 @@ namespace BMA.EHR.Recruit.Service.Controllers var p_Id = new MySqlParameter("@id", id); // --------------------------- - // 1️⃣ ดึงรายละเอียดสอบ (exam_info) + // 1️. ดึงรายละเอียดสอบ (exam_info) // --------------------------- using (var cmd = _context.Database.GetDbConnection().CreateCommand()) { @@ -2007,7 +2008,7 @@ namespace BMA.EHR.Recruit.Service.Controllers _context.Database.OpenConnection(); // --------------------------- - // 2️⃣ ใช้ DbDataReader แทน DataTable → ประหยัด memory และเร็วขึ้น + // 2️. ใช้ DbDataReader แทน DataTable → ประหยัด memory และเร็วขึ้น // --------------------------- using (var reader = cmd.ExecuteReader()) { @@ -2047,12 +2048,40 @@ namespace BMA.EHR.Recruit.Service.Controllers } // --------------------------- - // 3️⃣ ดึงสรุปคะแนน (sum_exam_info) + // 3️. ดึงสรุปคะแนน // --------------------------- dynamic header = null; - if (data.Count == 0) + if (data.Count > 0) { - header = new + int _count = await _context.Recruits.Where(x => x.Id == id).CountAsync(); + header = await _context.RecruitScores + .Include(x => x.ScoreImport) + .Where(x => x.ScoreImport.RecruitImportId == id) + .GroupBy(x => 1) + .Select(g => new + { + count = _count, + pass = g.Count(x => x.ExamStatus == "ผ่าน"), + notpass = g.Count(x => x.ExamStatus == "ไม่ผ่าน"), + missed_exam = g.Count(x => x.ExamStatus == "ขส."), + other = g.Count(x => + string.IsNullOrEmpty(x.ExamStatus) || + !new[] { "ผ่าน", "ไม่ผ่าน", "ขส." }.Contains(x.ExamStatus) + ) + }) + .FirstOrDefaultAsync() + ?? new // ใช้ null-coalescing กันกรณีไม่มีข้อมูล (แทน else เดิม) + { + count = 0, + pass = 0, + notpass = 0, + missed_exam = 0, + other = 0 + }; + } + else + { + header = new { count = 0, pass = 0, @@ -2061,50 +2090,10 @@ namespace BMA.EHR.Recruit.Service.Controllers other = 0 }; } - else - { - using (var cmd = _context.Database.GetDbConnection().CreateCommand()) - { - cmd.CommandTimeout = 0; - var sb = new StringBuilder(); - cmd.Parameters.Clear(); - sb.Append(" SELECT count, pass, notpass, missed_exam, other FROM sum_exam_info WHERE recruit_import_id = @id "); - cmd.Parameters.Add(p_Id); - cmd.CommandText = sb.ToString(); - _context.Database.OpenConnection(); - - using (var reader = cmd.ExecuteReader()) - { - if (!reader.HasRows) - { - header = new - { - count = 0, - pass = 0, - notpass = 0, - missed_exam = 0, - other = 0 - }; - } - else - { - reader.Read(); - header = new - { - count = reader["count"].ToString().ToInteger(), - pass = reader["pass"].ToString().ToInteger(), - notpass = reader["notpass"].ToString().ToInteger(), - missed_exam = reader["missed_exam"].ToString().ToInteger(), - other = reader["other"].ToString().ToInteger() - }; - } - } - } - } // --------------------------- - // 4️⃣ ดึง period + // 4️. ดึง period // --------------------------- var period = await _context.RecruitImports.AsQueryable() .Select(x => new diff --git a/Requests/Recruits/RecruitExamRequest.cs b/Requests/Recruits/RecruitExamRequest.cs index 91f4d92..4afe837 100644 --- a/Requests/Recruits/RecruitExamRequest.cs +++ b/Requests/Recruits/RecruitExamRequest.cs @@ -13,6 +13,8 @@ /// public string ExamResult { get; set; } + public string? keyword { get; set; } + public int Page { get; set; } = 1; public int PageSize { get; set; } = 10; }