using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Insignias; using BMA.EHR.Domain.Models.Organizations; using BMA.EHR.Domain.Models.Retirement; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; namespace BMA.EHR.Application.Repositories.Reports { public class InsigniaReportRepository { #region " Fields " private readonly IApplicationDBContext _dbContext; private readonly IWebHostEnvironment _hostingEnvironment; private readonly OrganizationCommonRepository _organizationCommonRepository; #endregion #region " Constructor and Destructor " public InsigniaReportRepository(IApplicationDBContext dbContext, OrganizationCommonRepository organizationCommonRepository, IWebHostEnvironment hostEnvironment) { _dbContext = dbContext; _hostingEnvironment = hostEnvironment; _organizationCommonRepository = organizationCommonRepository; } #endregion #region " Methods " public async Task GetYearInsigniaPeriod(Guid id) { var period = await _dbContext.Set() .FirstOrDefaultAsync(x => x.Id == id); if (period == null) throw new Exception(GlobalMessages.InsigniaPeriodNotFound); string thaiYear = period.Year.ToThaiYear().ToString(); return thaiYear; } public async Task GetKhr1Report(Guid id) { var period = await _dbContext.Set() .FirstOrDefaultAsync(x => x.Id == id); if (period == null) throw new Exception(GlobalMessages.InsigniaPeriodNotFound); var data_insignia = await _dbContext.Set() .Include(x => x.Profile) // .ThenInclude(x => x.OrganizationOrganization) // .ThenInclude(x => x.Type) // .Include(x => x.RequestInsignia) // .ThenInclude(x => x.InsigniaType) // .Include(x => x.Request) .Where(x => x.Request.Period == period) .Where(x => x.IsApprove == true) .Where(x => x.RequestInsignia.InsigniaType != null) .Where(x => x.RequestInsignia.InsigniaType.Name == "ชั้นสายสะพาย") .Select(x => new { Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name, RequestInsigniaName = x.RequestInsignia.Name, OcId = x.Request.Organization.Id }) .ToListAsync(); var insignia = (from r in data_insignia group r by new { OcId = r.OcId } into g select new { RowNo = 1, DepartmentName = _organizationCommonRepository.GetOrganizationNameFullPath(g.Key.OcId, false, false), G1Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "มหาปรมาภรณ์ช้างเผือก" ? 1 : 0), G1Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "มหาปรมาภรณ์ช้างเผือก" ? 1 : 0), G2Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "มหาวชิรมงกุฎ" ? 1 : 0), G2Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "มหาวชิรมงกุฎ" ? 1 : 0), G3Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "ประถมาภรณ์ช้างเผือก" ? 1 : 0), G3Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "ประถมาภรณ์ช้างเผือก" ? 1 : 0), G4Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "ประถมาภรณ์มงกุฎไทย" ? 1 : 0), G4Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "ประถมาภรณ์มงกุฎไทย" ? 1 : 0), }).ToList(); return insignia; } public async Task GetKhr2Report(Guid id) { var period = await _dbContext.Set() .FirstOrDefaultAsync(x => x.Id == id); if (period == null) throw new Exception(GlobalMessages.InsigniaPeriodNotFound); var data_insignia = await _dbContext.Set() .Include(x => x.Profile) // .ThenInclude(x => x.OrganizationOrganization) // .ThenInclude(x => x.Type) // .Include(x => x.RequestInsignia) // .ThenInclude(x => x.InsigniaType) // .Include(x => x.Request) .Where(x => x.Request.Period == period) .Where(x => x.IsApprove == true) .Where(x => x.RequestInsignia.InsigniaType != null) .Where(x => x.RequestInsignia.InsigniaType.Name == "ชั้นต่ำกว่าสายสะพาย") .Select(x => new { Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name, RequestInsigniaName = x.RequestInsignia.Name, OcId = x.Request.Organization.Id }) .ToListAsync(); var insignia = (from r in data_insignia group r by new { OcId = r.OcId } into g select new { RowNo = 1, DepartmentName = _organizationCommonRepository.GetOrganizationNameFullPath(g.Key.OcId, false, false), G1Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "ทวีติยาภรณ์ช้างเผือก" ? 1 : 0), G1Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "ทวีติยาภรณ์ช้างเผือก" ? 1 : 0), G2Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "ทวีติยาภรณ์มงกุฎไทย" ? 1 : 0), G2Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "ทวีติยาภรณ์มงกุฎไทย" ? 1 : 0), G3Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "ตริตาภรณ์ช้างเผือก" ? 1 : 0), G3Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "ตริตาภรณ์ช้างเผือก" ? 1 : 0), G4Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "ตริตาภรณ์มงกุฎไทย" ? 1 : 0), G4Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "ตริตาภรณ์มงกุฎไทย" ? 1 : 0), G5Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "จัตุรถาภรณ์ช้างเผือก" ? 1 : 0), G5Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "จัตุรถาภรณ์ช้างเผือก" ? 1 : 0), G6Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "จัตุรถาภรณ์มงกุฎไทย" ? 1 : 0), G6Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "จัตุรถาภรณ์มงกุฎไทย" ? 1 : 0), G7Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "เบญจมาภรณ์ช้างเผือก" ? 1 : 0), G7Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "เบญจมาภรณ์ช้างเผือก" ? 1 : 0), G8Male = g.Sum(x => x.Gendor == "ชาย" && x.RequestInsigniaName == "เบญจมาภรณ์มงกุฎไทย" ? 1 : 0), G8Female = g.Sum(x => x.Gendor == "หญิง" && x.RequestInsigniaName == "เบญจมาภรณ์มงกุฎไทย" ? 1 : 0), }).ToList(); return insignia; } #endregion } }