hrms-api-backend/BMA.EHR.Insignia/Controllers/InsigniaReportController.cs
2025-01-21 12:45:53 +07:00

321 lines
16 KiB
C#

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;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="documentService"></param>
/// <param name="repository"></param>
/// <param name="repositoryNoti"></param>
/// <param name="hostingEnvironment"></param>
/// <param name="httpContextAccessor"></param>
/// <param name="userProfileRepository"></param>
/// <param name="insigniaPeriodRepository"></param>
/// <param name="producer"></param>
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
/// <summary>
/// บัญชีแสดงรายชื่อผู้ขอพระราชทานเหรียญจักรพรรดิมาลา
/// </summary>
/// <param name="type">type </param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("report1/{type}")]
public async Task<ActionResult<ResponseObject>> GetInsigniaReport1Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_INSIGNIA_REPORT");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var data = await _repository.GetCoinReport(req.roundId, type, req.node, req.nodeId);
var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId);
var dataResult = new List<dynamic>();
var dataList = new List<dynamic>(data);
var detailList = new List<dynamic>();
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<object>();
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;
}
// ดึงข้อมูล NameLeft และ NameRight
var nameLeft = left.ElementAt(i).GetType().GetProperty("FullName").GetValue(left.ElementAt(i));
var nameRight = right.ElementAt(i).GetType().GetProperty("FullName").GetValue(right.ElementAt(i));
// ถ้าไม่มีทั้ง NameLeft และ NameRight ให้ข้ามไป
if (string.IsNullOrEmpty((string)nameLeft) && string.IsNullOrEmpty((string)nameRight))
{
continue; // ข้ามการทำงานของลูปในรอบนี้
}
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 = yearInsignalPeriod,
data = dataResult,
}
};
return Success(result);
}
catch
{
throw;
}
}
#endregion
#region 5
/// <summary>
/// บัญชีระดับผลการประเมินผลการปฏิบัติราชการในรอบ 5 ปี
/// </summary>
/// <param name="type">type </param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("report2/{type}")]
public async Task<ActionResult<ResponseObject>> GetInsigniaReport3Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_INSIGNIA_REPORT");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId);
var agency = "";
var data = await _repository.GetEvaluationResultReport(req.roundId, type, req.node, req.nodeId);
var result = new
{
template = "reportInsignia2",
reportName = "reportInsignia2",
data = new
{
agency = agency,
yearInsignalPeriod = yearInsignalPeriod,
data = data,
}
};
return Success(result);
}
catch
{
throw;
}
}
#endregion
#region
/// <summary>
/// บัญชีแสดงจำนวนชั้นตราเครื่องราชฯ
/// </summary>
/// <param name="type">type </param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("report3/{type}")]
public async Task<ActionResult<ResponseObject>> GetInsigniaReport2Async([FromBody] GetInsigniaDetailByNodeReportDto req, string type)
{
try
{
/* var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_INSIGNIA_REPORT");
var jsonData = JsonConvert.DeserializeObject<JObject>(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);
var dataResult = new List<dynamic>();
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 = "reportInsignia3",
reportName = "reportInsignia3",
data = new
{
yearInsignalPeriod = yearInsignalPeriod,
summaryTotal = summaryTotal,
data = dataResult,
}
};
return Success(result);
}
catch
{
throw;
}
}
#endregion
}
}