422 lines
22 KiB
C#
422 lines
22 KiB
C#
using BMA.EHR.Application.Repositories;
|
|
using BMA.EHR.Application.Repositories.MessageQueue;
|
|
using BMA.EHR.Discipline.Service.Requests;
|
|
using BMA.EHR.Domain.Common;
|
|
using BMA.EHR.Domain.Models.Discipline;
|
|
using BMA.EHR.Domain.Shared;
|
|
using BMA.EHR.Infrastructure.Persistence;
|
|
// using BMA.EHR.Placement.Service.Requests;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using System.Security.Claims;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.IO;
|
|
|
|
namespace BMA.EHR.DisciplineDirector.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/discipline/director")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("กรรมการระบบวินัย")]
|
|
public class DisciplineDirectorController : BaseController
|
|
{
|
|
private readonly DisciplineDbContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly PermissionRepository _permission;
|
|
private readonly UserProfileRepository _userProfileRepository;
|
|
|
|
public DisciplineDirectorController(DisciplineDbContext context,
|
|
MinIOService documentService,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
UserProfileRepository userProfileRepository,
|
|
PermissionRepository permission)
|
|
{
|
|
// _repository = repository;
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_permission = permission;
|
|
_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? token => _httpContextAccessor.HttpContext.Request.Headers["Authorization"];
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// list รายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{path}")]
|
|
public async Task<ActionResult<ResponseObject>> GetDiscipline(string path, int page = 1, int pageSize = 25, string keyword = "", string? sortBy = "", bool? descending = false)
|
|
{
|
|
// สิทธิ์การเข้าถึง
|
|
path = path.Trim().ToUpper();
|
|
string getPermission;
|
|
if (path == "MAIN")
|
|
{
|
|
getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_DISCIPLINE_INFO");
|
|
}
|
|
else if (path == "INVES")
|
|
{
|
|
getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_DISCIPLINE_INVESTIGATE");
|
|
}
|
|
else if (path == "DISCIP")
|
|
{
|
|
getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_DISCIPLINE_INTERROGATE");
|
|
}
|
|
else
|
|
{
|
|
getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_DISCIPLINE_INFO");
|
|
}
|
|
|
|
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
|
|
if (jsonData["status"]?.ToString() != "200")
|
|
{
|
|
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
|
}
|
|
var _permiss = jsonData["result"]?.ToString();
|
|
// สิทธิ์มองเห็นรายชื่อ
|
|
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
|
|
var profile = await _userProfileRepository.GetUserOC(userId, token.Replace("Bearer ", ""));
|
|
if (profile == null)
|
|
return Error(GlobalMessages.DataNotFound);
|
|
|
|
var data_search = (from x in _context.DisciplineDirectors.Include(x => x.DisciplineInvestigate_Directors).Include(x => x.DisciplineDisciplinary_DirectorInvestigates)
|
|
where (((x.Prefix ?? "") + (x.FirstName ?? "") + " " + (x.LastName ?? "")).Contains(keyword) ||
|
|
x.Position.Contains(keyword) ||
|
|
x.Email.Contains(keyword) ||
|
|
x.Phone.Contains(keyword) ||
|
|
x.Qualification.Contains(keyword)) &&
|
|
(_permiss != "OWNER" && x.RootDnaId == profile.RootDnaId || _permiss == "OWNER" && true)
|
|
select x).ToList();
|
|
var query = data_search
|
|
.Select(x => new
|
|
{
|
|
Id = x.Id,
|
|
Prefix = x.Prefix,
|
|
FirstName = x.FirstName,
|
|
LastName = x.LastName,
|
|
Position = x.Position,
|
|
Email = x.Email,
|
|
Phone = x.Phone,
|
|
Qualification = x.Qualification,
|
|
TotalInvestigate = x.DisciplineInvestigate_Directors.Count(),
|
|
TotalDisciplinary = x.DisciplineDisciplinary_DirectorInvestigates.Count(),
|
|
});
|
|
|
|
bool desc = descending ?? false;
|
|
if (!string.IsNullOrEmpty(sortBy))
|
|
{
|
|
if (sortBy == "position")
|
|
{
|
|
query = desc ? query.OrderByDescending(x => x.Position)
|
|
: query.OrderBy(x => x.Position);
|
|
}
|
|
else if (sortBy == "prefix" || sortBy == "firstName" || sortBy == "lastName")
|
|
{
|
|
query = desc ?
|
|
query
|
|
//.OrderByDescending(x => x.Prefix)
|
|
.OrderByDescending(x => x.FirstName)
|
|
.ThenByDescending(x => x.LastName) :
|
|
query
|
|
//.OrderBy(x => x.Prefix)
|
|
.OrderBy(x => x.FirstName)
|
|
.ThenBy(x => x.LastName);
|
|
}
|
|
else if (sortBy == "email")
|
|
{
|
|
{
|
|
query = desc ? query.OrderByDescending(x => x.Email)
|
|
: query.OrderBy(x => x.Email);
|
|
}
|
|
}
|
|
else if (sortBy == "phone")
|
|
{
|
|
{
|
|
query = desc ? query.OrderByDescending(x => x.Phone)
|
|
: query.OrderBy(x => x.Phone);
|
|
}
|
|
}
|
|
else if (sortBy == "qualification")
|
|
{
|
|
{
|
|
query = desc ? query.OrderByDescending(x => x.Qualification)
|
|
: query.OrderBy(x => x.Qualification);
|
|
}
|
|
}
|
|
else if (sortBy == "totalInvestigate")
|
|
{
|
|
{
|
|
query = desc ? query.OrderByDescending(x => x.TotalInvestigate)
|
|
: query.OrderBy(x => x.TotalInvestigate);
|
|
}
|
|
}
|
|
else if (sortBy == "totalDisciplinary")
|
|
{
|
|
{
|
|
query = desc ? query.OrderByDescending(x => x.TotalDisciplinary)
|
|
: query.OrderBy(x => x.TotalDisciplinary);
|
|
}
|
|
}
|
|
}
|
|
|
|
var data = query
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToList();
|
|
return Success(new { data, total = data_search.Count() });
|
|
}
|
|
|
|
/// <summary>
|
|
/// get รายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> GetByDiscipline(Guid id)
|
|
{
|
|
var getWorkflow = await _permission.GetPermissionAPIWorkflowAsync(id.ToString(), "SYS_DISCIPLINE_INFO");
|
|
if (getWorkflow == false)
|
|
{
|
|
var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_DISCIPLINE_INFO");
|
|
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
|
|
if (jsonData["status"]?.ToString() != "200")
|
|
{
|
|
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
|
}
|
|
}
|
|
var data = await _context.DisciplineDirectors
|
|
.Select(x => new
|
|
{
|
|
Id = x.Id,
|
|
Prefix = x.Prefix,
|
|
FirstName = x.FirstName,
|
|
LastName = x.LastName,
|
|
Position = x.Position,
|
|
Email = x.Email,
|
|
Phone = x.Phone,
|
|
Qualification = x.Qualification,
|
|
TotalInvestigate = x.DisciplineInvestigate_Directors.Count(),
|
|
TotalDisciplinary = x.DisciplineDisciplinary_DirectorInvestigates.Count(),
|
|
})
|
|
.Where(x => x.Id == id)
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound);
|
|
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>> CreateDiscipline([FromBody] DisciplineDirectorRequest req)
|
|
{
|
|
var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_DISCIPLINE_INFO");
|
|
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
|
|
if (jsonData["status"]?.ToString() != "200")
|
|
{
|
|
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
|
}
|
|
var dataDup = await _context.DisciplineDirectors.Where(x => x.FirstName == req.firstName && x.LastName == req.lastName).FirstOrDefaultAsync();
|
|
if (dataDup != null)
|
|
return Error("ชื่อกรรมการนี้มีอยู่ในระบบแล้ว", StatusCodes.Status404NotFound);
|
|
|
|
var disciplineDirector = new Domain.Models.Discipline.DisciplineDirector
|
|
{
|
|
Prefix = req.prefix,
|
|
FirstName = req.firstName,
|
|
LastName = req.lastName,
|
|
Position = req.position,
|
|
Email = req.email,
|
|
Phone = req.phone,
|
|
Qualification = req.qualification,
|
|
RootDnaId = req.rootDnaId,
|
|
CreatedFullName = FullName ?? "System Administrator",
|
|
CreatedUserId = UserId ?? "",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.DisciplineDirectors.AddAsync(disciplineDirector);
|
|
await _context.SaveChangesAsync();
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขรายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> UpdateDiscipline(Guid id, [FromBody] DisciplineDirectorRequest req)
|
|
{
|
|
var getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_DISCIPLINE_INFO");
|
|
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
|
|
if (jsonData["status"]?.ToString() != "200")
|
|
{
|
|
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
|
}
|
|
var dataDup = await _context.DisciplineDirectors.Where(x => x.FirstName == req.firstName && x.LastName == req.lastName && x.Id != id).FirstOrDefaultAsync();
|
|
if (dataDup != null)
|
|
return Error("ชื่อกรรมการนี้มีอยู่ในระบบแล้ว", StatusCodes.Status404NotFound);
|
|
|
|
var data = await _context.DisciplineDirectors.Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound);
|
|
|
|
data.Prefix = req.prefix;
|
|
data.FirstName = req.firstName;
|
|
data.LastName = req.lastName;
|
|
data.Position = req.position;
|
|
data.Email = req.email;
|
|
data.Phone = req.phone;
|
|
data.Qualification = req.qualification;
|
|
data.LastUpdateFullName = FullName ?? "System Administrator";
|
|
data.LastUpdateUserId = UserId ?? "";
|
|
data.LastUpdatedAt = DateTime.Now;
|
|
await _context.SaveChangesAsync();
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// ลบรายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpDelete("{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> DeleteDiscipline(Guid id)
|
|
{
|
|
var getPermission = await _permission.GetPermissionAPIAsync("DELETE", "SYS_DISCIPLINE_INFO");
|
|
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
|
|
if (jsonData["status"]?.ToString() != "200")
|
|
{
|
|
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
|
}
|
|
var data = await _context.DisciplineDirectors.Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound);
|
|
_context.DisciplineDirectors.Remove(data);
|
|
await _context.SaveChangesAsync();
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ประวัติการสืบสวน
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("investigate/{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> GetHistoryDisciplineInvestigate(Guid id)
|
|
{
|
|
var director = await _context.DisciplineDirectors.Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if (director == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound);
|
|
|
|
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
|
|
var profile = await _userProfileRepository.GetUserOC(userId, token.Replace("Bearer ", ""));
|
|
if (profile == null)
|
|
return Error(GlobalMessages.DataNotFound);
|
|
|
|
var data = await _context.DisciplineInvestigates
|
|
.Where(x => x.RootDnaId == profile.RootDnaId || x.RootDnaId == null)
|
|
.Where(x => x.DisciplineInvestigate_Directors
|
|
.Where(x => x.DisciplineDirector == director)
|
|
.FirstOrDefault() != null
|
|
)
|
|
.Select(x => new
|
|
{
|
|
Title = x.Title,
|
|
Director = x.DisciplineInvestigate_Directors.Select(y => new
|
|
{
|
|
CommandNo = y.CommandNo,
|
|
Duty = y.Duty,
|
|
Prefix = y.DisciplineDirector.Prefix,
|
|
FirstName = y.DisciplineDirector.FirstName,
|
|
LastName = y.DisciplineDirector.LastName,
|
|
}),
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// ประวัติการสอบสวน
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("disciplinary/{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> GetHistoryDisciplineDisciplinary(Guid id)
|
|
{
|
|
var director = await _context.DisciplineDirectors.Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if (director == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound);
|
|
|
|
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
|
|
var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId, token.Replace("Bearer ", ""));
|
|
if (profile == null)
|
|
return Error(GlobalMessages.DataNotFound);
|
|
|
|
var data = await _context.DisciplineDisciplinarys
|
|
.Where(x => x.RootDnaId == profile.RootDnaId || x.RootDnaId == null)
|
|
.Where(x => x.DisciplineDisciplinary_DirectorInvestigates
|
|
.Where(x => x.DisciplineDirector == director)
|
|
.FirstOrDefault() != null
|
|
)
|
|
.Select(x => new
|
|
{
|
|
Title = x.Title,
|
|
Director = x.DisciplineDisciplinary_DirectorInvestigates.Select(y => new
|
|
{
|
|
CommandNo = y.CommandNo,
|
|
Duty = y.Duty,
|
|
Prefix = y.DisciplineDirector.Prefix,
|
|
FirstName = y.DisciplineDirector.FirstName,
|
|
LastName = y.DisciplineDirector.LastName,
|
|
}),
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Success(data);
|
|
}
|
|
}
|
|
}
|