diff --git a/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs b/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs index fb09d9ab..da4809da 100644 --- a/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs +++ b/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs @@ -3,6 +3,7 @@ using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Extensions; +using BMA.EHR.Domain.Models.Commands.Core; using BMA.EHR.Domain.Models.HR; using BMA.EHR.Domain.Models.Insignias; using BMA.EHR.Domain.Models.MetaData; @@ -14,7 +15,9 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Metadata; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Net; +using System.Net.Http.Headers; namespace BMA.EHR.Application.Repositories.Reports { @@ -743,31 +746,81 @@ namespace BMA.EHR.Application.Repositories.Reports } //44-บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ - public async Task GetSummaryCoinReport(Guid id) + public async Task GetSummaryCoinReport(Guid id, string type = null, int node = -1, Guid nodeId = default) { 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) - .Where(x => x.Request.Period.Id == period.Id) - .Where(x => x.IsApprove == true) - .Where(x => x.Status == "PENDING") - .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, - Gendor = x.Gender, //_userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "", - RequestInsigniaName = x.RequestInsignia.Name, - InsigniaInitial = $"{x.RequestInsignia.Name}({x.RequestInsignia.ShortName})", - OcId = x.Request.OrganizationId - }) - .ToListAsync(); + /* var data_insignia = await _dbContext.Set() + //.Include(x => x.Profile) + .Where(x => x.Request.Period.Id == period.Id) + .Where(x => x.IsApprove == true) + .Where(x => x.Status == "PENDING") + .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, + Gendor = x.Gender, //_userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "", + RequestInsigniaName = x.RequestInsignia.Name, + InsigniaInitial = $"{x.RequestInsignia.Name}({x.RequestInsignia.ShortName})", + OcId = x.Request.OrganizationId + }) + .ToListAsync();*/ + var data_insigniaQuery = _dbContext.Set() + .Where(x => x.Request.Period.Id == period.Id) + .Where(x => x.IsApprove == true) + .Where(x => x.Status == "PENDING") + .Where(x => x.RequestInsignia.InsigniaType != null) + .Where(x => x.RequestInsignia.InsigniaType.Name == "เหรียญบำเหน็จในราชการ"); - var insignia = (from r in data_insignia + if (type == "officer") + { + data_insigniaQuery = data_insigniaQuery.Where(r => r.ProfileType == "officer"); + } + else if (type == "employee") + { + data_insigniaQuery = data_insigniaQuery.Where(r => r.ProfileType == "employee"); + } + + switch (node) + { + case 0: + data_insigniaQuery = data_insigniaQuery.Where(r => r.RootId == nodeId); + break; + + case 1: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child1Id == nodeId); + break; + + case 2: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child2Id == nodeId); + break; + + case 3: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child3Id == nodeId); + break; + + case 4: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child4Id == nodeId); + break; + + default: + break; + } + var data = await data_insigniaQuery + .Select(x => new + { + Gendor = x.Gender, // Gender as is, directly from x.Gender + RequestInsigniaName = x.RequestInsignia.Name, // Name of the Insignia + InsigniaInitial = $"{x.RequestInsignia.Name}({x.RequestInsignia.ShortName})", // Insignia's full name and short name + OcId = x.Request.OrganizationId // Organization ID + }) + .ToListAsync(); + + var insignia = (from r in data group r by new { OcId = r.OcId, InsigniaInitial = r.InsigniaInitial } into g select new { @@ -832,7 +885,7 @@ namespace BMA.EHR.Application.Repositories.Reports } //45-บัญชีแสดงรายชื่อผู้ขอพระราชทานเหรียญจักรพรรดิมาลา - public async Task GetCoinReport(Guid id) + public async Task GetCoinReport(Guid id, string type = null, int node = -1, Guid nodeId = default) { var period = await _dbContext.Set() .FirstOrDefaultAsync(x => x.Id == id); @@ -856,45 +909,89 @@ namespace BMA.EHR.Application.Repositories.Reports && r.Status == "PENDING" && r.RequestInsignia.InsigniaType != null && r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ" - select new + select new { Male = r.Gender == "ชาย" ? 1 : 0, Female = r.Gender == "หญิง" ? 1 : 0, }) .Distinct() .ToList(); - var data = (from r in await _dbContext.Set() - //.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.Status == "PENDING" - && r.RequestInsignia.InsigniaType != null - && r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ" - select new - { - InsigniaInitial = r.RequestInsignia.ShortName, - InsigniaName = r.RequestInsignia.Name, - ProfileId = r.ProfileId, - FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", - // $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}", - Gender = r.Gender, - Male = gender.Sum(x => x.Male), - Female = gender.Sum(x => x.Female), - InsigniaId = r.RequestInsignia.Id, - OCName = _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root, //_organizationCommonRepository.GetOrganizationNameFullPath(g.Key.OcId, false, false), - }) - .Distinct() - .ToList(); + var dataQuery = _dbContext.Set() + .Include(x => x.Request) + .ThenInclude(x => x.Period) + .Include(x => x.Request) + /*.ThenInclude(x => x.Organization)*/ + .Include(x => x.RequestInsignia) + .ThenInclude(x => x.InsigniaType) + .Where(r => r.Request.Period == period + && r.IsApprove == true + && r.Status == "PENDING" + && r.RequestInsignia.InsigniaType != null + && r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"); + + if (type == "officer") + { + dataQuery = dataQuery.Where(r => r.ProfileType == "officer"); + } + else if (type == "employee") + { + dataQuery = dataQuery.Where(r => r.ProfileType == "employee"); + } + + switch (node) + { + case 0: + dataQuery = dataQuery.Where(r => r.RootId == nodeId); + break; + + case 1: + dataQuery = dataQuery.Where(r => r.Child1Id == nodeId); + break; + + case 2: + dataQuery = dataQuery.Where(r => r.Child2Id == nodeId); + break; + + case 3: + dataQuery = dataQuery.Where(r => r.Child3Id == nodeId); + break; + + case 4: + dataQuery = dataQuery.Where(r => r.Child4Id == nodeId); + break; + + default: + break; + } + + // Grouping by ProfileId and InsigniaId, and calculating the sum for Male and Female + var data = await dataQuery + .GroupBy(r => new + { + r.RequestInsignia.ShortName, + r.RequestInsignia.Name, + r.ProfileId, + r.Prefix, + r.FirstName, + r.LastName, + r.Gender, + r.RequestInsignia.Id, + r.Request.OrganizationId + }) + .Select(group => new + { + InsigniaInitial = group.Key.ShortName, + InsigniaName = group.Key.Name, + ProfileId = group.Key.ProfileId, + FullName = $"{group.Key.Prefix}{group.Key.FirstName} {group.Key.LastName}", + Gender = group.Key.Gender, + Male = group.Count(r => r.Gender == "Male"), // Count male entries + Female = group.Count(r => r.Gender == "Female"), // Count female entries + InsigniaId = group.Key.Id, + OCName = _userProfileRepository.GetOc(group.Key.OrganizationId, 0, AccessToken).Root + }) + .Distinct() + .ToListAsync(); // 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 }) @@ -959,6 +1056,115 @@ namespace BMA.EHR.Application.Repositories.Reports return s_data; } + //47-บัญชีระดับผลการประเมินผลการปฏิบัติราชการในรอบ 5 ปี + public async Task GetEvaluationResultReport(Guid id, string type = null, int node = -1, Guid nodeId = default) + { + var period = await _dbContext.Set() + .FirstOrDefaultAsync(x => x.Id == id); + if (period == null) + throw new Exception(GlobalMessages.InsigniaPeriodNotFound); + + var data_insigniaQuery = _dbContext.Set() + .Where(x => x.Request.Period.Id == period.Id) + .Where(x => x.IsApprove == true) + .Where(x => x.Status == "PENDING") + .Where(x => x.RequestInsignia.InsigniaType != null); + /*.Where(x => x.RequestInsignia.InsigniaType.Name == "เหรียญบำเหน็จในราชการ");*/ + + if (type == "officer") + { + data_insigniaQuery = data_insigniaQuery.Where(r => r.ProfileType == "officer"); + } + else if (type == "employee") + { + data_insigniaQuery = data_insigniaQuery.Where(r => r.ProfileType == "employee"); + } + + switch (node) + { + case 0: + data_insigniaQuery = data_insigniaQuery.Where(r => r.RootId == nodeId); + break; + + case 1: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child1Id == nodeId); + break; + + case 2: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child2Id == nodeId); + break; + + case 3: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child3Id == nodeId); + break; + + case 4: + data_insigniaQuery = data_insigniaQuery.Where(r => r.Child4Id == nodeId); + break; + + default: + break; + } + var data = await data_insigniaQuery + .Select(x => new + { + FullName = $"{x.Prefix}{x.FirstName} {x.LastName}", + RequestInsigniaName = x.RequestInsignia.Name, // Name of the Insignia + }) + .ToListAsync(); + + /* var seq = 1; + foreach (var d in response!.result) + { + var _baseAPI = _configuration["API"]; + var _apiUrl = $"{_baseAPI}/org/profile/profileid/position/{d.person.id}"; + using (var _client = new HttpClient()) + { + _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + _client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); + var _req = new HttpRequestMessage(HttpMethod.Get, _apiUrl); + var _res = await _client.SendAsync(_req); + var _result = await _res.Content.ReadAsStringAsync(); + + var org = JsonConvert.DeserializeObject(_result); + + if (org == null || org.result == null) + continue; + + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = org.result.citizenId == null ? "" : org.result.citizenId, + Prefix = org.result.prefix == null ? "" : org.result.prefix, + FirstName = org.result.firstName == null ? "" : org.result.firstName, + LastName = org.result.lastName == null ? "" : org.result.lastName, + RefPlacementProfileId = org.result.profileId == null ? null : Guid.Parse(org.result.profileId), + }; + seq++; + resultData.Add(receiver); + } + }*/ + var insignia = data.Select((r, index) => new + { + RowNo = (index + 1).ToNumericText().ToThaiNumber(), + FullName = r.FullName, + RequestInsigniaName = r.RequestInsigniaName, + ResultY1APR = "-", + ResultY1OCT = "-", + ResultY2APR = "-", + ResultY2OCT = "-", + ResultY3APR = "-", + ResultY3OCT = "-", + ResultY4APR = "-", + ResultY4OCT = "-", + ResultY5APR = "-", + ResultY5OCT = "-", + Remark = "", + }).ToList(); + + return insignia; + } + //noti ยื่นเสนอคน public async Task NotifyInsignia() { diff --git a/BMA.EHR.Insignia/Controllers/InsigniaReportController.cs b/BMA.EHR.Insignia/Controllers/InsigniaReportController.cs index 51cd0c0c..7e34893c 100644 --- a/BMA.EHR.Insignia/Controllers/InsigniaReportController.cs +++ b/BMA.EHR.Insignia/Controllers/InsigniaReportController.cs @@ -94,7 +94,7 @@ namespace BMA.EHR.Insignia.Service.Controllers return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } - var data = await _repository.GetCoinReport(req.roundId); + var data = await _repository.GetCoinReport(req.roundId, type, req.node, req.nodeId); var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId); var dataResult = new List(); @@ -161,9 +161,13 @@ namespace BMA.EHR.Insignia.Service.Controllers { row = mergeList.Count + 1, ColLeft = colLeft.ToString().ToThaiNumber(), - NameLeft = left.ElementAt(i).GetType().GetProperty("FullName").GetValue(left.ElementAt(i)), + NameLeft = string.IsNullOrEmpty(left.ElementAt(i).GetType().GetProperty("FullName").GetValue(left.ElementAt(i))?.ToString()) + ? null + : left.ElementAt(i).GetType().GetProperty("FullName").GetValue(left.ElementAt(i)), ColRight = colRight.ToString().ToThaiNumber(), - NameRight = right.ElementAt(i).GetType().GetProperty("FullName").GetValue(right.ElementAt(i)), + NameRight = string.IsNullOrEmpty(right.ElementAt(i).GetType().GetProperty("FullName").GetValue(right.ElementAt(i))?.ToString()) + ? null + : right.ElementAt(i).GetType().GetProperty("FullName").GetValue(right.ElementAt(i)), InsigniaInitial = left.ElementAt(i).GetType().GetProperty("InsigniaInitial").GetValue(left.ElementAt(i)), InsigniaName = left.ElementAt(i).GetType().GetProperty("InsigniaName").GetValue(left.ElementAt(i)), Range = range.ToThaiNumber(), @@ -200,9 +204,9 @@ namespace BMA.EHR.Insignia.Service.Controllers } #endregion - #region บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ + #region บัญชีระดับผลการประเมินผลการปฏิบัติราชการในรอบ 5 ปี /// - /// บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ + /// บัญชีระดับผลการประเมินผลการปฏิบัติราชการในรอบ 5 ปี /// /// type /// @@ -210,7 +214,7 @@ namespace BMA.EHR.Insignia.Service.Controllers /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("report2/{type}")] - public async Task> GetInsigniaReport2Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type) + public async Task> GetInsigniaReport3Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type) { try { @@ -220,7 +224,49 @@ namespace BMA.EHR.Insignia.Service.Controllers { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } - var data = await _repository.GetSummaryCoinReport(req.roundId); + + var data = await _repository.GetEvaluationResultReport(req.roundId, type, req.node, req.nodeId); + + var result = new + { + template = "reportInsignia2", + reportName = "reportInsignia2", + data = new + { + data = data, + } + }; + return Success(result); + + } + catch + { + throw; + } + } + #endregion + + #region บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ + /// + /// บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ + /// + /// type + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("report3/{type}")] + public async Task> GetInsigniaReport2Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type) + { + try + { + /* var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_INSIGNIA_REPORT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + }*/ + var data = await _repository.GetSummaryCoinReport(req.roundId, type, req.node, req.nodeId); var summaryTotal = await _repository.GetSummaryTotalCoinReport(req.roundId); var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId); @@ -242,8 +288,8 @@ namespace BMA.EHR.Insignia.Service.Controllers var result = new { - template = "reportInsignia2", - reportName = "reportInsignia2", + template = "reportInsignia3", + reportName = "reportInsignia3", data = new { yearInsignalPeriod, @@ -261,44 +307,6 @@ namespace BMA.EHR.Insignia.Service.Controllers } #endregion - #region บัญชีระดับผลการประเมินผลการปฏิบัติราชการในรอบ 5 ปี - /// - /// บัญชีระดับผลการประเมินผลการปฏิบัติราชการในรอบ 5 ปี - /// - /// type - /// - /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ - /// ไม่ได้ Login เข้าระบบ - /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpPost("report3/{type}")] - public async Task> GetInsigniaReport3Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type) - { - try - { - var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_INSIGNIA_REPORT"); - var jsonData = JsonConvert.DeserializeObject(getPermission); - if (jsonData["status"]?.ToString() != "200") - { - return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); - } - - var result = new - { - template = "reportInsignia2", - reportName = "reportInsignia2", - data = new List() - }; - return Success(result); - - } - catch - { - throw; - } - } - #endregion - - } } diff --git a/BMA.EHR.Insignia/Requests/GetInsigniaDetailByNodeReportDto.cs b/BMA.EHR.Insignia/Requests/GetInsigniaDetailByNodeReportDto.cs index 0282b021..62895347 100644 --- a/BMA.EHR.Insignia/Requests/GetInsigniaDetailByNodeReportDto.cs +++ b/BMA.EHR.Insignia/Requests/GetInsigniaDetailByNodeReportDto.cs @@ -5,6 +5,6 @@ namespace BMA.EHR.Insignia.Service.Requests { public Guid roundId { get; set; } public int node { get; set; } - public string nodeId { get; set; } + public Guid nodeId { get; set; } } } \ No newline at end of file