From d692ea43590d6d387a6774cc3cab65453a4dae76 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 17 Jan 2025 15:06:28 +0700 Subject: [PATCH] report discipline --- .../Profiles/GetProfileByKeycloakIdRootDto.cs | 2 + .../Controllers/DisciplineReportController.cs | 106 ++++++++++++++---- .../Requests/DisciplineReportRequest.cs | 4 +- 3 files changed, 91 insertions(+), 21 deletions(-) diff --git a/BMA.EHR.Application/Responses/Profiles/GetProfileByKeycloakIdRootDto.cs b/BMA.EHR.Application/Responses/Profiles/GetProfileByKeycloakIdRootDto.cs index bb85f0ed..a1d9c7ce 100644 --- a/BMA.EHR.Application/Responses/Profiles/GetProfileByKeycloakIdRootDto.cs +++ b/BMA.EHR.Application/Responses/Profiles/GetProfileByKeycloakIdRootDto.cs @@ -9,10 +9,12 @@ namespace BMA.EHR.Application.Responses.Profiles public string? Prefix { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } + public string? CitizenId { get; set; } public Guid? Keycloak { get; set; } public string? PosNo { get; set; } public string? Position { get; set; } public string? PositionLevel { get; set; } + public string? PositionType { get; set; } public string? Oc { get; set; } } } diff --git a/BMA.EHR.Discipline.Service/Controllers/DisciplineReportController.cs b/BMA.EHR.Discipline.Service/Controllers/DisciplineReportController.cs index 06eeaa4e..599a0ae8 100644 --- a/BMA.EHR.Discipline.Service/Controllers/DisciplineReportController.cs +++ b/BMA.EHR.Discipline.Service/Controllers/DisciplineReportController.cs @@ -1,9 +1,12 @@ using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; +using BMA.EHR.Application.Responses; +using BMA.EHR.Application.Responses.Profiles; using BMA.EHR.Discipline.Service.Requests; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Models.Discipline; using BMA.EHR.Domain.Shared; +using BMA.EHR.Domain.Extensions; using BMA.EHR.Infrastructure.Persistence; // using BMA.EHR.Placement.Service.Requests; using Microsoft.AspNetCore.Authorization; @@ -28,17 +31,20 @@ namespace BMA.EHR.DisciplineSuspend.Service.Controllers private readonly MinIODisciplineService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly PermissionRepository _permission; + private readonly UserProfileRepository _userProfileRepository; public DisciplineReportController(DisciplineDbContext context, MinIODisciplineService documentService, IHttpContextAccessor httpContextAccessor, - PermissionRepository permission) + PermissionRepository permission, + UserProfileRepository userProfileRepository) { // _repository = repository; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; _permission = permission; + _userProfileRepository = userProfileRepository; } #region " Properties " @@ -46,6 +52,8 @@ namespace BMA.EHR.DisciplineSuspend.Service.Controllers 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 @@ -60,39 +68,99 @@ namespace BMA.EHR.DisciplineSuspend.Service.Controllers [HttpPost("{type}")] public async Task> GetReportDiscipline([FromBody] DisciplineReportRequest req, string type) { - // var data = await _context.DisciplineDisciplinary_ProfileComplaintInvestigates - // .Include(x => x.DisciplineDisciplinary) - // .Where(x => x.Id == d.id) - // .FirstOrDefaultAsync(); - var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_DISCIPLINE_REPORT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } + var profile = new List(); + if (type.Trim().ToUpper() == "OFFICER") + { + profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); + } + else + { + profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); + } - // var profile = new List(); - // if (type.Trim().ToUpper() == "OFFICER") - // { - // profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); - // } - // else - // { - // profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); - // } + if (req.posLevel != null || req.posType != null) + { + profile = profile.Where(x => x.PositionType == req.posType || x.PositionLevel == req.posLevel).ToList(); + } + + var data_search = await _context.DisciplineDisciplinarys + .Include(x => x.DisciplineDisciplinary_ProfileComplaintInvestigates) + .OrderByDescending(x => x.CreatedAt) + .ToListAsync(); + + if (req.status.Trim().ToUpper() != "ALL") + data_search = data_search.Where(x => x.Status.Contains(req.status.Trim().ToUpper())).ToList(); + + if (req.offenseDetail.Trim().ToUpper() != "ALL") + data_search = data_search.Where(x => x.OffenseDetails.Contains(req.status.Trim().ToUpper())).ToList(); + + if (req.disciplinaryFaultLevel.Trim().ToUpper() != "ALL") + data_search = data_search.Where(x => x.DisciplinaryFaultLevel.Contains(req.status.Trim().ToUpper())).ToList(); + + var mapData = data_search + .SelectMany((d) => d.DisciplineDisciplinary_ProfileComplaintInvestigates.Select((profile) => new + { + fullName = $"{profile.Prefix}{profile.FirstName} {profile.LastName}", + citizenId = profile.CitizenId, + position = profile.Position, + posLevel = profile.posLevelName, + oc = profile.Organization, + CreatedAt = profile.CreatedAt, + offense = d.OffenseDetails != null && d.OffenseDetails != "" + ? d.OffenseDetails == "NOT_SPECIFIED" + ? "ความผิดวินัยยังไม่ระบุ" + : d.OffenseDetails == "NOT_DEADLY" + ? "ความผิดวินัยไม่ร้ายแรง" + : d.OffenseDetails == "DEADLY" + ? "ความผิดวินัยร้ายแรง" + : "" + : "", + faultLevel = d.DisciplinaryFaultLevel, + })) + .Where(x => + x.citizenId != null && profile.Any(p => p.CitizenId == x.citizenId)) + .OrderByDescending(x => x.CreatedAt) + .ToList(); + + var count = 1; + var employees = new List(); + foreach (var p in mapData) + { + var emp = new + { + no = count, + fullName = p.fullName, + citizenId = p.citizenId, + position = p.position, + posLevel = p.posLevel, + oc = profile.FirstOrDefault(x => x.CitizenId == p.citizenId)?.Oc ?? "", + offense = p.offense, + faultLevel = p.faultLevel, + one = 0, + two = 0, + three = 0, + }; + employees.Add(emp); + count++; + } var result = new { template = "reportDiscipline", - reportName = "reportDiscipline", + reportName = "xlsx-report", data = new { - year = req.year + 543, - data = new List() + year = (req.year + 543).ToString().ToThaiNumber(), + dateCurrent = $"ณ วันที่ {DateTime.Now.Date.ToThaiShortDate().ToThaiNumber()}", + data = employees } }; - return Success(result); } } diff --git a/BMA.EHR.Discipline.Service/Requests/DisciplineReportRequest.cs b/BMA.EHR.Discipline.Service/Requests/DisciplineReportRequest.cs index 2fa79722..3d3cfe97 100644 --- a/BMA.EHR.Discipline.Service/Requests/DisciplineReportRequest.cs +++ b/BMA.EHR.Discipline.Service/Requests/DisciplineReportRequest.cs @@ -17,8 +17,8 @@ namespace BMA.EHR.Discipline.Service.Requests public string status { get; set; } - public string posType { get; set; } + public string? posType { get; set; } - public string posLevel { get; set; } + public string? posLevel { get; set; } } }