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 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 data = await _dbExamContext.Set().AsQueryable() .Where(x => x.Id == id) .Select(p => new { p.Id, AvatarId = p.ProfileImg == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.ProfileImg.Id, ExamIdenNumber = p.ExamIdenNumber == null ? "-" : p.ExamIdenNumber, PositionName = p.PositionExam == null ? "-" : p.PositionExam.PositionName, PositionLevelName = p.PositionExam == null ? "-" : p.PositionExam.PositionLevelName, PeriodExamName = p.PeriodExam == null ? "-" : p.PeriodExam.Name, PeriodExamRound = p.PeriodExam == null ? "-" : p.PeriodExam.Round.ToString(), PeriodExamYear = p.PeriodExam == null ? "-" : (p.PeriodExam.Year + 543).ToString(), FullName = $"{p.PrefixName}{p.FirstName} {p.LastName}", Religion = p.ReligionName == null ? "-" : p.ReligionName, Nationality = p.Nationality == null ? "-" : p.Nationality, DateOfBirth = p.DateOfBirth == null ? "-" : p.DateOfBirth.Value.ToThaiFullDate2(), Age = p.DateOfBirth == null ? "-" : p.DateOfBirth.Value.CalculateAgeStrV2(0, 0), CitizenId = p.CitizenId == null ? "-" : p.CitizenId, EducationLevelExamName = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationLevelExamName, EducationName = p.Educations.FirstOrDefault() == null ? null : (p.Educations.FirstOrDefault().EducationLevelExamName == "ปริญญาตรี" || p.Educations.FirstOrDefault().EducationLevelExamName == "ปริญญาโท" || p.Educations.FirstOrDefault().EducationLevelExamName == "ปริญญาเอก" ? p.Educations.FirstOrDefault().EducationName : null), EducationMajor = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationMajor, EducationLocation = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationLocation, EducationEndDate = p.Educations.FirstOrDefault() == null || p.Educations.FirstOrDefault().EducationEndDate == null ? "-" : p.Educations.FirstOrDefault().EducationEndDate.Value.ToThaiFullDate2(), EducationScores = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationScores, EducationType = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationType, EducationLevelHighName = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationLevelHighName, OccupationPositionType = p.OccupationPositionType == "other" ? "ผู้ปฏิบัติงานอื่นในกรุงเทพมหานคร" : (p.OccupationPositionType == "temp" ? "ลูกจ้างชั่วคราว" : (p.OccupationPositionType == "prem" ? "ลูกจ้างประจำ" : "-")), OccupationPosition = p.OccupationPosition == null ? "-" : p.OccupationPosition, OccupationSalary = p.OccupationSalary == null ? "-" : p.OccupationSalary.ToString(), OccupationGroup = p.OccupationGroup == null ? "-" : p.OccupationGroup, OccupationPile = p.OccupationPile == null ? "-" : p.OccupationPile, OccupationOrg = p.OccupationOrg == null ? "-" : p.OccupationOrg, OccupationTelephone = p.OccupationTelephone == null ? "-" : p.OccupationTelephone, CareersTotal = sb.ToString(), // Careers = p.Careers.Select(y => new // { // Position = y.Position, // Type = y.Type, // DurationStart = y.DurationStart.ToThaiShortDate2(), // DurationEnd = y.DurationEnd.ToThaiShortDate2(), // RangeDate = y.RangeDate, // }).ToList(), RegistAddress = p.RegistAddress == null ? "-" : p.RegistAddress, RegistProvinceName = p.RegistProvinceName == null ? "-" : p.RegistProvinceName, RegistDistrictName = p.RegistDistrictName == null ? "-" : p.RegistDistrictName, RegistSubDistrictName = p.RegistSubDistrictName == null ? "-" : p.RegistSubDistrictName, RegistZipCode = p.RegistZipCode == null ? "-" : p.RegistZipCode, CurrentAddress = p.CurrentAddress == null ? (p.RegistAddress == null ? "-" : p.RegistAddress) : p.CurrentAddress, CurrentProvinceName = p.CurrentProvinceName == null ? (p.RegistProvinceName == null ? "-" : p.RegistProvinceName) : p.CurrentProvinceName, CurrentDistrictName = p.CurrentDistrictName == null ? (p.RegistDistrictName == null ? "-" : p.RegistDistrictName) : p.CurrentDistrictName, CurrentSubDistrictName = p.CurrentSubDistrictName == null ? (p.RegistSubDistrictName == null ? "-" : p.RegistSubDistrictName) : p.CurrentSubDistrictName, CurrentZipCode = p.CurrentZipCode == null ? (p.RegistZipCode == null ? "-" : p.RegistZipCode) : p.CurrentZipCode, Telephone = p.Telephone == null ? "-" : p.Telephone, Email = p.Email == null ? "-" : p.Email, ContactFullName = $"{p.ContactPrefixName}{p.ContactFirstname} {p.ContactLastname}", ContactRelations = p.ContactRelations == null ? "-" : p.ContactRelations, ContactTel = p.ContactTel == null ? "-" : p.ContactTel, RegisterDate = p.RegisterDate == null ? "-" : p.RegisterDate.Value.ToThaiFullDate(), }) .FirstOrDefaultAsync(); if (data == null) throw new Exception(GlobalMessages.CandidateNotFound); return data; } 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, }; 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 } }