616 lines
34 KiB
C#
616 lines
34 KiB
C#
using System.Security.Claims;
|
|
using BMA.EHR.Application.Repositories;
|
|
using BMA.EHR.Application.Repositories.MessageQueue;
|
|
using BMA.EHR.Application.Requests;
|
|
using BMA.EHR.Domain.Common;
|
|
using BMA.EHR.Domain.Models.Insignias;
|
|
using BMA.EHR.Domain.Shared;
|
|
using BMA.EHR.Infrastructure.Persistence;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
|
|
using Newtonsoft.Json;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
|
|
namespace BMA.EHR.Insignia.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/insignia/manage")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("จัดสรรเครื่องราช")]
|
|
public class InsigniaManageController : BaseController
|
|
{
|
|
private readonly ApplicationDBContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly InsigniaPeriodsRepository _repository;
|
|
private readonly NotificationRepository _repositoryNoti;
|
|
|
|
public InsigniaManageController(ApplicationDBContext context,
|
|
MinIOService documentService,
|
|
InsigniaPeriodsRepository repository,
|
|
NotificationRepository repositoryNoti,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_repository = repository;
|
|
_repositoryNoti = repositoryNoti;
|
|
_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>
|
|
/// <param name="year">ปีการจัดสรร</param>
|
|
/// <param name="insigniaTypeId">Id ประเภทเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("type/{year}/{insigniaTypeId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetList(int year, Guid insigniaTypeId)
|
|
{
|
|
var insigniaType = await _context.InsigniaTypes
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaTypeId);
|
|
if (insigniaType == null)
|
|
return Error(GlobalMessages.InsigniaTypeNotFound);
|
|
var data = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Insignia.InsigniaType == insigniaType)
|
|
.Where(x => x.Year == year)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Insignia = p.Insignia.Name,
|
|
InsigniaId = p.Insignia.Id,
|
|
Year = p.Year,
|
|
Total = p.Total,
|
|
Allocate = p.InsigniaManageOrganiations.Sum(x => x.Total),
|
|
Remain = p.Total - p.InsigniaManageOrganiations.Sum(x => x.Total),
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.ToListAsync();
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// get รายละเอียดจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="insigniaManageId">Id จัดสรรเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{insigniaManageId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetById(Guid insigniaManageId)
|
|
{
|
|
var data = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Id == insigniaManageId)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Insignia = p.Insignia.Name,
|
|
InsigniaId = p.Insignia.Id,
|
|
Year = p.Year,
|
|
Total = p.Total,
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound, 404);
|
|
|
|
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([FromBody] InsigniaManageRequest req)
|
|
{
|
|
var insignia = await _context.Insignias.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == req.Insignia);
|
|
if (insignia == null)
|
|
return Error(GlobalMessages.InsigniaNotFound);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Insignia == insignia && x.Year == req.Year)
|
|
.FirstOrDefaultAsync();
|
|
if (insigniaManage != null)
|
|
return Error(GlobalMessages.InsigniaManageDupicate);
|
|
|
|
await _context.InsigniaManages.AddAsync(
|
|
new InsigniaManage
|
|
{
|
|
Insignia = insignia,
|
|
Year = req.Year,
|
|
Total = req.Total,
|
|
CreatedUserId = UserId ?? "System Administrator",
|
|
CreatedFullName = FullName ?? "",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
});
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ลบจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="insigniaManageId">Id จัดสรรเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpDelete("{insigniaManageId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> Delete(Guid insigniaManageId)
|
|
{
|
|
var deleted = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Id == insigniaManageId)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (deleted == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
_context.InsigniaManages.Remove(deleted);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="insigniaManageId">Id จัดสรรเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("{insigniaManageId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> Put([FromBody] InsigniaManageRequest req, Guid insigniaManageId)
|
|
{
|
|
var insignia = await _context.Insignias.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == req.Insignia);
|
|
if (insignia == null)
|
|
return Error(GlobalMessages.InsigniaNotFound);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Insignia == insignia && x.Year == req.Year && x.Id != insigniaManageId)
|
|
.FirstOrDefaultAsync();
|
|
if (insigniaManage != null)
|
|
return Error(GlobalMessages.InsigniaManageDupicate);
|
|
|
|
var uppdated = await _context.InsigniaManages.AsQueryable()
|
|
.Include(x => x.Insignia)
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaManageId);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
uppdated.Insignia = insignia;
|
|
// uppdated.Year = req.Year;
|
|
uppdated.Total = req.Total;
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// list หน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์
|
|
/// </summary>
|
|
/// <param name="insigniaManageId">Id จัดสรรเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("org/{insigniaManageId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetListOrganization(Guid insigniaManageId)
|
|
{
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaManageId);
|
|
if (insigniaManage == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
var insigniaManageOrg = await _context.InsigniaManageOrganiations.AsQueryable()
|
|
.Where(x => x.InsigniaManage == insigniaManage)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
OrganizationOrganization = p.OrganizationOrganization.Name,
|
|
Total = p.Total,
|
|
Allocate = p.InsigniaManageProfiles.Where(x => x.Status == false).Count(),
|
|
Remain = p.Total - p.InsigniaManageProfiles.Where(x => x.Status == false).Count(),
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.ToListAsync();
|
|
// if (insigniaManageOrg == null)
|
|
// return Error(GlobalMessages.InsigniaManageOrgNotFound);
|
|
return Success(insigniaManageOrg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// สร้างหน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost("org")]
|
|
public async Task<ActionResult<ResponseObject>> PostOrganization([FromBody] InsigniaManageOrganizationRequest req)
|
|
{
|
|
var organization = await _context.Organizations.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.FirstOrDefaultAsync(x => x.Id == req.OrganizationOrganizationId);
|
|
if (organization == null)
|
|
return Error(GlobalMessages.OrganizationNotFound);
|
|
if (organization.OrganizationOrganization == null)
|
|
return Error(GlobalMessages.OrganizationNotFound);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Include(x => x.InsigniaManageOrganiations)
|
|
.ThenInclude(x => x.OrganizationOrganization)
|
|
.FirstOrDefaultAsync(x => x.Id == req.insigniaManageId);
|
|
if (insigniaManage == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
var insigniaManageOrganiation = await _context.InsigniaManageOrganiations.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.OrganizationOrganization == organization.OrganizationOrganization && x.InsigniaManage == insigniaManage);
|
|
if (insigniaManageOrganiation != null)
|
|
return Error(GlobalMessages.InsigniaManageOrgDupicate);
|
|
|
|
var total = insigniaManage.InsigniaManageOrganiations.Where(x => x.OrganizationOrganization != organization.OrganizationOrganization).Sum(x => x.Total);
|
|
if (req.Total + total > insigniaManage.Total)
|
|
return Error(GlobalMessages.InsigniaManageOrgLimit);
|
|
await _context.InsigniaManageOrganiations.AddAsync(
|
|
new InsigniaManageOrganiation
|
|
{
|
|
OrganizationOrganization = organization.OrganizationOrganization,
|
|
Total = req.Total,
|
|
InsigniaManage = insigniaManage,
|
|
CreatedUserId = UserId ?? "System Administrator",
|
|
CreatedFullName = FullName ?? "",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
});
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ลบหน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์
|
|
/// </summary>
|
|
/// <param name="insigniaManageOrgId">Id หน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpDelete("org/{insigniaManageOrgId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> DeleteOrganization(Guid insigniaManageOrgId)
|
|
{
|
|
var deleted = await _context.InsigniaManageOrganiations.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaManageOrgId);
|
|
|
|
if (deleted == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
_context.InsigniaManageOrganiations.Remove(deleted);
|
|
await _context.SaveChangesAsync();
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขหน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์
|
|
/// </summary>
|
|
/// <param name="insigniaManageOrgId">Id หน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("org/{insigniaManageOrgId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> PutOrganization([FromBody] InsigniaManageOrganizationUpdateRequest req, Guid insigniaManageOrgId)
|
|
{
|
|
var uppdated = await _context.InsigniaManageOrganiations.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.Include(x => x.InsigniaManage)
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaManageOrgId);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Include(x => x.InsigniaManageOrganiations)
|
|
.ThenInclude(x => x.OrganizationOrganization)
|
|
.FirstOrDefaultAsync(x => x.Id == uppdated.InsigniaManage.Id);
|
|
if (insigniaManage == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
var total = insigniaManage.InsigniaManageOrganiations.Where(x => x.Id != insigniaManageOrgId).Sum(x => x.Total);
|
|
if (req.Total + total > insigniaManage.Total)
|
|
return Error(GlobalMessages.InsigniaManageOrgLimit);
|
|
|
|
uppdated.Total = req.Total;
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// list dashboard หน่วยงานจัดสรรเครื่องราชอิสริยาภรณ์
|
|
/// </summary>
|
|
/// <param name="insigniaManageId">Id จัดสรรเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("org/dashboard/{insigniaManageId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetListDashboardOrganization(Guid insigniaManageId)
|
|
{
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Include(x => x.InsigniaManageOrganiations)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Insignia = p.Insignia.Name,
|
|
InsigniaId = p.Insignia.Id,
|
|
Year = p.Year,
|
|
Total = p.Total,
|
|
Allocate = p.InsigniaManageOrganiations.Sum(x => x.Total),
|
|
Remain = p.Total - p.InsigniaManageOrganiations.Sum(x => x.Total),
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaManageId);
|
|
if (insigniaManage == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
return Success(insigniaManage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขยืมเครื่องราชฯ
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost("borrow")]
|
|
public async Task<ActionResult<ResponseObject>> PostBorrowInsignia([FromBody] InsigniaBorrowRequest req)
|
|
{
|
|
var organization = await _context.Organizations.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.FirstOrDefaultAsync(x => x.Id == req.BorrowOrganizationId);
|
|
if (organization == null)
|
|
return Error(GlobalMessages.OrganizationNotFound);
|
|
|
|
var insigniaNoteProfile = await _context.InsigniaNoteProfiles.AsQueryable()
|
|
.Include(x => x.RequestInsignia)
|
|
.Include(x => x.InsigniaNote)
|
|
.FirstOrDefaultAsync(x => x.Id == req.InsigniaNoteProfileId);
|
|
if (insigniaNoteProfile == null)
|
|
return Error(GlobalMessages.InsigniaRequestProfileNotFound);
|
|
if (insigniaNoteProfile.Status != "DONE")
|
|
return Error(GlobalMessages.InsigniaNoBorrow);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Year == insigniaNoteProfile.InsigniaNote.Year && x.Insignia == insigniaNoteProfile.RequestInsignia);
|
|
if (insigniaManage == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
var insigniaManageOrganiation = await _context.InsigniaManageOrganiations.AsQueryable()
|
|
.Include(x => x.InsigniaManageProfiles)
|
|
.FirstOrDefaultAsync(x => x.OrganizationOrganization == organization.OrganizationOrganization && x.InsigniaManage == insigniaManage);
|
|
if (insigniaManageOrganiation == null)
|
|
return Error(GlobalMessages.InsigniaManageOrgNotFound);
|
|
|
|
var insigniaManageProfile = await _context.InsigniaManageProfiles.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.InsigniaNoteProfile == insigniaNoteProfile && x.Status == false);
|
|
if (insigniaManageProfile != null)
|
|
return Error(GlobalMessages.InsigniaNotReturn);
|
|
|
|
if (insigniaManageOrganiation.Total <= insigniaManageOrganiation.InsigniaManageProfiles.Count())
|
|
return Error(GlobalMessages.InsigniaBorrowOrgLimit);
|
|
|
|
await _context.InsigniaManageProfiles.AddAsync(
|
|
new InsigniaManageProfile
|
|
{
|
|
Status = false,
|
|
InsigniaManageOrganiation = insigniaManageOrganiation,
|
|
BorrowOrganization = organization.OrganizationOrganization,
|
|
BorrowDate = req.BorrowDate,
|
|
InsigniaNoteProfile = insigniaNoteProfile,
|
|
CreatedUserId = UserId ?? "System Administrator",
|
|
CreatedFullName = FullName ?? "",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
});
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขคืนเครื่องราชฯ
|
|
/// </summary>
|
|
/// <param name="insigniaManageProfileId">Id ยืมเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("return/{insigniaManageProfileId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> PutReturnInsignia([FromBody] InsigniaReturnRequest req, Guid insigniaManageProfileId)
|
|
{
|
|
|
|
var uppdated = await _context.InsigniaManageProfiles.AsQueryable()
|
|
.Include(x => x.BorrowOrganization)
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaManageProfileId);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
uppdated.Status = true;
|
|
uppdated.ReturnDate = req.ReturnDate;
|
|
// if (req.ReturnOrganizationId == Guid.Parse("00000000-0000-0000-0000-000000000000"))
|
|
// {
|
|
uppdated.ReturnOrganization = uppdated.BorrowOrganization;
|
|
// }
|
|
// else
|
|
// {
|
|
// var organization = await _context.Organizations.AsQueryable()
|
|
// .Include(x => x.OrganizationOrganization)
|
|
// .FirstOrDefaultAsync(x => x.Id == req.ReturnOrganizationId);
|
|
// if (organization == null)
|
|
// return Error(GlobalMessages.OrganizationNotFound);
|
|
// uppdated.ReturnOrganization = organization.OrganizationOrganization;
|
|
// }
|
|
uppdated.ReturnReason = req.ReturnReason;
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// list รายการยืม/คืนเครื่องราชฯ
|
|
/// </summary>
|
|
/// <param name="year">ปียืมขอ</param>
|
|
/// <param name="insigniaTypeId">Id ประเภทเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("borrow/{year}/{insigniaTypeId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> ListBorrowReturnInsignia(int year, Guid insigniaTypeId)
|
|
{
|
|
var insigniaType = await _context.InsigniaTypes
|
|
.FirstOrDefaultAsync(x => x.Id == insigniaTypeId);
|
|
if (insigniaType == null)
|
|
return Error(GlobalMessages.InsigniaTypeNotFound);
|
|
var data = await _context.InsigniaManageProfiles.AsQueryable()
|
|
.Where(x => x.InsigniaNoteProfile.RequestInsignia.InsigniaType == insigniaType)
|
|
.Where(x => year == 0 ? x.Id != null : x.InsigniaManageOrganiation.InsigniaManage.Year == year)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Status = p.Status,
|
|
BorrowOrganization = p.BorrowOrganization == null ? null : p.BorrowOrganization.Name,
|
|
BorrowOrganizationId = p.BorrowOrganization == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.BorrowOrganization.Id,
|
|
BorrowDate = p.BorrowDate,
|
|
ReturnDate = p.ReturnDate,
|
|
ReturnOrganization = p.ReturnOrganization == null ? null : p.ReturnOrganization.Name,
|
|
ReturnOrganizationId = p.ReturnOrganization == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.ReturnOrganization.Id,
|
|
ReturnReason = p.ReturnReason,
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
|
|
InsigniaNoteProfileId = p.InsigniaNoteProfile.Id,
|
|
Prefix = p.InsigniaNoteProfile.Profile.Prefix == null ? null : p.InsigniaNoteProfile.Profile.Prefix.Name,
|
|
Position = p.InsigniaNoteProfile.Profile.Position == null ? null : p.InsigniaNoteProfile.Profile.Position.Name,
|
|
p.InsigniaNoteProfile.Profile.CitizenId,
|
|
p.InsigniaNoteProfile.Profile.ProfileType,
|
|
FullName = $"{(p.InsigniaNoteProfile.Profile.Prefix == null ? null : p.InsigniaNoteProfile.Profile.Prefix.Name)}{p.InsigniaNoteProfile.Profile.FirstName} {p.InsigniaNoteProfile.Profile.LastName}",
|
|
RequestInsignia = p.InsigniaNoteProfile.RequestInsignia == null ? null : p.InsigniaNoteProfile.RequestInsignia.Name,
|
|
RequestInsigniaId = p.InsigniaNoteProfile.RequestInsignia == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.InsigniaNoteProfile.RequestInsignia.Id,
|
|
RequestInsigniaShortName = p.InsigniaNoteProfile.RequestInsignia == null ? null : p.InsigniaNoteProfile.RequestInsignia.ShortName,
|
|
p.InsigniaNoteProfile.DateReceive,
|
|
p.InsigniaNoteProfile.OrganizationOrganizationSend,
|
|
p.InsigniaNoteProfile.OrganizationOrganizationReceive,
|
|
InsigniaNoteProfileStatus = p.InsigniaNoteProfile.Status,
|
|
p.InsigniaNoteProfile.Issue,
|
|
p.InsigniaNoteProfile.Date,
|
|
p.InsigniaNoteProfile.VolumeNo,
|
|
p.InsigniaNoteProfile.Section,
|
|
p.InsigniaNoteProfile.Page,
|
|
p.InsigniaNoteProfile.No,
|
|
p.InsigniaNoteProfile.DatePayment,
|
|
p.InsigniaNoteProfile.TypePayment,
|
|
p.InsigniaNoteProfile.Address,
|
|
p.InsigniaNoteProfile.Number,
|
|
p.InsigniaNoteProfile.Salary,
|
|
})
|
|
.ToListAsync();
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// get รายการยืม/คืนเครื่องราชฯ
|
|
/// </summary>
|
|
/// <param name="insigniaManageProfileId">Id ประเภทเครื่องราช</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("borrow/{insigniaManageProfileId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetBorrowReturnInsignia(Guid insigniaManageProfileId)
|
|
{
|
|
var data = await _context.InsigniaManageProfiles.AsQueryable()
|
|
.Where(x => x.Id == insigniaManageProfileId)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Status = p.Status,
|
|
BorrowOrganization = p.BorrowOrganization == null ? null : p.BorrowOrganization.Name,
|
|
BorrowOrganizationId = p.BorrowOrganization == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.BorrowOrganization.Id,
|
|
BorrowDate = p.BorrowDate,
|
|
ReturnDate = p.ReturnDate,
|
|
ReturnOrganization = p.ReturnOrganization == null ? null : p.ReturnOrganization.Name,
|
|
ReturnOrganizationId = p.ReturnOrganization == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.ReturnOrganization.Id,
|
|
ReturnReason = p.ReturnReason,
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(GlobalMessages.InsigniaBorrowNotFound);
|
|
return Success(data);
|
|
}
|
|
}
|
|
}
|