using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses.Reports; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Commands.Core; using BMA.EHR.Domain.Models.HR; using BMA.EHR.Domain.Models.OrganizationEmployee; using BMA.EHR.Domain.Models.Placement; using BMA.EHR.Domain.Models.Retirement; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; namespace BMA.EHR.Application.Repositories.Commands { public class CommandReportRepository : GenericRepository { #region " Fields " private readonly IApplicationDBContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; private readonly OrganizationCommonRepository _organizationCommonRepository; private readonly UserProfileRepository _userProfileRepository; #endregion #region " Constructor and Destuctor " public CommandReportRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor, OrganizationCommonRepository organizationCommonRepository, UserProfileRepository userProfileRepository) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; _organizationCommonRepository = organizationCommonRepository; _userProfileRepository = userProfileRepository; } #endregion #region " Properties " protected Guid UserOrganizationId { get { if (UserId != null || UserId != "") return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!)); else return Guid.Empty; } } #endregion #region " Methods " public async Task> GetCommandType01AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.PlacementEducations) .Include(x => x.Placement) .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType01Response { Education = p.PlacementEducations == null || p.PlacementEducations.Count == 0 ? "" : p.PlacementEducations.FirstOrDefault().Degree, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Oc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), PositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, PositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, PositionType = p.PositionType == null ? "" : p.PositionType.Name, PositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), Salary = p.Amount == null ? "0" : p.Amount.Value.ToNumericNoDecimalText().ToThaiNumber().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber(), ExamNumber = p.ExamNumber == null ? "0" : p.ExamNumber.Value.ToString().ToThaiNumber(), PlacementName = $"{p.Placement.Name.ToThaiNumber()} ครั้งที่ {p.Placement.Round.ToThaiNumber()}/{p.Placement.Year.ToThaiYear().ToString().ToThaiNumber()}", RemarkHorizontal = p.RemarkHorizontal, RemarkVertical = p.RemarkVertical, }).ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType02AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.PlacementEducations) .Include(x => x.Placement) .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType01Response { Education = p.PlacementEducations == null || p.PlacementEducations.Count == 0 ? "" : p.PlacementEducations.FirstOrDefault().Degree, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Oc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), PositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, PositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, PositionType = p.PositionType == null ? "" : p.PositionType.Name, PositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), Salary = p.Amount == null ? "" : p.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber(), ExamNumber = p.ExamNumber == null ? "" : p.ExamNumber.Value.ToString().ToThaiNumber(), PlacementName = $"{p.Placement.Name.ToThaiNumber()} ครั้งที่ {p.Placement.Round.ToThaiNumber()}/{p.Placement.Year.ToThaiYear().ToString().ToThaiNumber()}", RemarkHorizontal = p.RemarkHorizontal, RemarkVertical = p.RemarkVertical, }).ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType03AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.PlacementEducations) .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id join pf in _dbContext.Set() .Include(x => x.Position) .Include(x => x.PositionLevel) .Include(x => x.PositionType) .Include(x => x.PosNo) .Include(x => x.Salaries) on r.CitizenId equals pf.CitizenId orderby r.Sequence select new CommandType03Response { Education = p.PlacementEducations == null || p.PlacementEducations.Count == 0 ? "" : p.PlacementEducations.FirstOrDefault().Degree, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = pf.Oc == null ? "" : pf.Oc.Replace("/", " "), Probation = pf.IsProbation ? "(อยู่ระหว่างปฏิบัติหน้าที่ราชการ)" : "", OldPositionName = pf.Position == null ? "" : pf.Position.Name, OldPositionLevel = pf.PositionLevel == null ? "" : pf.PositionLevel.Name, OldPositionType = pf.PositionType == null ? "" : pf.PositionType.Name, OldPositionNumber = pf.PosNo == null ? "" : pf.PosNo.Name.ToThaiNumber(), OldSalary = pf.Salaries == null || pf.Salaries.Count == 0 ? "" : pf.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = p.Amount == null ? "" : p.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber(), RemarkHorizontal = p.RemarkHorizontal, RemarkVertical = p.RemarkVertical, }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType04AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.PlacementEducations) .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id join pf in _dbContext.Set() .Include(x => x.Position) .Include(x => x.PositionLevel) .Include(x => x.PositionType) .Include(x => x.PosNo) .Include(x => x.Salaries) on r.CitizenId equals pf.CitizenId orderby r.Sequence select new CommandType03Response { Education = p.PlacementEducations == null || p.PlacementEducations.Count == 0 ? "" : p.PlacementEducations.FirstOrDefault().Degree, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = pf.Oc == null ? "" : pf.Oc.Replace("/", " "), OldPositionName = pf.Position == null ? "" : pf.Position.Name, OldPositionLevel = pf.PositionLevel == null ? "" : pf.PositionLevel.Name, OldPositionType = pf.PositionType == null ? "" : pf.PositionType.Name, OldPositionNumber = pf.PosNo == null ? "" : pf.PosNo.Name.ToThaiNumber(), OldSalary = pf.Salaries == null || pf.Salaries.Count == 0 ? "" : pf.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = p.Amount == null ? "" : p.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber() }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType05_06AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id join pf in _dbContext.Set() .Include(x => x.Position) .Include(x => x.PositionLevel) .Include(x => x.PositionType) .Include(x => x.PosNo) .Include(x => x.Salaries) .Include(x => x.Educations) on r.CitizenId equals pf.CitizenId orderby r.Sequence select new CommandType03Response { // Education = pf.Educations == null || pf.Educations.Count == 0 ? "" : // pf.Educations.OrderByDescending(x => x.StartDate).FirstOrDefault().Degree, Education = p.EducationOld == null ? "-" : p.EducationOld, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = pf.Oc == null ? "" : pf.Oc.Replace("/", " "), OldPositionName = pf.Position == null ? "" : pf.Position.Name, OldPositionLevel = pf.PositionLevel == null ? "" : pf.PositionLevel.Name, OldPositionType = pf.PositionType == null ? "" : pf.PositionType.Name, OldPositionNumber = pf.PosNo == null ? "" : pf.PosNo.Name.ToThaiNumber(), // OldSalary = pf.Salaries == null || pf.Salaries.Count == 0 ? "" : pf.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), OldSalaryDate = pf.Salaries == null || pf.Salaries.Count == 0 ? "" : pf.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Date.Value.ToThaiShortDate2().ToThaiNumber(), NewOc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber(), Reason = p.Reason == null ? "-" : p.Reason, }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType07AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id join pf in _dbContext.Set() .Include(x => x.Position) .Include(x => x.PositionLevel) .Include(x => x.PositionType) .Include(x => x.PosNo) .Include(x => x.Salaries) .Include(x => x.Educations) on r.CitizenId equals pf.CitizenId orderby r.Sequence select new CommandType03Response { // Education = pf.Educations == null || pf.Educations.Count == 0 ? "" : // pf.Educations.OrderByDescending(x => x.StartDate).FirstOrDefault().Degree, Education = p.EducationOld == null ? "-" : p.EducationOld, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = pf.Oc == null ? "" : pf.Oc.Replace("/", " "), OldPositionName = pf.Position == null ? "" : pf.Position.Name, OldPositionLevel = pf.PositionLevel == null ? "" : pf.PositionLevel.Name, OldPositionType = pf.PositionType == null ? "" : pf.PositionType.Name, OldPositionNumber = pf.PosNo == null ? "" : pf.PosNo.Name.ToThaiNumber(), // OldSalary = pf.Salaries == null || pf.Salaries.Count == 0 ? "" : pf.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber(), Reason = p.Reason == null ? "-" : p.Reason, }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType08AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.Profile) .ThenInclude(x => x.Salaries) .Include(x => x.Profile) .ThenInclude(x => x.Position) .Include(x => x.Profile) .ThenInclude(x => x.PositionLevel) .Include(x => x.Profile) .ThenInclude(x => x.PositionType) .Include(x => x.Profile) .ThenInclude(x => x.PosNo) .Include(x => x.Profile) .ThenInclude(x => x.Educations) .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType03Response { // Education = p.Profile.Educations == null || p.Profile.Educations.Count == 0 ? "" : // p.Profile.Educations.OrderByDescending(x => x.StartDate).FirstOrDefault().Degree, Education = p.EducationOld == null ? "-" : p.EducationOld, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = p.Profile.Oc == null ? "" : p.Profile.Oc.Replace("/", " "), OldPositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, OldPositionLevel = p.Profile.PositionLevel == null ? "" : p.Profile.PositionLevel.Name, OldPositionType = p.Profile.PositionType == null ? "" : p.Profile.PositionType.Name, OldPositionNumber = p.Profile.PosNo == null ? "" : p.Profile.PosNo.Name.ToThaiNumber(), // OldSalary = p.Profile.Salaries == null || p.Profile.Salaries.Count == 0 ? "" : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = p.OrganizationPosition == null ? "" : _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.PositionDate == null ? "" : p.PositionDate.Value.ToThaiShortDate2().ToThaiNumber(), LeaveDate = p.Profile.LeaveDate == null ? "" : p.Profile.LeaveDate.Value.ToThaiShortDate2().ToThaiNumber(), Reason = p.Reason == null ? "-" : p.Reason, }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType09AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.Profile) .ThenInclude(x => x.Salaries) .Include(x => x.Profile) .ThenInclude(x => x.Position) .Include(x => x.Profile) .ThenInclude(x => x.PositionLevel) .Include(x => x.Profile) .ThenInclude(x => x.PositionType) .Include(x => x.Profile) .ThenInclude(x => x.PosNo) .Include(x => x.Profile) .ThenInclude(x => x.Educations) .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType03Response { // Education = p.Profile.Educations == null || p.Profile.Educations.Count == 0 ? "" : // p.Profile.Educations.OrderByDescending(x => x.StartDate).FirstOrDefault().Degree, Education = p.EducationOld == null ? "-" : p.EducationOld, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = p.Profile.Oc == null ? "" : p.Profile.Oc.Replace("/", " "), OldPositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, OldPositionLevel = p.Profile.PositionLevel == null ? "" : p.Profile.PositionLevel.Name, OldPositionType = p.Profile.PositionType == null ? "" : p.Profile.PositionType.Name, OldPositionNumber = p.Profile.PosNo == null ? "" : p.Profile.PosNo.Name.ToThaiNumber(), // OldSalary = p.Profile.Salaries == null || p.Profile.Salaries.Count == 0 ? "" : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = p.OrganizationPosition == null ? "" : _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.PositionDate == null ? "" : p.PositionDate.Value.ToThaiShortDate2().ToThaiNumber(), LeaveDate = p.Profile.LeaveDate == null ? "" : p.Profile.LeaveDate.Value.ToThaiShortDate2().ToThaiNumber(), MilitaryDate = p.MilitaryDate == null ? "" : p.MilitaryDate.Value.ToThaiShortDate2().ToThaiNumber(), Reason = p.Reason == null ? "-" : p.Reason, }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType11Async(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .Select(r => new CommandType11Response { CommandNo = r.Command.CommandNo.ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber(), CommandAffectDate = r.Command.CommandAffectDate == null ? "" : r.Command.CommandAffectDate.Value.ToThaiFullDate3().ToThaiNumber(), IssuerOrganizationName = r.Command.IssuerOrganizationName, AuthorizedUserFullName = r.Command.AuthorizedUserFullName, AuthorizedPosition = r.Command.AuthorizedPosition, CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PlacementCommandIssuer = r.Command.PlacementCommandIssuer, PlacementCommandNo = r.Command.PlacementCommandNo.ToThaiNumber(), PlacementCommandDate = r.Command.PlacementCommandDate == null ? "" : r.Command.PlacementCommandDate.Value.ToThaiFullDate3().ToThaiNumber(), Subject = $"เรื่อง {r.Command.CommandSubject}", }) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } return raw_data; } catch { throw; } } public async Task> GetCommandType12Async(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .Select(r => new CommandType11Response { CommandNo = r.Command.CommandNo.ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber(), CommandAffectDate = r.Command.CommandAffectDate == null ? "" : r.Command.CommandAffectDate.Value.ToThaiFullDate3().ToThaiNumber(), IssuerOrganizationName = r.Command.IssuerOrganizationName, AuthorizedUserFullName = r.Command.AuthorizedUserFullName, AuthorizedPosition = r.Command.AuthorizedPosition, CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PlacementCommandIssuer = r.Command.PlacementCommandIssuer, PlacementCommandNo = r.Command.PlacementCommandNo.ToThaiNumber(), PlacementCommandDate = r.Command.PlacementCommandDate == null ? "" : r.Command.PlacementCommandDate.Value.ToThaiFullDate3().ToThaiNumber(), Subject = $"เรื่อง {r.Command.CommandSubject}", }) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } return raw_data; } catch { throw; } } public async Task> GetCommandType13AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() on r.RefPlacementProfileId equals p.Id join pf in _dbContext.Set() .Include(x => x.Position) .Include(x => x.PositionLevel) .Include(x => x.PositionType) .Include(x => x.PosNo) .Include(x => x.Salaries) on r.CitizenId equals pf.CitizenId orderby r.Sequence select new CommandType13Response { Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Organization = pf.Oc == null ? "" : pf.Oc.Replace("/", " "), PositionName = pf.Position == null ? "" : pf.Position.Name, PositionLevel = pf.PositionLevel == null ? "" : pf.PositionLevel.Name, PositionType = pf.PositionType == null ? "" : pf.PositionType.Name, PositionNumber = pf.PosNo == null ? "" : pf.PosNo.Name.ToThaiNumber(), Salary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), //Salary = pf.Salaries == null || pf.Salaries.Count == 0 ? "" : pf.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), ActiveDate = p.Date == null ? "" : p.Date.Value.ToThaiShortDate2().ToThaiNumber(), ReceiveOrganizationName = r.Command!.ReceiveOrganizationName, Reason = p.Reason ?? "" }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType14AttachmentAsync(Guid id) { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.OrganizationPosition) .ThenInclude(x => x.Organization) .Include(x => x.PositionPath) .Include(x => x.PositionLevel) .Include(x => x.PositionNumber) .Include(x => x.PositionType) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType03Response { Education = p.EducationOld == null ? "-" : p.EducationOld, Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = p.OrganizationPositionOld, OldPositionName = p.OrganizationPositionOld, OldPositionLevel = p.PositionLevelOld, OldPositionType = p.PositionTypeOld, OldPositionNumber = p.PositionNumberOld.ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = _organizationCommonRepository.GetOrganizationNameFullPath(p.OrganizationPosition!.Organization!.Id, false, false, "/"), NewPositionName = p.PositionPath == null ? "" : p.PositionPath!.Name, NewPositionLevel = p.PositionLevel == null ? "" : p.PositionLevel.Name, NewPositionType = p.PositionType == null ? "" : p.PositionType.Name, NewPositionNumber = p.PositionNumber == null ? "" : p.PositionNumber.Name.ToThaiNumber(), NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.RecruitDate == null ? "" : p.RecruitDate.Value.ToThaiShortDate2().ToThaiNumber(), Reason = p.Reason == null ? "-" : p.Reason, }) .ToList(); return report_data; } public async Task> GetCommandType15AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(p => p.Profile) .ThenInclude(p => p.Position) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType15Response { Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, Organization = p.Organization, StartDate = p.DateStart == null ? "" : p.DateStart.Value.ToThaiShortDate2().ToThaiNumber(), EndDate = p.DateEnd == null ? "" : p.DateEnd.Value.ToThaiShortDate2().ToThaiNumber(), Reason = p.Reason == null ? "" : p.Reason.ToThaiNumber() }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType16Async(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(p => p.Profile) .ThenInclude(p => p.Position) .Include(p => p.Profile) .ThenInclude(p => p.Salaries) .Include(p => p.Profile) .ThenInclude(p => p.Prefix) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType16Response { CommandNo = r.Command.CommandNo.ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber(), CommandAffectDate = r.Command.CommandAffectDate == null ? "" : r.Command.CommandAffectDate.Value.ToThaiFullDate3().ToThaiNumber(), IssuerOrganizationName = r.Command.IssuerOrganizationName, AuthorizedUserFullName = r.Command.AuthorizedUserFullName, AuthorizedPosition = r.Command.AuthorizedPosition, GovAidCommandNo = r.Command.GovAidCommandNo.ToThaiNumber(), GovAidCommandDate = r.Command.GovAidCommandDate == null ? "" : r.Command.GovAidCommandDate.Value.ToThaiFullDate3().ToThaiNumber(), StartDate = p.Date == null ? "" : p.Date.Value.ToThaiFullDate3().ToThaiNumber(), ActiveDate = p.DateRepatriation == null ? "" : p.DateRepatriation.Value.ToThaiFullDate3().ToThaiNumber(), FullName = $"{p.Profile.Prefix!.Name}{p.Profile.FirstName!} {p.Profile.LastName!}", Subject = $"เรื่อง {r.Command.CommandSubject}", }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType17AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(p => p.Profile) .ThenInclude(p => p.Position) .Include(p => p.Profile) .ThenInclude(p => p.Salaries) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType17Response { Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, Organization = p.OrganizationPositionOld, PositionLevel = p.PositionLevelOld, PositionType = p.PositionTypeOld, PositionNumber = p.PositionNumberOld.ToThaiNumber(), ActiveDate = p.ActiveDate == null ? "" : p.ActiveDate.Value.ToThaiShortDate2().ToThaiNumber(), Salary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), //Salary = p.Profile.Salaries == null || p.Profile.Salaries.Count == 0 ? "" : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), Remark = p.Reason ?? "", RemarkHorizontal = p.RemarkHorizontal, }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType18Async(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(p => p.Profile) .ThenInclude(p => p.Position) .Include(p => p.Profile) .ThenInclude(p => p.Salaries) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType18Response { CommandNo = r.Command.CommandNo.ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber(), CommandAffectDate = r.Command.CommandAffectDate == null ? "" : r.Command.CommandAffectDate.Value.ToThaiFullDate3().ToThaiNumber(), IssuerOrganizationName = r.Command.IssuerOrganizationName, AuthorizedUserFullName = r.Command.AuthorizedUserFullName, AuthorizedPosition = r.Command.AuthorizedPosition, CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, Organization = p.OrganizationPositionOld, PositionLevel = p.PositionLevelOld, PositionType = p.PositionTypeOld, PositionNumber = p.PositionNumberOld.ToThaiNumber(), Salary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), //Salary = p.Profile.Salaries == null || p.Profile.Salaries.Count == 0 ? "" : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), Fault = r.Command.Fault.ToThaiNumber(), GuiltyBasis = r.Command.GuiltyBasis.ToThaiNumber(), ConclusionFireNo = r.Command!.ConclusionFireNo.ToThaiNumber(), ConclusionFireDate = r.Command.ConclusionFireDate == null ? "" : r.Command.ConclusionFireDate.Value.ToThaiFullDate3().ToThaiNumber(), ConclusionFireResolution = r.Command.ConclusionFireResolution.ToThaiNumber(), Subject = $"เรื่อง {r.Command.CommandSubject}", }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType19Async(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(p => p.Profile) .ThenInclude(p => p.Position) .Include(p => p.Profile) .ThenInclude(p => p.Salaries) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType18Response { CommandNo = r.Command.CommandNo.ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber(), CommandAffectDate = r.Command.CommandAffectDate == null ? "" : r.Command.CommandAffectDate.Value.ToThaiFullDate3().ToThaiNumber(), IssuerOrganizationName = r.Command.IssuerOrganizationName, AuthorizedUserFullName = r.Command.AuthorizedUserFullName, AuthorizedPosition = r.Command.AuthorizedPosition, CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, Organization = p.OrganizationPositionOld, PositionLevel = p.PositionLevelOld, PositionType = p.PositionTypeOld, PositionNumber = p.PositionNumberOld.ToThaiNumber(), Salary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), //Salary = p.Profile.Salaries == null || p.Profile.Salaries.Count == 0 ? "" : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), Fault = r.Command.Fault.ToThaiNumber(), GuiltyBasis = r.Command.GuiltyBasis.ToThaiNumber(), ConclusionFireNo = r.Command!.ConclusionFireNo.ToThaiNumber(), ConclusionFireDate = r.Command.ConclusionFireDate == null ? "" : r.Command.ConclusionFireDate.Value.ToThaiFullDate3().ToThaiNumber(), ConclusionFireResolution = r.Command.ConclusionFireResolution.ToThaiNumber(), Subject = $"เรื่อง {r.Command.CommandSubject}", }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType20Async(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(p => p.Profile) .ThenInclude(p => p.Position) .Include(p => p.Profile) .ThenInclude(p => p.Salaries) on r.RefPlacementProfileId equals p.Id select new CommandType18Response { CommandNo = r.Command.CommandNo.ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber(), CommandAffectDate = r.Command.CommandAffectDate == null ? "" : r.Command.CommandAffectDate.Value.ToThaiFullDate3().ToThaiNumber(), IssuerOrganizationName = r.Command.IssuerOrganizationName, AuthorizedUserFullName = r.Command.AuthorizedUserFullName, AuthorizedPosition = r.Command.AuthorizedPosition, CitizenId = r.CitizenId, FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", PositionName = p.Profile.Position == null ? "" : p.Profile.Position.Name, Organization = p.OrganizationPositionOld, PositionLevel = p.PositionLevelOld, PositionType = p.PositionTypeOld, PositionNumber = p.PositionNumberOld.ToThaiNumber(), Salary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), //Salary = p.Profile.Salaries == null || p.Profile.Salaries.Count == 0 ? "" : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), Fault = r.Command.Fault.ToThaiNumber(), GuiltyBasis = r.Command.GuiltyBasis.ToThaiNumber(), ConclusionFireNo = r.Command!.ConclusionFireNo.ToThaiNumber(), ConclusionFireDate = r.Command.ConclusionFireDate == null ? "" : r.Command.ConclusionFireDate.Value.ToThaiFullDate3().ToThaiNumber(), ConclusionFireResolution = r.Command.ConclusionFireResolution.ToThaiNumber() }) .ToList(); return report_data; } catch { throw; } } public async Task> GetCommandType21AttachmentAsync(Guid id) { try { var raw_data = await _dbContext.Set() .Include(c => c.Command) .Where(c => c.Command.Id == id) .ToListAsync(); if (raw_data == null) { throw new Exception(GlobalMessages.CommandNotFound); } var report_data = (from r in raw_data join p in _dbContext.Set() .Include(x => x.Profile) .ThenInclude(x => x.Position) .Include(x => x.Profile) .ThenInclude(x => x.PositionLevel) .Include(x => x.Profile) .ThenInclude(x => x.PositionType) .Include(x => x.Profile) .ThenInclude(x => x.PosNo) .Include(x => x.OrgEmployee) .ThenInclude(x => x.OrganizationAgency) .ThenInclude(x => x.OrganizationOrganization) //.Include(x => x.OrgEmployee) //.ThenInclude(x => x.PosNo) .Include(x => x.OrgEmployee) .ThenInclude(x => x.PositionEmployeePosition) .Include(x => x.OrgEmployee) .ThenInclude(x => x.OrganizationPositionEmployeeLevels) .ThenInclude(x => x.PositionEmployeeLevel) on r.RefPlacementProfileId equals p.Id orderby r.Sequence select new CommandType21Response { Seq = r.Sequence.ToString().ToThaiNumber(), CitizenId = r.CitizenId.ToThaiNumber(), FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Organization = p.OrgEmployee!.OrganizationAgency!.OrganizationOrganization!.Name, PositionName = p.OrgEmployee.PositionEmployeePosition == null ? "" : p.OrgEmployee.PositionEmployeePosition.Name.ToThaiNumber(), PositionLevel = p.OrgEmployee.OrganizationPositionEmployeeLevels == null ? "" : p.OrgEmployee.OrganizationPositionEmployeeLevels.First().PositionEmployeeLevel.Name.ToThaiNumber(), PositionType = p.Profile.PositionType == null ? "" : p.Profile.PositionType.Name.ToThaiNumber(), PositionNumber = p.OrgEmployee.PosNo == null ? "" : p.OrgEmployee.PosNo.ToThaiNumber(), Salary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), RetireDate = p.Profile.BirthDate.CalculateRetireDate().ToThaiFullDate3().ToThaiNumber(), CommandYear = r.Command.CommandYear.ToInteger().ToThaiYear().ToString().ToThaiNumber() }) .ToList(); return report_data; } catch { throw; } } #endregion } }