diff --git a/BMA.EHR.Application/Repositories/Reports/CandidateReportRepository.cs b/BMA.EHR.Application/Repositories/Reports/CandidateReportRepository.cs index 3f4abade..ce460b8d 100644 --- a/BMA.EHR.Application/Repositories/Reports/CandidateReportRepository.cs +++ b/BMA.EHR.Application/Repositories/Reports/CandidateReportRepository.cs @@ -94,21 +94,40 @@ namespace BMA.EHR.Application.Repositories.Reports if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); - var editorConfirmLists = string.IsNullOrEmpty(candidate.PeriodExam?.EditorConfirm) - ? new List + List editorConfirmLists; + + var textOnly = string.IsNullOrEmpty(candidate.PeriodExam?.EditorConfirm) + ? null + : Regex.Replace( + candidate.PeriodExam.EditorConfirm, + "<[^>]+>", + string.Empty + ) + .Replace(" ", " ") + .Trim(); + + if (!string.IsNullOrEmpty(textOnly)) + { + // ลบข้อความทั้งหมดก่อน "1." เพื่อให้เริ่มต้นที่ข้อแรกเสมอ + var cleanedText = Regex.Replace(textOnly, @"^.*?1\.", "1.").Trim(); + + // ถ้าข้อ 3 จบด้วย "มาตรา 1374." แล้วเลข 4. ติดกับประโยค ให้แทรกขึ้นบรรทัดใหม่เป็นข้อ 4. + if (!cleanedText.Contains("\n4.") && Regex.IsMatch(cleanedText, @"1374\.\s*")) { - "-" + cleanedText = Regex.Replace(cleanedText, @"1374\.\s*", "137\n4. "); } - : Regex.Matches( - Regex.Replace(candidate.PeriodExam.EditorConfirm, "<.*?>", string.Empty) - .Replace(" ", " ") - .Trim(), - @"[1-9]\.\s*(.*?)(?=[1-9]\.|$)", // ตอนนี้ Fix ไว้ให้กรอกเลขลำดับตามด้วย .เท่านั้น เช่น 1. 2. 3. และรองรับได้สูงสุดแค่ 1-9 เท่านั้น - RegexOptions.Singleline - ) - .Cast() - .Select((m, index) => $"{index + 1}. {m.Groups[1].Value.Trim()}") // สร้างหมายเลขข้อจาก Index - .ToList(); + + // แยกข้อความเป็นแต่ละข้อ โดย split เมื่อเจอ pattern "ตัวเลข. " + // ใช้ lookahead (?=...) เพื่อไม่กินตัวเลขทิ้ง และให้ตัวเลขยังอยู่ในผลลัพธ์ + editorConfirmLists = Regex.Split(cleanedText, @"(?=\d+\. )") + .Where(s => !string.IsNullOrWhiteSpace(s)) + .Select(s => s.Replace("\n", "").Replace("\r", "").Trim()) + .ToList(); + } + else + { + editorConfirmLists = new List { "-" }; + } return new {