hrms-api-backend/BMA.EHR.Retirement.Service/Controllers/RetirementDeceasedController.cs

320 lines
16 KiB
C#

using BMA.EHR.Application.Repositories;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Retirement;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Retirement.Service.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
using System.Security.Cryptography;
namespace BMA.EHR.Retirement.Service.Controllers
{
[Route("api/v{version:apiVersion}/retirement/deceased")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("ระบบแจ้งการถึงแก่กรรม")]
public class RetirementDeceasedController : BaseController
{
private readonly RetirementRepository _repository;
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
public RetirementDeceasedController(RetirementRepository repository,
ApplicationDBContext context,
MinIOService documentService,
IHttpContextAccessor httpContextAccessor)
{
_repository = repository;
_context = context;
_documentService = documentService;
_httpContextAccessor = httpContextAccessor;
}
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
/// <summary>
/// list รายการบันทึกเวียนแจ้งการถึงแก่กรรม
/// </summary>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet()]
public async Task<ActionResult<ResponseObject>> GetList()
{
var retirementDeceaseds = await _context.RetirementDeceaseds.AsQueryable()
.OrderByDescending(x => x.CreatedAt)
.Select(p => new
{
p.Id,
ProfileId = p.Profile.Id,
Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name,
p.Profile.FirstName,
p.Profile.LastName,
Position = p.Profile.Position == null ? null : p.Profile.Position.Name,
PositionType = p.Profile.PositionType == null ? null : p.Profile.PositionType.Name,
p.Profile.PositionLine,
// PositionLine = p.Profile.PositionLine == null ? null : p.Profile.PositionLine.Name,
PositionLevel = p.Profile.PositionLevel == null ? null : p.Profile.PositionLevel.Name,
p.Profile.PositionExecutive,
// PositionExecutive = p.Profile.PositionExecutive == null ? null : p.Profile.PositionExecutive.Name,
Organization = p.Profile.Oc,
// Organization = p.Profile.Oc == null ? null : p.Profile.Oc.Name,
p.Number,
p.Date,
p.Location,
p.Reason,
FileName = p.Document == null ? null : p.Document.FileName,
PathName = p.Document == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Document.Id,
p.IsActive,
p.CreatedAt,
})
.ToListAsync();
var _retirementDeceaseds = new List<dynamic>();
foreach (var data in retirementDeceaseds)
{
var _data = new
{
data.Id,
data.ProfileId,
data.Prefix,
data.FirstName,
data.LastName,
data.Position,
data.PositionExecutive,
data.PositionType,
data.PositionLine,
data.PositionLevel,
data.Organization,
data.Number,
data.Date,
data.Location,
data.Reason,
data.FileName,
PathName = data.PathName == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.PathName),
data.IsActive,
data.CreatedAt,
};
_retirementDeceaseds.Add(_data);
}
return Success(_retirementDeceaseds);
}
/// <summary>
/// get รายละเอียดบันทึกเวียนแจ้งการถึงแก่กรรม
/// </summary>
/// <param name="id">Id การถึงแก่กรรม</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetDetailByUser(Guid id)
{
var data = await _context.RetirementDeceaseds.AsQueryable()
.Where(x => x.Id == id)
.Select(p => new
{
p.Id,
ProfileId = p.Profile.Id,
Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name,
PrefixId = p.Profile.Prefix == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Prefix.Id,
p.Profile.FirstName,
p.Profile.LastName,
Position = p.Profile.Position == null ? null : p.Profile.Position.Name,
PositionId = p.Profile.Position == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Position.Id,
PositionType = p.Profile.PositionType == null ? null : p.Profile.PositionType.Name,
PositionTypeId = p.Profile.PositionType == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.PositionType.Id,
p.Profile.PositionLine,
p.Profile.PositionLineId,
// PositionLine = p.Profile.PositionLine == null ? null : p.Profile.PositionLine.Name,
PositionLevel = p.Profile.PositionLevel == null ? null : p.Profile.PositionLevel.Name,
PositionLevelId = p.Profile.PositionLevel == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.PositionLevel.Id,
p.Profile.PositionExecutive,
p.Profile.PositionExecutiveId,
// PositionExecutive = p.Profile.PositionExecutive == null ? null : p.Profile.PositionExecutive.Name,
Organization = p.Profile.Oc,
OrganizationId = p.Profile.OcId,
// Organization = p.Profile.Oc == null ? null : p.Profile.Oc.Name,
p.Number,
p.Date,
p.Location,
p.Reason,
FileName = p.Document == null ? null : p.Document.FileName,
PathName = p.Document == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Document.Id,
p.IsActive,
p.CreatedAt,
})
.FirstOrDefaultAsync();
if (data == null)
return Error(GlobalMessages.RetirementDeceasedNotFound, 404);
var _data = new
{
data.Id,
data.ProfileId,
data.Prefix,
data.PrefixId,
data.FirstName,
data.LastName,
data.Position,
data.PositionExecutive,
data.PositionType,
data.PositionLine,
data.PositionLevel,
data.Organization,
data.PositionId,
data.PositionExecutiveId,
data.PositionTypeId,
data.PositionLineId,
data.PositionLevelId,
data.OrganizationId,
data.Number,
data.Date,
data.Location,
data.Reason,
data.FileName,
PathName = data.PathName == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.PathName),
data.IsActive,
data.CreatedAt,
};
return Success(_data);
}
/// <summary>
/// สร้าง รายละเอียดบันทึกเวียนแจ้งการถึงแก่กรรม
/// </summary>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost()]
public async Task<ActionResult<ResponseObject>> Post([FromForm] RetirementDeceasedRequest req)
{
var profile = await _context.Profiles
.FirstOrDefaultAsync(x => x.Id == req.ProfileId);
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
var retirementDeceased = new RetirementDeceased
{
Profile = profile,
Number = req.Number,
Date = req.Date,
Location = req.Location,
Reason = req.Reason,
IsActive = true,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.RetirementDeceaseds.AddAsync(retirementDeceased);
await _context.SaveChangesAsync();
if (Request.Form.Files != null && Request.Form.Files.Count != 0)
{
var file = Request.Form.Files[0];
var fileExtension = Path.GetExtension(file.FileName);
var doc = await _documentService.UploadFileAsync(file, file.FileName);
var _doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == doc.Id);
if (_doc != null)
retirementDeceased.Document = _doc;
}
await _context.SaveChangesAsync();
return Success();
}
/// <summary>
/// แก้ไข รายละเอียดบันทึกเวียนแจ้งการถึงแก่กรรม
/// </summary>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Put([FromForm] RetirementDeceasedRequest req, Guid id)
{
var updated = await _context.RetirementDeceaseds
.Include(x => x.Document)
.FirstOrDefaultAsync(x => x.Id == id);
if (updated == null)
return Error(GlobalMessages.RetirementDeceasedNotFound, 404);
if (Request.Form.Files != null && Request.Form.Files.Count != 0)
{
if (updated.Document != null)
await _documentService.DeleteFileAsync(updated.Document.Id);
var file = Request.Form.Files[0];
var fileExtension = Path.GetExtension(file.FileName);
var doc = await _documentService.UploadFileAsync(file, file.FileName);
var _doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == doc.Id);
if (_doc != null)
updated.Document = _doc;
}
updated.Number = req.Number;
updated.Date = req.Date;
updated.Location = req.Location;
updated.Reason = req.Reason;
updated.LastUpdateFullName = FullName ?? "System Administrator";
updated.LastUpdateUserId = UserId ?? "";
updated.LastUpdatedAt = DateTime.Now;
await _context.SaveChangesAsync();
return Success();
}
/// <summary>
/// ลบ รายละเอียดบันทึกเวียนแจ้งการถึงแก่กรรม
/// </summary>
/// <param name="id">Id การถึงแก่กรรม</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpDelete("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Delete(Guid id)
{
var deleted = await _context.RetirementDeceaseds.AsQueryable()
.Include(x => x.Document)
.FirstOrDefaultAsync(x => x.Id == id);
if (deleted == null)
return Error(GlobalMessages.RetirementDeceasedNotFound, 404);
if (deleted.Document != null)
await _documentService.DeleteFileAsync(deleted.Document.Id);
_context.RetirementDeceaseds.Remove(deleted);
await _context.SaveChangesAsync();
return Success();
}
}
}