404 lines
20 KiB
C#
404 lines
20 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.Placement;
|
|
using BMA.EHR.Domain.Shared;
|
|
using BMA.EHR.Infrastructure.Persistence;
|
|
using BMA.EHR.Placement.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.Placement.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/placement/Receive")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("ระบบรับโอน")]
|
|
public class PlacementReceiveController : BaseController
|
|
{
|
|
private readonly PlacementRepository _repository;
|
|
private readonly ApplicationDBContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public PlacementReceiveController(PlacementRepository 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;
|
|
|
|
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
|
|
|
#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>> GetListByAdmin()
|
|
{
|
|
|
|
var placementReceives = await _context.PlacementReceives.AsQueryable()
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
p.CitizenId,
|
|
Prefix = p.Prefix.Name,
|
|
p.Firstname,
|
|
p.Lastname,
|
|
p.DateOfBirth,
|
|
Gender = p.Gender == null ? null : p.Gender.Name,
|
|
p.Status,
|
|
p.RecruitDate,
|
|
PositionNumber = p.PositionNumber == null ? null : p.PositionNumber.Name,
|
|
PositionPath = p.PositionPath == null ? null : p.PositionPath.Name,
|
|
PositionPathSide = p.PositionPathSide == null ? null : p.PositionPathSide.Name,
|
|
PositionType = p.PositionType == null ? null : p.PositionType.Name,
|
|
PositionLine = p.PositionLine == null ? null : p.PositionLine.Name,
|
|
PositionLevel = p.PositionLevel == null ? null : p.PositionLevel.Name,
|
|
PosNoId = p.PositionNumber == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionNumber.Id,
|
|
PositionId = p.PositionPath == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionPath.Id,
|
|
PositionPathSideId = p.PositionPathSide == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionPathSide.Id,
|
|
PositionTypeId = p.PositionType == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionType.Id,
|
|
PositionLineId = p.PositionLine == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLine.Id,
|
|
PositionLevelId = p.PositionLevel == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLevel.Id,
|
|
OrganizationPositionId = p.OrganizationPosition == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.OrganizationPosition.Id,
|
|
p.IsActive,
|
|
p.CreatedAt,
|
|
})
|
|
.ToListAsync();
|
|
if (PlacementAdmin == true)
|
|
placementReceives.Where(x => x.Status.Trim().ToUpper().Contains("DONE"));
|
|
|
|
return Success(placementReceives);
|
|
}
|
|
|
|
/// <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.PlacementReceives.AsQueryable()
|
|
.Where(x => x.Id == id)
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
p.CitizenId,
|
|
Prefix = p.Prefix.Id,
|
|
p.Firstname,
|
|
p.Lastname,
|
|
p.DateOfBirth,
|
|
Gender = p.Gender == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Gender.Id,
|
|
p.Nationality,
|
|
p.Race,
|
|
Religion = p.Religion == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Religion.Id,
|
|
BloodGroup = p.BloodGroup == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.BloodGroup.Id,
|
|
Relationship = p.Relationship == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Relationship.Id,
|
|
p.TelephoneNumber,
|
|
p.Status,
|
|
p.RecruitDate,
|
|
PosNoId = p.PositionNumber == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionNumber.Id,
|
|
PositionId = p.PositionPath == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionPath.Id,
|
|
PositionPathSideId = p.PositionPathSide == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionPathSide.Id,
|
|
PositionTypeId = p.PositionType == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionType.Id,
|
|
PositionLineId = p.PositionLine == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLine.Id,
|
|
PositionLevelId = p.PositionLevel == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLevel.Id,
|
|
OrganizationPositionId = p.OrganizationPosition == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.OrganizationPosition.Id,
|
|
p.CreatedAt,
|
|
PlacementReceiveDocs = p.PlacementReceiveDocs.Where(d => d.Document != null).Select(d => new { d.Document.Id, d.Document.FileName }),
|
|
})
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(GlobalMessages.DataNotFound, 404);
|
|
|
|
var placementReceiveDocs = new List<dynamic>();
|
|
foreach (var doc in data.PlacementReceiveDocs)
|
|
{
|
|
var _doc = new
|
|
{
|
|
doc.FileName,
|
|
PathName = await _documentService.ImagesPath(doc.Id)
|
|
};
|
|
placementReceiveDocs.Add(_doc);
|
|
}
|
|
var _data = new
|
|
{
|
|
data.Id,
|
|
data.CitizenId,
|
|
data.Prefix,
|
|
data.Firstname,
|
|
data.Lastname,
|
|
data.DateOfBirth,
|
|
data.Gender,
|
|
data.Nationality,
|
|
data.Race,
|
|
data.Religion,
|
|
data.BloodGroup,
|
|
data.Relationship,
|
|
data.TelephoneNumber,
|
|
data.Status,
|
|
data.RecruitDate,
|
|
data.PosNoId,
|
|
data.PositionId,
|
|
data.PositionPathSideId,
|
|
data.PositionTypeId,
|
|
data.PositionLineId,
|
|
data.PositionLevelId,
|
|
data.OrganizationPositionId,
|
|
data.CreatedAt,
|
|
Docs = placementReceiveDocs,
|
|
};
|
|
|
|
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] PlacementReceiveRequest req)
|
|
{
|
|
var profile = await _context.Profiles
|
|
.FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId));
|
|
if (profile == null)
|
|
return Error(GlobalMessages.DataNotFound, 404);
|
|
|
|
var prefix = await _context.Prefixes.FindAsync(req.Prefix);
|
|
if (prefix == null)
|
|
return Error(GlobalMessages.PrefixNotFound, 404);
|
|
|
|
var placementReceive = new PlacementReceive
|
|
{
|
|
CitizenId = req.CitizenId,
|
|
Prefix = prefix,
|
|
Firstname = req.Firstname,
|
|
Lastname = req.Lastname,
|
|
DateOfBirth = req.DateOfBirth,
|
|
Gender = await _context.Genders.FindAsync(req.Gender),
|
|
Nationality = req.Nationality,
|
|
Race = req.Race,
|
|
Religion = await _context.Religions.FindAsync(req.Religion),
|
|
BloodGroup = await _context.BloodGroups.FindAsync(req.BloodGroup),
|
|
Relationship = await _context.Relationships.FindAsync(req.Relationship),
|
|
TelephoneNumber = req.TelephoneNumber,
|
|
Status = "PENDING",
|
|
CreatedUserId = FullName ?? "",
|
|
CreatedFullName = UserId ?? "System Administrator",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.PlacementReceives.AddAsync(placementReceive);
|
|
await _context.SaveChangesAsync();
|
|
if (Request.Form.Files != null && Request.Form.Files.Count != 0)
|
|
{
|
|
foreach (var file in Request.Form.Files)
|
|
{
|
|
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)
|
|
{
|
|
var placementReceiveDoc = new PlacementReceiveDoc
|
|
{
|
|
PlacementReceive = placementReceive,
|
|
Document = _doc,
|
|
CreatedUserId = FullName ?? "",
|
|
CreatedFullName = UserId ?? "System Administrator",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.PlacementReceiveDocs.AddAsync(placementReceiveDoc);
|
|
|
|
}
|
|
}
|
|
}
|
|
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>
|
|
[HttpPut("position/{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> UpdatePositionPlacementReceive([FromBody] PersonSelectPositionReceiveRequest req, Guid id)
|
|
{
|
|
var uppdated = await _context.PlacementReceives
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.PlacementReceiveNotFound, 404);
|
|
if (req.PosNoId != null)
|
|
{
|
|
var save_posNo = await _context.PositionNumbers.FindAsync(req.PosNoId);
|
|
if (save_posNo == null)
|
|
return Error(GlobalMessages.PositionPosNoNotFound, 404);
|
|
uppdated.PositionNumber = save_posNo;
|
|
|
|
var save_orgPosition = await _context.OrganizationPositions.FirstOrDefaultAsync(x => x.PositionNumber == save_posNo);
|
|
if (save_orgPosition == null)
|
|
return Error(GlobalMessages.PositionPosNoNotFound, 404);
|
|
uppdated.OrganizationPosition = save_orgPosition;
|
|
}
|
|
|
|
if (req.PositionId != null)
|
|
{
|
|
var save = await _context.PositionPaths.FindAsync(req.PositionId);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionPathNotFound, 404);
|
|
uppdated.PositionPath = save;
|
|
}
|
|
|
|
if (req.PositionLevelId != null)
|
|
{
|
|
var save = await _context.PositionLevels.FindAsync(req.PositionLevelId);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionLevelNotFound, 404);
|
|
uppdated.PositionLevel = save;
|
|
}
|
|
|
|
if (req.PositionLineId != null)
|
|
{
|
|
var save = await _context.PositionLines.FindAsync(req.PositionLineId);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionLineNotFound, 404);
|
|
uppdated.PositionLine = save;
|
|
}
|
|
|
|
if (req.PositionPathSideId != null)
|
|
{
|
|
var save = await _context.PositionPathSides.FindAsync(req.PositionPathSideId);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionPathSideNotFound, 404);
|
|
uppdated.PositionPathSide = save;
|
|
}
|
|
|
|
if (req.PositionTypeId != null)
|
|
{
|
|
var save = await _context.PositionTypes.FindAsync(req.PositionTypeId);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionTypeNotFound, 404);
|
|
uppdated.PositionType = save;
|
|
}
|
|
|
|
uppdated.RecruitDate = req.RecruitDate;
|
|
uppdated.Status = "DONE";
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
// /// <summary>
|
|
// /// View หน่วยงาน
|
|
// /// </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("position/{id:length(36)}")]
|
|
// public async Task<ActionResult<ResponseObject>> GetPositionPlacementReceive(Guid id)
|
|
// {
|
|
// var uppdated = await _context.PlacementReceives
|
|
// .FirstOrDefaultAsync(x => x.Id == id);
|
|
// if (uppdated == null)
|
|
// return Error(GlobalMessages.PlacementReceiveNotFound, 404);
|
|
|
|
// uppdated.Status = "DONE";
|
|
// uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
// uppdated.LastUpdateUserId = UserId ?? "";
|
|
// uppdated.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.PlacementReceives.AsQueryable()
|
|
.Include(x => x.PlacementReceiveDocs)
|
|
.ThenInclude(x => x.Document)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (deleted == null)
|
|
return NotFound();
|
|
var placementReceiveDocs = new List<dynamic>();
|
|
foreach (var doc in deleted.PlacementReceiveDocs)
|
|
{
|
|
if (doc.Document != null)
|
|
placementReceiveDocs.Add(doc.Document.Id);
|
|
}
|
|
_context.PlacementReceiveDocs.RemoveRange(deleted.PlacementReceiveDocs);
|
|
await _context.SaveChangesAsync();
|
|
_context.PlacementReceives.Remove(deleted);
|
|
foreach (var doc in placementReceiveDocs)
|
|
{
|
|
if (doc != null)
|
|
await _documentService.DeleteFileAsync(doc);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
}
|
|
}
|