using System.Reflection.Metadata; using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.HR; using BMA.EHR.Domain.Models.Organizations; using BMA.EHR.Domain.Models.Retirement; using BMA.EHR.Domain.ModelsExam.Candidate; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; 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 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, PositionName = p.PositionExam == null ? "" : p.PositionExam.PositionName, PeriodExamName = p.PeriodExam == null ? "" : p.PeriodExam.Name, PeriodExamRound = p.PeriodExam == null ? "" : p.PeriodExam.Round.ToString(), PeriodExamYear = p.PeriodExam == null ? "" : p.PeriodExam.Year.ToString(), FullName = $"{p.PrefixName}{p.FirstName} {p.LastName}", Religion = p.ReligionName, Nationality = p.Nationality, DateOfBirth = p.DateOfBirth == null ? "" : p.DateOfBirth.Value.ToThaiShortDate2(), Age = p.DateOfBirth == null ? "" : p.DateOfBirth.Value.CalculateAgeStrV2(0, 0), CitizenId = p.CitizenId, EducationLevelExamName = p.Educations.FirstOrDefault() == null ? "" : p.Educations.FirstOrDefault().EducationLevelExamName, EducationName = p.Educations.FirstOrDefault() == null ? "" : p.Educations.FirstOrDefault().EducationName, 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.ToThaiShortDate2(), 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 == "perm" ? "ลูกจ้างประจำ" : "-")), OccupationPosition = p.OccupationPosition, OccupationSalary = p.OccupationSalary, OccupationGroup = p.OccupationGroup, OccupationPile = p.OccupationPile, OccupationOrg = p.OccupationOrg, OccupationTelephone = p.OccupationTelephone, 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, RegistProvinceName = p.RegistProvinceName, RegistDistrictName = p.RegistDistrictName, RegistSubDistrictName = p.RegistSubDistrictName, RegistZipCode = p.RegistZipCode, CurrentAddress = p.CurrentAddress, CurrentProvinceName = p.CurrentProvinceName, CurrentSubDistrictName = p.CurrentSubDistrictName, CurrentDistrictName = p.CurrentDistrictName, CurrentZipCode = p.CurrentZipCode, Telephone = p.Telephone, Email = p.Email, ContactFullName = $"{p.ContactPrefixName}{p.ContactFirstname} {p.ContactLastname}", ContactRelations = p.ContactRelations, ContactTel = 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 data = await _dbExamContext.Set().AsQueryable() .Where(x => x.Candidate != null) .Where(x => x.Candidate.Id == id) .Select(p => new { Position = p.Position, Type = p.Type, DurationStart = p.DurationStart.ToThaiShortDate2(), DurationEnd = p.DurationEnd.ToThaiShortDate2(), RangeDate = p.RangeDate, }) .ToListAsync(); 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 } }