509 lines
28 KiB
C#
509 lines
28 KiB
C#
using System.Security.Claims;
|
|
using BMA.EHR.Application.Repositories;
|
|
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 Newtonsoft.Json;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
|
|
namespace BMA.EHR.Insignia.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/insignia/request")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("เครื่องราช")]
|
|
public class InsigniaRequestController : BaseController
|
|
{
|
|
private readonly ApplicationDBContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly InsigniaPeriodsRepository _repository;
|
|
private readonly string Royal_Type = "Royal";
|
|
|
|
public InsigniaRequestController(ApplicationDBContext context,
|
|
MinIOService documentService,
|
|
InsigniaPeriodsRepository repository,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_repository = repository;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
#region " Private "
|
|
|
|
private static string GetRequestlStatusText(string status)
|
|
{
|
|
switch (status.ToLower())
|
|
{
|
|
case "st1": return "จัดทำรายชื่อ";
|
|
case "st2": return "รอ ผอ. โรงเรียนรับรอง";
|
|
case "st3": return "รอเจ้าหน้าที่เขตตรวจสอบ";
|
|
case "st3p": return "รอนำเสนอผู้อำนวยการเขต";
|
|
case "st4": return "รอเสนอสำนักการศึกษา";
|
|
case "st5": return "รอเจ้าหน้าที่ สนศ. ตรวจสอบ";
|
|
case "st5p": return "เจ้าหน้าที่ สนศ. ตรวจสอบแล้ว";
|
|
case "pending": return "รอออกคำสั่ง";
|
|
case "finish": return "ออกคำสั่งแล้ว";
|
|
default: return "สถานะไม่ถูกต้อง";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " ดึงเครื่องราชฯ ล่าสุดของครู (GetInsigniaLast) "
|
|
private InsigniaItem GetInsigniaLast(Guid? id)
|
|
{
|
|
var insignia = _context.Insignias.AsQueryable()
|
|
.Where(i => id != null ? i.Id == id : i.Name.Contains("ตริตาภรณ์มงกุฎไทย")).Select(i => new InsigniaItem
|
|
{
|
|
Id = i.Id,
|
|
Name = i.Name,
|
|
ShortName = i.ShortName,
|
|
Level = i.InsigniaType == null ? null : i.InsigniaType.Name,
|
|
LevelId = i.InsigniaType == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : i.InsigniaType.Id
|
|
}).FirstOrDefault();
|
|
|
|
return insignia;
|
|
}
|
|
#endregion
|
|
|
|
#region " จัดทำรายชื่อครูที่มีสิทธิในการยืนขอเครื่องราชฯ "
|
|
|
|
[HttpGet("old/{role}/{ocId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetInsignaiRequest(Guid ocId, string role)
|
|
{
|
|
var result = await _repository.GetInsigniaRequest(Royal_Type, ocId);
|
|
if (result != null)
|
|
{
|
|
Guid period = result.PeriodId;
|
|
string periodName = result.Name;
|
|
string requestStatus = result.RequestStatus;
|
|
var resend = new InsigniaResults
|
|
{
|
|
PeriodId = period,
|
|
Year = result.Year,
|
|
Name = periodName,
|
|
RequestStatus = requestStatus,
|
|
OrganizationName = result.OrganizationName,
|
|
Items = new List<InsigniaRequestItem>()
|
|
};
|
|
var candidate = _repository.GetInsigniaCandidate(period, ocId);
|
|
|
|
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
|
|
if (requestStatus == null)
|
|
{
|
|
// บันทึกรายชื่อ
|
|
_repository.InsertCandidate(period, ocId, candidate);
|
|
}
|
|
if (role == "officer")
|
|
{
|
|
resend.Items = _repository.InsigniaHasProfile(period, ocId);
|
|
return Success(resend);
|
|
}
|
|
else
|
|
{
|
|
var passData = _context.InsigniaRequests.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.Include(x => x.RequestProfiles)
|
|
.Where(x => x.OrganizationOrganization.Id == ocId)
|
|
.Where(x => x.Period.Id == period)
|
|
.Select(ir => new
|
|
{
|
|
requstID = ir.Id,
|
|
requstStatus = ir.RequestStatus,
|
|
requstStatusName = GetRequestlStatusText(ir.RequestStatus),
|
|
fkInstituteId = -1,
|
|
fkDivisionId = ir.OrganizationOrganization.Id,
|
|
fkDivision = ir.OrganizationOrganization.Name,
|
|
fkInstitute = "",
|
|
fkPeriodId = ir.Period.Id,
|
|
insigniaRequestHasProfile = ir.RequestProfiles.AsQueryable()
|
|
.Include(x => x.Profile)
|
|
.ThenInclude(x => x.Position)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.PositionNumber)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.AcademicStanding)
|
|
.Include(x => x.RequestInsignia)
|
|
.ThenInclude(x => x.InsigniaType)
|
|
.Select(irp => new
|
|
{
|
|
request_id = irp.Request.Id,
|
|
isApprove = irp.IsApprove,
|
|
statusInstitute = irp.IsApprove.ToString(),
|
|
request_date = irp.RequestDate,
|
|
profileId = irp.Profile.Id,
|
|
// prefix = irp.Profile.Prefix,
|
|
firstname = irp.Profile.FirstName,
|
|
lastname = irp.Profile.LastName,
|
|
// posno = irp.Profile.PositionNumber.Id,
|
|
type = irp.Profile.ProfileType,
|
|
// position = irp.Profile.Position.Name,
|
|
// rank = irp.Profile.AcademicStanding.Name,
|
|
instituteName = "",
|
|
instituteId = -1,
|
|
// divisionName = irp.Profile.OrganizationOrganization.Name,
|
|
// divisionId = irp.Profile.OrganizationOrganization.Id,
|
|
lastInsigniaName = "",
|
|
requestInsigniaLevel = irp.RequestInsignia.InsigniaType == null ? null : irp.RequestInsignia.InsigniaType.Name,
|
|
requestInsigniaName = irp.RequestInsignia.Name,
|
|
requestQua = irp.QualificationStatus,
|
|
requestDoc = irp.DocumentStatus,
|
|
requestNote = irp.Note,
|
|
requestSalary = irp.Salary,
|
|
})
|
|
.Where(x => x.isApprove)
|
|
.OrderBy(y => y.profileId)
|
|
.ToList()
|
|
})
|
|
.ToList()
|
|
.FirstOrDefault();
|
|
|
|
var failData = _context.InsigniaRequests.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.Include(x => x.RequestProfiles)
|
|
.Where(x => x.OrganizationOrganization.Id == ocId)
|
|
.Where(x => x.Period.Id == period)
|
|
.Select(ir => new
|
|
{
|
|
requstID = ir.Id,
|
|
requstStatus = ir.RequestStatus,
|
|
requstStatusName = GetRequestlStatusText(ir.RequestStatus),
|
|
fkInstituteId = -1,
|
|
fkDivisionId = ir.OrganizationOrganization.Id,
|
|
fkDivision = ir.OrganizationOrganization.Name,
|
|
fkInstitute = "",
|
|
fkPeriodId = ir.Period.Id,
|
|
insigniaRequestHasProfile = ir.RequestProfiles.AsQueryable()
|
|
.Include(x => x.Profile)
|
|
.ThenInclude(x => x.Position)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.PositionNumber)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.AcademicStanding)
|
|
.Include(x => x.RequestInsignia)
|
|
.ThenInclude(x => x.InsigniaType)
|
|
.Select(irp => new
|
|
{
|
|
request_id = irp.Request.Id,
|
|
isApprove = irp.IsApprove,
|
|
statusInstitute = irp.IsApprove.ToString(),
|
|
request_date = irp.RequestDate,
|
|
profileId = irp.Profile.Id,
|
|
// prefix = irp.Profile.Prefix,
|
|
firstname = irp.Profile.FirstName,
|
|
lastname = irp.Profile.LastName,
|
|
// posno = irp.Profile.PositionNumber.Id,
|
|
type = irp.Profile.ProfileType,
|
|
// position = irp.Profile.Position.Name,
|
|
// rank = irp.Profile.AcademicStanding.Name,
|
|
instituteName = "",
|
|
instituteId = -1,
|
|
// divisionName = irp.Profile.OrganizationOrganization.Name,
|
|
// divisionId = irp.Profile.OrganizationOrganization.Id,
|
|
lastInsigniaName = "",
|
|
requestInsigniaLevel = irp.RequestInsignia.InsigniaType == null ? null : irp.RequestInsignia.InsigniaType.Name,
|
|
requestInsigniaName = irp.RequestInsignia.Name,
|
|
requestQua = irp.QualificationStatus,
|
|
requestDoc = irp.DocumentStatus,
|
|
requestNote = irp.Note,
|
|
requestSalary = irp.Salary,
|
|
})
|
|
.Where(x => !x.isApprove)
|
|
.OrderBy(y => y.profileId)
|
|
.ToList()
|
|
})
|
|
.ToList()
|
|
.FirstOrDefault();
|
|
|
|
var period_data = (from p in _context.InsigniaPeriods.AsQueryable()
|
|
where p.Id == period
|
|
select new
|
|
{
|
|
periodName = p.Name,
|
|
periodYear = p.Year,
|
|
}).FirstOrDefault();
|
|
|
|
return Success(new { passData = passData, failData = failData, period = period_data });
|
|
}
|
|
// select data to display
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
[HttpGet("{role}/{ocId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetInsignaiRequestBkk(Guid ocId, string role)
|
|
{
|
|
var result = await _repository.GetInsigniaRequest(Royal_Type, ocId);
|
|
if (result != null)
|
|
{
|
|
Guid period = result.PeriodId;
|
|
string periodName = result.Name;
|
|
string requestStatus = result.RequestStatus;
|
|
var resend = new InsigniaResults
|
|
{
|
|
PeriodId = period,
|
|
Year = result.Year,
|
|
Name = periodName,
|
|
RequestStatus = requestStatus,
|
|
OrganizationName = result.OrganizationName,
|
|
Items = new List<InsigniaRequestItem>()
|
|
};
|
|
var candidate = _repository.GetInsigniaCandidateBKK(period, ocId);
|
|
|
|
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
|
|
if (requestStatus == null)
|
|
{
|
|
// บันทึกรายชื่อ
|
|
_repository.InsertCandidate(period, ocId, candidate);
|
|
}
|
|
if (role == "officer")
|
|
{
|
|
resend.Items = _repository.InsigniaHasProfile(period, ocId);
|
|
return Success(resend);
|
|
|
|
}
|
|
else
|
|
{
|
|
var passData = _context.InsigniaRequests.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.Include(x => x.RequestProfiles)
|
|
.Where(x => x.OrganizationOrganization.Id == ocId)
|
|
.Where(x => x.Period.Id == period)
|
|
.Select(ir => new
|
|
{
|
|
requstID = ir.Id,
|
|
requstStatus = ir.RequestStatus,
|
|
requstStatusName = GetRequestlStatusText(ir.RequestStatus),
|
|
fkInstituteId = -1,
|
|
fkDivisionId = ir.OrganizationOrganization.Id,
|
|
fkDivision = ir.OrganizationOrganization.Name,
|
|
fkInstitute = "",
|
|
fkPeriodId = ir.Period.Id,
|
|
insigniaRequestHasProfile = ir.RequestProfiles.AsQueryable()
|
|
.Include(x => x.Profile)
|
|
.ThenInclude(x => x.Position)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.PositionNumber)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.AcademicStanding)
|
|
.Include(x => x.RequestInsignia)
|
|
.ThenInclude(x => x.InsigniaType)
|
|
.Select(irp => new
|
|
{
|
|
request_id = irp.Request.Id,
|
|
isApprove = irp.IsApprove,
|
|
statusInstitute = irp.IsApprove.ToString(),
|
|
request_date = irp.RequestDate,
|
|
profileId = irp.Profile.Id,
|
|
// prefix = irp.Profile.Prefix,
|
|
firstname = irp.Profile.FirstName,
|
|
lastname = irp.Profile.LastName,
|
|
// posno = irp.Profile.PositionNumber.Id,
|
|
type = irp.Profile.ProfileType,
|
|
// position = irp.Profile.Position.Name,
|
|
// rank = $"{irp.Profile.PositionType.Name}/{irp.Profile.PositionLevel.Name}",
|
|
instituteName = "",
|
|
instituteId = -1,
|
|
// divisionName = irp.Profile.OrganizationOrganization.Name,
|
|
// divisionId = irp.Profile.OrganizationOrganization.Id,
|
|
lastInsigniaName = "",
|
|
requestInsigniaLevel = irp.RequestInsignia.InsigniaType.Name,
|
|
requestInsigniaName = irp.RequestInsignia.Name,
|
|
requestQua = irp.QualificationStatus,
|
|
requestDoc = irp.DocumentStatus,
|
|
requestNote = irp.Note,
|
|
requestSalary = irp.Salary,
|
|
matchingConditions = JsonConvert.DeserializeObject<List<MatchingCondition>>(irp.MatchingConditions)
|
|
})
|
|
.Where(x => x.isApprove)
|
|
.OrderBy(y => y.profileId)
|
|
.ToList()
|
|
})
|
|
.ToList()
|
|
.FirstOrDefault();
|
|
|
|
var failData = _context.InsigniaRequests.AsQueryable()
|
|
.Include(x => x.OrganizationOrganization)
|
|
.Include(x => x.RequestProfiles)
|
|
.Where(x => x.OrganizationOrganization.Id == ocId)
|
|
.Where(x => x.Period.Id == period)
|
|
.Select(ir => new
|
|
{
|
|
requstID = ir.Id,
|
|
requstStatus = ir.RequestStatus,
|
|
requstStatusName = GetRequestlStatusText(ir.RequestStatus),
|
|
fkInstituteId = -1,
|
|
fkDivisionId = ir.OrganizationOrganization.Id,
|
|
fkDivision = ir.OrganizationOrganization.Name,
|
|
fkInstitute = "",
|
|
fkPeriodId = ir.Period.Id,
|
|
insigniaRequestHasProfile = ir.RequestProfiles.AsQueryable()
|
|
.Include(x => x.Profile)
|
|
.ThenInclude(x => x.Position)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.PositionNumber)
|
|
.Include(x => x.Profile)
|
|
// .ThenInclude(x => x.AcademicStanding)
|
|
.Include(x => x.RequestInsignia)
|
|
.ThenInclude(x => x.InsigniaType)
|
|
.Select(irp => new
|
|
{
|
|
request_id = irp.Request.Id,
|
|
isApprove = irp.IsApprove,
|
|
statusInstitute = irp.IsApprove.ToString(),
|
|
request_date = irp.RequestDate,
|
|
profileId = irp.Profile.Id,
|
|
// prefix = irp.Profile.Prefix,
|
|
firstname = irp.Profile.FirstName,
|
|
lastname = irp.Profile.LastName,
|
|
// posno = irp.Profile.PositionNumber.Id,
|
|
type = irp.Profile.ProfileType,
|
|
// position = irp.Profile.Position.Name,
|
|
// rank = irp.Profile.AcademicStanding.Name,
|
|
instituteName = "",
|
|
instituteId = -1,
|
|
// divisionName = irp.Profile.OrganizationOrganization.Name,
|
|
// divisionId = irp.Profile.OrganizationOrganization.Id,
|
|
lastInsigniaName = "",
|
|
requestInsigniaLevel = irp.RequestInsignia.InsigniaType.Name,
|
|
requestInsigniaName = irp.RequestInsignia.Name,
|
|
requestQua = irp.QualificationStatus,
|
|
requestDoc = irp.DocumentStatus,
|
|
requestNote = irp.Note,
|
|
requestSalary = irp.Salary,
|
|
matchingConditions = JsonConvert.DeserializeObject<List<MatchingCondition>>(irp.MatchingConditions)
|
|
})
|
|
.Where(x => !x.isApprove)
|
|
.OrderBy(y => y.profileId)
|
|
.ToList()
|
|
})
|
|
.ToList()
|
|
.FirstOrDefault();
|
|
|
|
var period_data = (from p in _context.InsigniaPeriods.AsQueryable()
|
|
where p.Id == period
|
|
select new
|
|
{
|
|
periodName = p.Name,
|
|
periodYear = p.Year,
|
|
}).FirstOrDefault();
|
|
|
|
return Success(new { passData = passData, failData = failData, period = period_data });
|
|
}
|
|
// select data to display
|
|
}
|
|
|
|
return Success();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " บันทึกหมายเหตุ "
|
|
|
|
[HttpPut("note/{profileId}")]
|
|
public async Task<ActionResult<ResponseObject>> SaveNote(Guid profileId, SaveRequsetNote items)
|
|
{
|
|
var id = await _repository.GetRequestId(items.PeriodId, items.OcId);
|
|
var note = _context.InsigniaRequestProfiles.AsQueryable()
|
|
.Where(d => d.Profile.Id == profileId && d.Request.Id == id).FirstOrDefault();
|
|
if (note != null)
|
|
note.Note = items.Note;
|
|
_context.SaveChanges();
|
|
return Success();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " บันทึกรายชื่อครูในการขอยื่นเครื่องราชฯ เเต่ยังไม่ส่งไปยัง ผอ.โรงเรียน "
|
|
|
|
[HttpPut("approve/{ocId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> SaveRequestList(Guid ocId, InsigniaApproveRequest items)
|
|
{
|
|
var result = await _repository.GetInsigniaRequest(Royal_Type, ocId);
|
|
if (result != null)
|
|
await _repository.SaveAprove(result.PeriodId, ocId, items);
|
|
return Success();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " เปลี่ยน status เป็น st2 รอ ผอ.สำนักรับรอง "
|
|
|
|
[HttpPost("status/officer/send/{ocId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> ChangeStatusToSt2(Guid ocId, InsigniaApproveRequest items)
|
|
{
|
|
var result = await _repository.GetInsigniaRequest(Royal_Type, ocId);
|
|
|
|
if (items != null)
|
|
{
|
|
_repository.SaveAprove(result.PeriodId, ocId, items);
|
|
}
|
|
var requestId = await _repository.GetRequestId(result.PeriodId, ocId);
|
|
var requestNew = await _context.InsigniaRequests.FirstOrDefaultAsync(i => i.Id == requestId);
|
|
if (requestNew != null)
|
|
{
|
|
requestNew.RequestStatus = "st2";
|
|
_context.SaveChanges();
|
|
return Success();
|
|
}
|
|
else
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " เปลี่ยน status สำหรับ ผอ.สำนัก "
|
|
|
|
[HttpPost("status/director/approve/{ocId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> ChangeStatusToSt5p(Guid ocId)
|
|
{
|
|
var result = await _repository.GetInsigniaRequest(Royal_Type, ocId);
|
|
if (result == null)
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
var requestId = await _repository.GetRequestId(result.PeriodId, ocId);
|
|
if (requestId == null)
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
var requestNew = await _context.InsigniaRequests.FirstOrDefaultAsync(i => i.Id == requestId);
|
|
if (requestNew != null)
|
|
{
|
|
requestNew.RequestStatus = "st5p";
|
|
_context.SaveChanges();
|
|
return Success();
|
|
}
|
|
else
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
}
|
|
|
|
[HttpPost("status/director/reject/{ocId:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> ChangeStatusToSt1(Guid ocId)
|
|
{
|
|
var result = await _repository.GetInsigniaRequest(Royal_Type, ocId);
|
|
if (result == null)
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
var requestId = await _repository.GetRequestId(result.PeriodId, ocId);
|
|
if (requestId == null)
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
var requestNew = await _context.InsigniaRequests.FirstOrDefaultAsync(i => i.Id == requestId);
|
|
if (requestNew != null)
|
|
{
|
|
requestNew.RequestStatus = "st1";
|
|
_context.SaveChanges();
|
|
return Success();
|
|
}
|
|
else
|
|
return Error(GlobalMessages.InvalidInsigniaRequest);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|