hrms-api-backend/BMA.EHR.Application/Repositories/Reports/CandidateReportRepository.cs
2024-09-27 17:42:59 +07:00

209 lines
12 KiB
C#

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<dynamic> GetExamCandidateAsync(Guid id)
{
var careers = await _dbExamContext.Set<Career>()
.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<Candidate>().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.ToThaiNumber(),
PositionName = p.PositionExam == null ? "-" : p.PositionExam.PositionName.ToThaiNumber(),
PositionLevelName = p.PositionExam == null ? "-" : p.PositionExam.PositionLevelName.ToThaiNumber(),
PeriodExamName = p.PeriodExam == null ? "-" : p.PeriodExam.Name.ToThaiNumber(),
PeriodExamRound = p.PeriodExam == null ? "-" : p.PeriodExam.Round.ToString().ToThaiNumber(),
PeriodExamYear = p.PeriodExam == null ? "-" : (p.PeriodExam.Year + 543).ToString().ToThaiNumber(),
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().ToThaiNumber(),
Age = p.DateOfBirth == null ? "-" : p.DateOfBirth.Value.CalculateAgeStrV2(0, 0).ToThaiNumber(),
CitizenId = p.CitizenId == null ? "-" : p.CitizenId.ToThaiNumber(),
EducationLevelExamName = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationLevelExamName.ToThaiNumber(),
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.ToThaiNumber(),
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().ToThaiNumber(),
EducationScores = p.Educations.FirstOrDefault() == null ? "-" : p.Educations.FirstOrDefault().EducationScores.ToThaiNumber(),
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.Value.ToString("N0").ToThaiNumber(),
OccupationGroup = p.OccupationGroup == null ? "-" : p.OccupationGroup,
OccupationPile = p.OccupationPile == null ? "-" : p.OccupationPile,
OccupationOrg = p.OccupationOrg == null ? "-" : p.OccupationOrg,
OccupationTelephone = p.OccupationTelephone == null ? "-" : p.OccupationTelephone.ToThaiNumber(),
CareersTotal = sb.ToString().ToThaiNumber(),
// 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.ToThaiNumber(),
RegistProvinceName = p.RegistProvinceName == null ? "-" : p.RegistProvinceName,
RegistDistrictName = p.RegistDistrictName == null ? "-" : p.RegistDistrictName,
RegistSubDistrictName = p.RegistSubDistrictName == null ? "-" : p.RegistSubDistrictName,
RegistZipCode = p.RegistZipCode == null ? "-" : p.RegistZipCode.ToThaiNumber(),
CurrentAddress = p.CurrentAddress == null ? (p.RegistAddress == null ? "-" : p.RegistAddress.ToThaiNumber()) : p.CurrentAddress.ToThaiNumber(),
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.ToThaiNumber()) : p.CurrentZipCode.ToThaiNumber(),
Telephone = p.Telephone == null ? "-" : p.Telephone.ToThaiNumber(),
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.ToThaiNumber(),
RegisterDate = p.RegisterDate == null ? "-" : p.RegisterDate.Value.ToThaiFullDate().ToThaiNumber(),
})
.FirstOrDefaultAsync();
if (data == null)
throw new Exception(GlobalMessages.CandidateNotFound);
return data;
}
public async Task<dynamic> GetExamCareerCandidateAsync(Guid id)
{
var items = await _dbExamContext.Set<Career>().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<dynamic>();
foreach (var item in items)
{
var _data = new
{
Position = item.Position,
Type = item.Type,
DurationStart = item.DurationStart.ToThaiNumber(),
DurationEnd = item.DurationEnd.ToThaiNumber(),
RangeDate = item.RangeDate.ToThaiNumber(),
Index = retVal.ToString().ToThaiNumber(),
};
data.Add(_data);
retVal++;
}
return data;
}
public async Task<dynamic> GetExamAvatarCandidateAsync(Guid id)
{
var data = await _dbExamContext.Set<Candidate>().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
}
}