hrms-api-backend/BMA.EHR.Command.Service/Controllers/OrderController.cs
suphonchai Phoonsawat 109016e2b5 Merge remote-tracking branch 'origin/develop' into develop
# Conflicts:
#	BMA.EHR.Placement.Service/appsettings.json
2023-07-20 15:26:44 +07:00

285 lines
12 KiB
C#

using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Commands;
using BMA.EHR.Command.Service.Requests;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
namespace BMA.EHR.Command.Service.Controllers
{
[Route("api/v{version:apiVersion}/order")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API ระบบออกคำสั่ง")]
public class OrderController : BaseController
{
#region " Fields "
private readonly PlacementCommandRepository _repository;
private readonly PlacementRepository _placementRepository;
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constuctor and Destructor "
public OrderController(PlacementCommandRepository repository,
PlacementRepository placementRepository,
ApplicationDBContext context,
MinIOService documentService,
IHttpContextAccessor httpContextAccessor)
{
_repository = repository;
_context = context;
_documentService = documentService;
_httpContextAccessor = httpContextAccessor;
_placementRepository = placementRepository;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
#endregion
#region " Methods "
/// <summary>
/// แสดงปีเป็นปีพุทธศักราช โดยดึงจากข้อมูลที่มีในระบบ
/// </summary>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("fiscal-year")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetFiscal()
{
var data = await _repository.GetAllAsync();
if (data != null)
{
var _data = data.GroupBy(x => x.CommandYear).Select(x => new
{
Id = x.FirstOrDefault().CommandYear,
Name = x.FirstOrDefault().CommandYear + 543,
}).ToList();
return Success(_data);
}
return Success(data);
}
/// <summary>
/// PM7-19 : หน้าจอรายการออกคำสั่ง
/// </summary>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetAllAsync()
{
try
{
var data = (await _repository.GetAllAsync())
.Select(d => new
{
OrderName = d.CommandSubject,
OrderNo = d.CommandNo,
FiscalYear = d.CommandYear,
OrderDate = d.CommandAffectDate,
OrderByOrganization = d.IssuerOrganizationName,
OrderBy = d.AuthorizedUserFullName,
OrderStatusValue = d.CommandStatusId,
OrderStatusName = d.CommandStatus.Name,
OrderTypeValue = d.CommandTypeId,
OrderTypeName = d.CommandType.Name
}).ToList();
return Success(data);
}
catch
{
throw;
}
}
/// <summary>
/// PM7-20 : ลบรายการคำสั่ง
/// </summary>
/// <param name="orderId">Record Id ของคำสั่ง</param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpDelete("{orderId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> DeleteAsync(Guid orderId)
{
try
{
var data = await _repository.GetByIdAsync(orderId);
if (data == null)
throw new Exception(GlobalMessages.DataNotFound);
await _repository.DeleteAsync(data!);
return Success(data);
}
catch
{
throw;
}
}
/// <summary>
/// PM7-21 : รายละเอียดการออกคำสั่ง
/// </summary>
/// <param name="orderId">Record Id ของคำสั่ง</param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{orderId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetByIdAsync(Guid orderId)
{
try
{
var data = await _repository.GetByIdAsync(orderId);
if (data == null)
throw new Exception(GlobalMessages.DataNotFound);
var result = new
{
orderId = orderId,
orderTypeValue = data.CommandTypeId,
orderTitle = data.CommandSubject,
orderNo = data.CommandNo,
orderYear = data.CommandYear,
orderDate = data.CommandAffectDate,
orderBy = data.IssuerOrganizationId,
signatoryBy = data.AuthorizedUserFullName,
signatoryPosition = data.AuthorizedPosition,
examRound = data.ExamRoundId,
registerPosition = "",
conclusionRegisterNo = data.ConclusionRegisterNo,
conclusionRegisterDate = data.ConclusionRegisterDate,
conclusionResultNo = data.ConclusionResultNo,
conclusionResultDate = data.ConclusionResultDate,
};
return Success(result);
}
catch
{
throw;
}
}
/// <summary>
/// PM7-22 : สร้างข้อมูลรายละเอียดการออกคำสั่ง
/// </summary>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> PostAsync([FromBody] CreateCommandRequest req)
{
try
{
var inserted = new Domain.Models.Commands.Core.Command
{
CommandNo = req.orderNo.ToString(),
CommandYear = req.orderYear.ToString(),
CommandSubject = req.orderTitle,
PositionName = req.registerPosition,
CommandTypeId = req.orderTypeValue,
IssuerOrganizationId = req.orderBy,
AuthorizedUserFullName = req.signatoryBy,
AuthorizedPosition = req.signatoryPosition,
ExamRoundId = req.examRound,
ConclusionRegisterNo = req.conclusionRegisterNo,
ConclusionRegisterDate = req.conclusionRegisterDate,
ConclusionResultNo = req.conclusionResultNo,
ConclusionResultDate = req.conclusionResultDate
};
var result = await _repository.AddAsync(inserted);
return Success(result);
}
catch
{
throw;
}
}
/// <summary>
/// PM7-24 : dropdown รอบการสอบ หน้ารายละเอียดการออกคำสั่ง
/// </summary>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("detail/exam-round")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetExamRoundAsync()
{
try
{
var data = (await _placementRepository.GetAllAsync())
.Select(x => new
{
examRoundValue = x.Id,
examRoundName = $"{x.Name} ครั้งที่ {x.Round}/{x.Year.ToThaiYear()}"
})
.ToList();
return Success(data);
}
catch
{
throw;
}
}
#endregion
}
}