query ขร 1-4

This commit is contained in:
DESKTOP-2S5P7D1\Windows 10 2023-09-04 16:02:20 +07:00
parent ee884f9434
commit 40861853dd
3 changed files with 184 additions and 23 deletions

View file

@ -1,6 +1,7 @@
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.Insignias;
using BMA.EHR.Domain.Models.Organizations;
using BMA.EHR.Domain.Models.Retirement;
@ -17,6 +18,7 @@ namespace BMA.EHR.Application.Repositories.Reports
private readonly IApplicationDBContext _dbContext;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly OrganizationCommonRepository _organizationCommonRepository;
private readonly string CRLF = "\r\n";
#endregion
@ -43,6 +45,7 @@ namespace BMA.EHR.Application.Repositories.Reports
string thaiYear = period.Year.ToThaiYear().ToString();
return thaiYear;
}
public async Task<dynamic> GetKhr1Report(Guid id)
{
var period = await _dbContext.Set<InsigniaPeriod>()
@ -52,11 +55,6 @@ namespace BMA.EHR.Application.Repositories.Reports
var data_insignia = await _dbContext.Set<InsigniaRequestProfile>()
.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)
@ -87,6 +85,7 @@ namespace BMA.EHR.Application.Repositories.Reports
return insignia;
}
public async Task<dynamic> GetKhr2Report(Guid id)
{
var period = await _dbContext.Set<InsigniaPeriod>()
@ -96,11 +95,6 @@ namespace BMA.EHR.Application.Repositories.Reports
var data_insignia = await _dbContext.Set<InsigniaRequestProfile>()
.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)
@ -140,6 +134,168 @@ namespace BMA.EHR.Application.Repositories.Reports
return insignia;
}
public async Task<dynamic> GetKhr3Report(Guid id)
{
var period = await _dbContext.Set<InsigniaPeriod>()
.FirstOrDefaultAsync(x => x.Id == id);
if (period == null)
throw new Exception(GlobalMessages.InsigniaPeriodNotFound);
var data = (from r in await _dbContext.Set<InsigniaRequestProfile>()
.Include(x => x.Profile)
.ThenInclude(x => x.Gender)
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Include(x => x.Request)
.ThenInclude(x => x.Period)
.Include(x => x.Request)
.ThenInclude(x => x.Organization)
.Include(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.ToListAsync()
where r.Request.Period == period
&& r.IsApprove == true
&& r.RequestInsignia.InsigniaType != null
&& r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"
select new
{
InsigniaInitial = r.RequestInsignia.ShortName,
InsigniaName = r.RequestInsignia.Name,
ProfileId = r.Profile.Id,
FullName = $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
Gender = r.Profile.Gender == null ? null : r.Profile.Gender.Name,
InsigniaId = r.RequestInsignia.Id,
OCName = _organizationCommonRepository.GetOrganizationNameFullPath(r.Request.Organization.Id, false, false)
})
.Distinct()
.ToList();
// loop to add temp row with 50 rows per page
var insigniaList = data.Select(x => new { InsigniaId = x.InsigniaId, InsigniaInitial = x.InsigniaInitial, InsigniaName = x.InsigniaName })
.Distinct().ToList();
foreach (var ins in insigniaList)
{
var count = data.Where(x => x.InsigniaId == ins.InsigniaId).Count();
var mod_val = count <= 50 ? 50 - count : count % 50.0;
for (int i = 0; i < mod_val; i++)
{
var p = new
{
InsigniaInitial = ins.InsigniaInitial,
InsigniaName = ins.InsigniaName,
ProfileId = Guid.Parse("00000000-0000-0000-0000-000000000000"),
FullName = "",
Gender = "",
InsigniaId = ins.InsigniaId,
OCName = ""
};
data.Add(p);
}
}
return data;
}
public async Task<dynamic> GetKhr4Report(Guid id)
{
var period = await _dbContext.Set<InsigniaPeriod>()
.FirstOrDefaultAsync(x => x.Id == id);
if (period == null)
throw new Exception(GlobalMessages.InsigniaPeriodNotFound);
var teacher_data = (from r in await _dbContext.Set<InsigniaRequestProfile>()
.Include(x => x.Profile)
.ThenInclude(x => x.Salaries)
.Include(x => x.Profile)
.ThenInclude(x => x.Gender)
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Include(x => x.Profile)
.ThenInclude(x => x.PositionType)
.Include(x => x.Profile)
.ThenInclude(x => x.PositionLevel)
.Include(x => x.Request)
.ThenInclude(x => x.Period)
.Include(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.Include(x => x.Request)
.ThenInclude(x => x.Organization)
.ToListAsync()
where r.Request.Period == period
&& r.IsApprove == true
&& r.RequestInsignia.InsigniaType != null
&& r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"
select new
{
InsigniaInitial = r.RequestInsignia.ShortName,
InsigniaName = r.RequestInsignia.Name,
ProfileId = r.Profile.Id,
FullName = $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
ShowProfileId = r.Profile.Id,
Type = r.Profile.PositionType?.Name,
AcademicStanding = "",
Level = r.Profile.PositionLevel?.Name,
DateStart = r.Profile.DateStart == null ? null : r.Profile.DateStart.Value.ToThaiShortDate(),
SalaryAmount = r.Profile.Salaries.Count() == 0 ? 0 :
r.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount,
InsigniaRecv = "",
InsigniaRecvDate = "",
InsigniaRequest = r.RequestInsignia.ShortName,
Remark = "",
Position = r.Profile.Position?.Name +
(r.Profile.PositionType == null ? null : " ประเภท" + r.Profile.PositionType?.Name) +
(r.Profile.PositionLevel == null ? null : " ระดับ" + r.Profile.PositionLevel?.Name) +
CRLF,
OCName = _organizationCommonRepository.GetOrganizationNameFullPath(r.Request.Organization.Id, false, false)
})
.Distinct()
.ToList();
var insignia_data = (from r in await _dbContext.Set<InsigniaRequestProfile>()
.Include(x => x.Profile)
.ThenInclude(x => x.Gender)
.Include(x => x.Profile)
.ThenInclude(x => x.Insignias)
.ThenInclude(x => x.Insignia)
.Include(x => x.Request)
.ThenInclude(x => x.Period)
.Include(x => x.Request)
.ThenInclude(x => x.Organization)
.Include(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.ToListAsync()
where r.Request.Period == period
&& r.IsApprove == true
&& r.RequestInsignia.InsigniaType != null
&& r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"
select new
{
InsigniaInitial = r.RequestInsignia.ShortName,
InsigniaName = r.RequestInsignia.Name,
ProfileId = r.Profile.Id,
FullName = $"",
ShowProfileId = Guid.Parse("00000000-0000-0000-0000-000000000000"),
Type = "",
AcademicStanding = "",
Level = "",
DateStart = "",
SalaryAmount = new double?(0),
InsigniaRecv = r.Profile.Insignias.Count() == 0 ? null :
(r.Profile.Insignias.OrderByDescending(x => x.Year).FirstOrDefault().Insignia == null ? null : r.Profile.Insignias.OrderByDescending(x => x.Year).FirstOrDefault().Insignia.ShortName),
InsigniaRecvDate = r.Profile.Insignias.Count() == 0 ? null :
(r.Profile.Insignias.OrderByDescending(x => x.Year).FirstOrDefault().DateAnnounce == null ? null : r.Profile.Insignias.OrderByDescending(x => x.Year).FirstOrDefault().DateAnnounce.Value.ToThaiShortDate()),
InsigniaRequest = "",
Remark = "",
Position = "",
OCName = _organizationCommonRepository.GetOrganizationNameFullPath(r.Request.Organization.Id, false, false)
})
.Distinct()
.ToList();
var data2 = teacher_data.Union(insignia_data).ToList();
return data2;
}
#endregion
}
}