using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.ModelsExam.Candidate; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using System.Text; using System.Text.RegularExpressions; using static BMA.EHR.Domain.Extensions.DateTimeExtension; namespace BMA.EHR.Application.Repositories.Reports { public class CandidateReportRepository { #region " Fields " private readonly IApplicationDBContext _dbContext; private readonly IApplicationDBExamContext _dbExamContext; private readonly IWebHostEnvironment _hostingEnvironment; private readonly MinIOService _documentService; private readonly OrganizationCommonRepository _organizationCommonRepository; #endregion #region " Constructor and Destructor " public CandidateReportRepository(IApplicationDBContext dbContext, IApplicationDBExamContext dbExamContext, MinIOService documentService, OrganizationCommonRepository organizationCommonRepository, IWebHostEnvironment hostEnvironment) { _dbContext = dbContext; _dbExamContext = dbExamContext; _hostingEnvironment = hostEnvironment; _organizationCommonRepository = organizationCommonRepository; _documentService = documentService; } #endregion #region " Methods " #region ใบสมัครสอบ public async Task GetExamCandidateAsync(Guid id) { var careers = await _dbExamContext.Set() .AsQueryable() .Where(x => x.Candidate.Id == id) .ToListAsync(); var yearDiff = 0; var monthDiff = 0; var dayDiff = 0; var sb = new StringBuilder(); foreach (var career in careers) { if (career.DurationEnd < career.DurationStart) { var rangeObj = career.DurationEnd.CalculateBetweenDateV2Value(career.DurationStart); yearDiff = yearDiff + rangeObj.years; monthDiff = monthDiff + rangeObj.months; dayDiff = dayDiff + rangeObj.days; } else { var rangeObj = career.DurationStart.CalculateBetweenDateV2Value(career.DurationEnd); yearDiff = yearDiff + rangeObj.years; monthDiff = monthDiff + rangeObj.months; dayDiff = dayDiff + rangeObj.days; } if (dayDiff >= 30) { monthDiff = monthDiff + (int)(dayDiff / 30); dayDiff = dayDiff % 30; } if (monthDiff >= 12) { yearDiff = yearDiff + (int)(monthDiff / 12); monthDiff = monthDiff % 12; } sb.Clear(); sb.Append(yearDiff == 0 ? "" : $"{yearDiff} ปี "); sb.Append(monthDiff == 0 ? "" : $"{monthDiff} เดือน "); sb.Append(dayDiff == 0 ? "" : $"{dayDiff} วัน "); } var candidate = await _dbExamContext.Set() .Where(x => x.Id == id) .Include(p => p.PeriodExam) .Include(p => p.Educations) .Include(p => p.PositionExam) .Include(p => p.ProfileImg) .FirstOrDefaultAsync(); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); // แปลง EditorConfirm เป็น List { "1. ข้าพเจ้าขอให้คำรับรองว่า ข้อความดังกล่าวข้างต้นเป็นจริงทุกประการ และข้าพเจ้ามีคุณสมบัติทั่วไปและไม่มีลักษณะต้องห้ามตามมาตรา 43 แห่งพระราชบัญญัติระเบียบข้าราชการกรุงเทพมหานครและบุคลากรกรุงเทพมหานคร พ.ศ. 2554 และมีคุณสมบัติเฉพาะสำหรับตำแหน่งที่สมัครตรงตามประกาศรับสมัคร", "2. กรณีข้าพเจ้ามีลักษณะต้องห้าม ตามมาตรา 43 ข. แห่งพระราชบัญญัติระเบียบข้าราชการกรุงเทพมหานครและบุคลากรกรุงเทพมหานคร พ.ศ. 2554 และประสงค์จะยื่นคำขอยกเว้นเข้ารับราชการฯ ต่อสำนักงาน ก.ก. ตามระเบียบ ก.ก. ว่าด้วยการยกเว้นให้ผู้มีลักษณะต้องห้ามเข้ารับราชการเป็นข้าราชการกรุงเทพมหานคร พ.ศ. 2556 ตามมติ ก.ก. ครั้งที่ 7/2556 เมื่อวันที่ 15 สิงหาคม 2556 ข้าพเจ้าจะยื่นคำขอยกเว้นฯ ภายในวันปิดรับสมัคร ทั้งนี้ หากยื่นภายหลังกำหนด สำนักงาน ก.ก. จะไม่รับคำขอดังกล่าว", "3. ข้าพเจ้าจะยื่นหลักฐานและเอกสารที่แสดงว่า เป็นผู้มีคุณสมบัติทั่วไปและมีคุณสมบัติเฉพาะสำหรับตำแหน่งที่สมัครตรงตามประกาศรับสมัครภายในระยะเวลาที่กำหนดตามประกาศฯ หากมีการตรวจสอบหลักฐานและเอกสาร และหรือคุณวุฒิการศึกษาของข้าพเจ้าในภายหลังปรากฏว่าข้าพเจ้ามีคุณสมบัติไม่ตรงหรือมีลักษณะต้องห้ามตามประกาศรับสมัครหรือไม่ได้รับการยกเว้น ให้ถือว่า ข้าพเจ้าเป็นผู้ขาดคุณสมบัติในการสมัครครั้งนี้มาตั้งแต่ต้น และไม่มีสิทธิได้รับการบรรจุและแต่งตั้งให้ดำรงตำแหน่ง และข้าพเจ้าจะไม่มีสิทธิเรียกร้องใดๆ ทั้งสิ้น และหากข้าพเจ้าจงใจกรอกข้อความอันเป็นเท็จ อาจมีความผิดทางอาญาฐานแจ้งความเท็จต่อเจ้าพนักงาน ตามประมวลกฎหมายอาญา มาตรา 137", "4. ข้าพเจ้าได้ศึกษาและทำความเข้าใจพระราชบัญญัติคุ้มครองข้อมูลส่วนบุคคล พ.ศ. 2562 ตลอดจนประกาศและระเบียบที่เกี่ยวข้องโดยละเอียดครบถ้วนแล้ว ข้าพเจ้าขอแสดงความยินยอมให้หน่วยงานหรือบุคคลที่เกี่ยวข้องกับการดำเนินการสรรหา สามารถเก็บ รวบรวม ใช้ และเปิดเผยข้อมูลส่วนบุคคลที่เกี่ยวข้องกับข้าพเจ้า เพื่อประโยชน์ของทางราชการตามกฎหมายที่เกี่ยวข้อง", "5. ข้าพเจ้ารับทราบว่า การส่งใบสมัครคัดเลือกฯ พร้อมเอกสารนี้ ยังไม่ได้รับการตรวจสอบคุณสมบัติตามหลักเกณฑ์ ประกาศรับสมัครคัดเลือก และเงื่อนไขต่างๆ โดยจะได้รับการตรวจสอบคุณสมบัติเมื่อเป็นผู้สอบผ่านข้อเขียนภาคความรู้ความสามารถที่ใช้เฉพาะตำแหน่งแล้วเท่านั้น" } /*: Regex.Matches( Regex.Replace(candidate.PeriodExam.EditorConfirm, "<.*?>", string.Empty) .Replace(" ", " ") .Trim(), @"(?:[1-9]|[1-9][0-9]|100)\.\s*.*?(?=(?:[1-9]|[1-9][0-9]|100)\.|$)", RegexOptions.Singleline ) .Cast() .Select(m => m.Groups[1].Value.Trim()) .ToList()*/; return new { candidate.Id, AvatarId = candidate.ProfileImg?.Id ?? Guid.Empty, ExamIdenNumber = candidate.ExamIdenNumber ?? "-", PositionName = candidate.PositionExam?.PositionName ?? "-", PositionLevelName = candidate.PositionExam?.PositionLevelName ?? "-", PeriodExamName = candidate.PeriodExam?.Name ?? "-", PeriodExamRound = candidate.PeriodExam?.Round.ToString() ?? "-", PeriodExamYear = candidate.PeriodExam != null ? (candidate.PeriodExam.Year + 543).ToString() : "-", FullName = $"{candidate.PrefixName}{candidate.FirstName} {candidate.LastName}", Religion = candidate.ReligionName ?? "-", Nationality = candidate.Nationality ?? "-", DateOfBirth = candidate.DateOfBirth?.ToThaiFullDate2() ?? "-", Age = candidate.DateOfBirth?.CalculateAgeStrV2(0, 0) ?? "-", CitizenId = candidate.CitizenId ?? "-", EducationLevelExamName = candidate.Educations.FirstOrDefault()?.EducationLevelExamName ?? "-", EducationName = (candidate.Educations.FirstOrDefault()?.EducationLevelExamName is "ปริญญาตรี" or "ปริญญาโท" or "ปริญญาเอก") ? candidate.Educations.FirstOrDefault()?.EducationName : null, EducationMajor = candidate.Educations.FirstOrDefault()?.EducationMajor ?? "-", EducationLocation = candidate.Educations.FirstOrDefault()?.EducationLocation ?? "-", EducationEndDate = candidate.Educations.FirstOrDefault()?.EducationEndDate?.ToThaiFullDate2() ?? "-", EducationScores = candidate.Educations.FirstOrDefault()?.EducationScores ?? "-", EducationType = candidate.Educations.FirstOrDefault()?.EducationType ?? "-", EducationLevelHighName = candidate.Educations.FirstOrDefault()?.EducationLevelHighName ?? "-", OccupationPositionType = candidate.OccupationPositionType == "other" ? "ผู้ปฏิบัติงานอื่นในกรุงเทพมหานคร" : candidate.OccupationPositionType == "temp" ? "ลูกจ้างชั่วคราว" : candidate.OccupationPositionType == "prem" ? "ลูกจ้างประจำ" : "-", OccupationPosition = candidate.OccupationPosition ?? "-", OccupationSalary = candidate.OccupationSalary.HasValue ? (candidate.OccupationSalary.Value % 1 == 0 ? candidate.OccupationSalary.Value.ToString("N0") : candidate.OccupationSalary.Value.ToString("N2")) : "-", OccupationGroup = candidate.OccupationGroup ?? "-", OccupationPile = candidate.OccupationPile ?? "-", OccupationOrg = candidate.OccupationOrg ?? "-", OccupationTelephone = candidate.OccupationTelephone ?? "-", CareersTotal = sb.ToString(), RegistAddress = candidate.RegistAddress ?? "-", RegistProvinceName = candidate.RegistProvinceName ?? "-", RegistDistrictName = candidate.RegistDistrictName ?? "-", RegistSubDistrictName = candidate.RegistSubDistrictName ?? "-", RegistZipCode = candidate.RegistZipCode ?? "-", CurrentAddress = candidate.CurrentAddress ?? candidate.RegistAddress ?? "-", CurrentProvinceName = candidate.CurrentProvinceName ?? candidate.RegistProvinceName ?? "-", CurrentDistrictName = candidate.CurrentDistrictName ?? candidate.RegistDistrictName ?? "-", CurrentSubDistrictName = candidate.CurrentSubDistrictName ?? candidate.RegistSubDistrictName ?? "-", CurrentZipCode = candidate.CurrentZipCode ?? candidate.RegistZipCode ?? "-", Telephone = candidate.Telephone ?? "-", Email = candidate.Email ?? "-", ContactFullName = $"{candidate.ContactPrefixName}{candidate.ContactFirstname} {candidate.ContactLastname}", ContactRelations = candidate.ContactRelations ?? "-", ContactTel = candidate.ContactTel ?? "-", RegisterDate = candidate.RegisterDate?.ToThaiFullDate() ?? "-", IsBachelors = candidate.PositionExam != null && !string.IsNullOrEmpty(candidate.PositionExam.EducationLevel) && candidate.PositionExam.EducationLevel.Trim().ToUpper() == "BACHELOR" ? new List { new { label = "ชื่อปริญญา", value = candidate.Educations.FirstOrDefault()?.EducationName ?? "-" }, new { label = "สาขาวิชา/วิชาเอก", value = candidate.Educations.FirstOrDefault()?.EducationMajor ?? "-" } } : new List { new { label = "สาขาวิชา/วิชาเอก", value = candidate.Educations.FirstOrDefault()?.EducationMajor ?? "-" } }, EditorConfirms = editorConfirmLists }; } public async Task GetExamCareerCandidateAsync(Guid id) { var items = await _dbExamContext.Set().AsQueryable() .Where(x => x.Candidate != null) .Where(x => x.Candidate.Id == id) .OrderBy(x => x.DurationStart) .Select(p => new { Position = p.Position, Type = p.Type, DurationStart = p.DurationStart.ToThaiShortDate2(), DurationEnd = p.DurationEnd.ToThaiShortDate2(), RangeDate = p.RangeDate, }) .ToListAsync(); int retVal = 1; var data = new List(); foreach (var item in items) { var _data = new { Position = item.Position, Type = item.Type, DurationStart = item.DurationStart, DurationEnd = item.DurationEnd, RangeDate = item.RangeDate, Index = retVal.ToString(), }; data.Add(_data); retVal++; } return data; } public async Task GetExamAvatarCandidateAsync(Guid id) { var data = await _dbExamContext.Set().AsQueryable() .Where(x => x.Id == id) .Select(p => new { AvatarId = p.ProfileImg == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.ProfileImg.Id, }) .FirstOrDefaultAsync(); if (data == null) throw new Exception(GlobalMessages.CandidateNotFound); return data.AvatarId; } #endregion #endregion } }