fix : เปลี่ยนชื่อ Folder + Project เอาคำว่า Service ออก เพื่อให้สามารถ run บน macOS Sonoma ได้ (ไม่เปลี่ยนจะไม่สามารถ run ได้ เนื่องจาก macOS จะมองว่าเป็น application ของ mac)
This commit is contained in:
parent
9d90c98800
commit
59340500b7
42 changed files with 21614 additions and 3532 deletions
|
|
@ -1,721 +0,0 @@
|
|||
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 OfficeOpenXml.Export.ToDataTable;
|
||||
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;
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
|
||||
public InsigniaManageController(ApplicationDBContext context,
|
||||
MinIOService documentService,
|
||||
InsigniaPeriodsRepository repository,
|
||||
NotificationRepository repositoryNoti,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
UserProfileRepository userProfileRepository)
|
||||
{
|
||||
_context = context;
|
||||
_documentService = documentService;
|
||||
_repository = repository;
|
||||
_repositoryNoti = repositoryNoti;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
}
|
||||
|
||||
#region " Properties "
|
||||
|
||||
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
||||
|
||||
private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
|
||||
|
||||
#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,
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
CreatedUserId = UserId ?? "",
|
||||
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 = _userProfileRepository.GetOc(p.OrganizationId, 0, AccessToken) == null ? "" : _userProfileRepository.GetOc(p.OrganizationId, 0, AccessToken)!.Root,
|
||||
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();
|
||||
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 = _userProfileRepository.GetOc(req.OrganizationOrganizationId, 0, AccessToken);
|
||||
|
||||
//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.OrganizationId == organization.RootId && x.InsigniaManage == insigniaManage);
|
||||
if (insigniaManageOrganiation != null)
|
||||
return Error(GlobalMessages.InsigniaManageOrgDupicate);
|
||||
|
||||
var total = insigniaManage.InsigniaManageOrganiations.Where(x => x.OrganizationId != organization.RootId).Sum(x => x.Total);
|
||||
if (req.Total + total > insigniaManage.Total)
|
||||
return Error(GlobalMessages.InsigniaManageOrgLimit);
|
||||
await _context.InsigniaManageOrganiations.AddAsync(
|
||||
new InsigniaManageOrganiation
|
||||
{
|
||||
//OrganizationOrganization = organization.OrganizationOrganization,
|
||||
OrganizationId = organization.RootId ?? Guid.Empty,
|
||||
Total = req.Total,
|
||||
InsigniaManage = insigniaManage,
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
CreatedUserId = UserId ?? "",
|
||||
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 insigniaNoteProfile = await _context.InsigniaNoteProfiles.AsQueryable()
|
||||
.Include(x => x.RequestInsignia)
|
||||
.Include(x => x.InsigniaNote)
|
||||
//.Include(x => x.Profile)
|
||||
.FirstOrDefaultAsync(x => x.Id == req.InsigniaNoteProfileId);
|
||||
if (insigniaNoteProfile == null)
|
||||
return Error(GlobalMessages.InsigniaRequestProfileNotFound);
|
||||
if (insigniaNoteProfile.Status != "DONE")
|
||||
return Error(GlobalMessages.InsigniaNoBorrow);
|
||||
|
||||
//var _organization = await _context.Organizations.AsQueryable()
|
||||
// .FirstOrDefaultAsync(x => x.Id == insigniaNoteProfile.Profile.OcId);
|
||||
|
||||
//TODO : Hardcode OCId
|
||||
if (req.BorrowOrganizationId == null) req.BorrowOrganizationId = Guid.Parse("e8493cd1-d371-402e-add6-566e68d5d1b3");
|
||||
|
||||
var organization = _userProfileRepository.GetOc(req.BorrowOrganizationId.Value, 0, AccessToken);
|
||||
//if (organization == null)
|
||||
// return Error(GlobalMessages.OrganizationNotFound);
|
||||
|
||||
//var organization = await _context.Organizations.AsQueryable()
|
||||
// .Include(x => x.OrganizationOrganization)
|
||||
// .FirstOrDefaultAsync(x => x.Id == _organization.OrganizationAgencyId);
|
||||
//if (organization == null)
|
||||
// return Error(GlobalMessages.OrganizationNotFound);
|
||||
|
||||
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.OrganizationId == organization.RootId && 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,
|
||||
BorrowOrganizationId = organization.RootId,
|
||||
BorrowDate = req.BorrowDate,
|
||||
InsigniaNoteProfile = insigniaNoteProfile,
|
||||
CreatedFullName = FullName ?? "System Administrator",
|
||||
CreatedUserId = UserId ?? "",
|
||||
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.ReturnOrganizationId = uppdated.BorrowOrganizationId;
|
||||
// }
|
||||
// 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 rawData = 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,
|
||||
BorrowOrganization = p.BorrowOrganizationId == null ? null : _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken),
|
||||
ReturnOrganization = p.ReturnOrganizationId == null ? null : _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken),
|
||||
Profile = p.InsigniaNoteProfile.ProfileId == null ? null : _userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken),
|
||||
|
||||
Status = p.Status,
|
||||
BorrowDate = p.BorrowDate,
|
||||
ReturnDate = p.ReturnDate,
|
||||
ReturnReason = p.ReturnReason,
|
||||
LastUpdatedAt = p.LastUpdatedAt,
|
||||
CreatedAt = p.CreatedAt,
|
||||
|
||||
InsigniaNoteProfileId = p.InsigniaNoteProfile.Id,
|
||||
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();
|
||||
|
||||
var data = rawData
|
||||
.Select(p => new
|
||||
{
|
||||
p.Id,
|
||||
p.Status,
|
||||
BorrowOrganization = p.BorrowOrganization == null ? "" : p.BorrowOrganization.Root,
|
||||
BorrowOrganizationId = p.BorrowOrganization == null ? Guid.Empty : p.BorrowOrganization.RootId,
|
||||
p.BorrowDate,
|
||||
p.ReturnDate,
|
||||
ReturnOrganization = p.ReturnOrganization == null ? "" : p.ReturnOrganization.Root,
|
||||
ReturnOrganizationId = p.ReturnOrganization == null ? Guid.Empty : p.ReturnOrganization.RootId,
|
||||
p.ReturnReason,
|
||||
p.LastUpdatedAt,
|
||||
p.CreatedAt,
|
||||
p.InsigniaNoteProfileId,
|
||||
CitizenId = p.Profile == null ? "" : p.Profile.CitizenId,
|
||||
Prefix = p.Profile == null ? "" : p.Profile.Prefix,
|
||||
Position = p.Profile == null ? "" : p.Profile.Position,
|
||||
FullName = p.Profile == null ? "" : $"{p.Profile.Prefix}{p.Profile.FirstName} {p.Profile.LastName}",
|
||||
ProfileType = p.Profile == null ? "" : p.Profile.ProfileType,
|
||||
p.RequestInsignia,
|
||||
p.RequestInsigniaId,
|
||||
p.RequestInsigniaShortName,
|
||||
p.DateReceive,
|
||||
p.OrganizationOrganizationSend,
|
||||
p.OrganizationOrganizationReceive,
|
||||
p.InsigniaNoteProfileStatus,
|
||||
p.Issue,
|
||||
p.Date,
|
||||
p.VolumeNo,
|
||||
p.Section,
|
||||
p.Page,
|
||||
p.No,
|
||||
p.DatePayment,
|
||||
p.TypePayment,
|
||||
p.Address,
|
||||
p.Number,
|
||||
p.Salary,
|
||||
|
||||
})
|
||||
.ToList();
|
||||
|
||||
//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 = _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken) == null ? null : _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken).Root,
|
||||
// BorrowOrganizationId = _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken) == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken).RootId,
|
||||
// BorrowDate = p.BorrowDate,
|
||||
// ReturnDate = p.ReturnDate,
|
||||
// ReturnOrganization = _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken) == null ? null : _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken).Root,
|
||||
// ReturnOrganizationId = _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken) == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken).RootId,
|
||||
// ReturnReason = p.ReturnReason,
|
||||
// LastUpdatedAt = p.LastUpdatedAt,
|
||||
// CreatedAt = p.CreatedAt,
|
||||
|
||||
// InsigniaNoteProfileId = p.InsigniaNoteProfile.Id,
|
||||
// Prefix = _userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).Prefix,
|
||||
// Position = _userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).Position,
|
||||
// _userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).CitizenId,
|
||||
// _userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).ProfileType,
|
||||
// FullName = $"{_userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).Prefix}{_userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).FirstName} {_userProfileRepository.GetOfficerProfileById(p.InsigniaNoteProfile.ProfileId.Value, AccessToken).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 = _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken) == null ? null : _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken).Root,
|
||||
BorrowOrganizationId = _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken) == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : _userProfileRepository.GetOc(p.BorrowOrganizationId!.Value, 0, AccessToken).RootId,
|
||||
BorrowDate = p.BorrowDate,
|
||||
ReturnDate = p.ReturnDate,
|
||||
ReturnOrganization = _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken) == null ? null : _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken).Root,
|
||||
ReturnOrganizationId = _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken) == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : _userProfileRepository.GetOc(p.ReturnOrganizationId!.Value, 0, AccessToken).RootId,
|
||||
ReturnReason = p.ReturnReason,
|
||||
LastUpdatedAt = p.LastUpdatedAt,
|
||||
CreatedAt = p.CreatedAt,
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
if (data == null)
|
||||
return Error(GlobalMessages.InsigniaBorrowNotFound);
|
||||
return Success(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue