using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Application.Repositories.Reports; using BMA.EHR.Application.Requests; using BMA.EHR.Application.Responses.Insignias; using BMA.EHR.Application.Responses.Organizations; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Insignias; using BMA.EHR.Domain.Shared; using BMA.EHR.Infrastructure.Persistence; using BMA.EHR.Insignia.Service.Requests; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OfficeOpenXml; using RabbitMQ.Client; using Swashbuckle.AspNetCore.Annotations; using System.Security.Claims; using System.Text; namespace BMA.EHR.Insignia.Service.Controllers { [Route("api/v{version:apiVersion}/insignia/report")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("เครื่องราชรายงาน")] public class InsigniaReportController : BaseController { private readonly ApplicationDBContext _context; private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; private readonly InsigniaReportRepository _repository; private readonly PermissionRepository _permission; private readonly IHttpContextAccessor _httpContextAccessor; /// /// /// /// /// /// /// /// /// /// /// /// public InsigniaReportController(ApplicationDBContext context, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, InsigniaReportRepository repository, IHttpContextAccessor httpContextAccessor, PermissionRepository permission) { _context = context; _hostingEnvironment = hostingEnvironment; _configuration = configuration; _repository = repository; _permission = permission; _httpContextAccessor = httpContextAccessor; } #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"]; #endregion #region บัญชีแสดงรายชื่อผู้ขอพระราชทานเหรียญจักรพรรดิมาลา /// /// บัญชีแสดงรายชื่อผู้ขอพระราชทานเหรียญจักรพรรดิมาลา /// /// type /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("report1/{type}")] public async Task> GetInsigniaReport1Async([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.GetCoinReport(req.roundId); var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId); var dataResult = new List(); var dataList = new List(data); var detailList = new List(); var check = 0; var status = false; for (int i = 0; i < dataList.Count; i++) { check++; var gender = dataList[i].GetType().GetProperty("Gender").GetValue(dataList[i]); detailList.Add(new { status = status, row = detailList.Count + 1, InsigniaName = dataList[i].GetType().GetProperty("InsigniaName").GetValue(dataList[i]), FullName = dataList[i].GetType().GetProperty("FullName").GetValue(dataList[i]), InsigniaInitial = dataList[i].GetType().GetProperty("InsigniaInitial").GetValue(dataList[i]), Male = gender == "ชาย" ? 1 : 0, Female = gender == "หญิง" ? 1 : 0, }); if (check == 25) { status = true; } else if (check == 50) { status = false; check = 0; } } if (detailList.Count > 0) { var left = detailList.Where(x => x.status == false); var right = detailList.Where(x => x.status == true); var mergeList = new List(); var InsigniaName = string.Empty; var range = string.Empty; var male = 0; var female = 0; var start = 0; var stop = 0; var colLeft = 0; var colRight = 0; for (int i = 0; i < detailList.Count / 2; i++) { if (InsigniaName != (string)left.ElementAt(i).GetType().GetProperty("InsigniaName").GetValue(left.ElementAt(i))) { InsigniaName = (string)left.ElementAt(i).GetType().GetProperty("InsigniaName").GetValue(left.ElementAt(i)); male = left.Count(x => x.Male == 1 && x.InsigniaName == InsigniaName) + right.Count(x => x.Male == 1 && x.InsigniaName == InsigniaName); female = left.Count(x => x.Female == 1 && x.InsigniaName == InsigniaName) + right.Count(x => x.Female == 1 && x.InsigniaName == InsigniaName); var countGroup = detailList.Count(x => x.InsigniaName == InsigniaName); var countGroupTemp = detailList.Count(x => x.InsigniaName == InsigniaName && x.FullName == ""); start = i == 0 ? (mergeList.Count + 1) : (stop + 1); stop = i == 0 ? (mergeList.Count) + (countGroup - countGroupTemp) : (countGroup - countGroupTemp) == 1 ? start : start + (countGroup - countGroupTemp); range = countGroup - countGroupTemp != 1 ? $"{start} - {stop}" : $"{start}"; colLeft = start; colRight = start + 25; } mergeList.Add(new { row = mergeList.Count + 1, ColLeft = colLeft.ToString().ToThaiNumber(), NameLeft = 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)), 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(), Male = male.ToString().ToThaiNumber(), Female = female.ToString().ToThaiNumber(), }); if (mergeList.Count % 25 == 0) { colLeft = colRight; colRight = colLeft + 25; } colLeft++; colRight++; } dataResult = mergeList; } var result = new { template = "reportInsignia1", reportName = "reportInsignia1", data = new { yearInsignalPeriod, data = dataResult, } }; return Success(result); } catch { throw; } } #endregion #region บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ /// /// บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ /// /// type /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("report2/{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); var summaryTotal = await _repository.GetSummaryTotalCoinReport(req.roundId); var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId); var dataResult = new List(); foreach (var _data in data) { dataResult.Add(new { RowNo = (dataResult.Count + 1).ToString().ToThaiNumber(), DepartmentName = _data.GetType().GetProperty("DepartmentName").GetValue(_data), InsigniaInitial = _data.GetType().GetProperty("InsigniaInitial").GetValue(_data), G1Male = _data.GetType().GetProperty("G1Male").GetValue(_data), G1Female = _data.GetType().GetProperty("G1Female").GetValue(_data), G2Male = _data.GetType().GetProperty("G2Male").GetValue(_data), G2Female = _data.GetType().GetProperty("G2Female").GetValue(_data), Remark = _data.GetType().GetProperty("Remark").GetValue(_data), }); } var result = new { template = "reportInsignia2", reportName = "reportInsignia2", data = new { yearInsignalPeriod, summaryTotal, data = dataResult, } }; return Success(result); } catch { throw; } } #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 } }