apiจัดสรร

This commit is contained in:
Kittapath 2023-08-25 18:18:28 +07:00
parent 0aa0aedba9
commit 15931fbaca
27 changed files with 64051 additions and 120 deletions

View file

@ -8,6 +8,7 @@ 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;
@ -25,7 +26,6 @@ namespace BMA.EHR.Insignia.Service.Controllers
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly InsigniaPeriodsRepository _repository;
private readonly string Royal_Type = "Royal";
public InsigniaManageController(ApplicationDBContext context,
MinIOService documentService,
@ -49,18 +49,24 @@ namespace BMA.EHR.Insignia.Service.Controllers
/// <summary>
/// list จัดสรรเครื่องราช
/// </summary>
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
/// <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}")]
public async Task<ActionResult<ResponseObject>> GetList(string type)
[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.Type == type.Trim().ToUpper())
.OrderByDescending(x => x.Year)
.Where(x => x.Insignia.InsigniaType == insigniaType)
.Where(x => x.Year == year)
.OrderByDescending(x => x.CreatedAt)
.Select(p => new
{
Id = p.Id,
@ -68,7 +74,8 @@ namespace BMA.EHR.Insignia.Service.Controllers
InsigniaId = p.Insignia.Id,
Year = p.Year,
Total = p.Total,
Type = p.Type,
Allocate = p.InsigniaManageOrganiations.Sum(x => x.Total),
Remain = p.Total - p.InsigniaManageOrganiations.Sum(x => x.Total),
LastUpdatedAt = p.LastUpdatedAt,
CreatedAt = p.CreatedAt,
})
@ -79,19 +86,17 @@ namespace BMA.EHR.Insignia.Service.Controllers
/// <summary>
/// get รายละเอียดจัดสรรเครื่องราช
/// </summary>
/// <param name="id">Id จัดสรรเครื่องราช</param>
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
/// <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("{type}/{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetById(string type, Guid id)
[HttpGet("{insigniaManageId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetById(Guid insigniaManageId)
{
var data = await _context.InsigniaManages.AsQueryable()
.Where(x => x.Type == type.Trim().ToUpper())
.Where(x => x.Id == id)
.Where(x => x.Id == insigniaManageId)
.Select(p => new
{
Id = p.Id,
@ -99,7 +104,6 @@ namespace BMA.EHR.Insignia.Service.Controllers
InsigniaId = p.Insignia.Id,
Year = p.Year,
Total = p.Total,
Type = p.Type,
LastUpdatedAt = p.LastUpdatedAt,
CreatedAt = p.CreatedAt,
})
@ -113,14 +117,13 @@ namespace BMA.EHR.Insignia.Service.Controllers
/// <summary>
/// สร้างจัดสรรเครื่องราช
/// </summary>
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("{type}")]
public async Task<ActionResult<ResponseObject>> Post([FromForm] InsigniaManageRequest req, string type)
[HttpPost()]
public async Task<ActionResult<ResponseObject>> Post([FromForm] InsigniaManageRequest req)
{
var insignia = await _context.Insignias.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == req.Insignia);
@ -131,22 +134,21 @@ namespace BMA.EHR.Insignia.Service.Controllers
.Where(x => x.Insignia == insignia && x.Year == req.Year)
.FirstOrDefaultAsync();
if (insigniaManage != null)
return Error(GlobalMessages.InsigniaManageNotFound);
return Error(GlobalMessages.InsigniaManageDupicate);
var period = new InsigniaManage
{
Insignia = insignia,
Year = req.Year,
Total = req.Total,
Type = type.Trim().ToUpper(),
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.InsigniaManages.AddAsync(period);
await _context.InsigniaManages.AddAsync(
new InsigniaManage
{
Insignia = insignia,
Year = req.Year,
Total = req.Total,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
await _context.SaveChangesAsync();
return Success();
@ -155,19 +157,17 @@ namespace BMA.EHR.Insignia.Service.Controllers
/// <summary>
/// ลบจัดสรรเครื่องราช
/// </summary>
/// <param name="id">Id จัดสรรเครื่องราช</param>
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
/// <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("{type}/{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Delete(Guid id, string type)
[HttpDelete("{insigniaManageId:length(36)}")]
public async Task<ActionResult<ResponseObject>> Delete(Guid insigniaManageId)
{
var deleted = await _context.InsigniaManages.AsQueryable()
.Where(x => x.Type == type.Trim().ToUpper())
.Where(x => x.Id == id)
.Where(x => x.Id == insigniaManageId)
.FirstOrDefaultAsync();
if (deleted == null)
@ -181,15 +181,14 @@ namespace BMA.EHR.Insignia.Service.Controllers
/// <summary>
/// แก้ไขจัดสรรเครื่องราช
/// </summary>
/// <param name="id">Id จัดสรรเครื่องราช</param>
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
/// <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("{type}/{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Put([FromForm] InsigniaManageRequest req, Guid id, string type)
[HttpPut("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Put([FromForm] InsigniaManageRequest req, Guid insigniaManageId)
{
var insignia = await _context.Insignias.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == req.Insignia);
@ -197,21 +196,20 @@ namespace BMA.EHR.Insignia.Service.Controllers
return Error(GlobalMessages.InsigniaNotFound);
var insigniaManage = await _context.InsigniaManages.AsQueryable()
.Where(x => x.Insignia == insignia && x.Year == req.Year && x.Id != id)
.Where(x => x.Insignia == insignia && x.Year == req.Year && x.Id != insigniaManageId)
.FirstOrDefaultAsync();
if (insigniaManage != null)
return Error(GlobalMessages.InsigniaManageNotFound);
return Error(GlobalMessages.InsigniaManageDupicate);
var uppdated = await _context.InsigniaManages.AsQueryable()
.Include(x => x.Insignia)
.FirstOrDefaultAsync(x => x.Id == id);
.FirstOrDefaultAsync(x => x.Id == insigniaManageId);
if (uppdated == null)
return Error(GlobalMessages.InsigniaManageNotFound);
uppdated.Insignia = insignia;
uppdated.Year = req.Year;
// uppdated.Year = req.Year;
uppdated.Total = req.Total;
uppdated.Type = type.Trim().ToUpper();
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
uppdated.LastUpdateUserId = UserId ?? "";
uppdated.LastUpdatedAt = DateTime.Now;
@ -221,5 +219,347 @@ namespace BMA.EHR.Insignia.Service.Controllers
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,
})
.FirstOrDefaultAsync();
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 organizationOrganization = await _context.OrganizationOrganizations.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == req.OrganizationOrganizationId);
if (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 total = insigniaManage.InsigniaManageOrganiations.Where(x => x.OrganizationOrganization != organizationOrganization).Sum(x => x.Total);
if (req.Total + total > insigniaManage.Total)
return Error(GlobalMessages.InsigniaManageOrgLimit);
await _context.InsigniaManageOrganiations.AddAsync(
new InsigniaManageOrganiation
{
OrganizationOrganization = organizationOrganization,
Total = req.Total,
InsigniaManage = insigniaManage,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
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([FromForm] 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([FromForm] InsigniaBorrowRequest req)
{
var organizationOrganization = await _context.OrganizationOrganizations.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == req.BorrowOrganizationId);
if (organizationOrganization == null)
return Error(GlobalMessages.OrganizationNotFound);
var insigniaNoteProfile = await _context.InsigniaNoteProfiles.AsQueryable()
.Include(x => x.RequestInsignia)
.FirstOrDefaultAsync(x => x.Id == req.InsigniaNoteProfileId);
if (insigniaNoteProfile == null)
return Error(GlobalMessages.InsigniaRequestProfileNotFound);
var insigniaManage = await _context.InsigniaManages.AsQueryable()
.FirstOrDefaultAsync(x => x.Year == DateTime.Now.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 == organizationOrganization && x.InsigniaManage == insigniaManage);
if (insigniaManageOrganiation == null)
return Error(GlobalMessages.InsigniaManageOrgNotFound);
var insigniaManageProfile = await _context.InsigniaManageProfiles.AsQueryable()
.FirstOrDefaultAsync(x => x.InsigniaNoteProfile == insigniaNoteProfile && x.InsigniaManageOrganiation == insigniaManageOrganiation && 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 = organizationOrganization,
BorrowDate = req.BorrowDate,
InsigniaNoteProfile = insigniaNoteProfile,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
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([FromForm] InsigniaReturnRequest req, Guid insigniaManageProfileId)
{
var organizationOrganization = await _context.OrganizationOrganizations.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == req.ReturnOrganizationId);
if (organizationOrganization == null)
return Error(GlobalMessages.OrganizationNotFound);
var uppdated = await _context.InsigniaManageProfiles.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == insigniaManageProfileId);
if (uppdated == null)
return Error(GlobalMessages.InsigniaManageNotFound);
uppdated.Status = true;
uppdated.ReturnDate = req.ReturnDate;
uppdated.ReturnOrganization = 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 => 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,
})
.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);
}
}
}

View file

@ -3,6 +3,7 @@ using System.Security.Claims;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Requests;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Models.HR;
using BMA.EHR.Domain.Models.Insignias;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
@ -125,6 +126,73 @@ namespace BMA.EHR.Insignia.Service.Controllers
}
}
private async Task<List<ImportReceiveRequest>> ReadExcelImportReceive(IFormFile formFile)
{
var list = new List<ImportReceiveRequest>();
using (var stream = new MemoryStream())
{
await formFile.CopyToAsync(stream);
using (var package = new ExcelPackage(stream))
{
for (int i = 0; i < package.Workbook.Worksheets.Count(); i++)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[i];
var rowCount = worksheet.Dimension.Rows;
for (int row = 2; row <= rowCount; row++)
{
list.Add(new ImportReceiveRequest
{
Number = worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString() : null,
RequestInsignia = worksheet.Cells[row, 2].Value != null ? worksheet.Cells[row, 2].Value.ToString() : null,
CitizanId = worksheet.Cells[row, 3].Value != null ? worksheet.Cells[row, 3].Value.ToString() : null,
Prefix = worksheet.Cells[row, 4].Value != null ? worksheet.Cells[row, 4].Value.ToString() : null,
FullName = worksheet.Cells[row, 5].Value != null ? worksheet.Cells[row, 5].Value.ToString() : null,
Position = worksheet.Cells[row, 6].Value != null ? worksheet.Cells[row, 6].Value.ToString() : null,
DateReceive = worksheet.Cells[row, 7].Value != null ? DateTime.Parse(worksheet.Cells[row, 7].Value.ToString()) : null,
OrgSend = worksheet.Cells[row, 8].Value != null ? worksheet.Cells[row, 8].Value.ToString() : null,
OrgReceive = worksheet.Cells[row, 9].Value != null ? worksheet.Cells[row, 9].Value.ToString() : null,
Date = worksheet.Cells[row, 10].Value != null ? DateTime.Parse(worksheet.Cells[row, 10].Value.ToString()) : null,
VolumeNo = worksheet.Cells[row, 11].Value != null ? worksheet.Cells[row, 11].Value.ToString() : null,
Section = worksheet.Cells[row, 12].Value != null ? worksheet.Cells[row, 12].Value.ToString() : null,
Page = worksheet.Cells[row, 13].Value != null ? worksheet.Cells[row, 13].Value.ToString() : null,
No = worksheet.Cells[row, 14].Value != null ? worksheet.Cells[row, 14].Value.ToString() : null,
});
}
}
}
}
return list;
}
private async Task<List<ImportInvoiceRequest>> ReadExcelImportInvoice(IFormFile formFile)
{
var list = new List<ImportInvoiceRequest>();
using (var stream = new MemoryStream())
{
await formFile.CopyToAsync(stream);
using (var package = new ExcelPackage(stream))
{
for (int i = 0; i < package.Workbook.Worksheets.Count(); i++)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[i];
var rowCount = worksheet.Dimension.Rows;
for (int row = 2; row <= rowCount; row++)
{
list.Add(new ImportInvoiceRequest
{
CitizanId = worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString() : null,
Number = worksheet.Cells[row, 2].Value != null ? worksheet.Cells[row, 2].Value.ToString() : null,
DatePayment = worksheet.Cells[row, 3].Value != null ? DateTime.Parse(worksheet.Cells[row, 3].Value.ToString()) : null,
TypePayment = worksheet.Cells[row, 4].Value != null ? worksheet.Cells[row, 4].Value.ToString() : null,
Address = worksheet.Cells[row, 5].Value != null ? worksheet.Cells[row, 5].Value.ToString() : null,
});
}
}
}
}
return list;
}
#endregion
#region " ดึงเครื่องราชฯ ล่าสุดของครู (GetInsigniaLast) "
@ -1272,6 +1340,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
if (profile == null)
return Error(GlobalMessages.DataNotFound);
var insignia = await _context.Insignias
.Include(x => x.InsigniaType)
.FirstOrDefaultAsync(x => x.Id == req.InsigniaId);
if (insignia == null)
return Error(GlobalMessages.InsigniaNotFound);
@ -1313,7 +1382,29 @@ namespace BMA.EHR.Insignia.Service.Controllers
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
//////////////บันทึกลงทะเบียน
await _context.ProfileInsignias.AddAsync(new ProfileInsignia
{
Year = insigniaNote.Year,
No = req.No,
Issue = req.VolumeNo,
VolumeNo = req.VolumeNo,
// Volume = req.Volume,
Section = req.Section,
Page = req.Page,
DateAnnounce = req.DateReceive,
ReceiveDate = req.Date,
InsigniaType = insignia.InsigniaType == null ? null : insignia.InsigniaType.Name,
Insignia = insignia,
// RefCommandNo = req.RefCommandNo,
// RefCommandDate = req.RefCommandDate,
Profile = profile,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
}
else
{
@ -1434,6 +1525,9 @@ namespace BMA.EHR.Insignia.Service.Controllers
.ThenInclude(x => x.Profile)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.FirstOrDefaultAsync(x => x.Id == insigniaNoteId);
if (insigniaNote == null)
return Error(GlobalMessages.InsigniaRequestNotFound);
@ -1461,7 +1555,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
var _insignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
if (_insignia == null)
continue;
await _context.InsigniaNoteProfiles.AddAsync(new InsigniaNoteProfile
profile = new InsigniaNoteProfile
{
RequestDate = DateTime.Now,
Salary = _profile.Salaries.Count() == 0 ? null : _profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount,
@ -1480,7 +1574,8 @@ namespace BMA.EHR.Insignia.Service.Controllers
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
};
await _context.InsigniaNoteProfiles.AddAsync(profile);
}
else
{
@ -1497,7 +1592,29 @@ namespace BMA.EHR.Insignia.Service.Controllers
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
//////////////บันทึกลงทะเบียน
await _context.ProfileInsignias.AddAsync(new ProfileInsignia
{
Year = insigniaNote.Year,
No = profile.No,
Issue = profile.VolumeNo,
VolumeNo = profile.VolumeNo,
// Volume = profile.Volume,
Section = profile.Section,
Page = profile.Page,
DateAnnounce = profile.DateReceive,
ReceiveDate = profile.Date,
InsigniaType = profile.RequestInsignia.InsigniaType == null ? null : profile.RequestInsignia.InsigniaType.Name,
Insignia = profile.RequestInsignia,
// RefCommandNo = req.RefCommandNo,
// RefCommandDate = req.RefCommandDate,
Profile = profile.Profile,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
}
await _context.SaveChangesAsync();
return Success();
@ -1551,71 +1668,223 @@ namespace BMA.EHR.Insignia.Service.Controllers
return Success();
}
private async Task<List<ImportReceiveRequest>> ReadExcelImportReceive(IFormFile formFile)
/// <summary>
/// preview บันทึกผลการได้รับเครื่องราชฯ
/// </summary>
/// <param name="insigniaNoteId">Id รอบบันทึกผลเครื่องราช</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("preview/receice/{insigniaNoteId:length(36)}"), DisableRequestSizeLimit]
public async Task<ActionResult<ResponseObject>> PreviewReceiceProfile([FromForm] ImportFileRequest req, Guid insigniaNoteId)
{
var list = new List<ImportReceiveRequest>();
using (var stream = new MemoryStream())
var insigniaNote = await _context.InsigniaNotes
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.ThenInclude(x => x.Position)
.FirstOrDefaultAsync(x => x.Id == insigniaNoteId);
if (insigniaNote == null)
return Error(GlobalMessages.InsigniaRequestNotFound);
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
{
await formFile.CopyToAsync(stream);
using (var package = new ExcelPackage(stream))
{
for (int i = 0; i < package.Workbook.Worksheets.Count(); i++)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[i];
var rowCount = worksheet.Dimension.Rows;
for (int row = 2; row <= rowCount; row++)
{
list.Add(new ImportReceiveRequest
{
Number = worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString() : null,
RequestInsignia = worksheet.Cells[row, 2].Value != null ? worksheet.Cells[row, 2].Value.ToString() : null,
CitizanId = worksheet.Cells[row, 3].Value != null ? worksheet.Cells[row, 3].Value.ToString() : null,
Prefix = worksheet.Cells[row, 4].Value != null ? worksheet.Cells[row, 4].Value.ToString() : null,
FullName = worksheet.Cells[row, 5].Value != null ? worksheet.Cells[row, 5].Value.ToString() : null,
Position = worksheet.Cells[row, 6].Value != null ? worksheet.Cells[row, 6].Value.ToString() : null,
DateReceive = worksheet.Cells[row, 7].Value != null ? DateTime.Parse(worksheet.Cells[row, 7].Value.ToString()) : null,
OrgSend = worksheet.Cells[row, 8].Value != null ? worksheet.Cells[row, 8].Value.ToString() : null,
OrgReceive = worksheet.Cells[row, 9].Value != null ? worksheet.Cells[row, 9].Value.ToString() : null,
Date = worksheet.Cells[row, 10].Value != null ? DateTime.Parse(worksheet.Cells[row, 10].Value.ToString()) : null,
VolumeNo = worksheet.Cells[row, 11].Value != null ? worksheet.Cells[row, 11].Value.ToString() : null,
Section = worksheet.Cells[row, 12].Value != null ? worksheet.Cells[row, 12].Value.ToString() : null,
Page = worksheet.Cells[row, 13].Value != null ? worksheet.Cells[row, 13].Value.ToString() : null,
No = worksheet.Cells[row, 14].Value != null ? worksheet.Cells[row, 14].Value.ToString() : null,
});
}
}
}
return Error(GlobalMessages.NoFileToUpload);
}
return list;
var file = Request.Form.Files[0];
if (!Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
{
return Error("นามสกุลไฟล์ต้องเป็น .xlsx!");
}
var items = await ReadExcelImportReceive(file);
var _insigniaNoteProfiles = new List<dynamic>();
foreach (var item in items)
{
var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.Profile.CitizenId == item.CitizanId);
if (profile == null)
{
var _profile = await _context.Profiles
.Include(x => x.Salaries)
.FirstOrDefaultAsync(x => x.CitizenId == item.CitizanId);
if (_profile == null)
continue;
var _insignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
if (_insignia == null)
continue;
profile = new InsigniaNoteProfile
{
RequestDate = DateTime.Now,
Salary = _profile.Salaries.Count() == 0 ? null : _profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount,
IsApprove = true,
Status = "DONE",
Number = item.Number,
RequestInsignia = _insignia,
DateReceive = item.DateReceive,
Date = item.Date,
VolumeNo = item.VolumeNo,
Section = item.Section,
Page = item.Page,
No = item.No,
Profile = _profile,
InsigniaNote = insigniaNote,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
}
else
{
profile.Status = "DONE";
profile.Number = item.Number;
profile.RequestInsignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia) == null ? profile.RequestInsignia : await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
profile.DateReceive = item.DateReceive;
profile.Date = item.Date;
profile.VolumeNo = item.VolumeNo;
profile.Section = item.Section;
profile.Page = item.Page;
profile.No = item.No;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
_insigniaNoteProfiles.Add(
new
{
profile.Id,
Prefix = profile.Profile.Prefix == null ? null : profile.Profile.Prefix.Name,
Position = profile.Profile.Position == null ? null : profile.Profile.Position.Name,
profile.Profile.CitizenId,
profile.Profile.ProfileType,
FullName = $"{profile.Profile.FirstName} {profile.Profile.LastName}",
RequestInsignia = profile.RequestInsignia == null ? null : profile.RequestInsignia.Name,
RequestInsigniaId = profile.RequestInsignia == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : profile.RequestInsignia.Id,
RequestInsigniaShortName = profile.RequestInsignia == null ? null : profile.RequestInsignia.ShortName,
profile.DateReceive,
profile.OrganizationOrganizationSend,
profile.OrganizationOrganizationReceive,
profile.Status,
profile.Issue,
profile.Date,
profile.VolumeNo,
profile.Section,
profile.Page,
profile.No,
profile.DatePayment,
profile.TypePayment,
profile.Address,
profile.Number,
profile.Salary,
}
);
}
return Success(_insigniaNoteProfiles);
}
private async Task<List<ImportInvoiceRequest>> ReadExcelImportInvoice(IFormFile formFile)
/// <summary>
/// preview บันทึกผลใบกำกับเครื่องราชฯ
/// </summary>
/// <param name="insigniaNoteId">Id รอบบันทึกผลเครื่องราช</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("preview/invoice/{insigniaNoteId:length(36)}"), DisableRequestSizeLimit]
public async Task<ActionResult<ResponseObject>> PreviewInvoiceProfile([FromForm] ImportFileRequest req, Guid insigniaNoteId)
{
var list = new List<ImportInvoiceRequest>();
using (var stream = new MemoryStream())
var insigniaNote = await _context.InsigniaNotes
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.Profile)
.ThenInclude(x => x.Position)
.FirstOrDefaultAsync(x => x.Id == insigniaNoteId);
if (insigniaNote == null)
return Error(GlobalMessages.InsigniaRequestNotFound);
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
{
await formFile.CopyToAsync(stream);
using (var package = new ExcelPackage(stream))
{
for (int i = 0; i < package.Workbook.Worksheets.Count(); i++)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[i];
var rowCount = worksheet.Dimension.Rows;
for (int row = 2; row <= rowCount; row++)
{
list.Add(new ImportInvoiceRequest
{
CitizanId = worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString() : null,
Number = worksheet.Cells[row, 2].Value != null ? worksheet.Cells[row, 2].Value.ToString() : null,
DatePayment = worksheet.Cells[row, 3].Value != null ? DateTime.Parse(worksheet.Cells[row, 3].Value.ToString()) : null,
TypePayment = worksheet.Cells[row, 4].Value != null ? worksheet.Cells[row, 4].Value.ToString() : null,
Address = worksheet.Cells[row, 5].Value != null ? worksheet.Cells[row, 5].Value.ToString() : null,
});
}
}
}
return Error(GlobalMessages.NoFileToUpload);
}
return list;
var file = Request.Form.Files[0];
if (!Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
{
return Error("นามสกุลไฟล์ต้องเป็น .xlsx!");
}
var items = await ReadExcelImportInvoice(file);
foreach (var item in items)
{
var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.Profile.CitizenId == item.CitizanId);
if (profile == null)
continue;
profile.Number = item.Number;
profile.DatePayment = item.DatePayment;
profile.TypePayment = item.TypePayment;
profile.Address = item.Address;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
var _insigniaNoteProfiles = new List<dynamic>();
foreach (var item in items)
{
var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.Profile.CitizenId == item.CitizanId);
if (profile == null)
continue;
profile.Status = "DONE";
profile.Number = item.Number;
profile.DatePayment = item.DatePayment;
profile.TypePayment = item.TypePayment;
profile.Address = item.Address;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
_insigniaNoteProfiles.Add(
new
{
profile.Id,
Prefix = profile.Profile.Prefix == null ? null : profile.Profile.Prefix.Name,
Position = profile.Profile.Position == null ? null : profile.Profile.Position.Name,
profile.Profile.CitizenId,
profile.Profile.ProfileType,
FullName = $"{profile.Profile.FirstName} {profile.Profile.LastName}",
RequestInsignia = profile.RequestInsignia == null ? null : profile.RequestInsignia.Name,
RequestInsigniaId = profile.RequestInsignia == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : profile.RequestInsignia.Id,
RequestInsigniaShortName = profile.RequestInsignia == null ? null : profile.RequestInsignia.ShortName,
profile.DateReceive,
profile.OrganizationOrganizationSend,
profile.OrganizationOrganizationReceive,
profile.Status,
profile.Issue,
profile.Date,
profile.VolumeNo,
profile.Section,
profile.Page,
profile.No,
profile.DatePayment,
profile.TypePayment,
profile.Address,
profile.Number,
profile.Salary,
}
);
}
return Success(_insigniaNoteProfiles);
}
}