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 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 RetireReportRepository { #region " Fields " private readonly IApplicationDBContext _dbContext; private readonly IWebHostEnvironment _hostingEnvironment; private readonly MinIOService _documentService; private readonly OrganizationCommonRepository _organizationCommonRepository; #endregion #region " Constructor and Destructor " public RetireReportRepository(IApplicationDBContext dbContext, MinIOService documentService, OrganizationCommonRepository organizationCommonRepository, IWebHostEnvironment hostEnvironment) { _dbContext = dbContext; _hostingEnvironment = hostEnvironment; _organizationCommonRepository = organizationCommonRepository; _documentService = documentService; } #endregion #region " Methods " //public async Task> GetListRetirePeriodAsync(Guid Id) //{ // try // { // // 1. query // //var data = await _dbContext.Set() // // .Include(x => x.RetirementProfiles) // // .ThenInclude(x => x.Profile) // // .ToListAsync(); // // 2. data not found throw exception // // 3. Load Report File // //return data; // } // catch // { // throw; // } //} #region รายงานประกาศเกษียณ public async Task GetProfileRetirementdAsync(Guid retireId) { var retire = await _dbContext.Set() .Include(x => x.RetirementProfiles) .FirstOrDefaultAsync(x => x.Id == retireId); // var retires = new List(); if (retire == null) { var retireHistorys = await _dbContext.Set().AsQueryable() .FirstOrDefaultAsync(x => x.Id == retireId); if (retireHistorys == null) return null; //var profile_retireHistory = await _dbContext.Set() // .Where(x => x.RetirementPeriod == retire) // .OrderBy(x => x.Order) // .Select(x => new // { // order = x.Order, // id = x.Id, // reason = x.Reason, // remove = x.Remove, // profileId = x.Profile.Id, // citizenId = x.Profile.CitizenId, // prefix = x.Profile.Prefix == null ? string.Empty : x.Profile.Prefix.Name, // fullName = $"{x.Profile.FirstName} {x.Profile.LastName}", // organizationOrganization = x.Profile.OrganizationOrganization, // oc = x.Profile.Oc, // position = x.Profile.Position == null ? string.Empty : x.Profile.Position.Name, // positionType = x.Profile.PositionType == null ? string.Empty : x.Profile.PositionType.Name, // positionExecutive = x.Profile.PositionExecutive, // posNo = x.Profile.PosNo == null ? string.Empty : x.Profile.PosNo.Name, // positionEmployeePosition = x.Profile.PositionEmployeePosition, // positionEmployeeLevel = x.Profile.PositionEmployeeLevel, // positionEmployeeGroup = x.Profile.PositionEmployeeGroup, // posNoEmployee = x.Profile.PosNoEmployee, // }) // .ToListAsync(); //return new { retireHistorys.Detail, retireHistorys.Id, retireHistorys.CreatedAt, Year = retireHistorys.Year.ToThaiYear().ToString().ToThaiNumber(), retireHistorys.Round, retireHistorys.Type, retireHistorys.TypeReport, Total = retireHistorys.Total.ToString().ToThaiNumber(), profile = profile_retireHistory }; using (var client = new HttpClient()) { var url = await _documentService.ImagesPathByName($"{retireHistorys.ProfileFile}.json"); var responseTask = client.GetAsync(url); var results = responseTask.Result; var json = results.Content.ReadAsStringAsync().Result; List profiles = JsonConvert.DeserializeObject>(json); if (retireHistorys.TypeReport != null) { retireHistorys.Total = profiles.Where(x => x.remove == retireHistorys.TypeReport).Count(); profiles = profiles.Where(x => x.remove == retireHistorys.TypeReport).OrderBy(x => x.order).ToList(); } else { profiles = profiles.OrderBy(x => x.order).ToList(); } var mapProfiles = new List(); if (profiles.Count > 0) { mapProfiles = profiles.Select((profile, index) => new ProfileRetireJsonRequest { order = (index + 1).ToString().ToThaiNumber(), fullName = $"{profile.prefix}{profile.firstName} {profile.lastName}", root = profile.root, position = profile.position != "" && profile.position != null ? profile.position : "-", posNo = profile.posNo != "" && profile.posNo != null ? profile.posNo?.ToThaiNumber() : "-", reason = profile.reason != "" && profile.reason != null ? profile.reason : "-", }).ToList(); } string SignDate = retireHistorys.SignDate != null ? DateTime.Parse(retireHistorys.SignDate.ToString()).ToThaiFullDate().ToString().ToThaiNumber() : "-"; return new { SignDate, retireHistorys.Detail, retireHistorys.Id, retireHistorys.CreatedAt, Year = retireHistorys.Year.ToThaiYear().ToString().ToThaiNumber(), retireHistorys.Round, retireHistorys.Type, retireHistorys.TypeReport, Total = retireHistorys.Total.ToString().ToThaiNumber(), profiles = mapProfiles }; } } else { var profile_retire = await _dbContext.Set() .Include(x => x.RetirementPeriod) .Where(x => x.RetirementPeriod == retire && (retire.TypeReport == null || retire.TypeReport == x.Remove)) .OrderBy(x => x.Order) .Select(x => new { order = x.Order, id = x.Id, reason = x.Reason, remove = x.Remove, profileId = x.profileId, prefix = x.prefix, firstName = x.firstName, lastName = x.lastName, citizenId = x.citizenId, root = x.root, rootId = x.rootId, rootShortName = x.rootShortName, child1 = x.child1, child1Id = x.child1Id, child1ShortName = x.child1ShortName, child2 = x.child2, child2Id = x.child2Id, child2ShortName = x.child2ShortName, child3 = x.child3, child3Id = x.child3Id, child3ShortName = x.child3ShortName, child4 = x.child4, child4Id = x.child4Id, child4ShortName = x.child4ShortName, posMasterNo = x.posMasterNo, position = x.position, posTypeId = x.posTypeId, posTypeName = x.posTypeName, posTypeRank = x.posTypeRank, posLevelId = x.posLevelId, posLevelName = x.posLevelName, posLevelRank = x.posLevelRank, posExecutiveId = x.posExecutiveId, posExecutiveName = x.posExecutiveName, posNo = x.posNo, }) .ToListAsync(); // foreach (var r in profile_retire) // { // var data = new ProfileJsonRequest // { // order = r.order, // id = r.id, // reason = r.reason, // remove = r.remove, // profileId = r.profileId, // citizenId = r.citizenId, // prefix = r.prefix, // fullName = r.fullName, // organizationOrganization = r.organizationOrganization, // oc = r.ocId == null ? null : _organizationCommonRepository.GetOrganizationNameFullPath(r.ocId.Value), // position = r.position, // positionType = r.positionType, // positionExecutive = r.positionExecutive, // posNo = r.posNo, // positionEmployeePosition = r.positionEmployeePosition, // positionEmployeeLevel = r.positionEmployeeLevel, // positionEmployeeGroup = r.positionEmployeeGroup, // posNoEmployee = r.posNoEmployee, // }; // retires.Add(data); // } var mapProfiles = new List(); if (profile_retire.Count > 0) { mapProfiles = profile_retire.Select((profile, index) => new ProfileRetireJsonRequest { order = (index + 1).ToString().ToThaiNumber(), fullName = $"{profile.prefix}{profile.firstName} {profile.lastName}", root = profile.root, position = profile.position != "" && profile.position != null ? profile.position : "-", posNo = profile.posNo != "" && profile.posNo != null ? profile.posNo?.ToThaiNumber() : "-", reason = profile.reason != "" && profile.reason != null ? profile.reason : "-", }).ToList(); } string SignDate = retire.SignDate != null ? DateTime.Parse(retire.SignDate.ToString()).ToThaiFullDate().ToString().ToThaiNumber() : "-"; return new { SignDate, retire.Detail, retire.Id, retire.CreatedAt, Year = retire.Year.ToThaiYear().ToString().ToThaiNumber(), retire.Round, retire.Type, retire.TypeReport, Total = profile_retire.Count.ToString().ToThaiNumber(), profiles = mapProfiles }; } } #endregion #region รายงานบันทึกการถึงแก่กรรม public async Task GetRetirementDeceasedAsync(Guid id) { var data = await _dbContext.Set().AsQueryable() .Where(x => x.Id == id) .Select(p => new { p.Id, p.prefix, p.profileId, p.firstName, p.lastName, p.root, p.rootShortName, p.child1, p.child1ShortName, p.child2, p.child2ShortName, p.child3, p.child3ShortName, p.child4, p.child4ShortName, p.posMasterNo, p.posLevelName, p.posTypeName, // ProfileId = p.Profile.Id, // Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name, // PrefixId = p.Profile.Prefix == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Prefix.Id, // p.Profile.FirstName, // p.Profile.LastName, // Position = p.Profile.Position == null ? null : p.Profile.Position.Name, // PositionId = p.Profile.Position == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Position.Id, // PositionType = p.Profile.PositionType == null ? null : p.Profile.PositionType.Name, // PositionTypeId = p.Profile.PositionType == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.PositionType.Id, // p.Profile.PositionLine, // p.Profile.PositionLineId, // PositionLevel = p.Profile.PositionLevel == null ? null : p.Profile.PositionLevel.Name, // PositionLevelId = p.Profile.PositionLevel == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.PositionLevel.Id, // p.Profile.PositionExecutive, // p.Profile.PositionExecutiveId, // Organization = p.Profile.Oc, // OrganizationId = p.Profile.OcId, p.position, p.Number, p.Date, p.Location, p.Reason, }) .FirstOrDefaultAsync(); if (data == null) return null; // string Prefix = string.IsNullOrEmpty(data.Prefix.ToString()) ? string.Empty : data.Prefix.ToString(); // string FirstName = string.IsNullOrEmpty(data.FirstName.ToString()) ? string.Empty : data.FirstName.ToString(); // string LastName = string.IsNullOrEmpty(data.LastName.ToString()) ? string.Empty : data.LastName.ToString(); // string FullName = $"{Prefix} {FirstName} {LastName}"; string Date = string.IsNullOrEmpty(data.Date.ToString()) ? "-" : DateTime.Parse(data.Date.ToString()).ToThaiFullDate().ToString().ToThaiNumber(); string CurrentDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")).ToThaiFullDate().ToString().ToThaiNumber(); return new { FullName = $"{data.prefix} {data.firstName} {data.lastName}", Date, CurrentDate, data.profileId, data.prefix, data.firstName, data.lastName, data.root, data.rootShortName, data.child1, data.child1ShortName, data.child2, data.child2ShortName, data.child3, data.child3ShortName, data.child4, data.child4ShortName, data.posMasterNo, data.posLevelName, data.posTypeName, Number = data.Number != null ? data.Number.ToThaiNumber() : "-", Location = data.Location != null ? data.Location.ToThaiNumber() : "-", data.Reason, Position = data.position, }; } #endregion #region หัวกระดาษหนังสือเวียน public async Task GetHeadRetirementDeceasedAsync(Guid id) { var data = await _dbContext.Set().AsQueryable() // .Include(x => x.Profile) .Where(x => x.Id == id) .FirstOrDefaultAsync(); var oc = ""; if (data == null) return new { Oc = oc, Number = $"กท /", Date = "", Subject = "ข้าราชการถึงแก่กรรม", Send = "หัวหน้าสำนักงาน ก.ก.", }; // if (data.Profile.OcId != null) // { // var organization = await _dbContext.Set().AsQueryable() // .Where(x => x.Id == data.Profile.OcId) // .FirstOrDefaultAsync(); // if (organization != null) // { // var organizationAgency = await _dbContext.Set().AsQueryable() // .Where(x => x.Id == organization.OrganizationAgencyId) // .FirstOrDefaultAsync(); // if (organizationAgency != null) // { // var agency = await _dbContext.Set().AsQueryable() // .Where(x => x.Id == organizationAgency.Id) // .FirstOrDefaultAsync(); // if (agency != null) // { // oc = agency.OrganizationOrganization?.Name; // } // } // } // } return new { Oc = oc, Number = $"กท /{(data.CreatedAt.Year.ToThaiYear()).ToString().ToThaiNumber()}", Date = data.CreatedAt.ToThaiFullDate2().ToString().ToThaiNumber(), Subject = "ข้าราชการถึงแก่กรรม", Send = "หัวหน้าสำนักงาน ก.ก.", }; } #endregion #region บันทึกหนังสือเวียน public async Task UploadFileRetirementDeceasedAsync(Guid id, Domain.Models.Documents.Document file) { var data = await _dbContext.Set().AsQueryable() // .Include(x => x.Profile) .Where(x => x.Id == id) .FirstOrDefaultAsync(); if (data != null) data.DocumentForward = file; await _dbContext.SaveChangesAsync(); } #endregion #region แบบฟอร์มหนังสือขอลาออกจากราชการ public async Task GetResignByUser(Guid id) { var data = await _dbContext.Set().AsQueryable() .Where(x => x.Id == id) .Select(p => new { p.Id, p.prefix, p.firstName, p.lastName, p.profileId, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.Status, salary = p.AmountOld, p.ApproveReason, p.RejectReason, p.IsActive, p.CreatedAt, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.OligarchReject, p.OligarchApproveReason, p.OligarchRejectReason, p.OligarchRejectDate, p.CommanderReject, p.CommanderApproveReason, p.CommanderRejectReason, p.CommanderRejectDate, p.RemarkHorizontal, }) .FirstOrDefaultAsync(); if (data == null) return null; var _data = new { data.Id, data.profileId, data.prefix, data.firstName, data.lastName, data.Location, FullName = $"{data.prefix}{data.firstName} {data.lastName}", SendDate = string.IsNullOrEmpty(data.SendDate.ToString()) ? string.Empty : DateTime.Parse(data.SendDate.ToString()).ToThaiFullDate().ToString().ToThaiNumber(), ActiveDate = string.IsNullOrEmpty(data.ActiveDate.ToString()) ? string.Empty : DateTime.Parse(data.ActiveDate.ToString()).ToThaiFullDate().ToString().ToThaiNumber(), data.Reason, data.Status, data.salary, data.PositionTypeOld, data.PositionLevelOld, data.PositionNumberOld, data.OrganizationPositionOld, data.ApproveReason, data.RejectReason, data.IsActive, data.CreatedAt, data.OligarchReject, data.OligarchApproveReason, data.OligarchRejectReason, OligarchRejectDate = string.IsNullOrEmpty(data.OligarchRejectDate.ToString()) ? string.Empty : DateTime.Parse(data.OligarchRejectDate.ToString()).ToThaiFullDate().ToString().ToThaiNumber(), data.CommanderReject, data.CommanderApproveReason, data.CommanderRejectReason, CommanderRejectDate = string.IsNullOrEmpty(data.CommanderRejectDate.ToString()) ? string.Empty : DateTime.Parse(data.CommanderRejectDate.ToString()).ToThaiFullDate().ToString().ToThaiNumber(), data.RemarkHorizontal, }; return _data; } #endregion #endregion } }