hrms-api-backend/BMA.EHR.Placement.Service/Controllers/PlacementController.cs
2023-07-05 10:22:42 +07:00

288 lines
12 KiB
C#

using BMA.EHR.Application.Repositories;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Placement.Service.Requests;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
namespace BMA.EHR.Placement.Service.Controllers
{
[Route("api/[controller]/placement")]
[ApiController]
[Produces("application/json")]
//[Authorize]
[SwaggerTag("ระบบบรรจุ")]
public class PlacementController : BaseController
{
private readonly PlacementRepository _repository;
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
public PlacementController(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;
#endregion
[HttpGet]
public async Task<ActionResult<ResponseObject>> Get()
{
var data = await _repository.GetAllAsync();
return Success(data);
}
[HttpGet("fiscal")]
public async Task<ActionResult<ResponseObject>> GetFiscal()
{
var data = await _repository.GetAllAsync();
if (data != null)
{
var _data = data.GroupBy(x => x.Year).Select(x => new
{
Id = x.FirstOrDefault().Year,
Name = x.FirstOrDefault().Year + 543,
}).ToList();
return Success(_data);
}
return Success(data);
}
[HttpGet("exam/{year}")]
public async Task<ActionResult<ResponseObject>> GetExam(int year)
{
var data = await _context.Placements.Where(x => year > 0 ? (x.Year == year) : (x.Year > 0)).Select(x => new
{
Id = x.Id,
ExamRound = x.Name,
ExamOrder = x.Round,
FiscalYear = x.Year + 543,
NumberOfCandidates = x.PlacementProfiles.Count(),
ExamTypeValue = x.PlacementType.Id,
ExamTypeName = x.PlacementType.Name,
AccountStartDate = x.StartDate,
AccountEndDate = x.EndDate,
AccountExpirationDate = x.EndDate,
IsExpired = x.EndDate.Date < DateTime.Now.Date,
}).ToListAsync();
return Success(data);
}
[HttpGet("pass/{examId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetExamByPlacement(Guid examId)
{
var data = await _context.PlacementProfiles.Where(x => x.Placement.Id == examId).Select(x => new
{
PersonalId = x.Id,
FullName = x.Prefix == null ? null : x.Prefix.Name + $"{x.Firstname} {x.Lastname}",
IdCard = x.CitizenId,
ProfilePhoto = x.Id,
OrganizationName = x.OrganizationPosition == null ? null : x.OrganizationPosition.OrganizationId,////
OrganizationShortName = x.OrganizationPosition == null ? null : x.OrganizationPosition.OrganizationId,////
PositionNumber = x.PositionNumber == null ? null : x.PositionNumber.Name,
PositionPath = x.PositionPath == null ? null : x.PositionPath.Name,
ReportingDate = x.ReportingDate,
BmaOfficer = x.IsOfficer,
StatusId = x.PlacementStatus,
Disclaim = x.IsRelief,
}).ToListAsync();
return Success(data);
}
[HttpGet("personal/{personalId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetProfileByUser(Guid personalId)
{
var person = await _context.PlacementProfiles.FindAsync(personalId);
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
if (person.IsProperty == null || Newtonsoft.Json.JsonConvert.DeserializeObject<List<PersonPropertyRequest>>(person.IsProperty).Count() == 0)
{
var isProperty = await _context.PlacementIsProperties.Select(x => new PersonPropertyRequest
{
Name = x.Name,
Value = false,
}).ToListAsync();
person.IsProperty = Newtonsoft.Json.JsonConvert.SerializeObject(isProperty);
person.LastUpdateFullName = FullName ?? "System Administrator";
person.LastUpdateUserId = UserId ?? "";
person.LastUpdatedAt = DateTime.Now;
await _context.SaveChangesAsync();
}
var data = await _context.PlacementProfiles.Select(x => new
{
PersonalId = x.Id,
IdCard = x.CitizenId,
FullName = x.Prefix == null ? null : x.Prefix.Name + $"{x.Firstname} {x.Lastname}",
DateOfBirth = x.DateOfBirth,
Gender = x.Gender == null ? null : x.Gender.Name,
Address = $"{x.RegistAddress}" +
(x.RegistSubDistrict == null ? null : " แขวง") + (x.RegistSubDistrict == null ? null : x.RegistSubDistrict.Name) +
(x.RegistDistrict == null ? null : " เขต") + (x.RegistDistrict == null ? null : x.RegistDistrict.Name) +
(x.RegistProvince == null ? null : " จังหวัด") + (x.RegistProvince == null ? null : x.RegistProvince.Name) +
(x.RegistSubDistrict == null ? null : " ") + (x.RegistSubDistrict == null ? null : x.RegistSubDistrict.ZipCode),
Education = x.PlacementEducations.Select(p => new
{
EducationLevel = p.EducationLevel == null ? null : p.EducationLevel.Name,
Major = p.Major,
Scores = p.Scores,
Name = p.Name,
DurationStart = p.DurationStart,
DurationEnd = p.DurationEnd,
}),
PointA = x.PointA,
PointB = x.PointB,
PointC = x.PointC,
PointTotalA = x.PointTotalA,
PointTotalB = x.PointTotalB,
PointTotalC = x.PointTotalC,
Point = x.PointA + x.PointB + x.PointC,
PointTotal = x.PointTotalA + x.PointTotalB + x.PointTotalC,
ExamNumber = x.ExamNumber,
ExamRound = x.ExamRound,
Pass = x.Pass,
IsProperty = x.IsProperty == null ? null : Newtonsoft.Json.JsonConvert.DeserializeObject<List<PersonPropertyRequest>>(x.IsProperty),
}).FirstOrDefaultAsync(x => x.PersonalId == personalId);
return Success(data);
}
[HttpPut("property/{personalId:length(36)}")]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUser([FromBody] List<PersonPropertyRequest> req, Guid personalId)
{
var person = await _context.PlacementProfiles.FindAsync(personalId);
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
person.IsProperty = Newtonsoft.Json.JsonConvert.SerializeObject(req);
person.LastUpdateFullName = FullName ?? "System Administrator";
person.LastUpdateUserId = UserId ?? "";
person.LastUpdatedAt = DateTime.Now;
await _context.SaveChangesAsync();
return Success();
}
[HttpGet("pass/stat/{examId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetDashboardByPlacement(Guid examId)
{
var placement = await _context.Placements
.Where(x => x.Id == examId)
.Select(x => new
{
Total = x.PlacementProfiles.Count(),
UnContain = x.PlacementProfiles.Where(p => p.PlacementStatus.Trim().ToUpper() == "UN-CONTAIN").Count(),
PrepareContain = x.PlacementProfiles.Where(p => p.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN").Count(),
Contain = x.PlacementProfiles.Where(p => p.PlacementStatus.Trim().ToUpper() == "CONTAIN").Count(),
Disclaim = x.PlacementProfiles.Where(p => p.PlacementStatus.Trim().ToUpper() == "DISCLAIM").Count(),
}).FirstOrDefaultAsync();
if (placement == null)
return Error(GlobalMessages.DataNotFound, 404);
return Success(placement);
}
[HttpPost("pass/deferment"), DisableRequestSizeLimit]
public async Task<ActionResult<ResponseObject>> UpdatePersonDeferment()
{
var person = await _context.PlacementProfiles.FindAsync(Request.Form.ContainsKey("personalId") ? Guid.Parse(Request.Form["personalId"]) : Guid.Parse("00000000-0000-0000-0000-000000000000"));
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
person.IsRelief = true;
person.ReliefReason = Request.Form.ContainsKey("note") ? Request.Form["note"] : "";
person.PlacementStatus = "UN-CONTAIN";
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);
person.ReliefDoc = doc;
}
person.LastUpdateFullName = FullName ?? "System Administrator";
person.LastUpdateUserId = UserId ?? "";
person.LastUpdatedAt = DateTime.Now;
await _context.SaveChangesAsync();
return Success();
}
[HttpPost("pass/disclaim")]
public async Task<ActionResult<ResponseObject>> UpdatePersonDisclaim([FromBody] PersonDisclaimRequest req)
{
var person = await _context.PlacementProfiles.FindAsync(req.PersonalId);
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
person.RejectReason = req.Note;
person.PlacementStatus = "DISCLAIM";
person.LastUpdateFullName = FullName ?? "System Administrator";
person.LastUpdateUserId = UserId ?? "";
person.LastUpdatedAt = DateTime.Now;
await _context.SaveChangesAsync();
return Success();
}
[HttpGet("pass/deferment/{personalId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetPersonDeferment(Guid personalId)
{
var person = await _context.PlacementProfiles.Include(x => x.ReliefDoc).FirstOrDefaultAsync(x => x.Id == personalId);
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
var data = new
{
ReliefReason = person.ReliefReason,
ReliefDoc = person.ReliefDoc == null ? null : await _documentService.ImagesPath(person.ReliefDoc.Id),
};
return Success(data);
}
[HttpGet("pass/disclaim/{personalId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetPersonDisclaim(Guid personalId)
{
var person = await _context.PlacementProfiles.FindAsync(personalId);
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
var data = new
{
RejectReason = person.RejectReason,
};
return Success(data);
}
[HttpPost("pass")]
public async Task<ActionResult<ResponseObject>> UpdatePositionByPerson(Guid personalId)
{
var person = await _context.PlacementProfiles.FindAsync(personalId);
if (person == null)
return Error(GlobalMessages.DataNotFound, 404);
return Success();
}
}
}