using Amazon.S3.Model.Internal.MarshallTransformations; using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Requests.Commands; using BMA.EHR.Command.Service.Requests; using BMA.EHR.Command.Service.Responses; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Commands.Core; using BMA.EHR.Domain.Shared; using BMA.EHR.Infrastructure.Persistence; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; using System.Security.Claims; namespace BMA.EHR.Command.Service.Controllers { [Route("api/v{version:apiVersion}/order")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("API ระบบออกคำสั่ง")] public class OrderController : BaseController { #region " Fields " private readonly CommandRepository _repository; private readonly PlacementRepository _placementRepository; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly PrefixRepository _prefixRepository; private readonly CommandTypeRepository _commandTypeRepository; private readonly CommandStatusRepository _commandStatusRepository; private readonly UserProfileRepository _userProfileRepository; #endregion #region " Constuctor and Destructor " public OrderController(CommandRepository repository, PlacementRepository placementRepository, ApplicationDBContext context, MinIOService documentService, IHttpContextAccessor httpContextAccessor, PrefixRepository prefixRepository, CommandTypeRepository commandTypeRepository, CommandStatusRepository commandStatusRepository, UserProfileRepository userProfileRepository) { _repository = repository; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; _placementRepository = placementRepository; _prefixRepository = prefixRepository; _commandTypeRepository = commandTypeRepository; _commandStatusRepository = commandStatusRepository; _userProfileRepository = userProfileRepository; } #endregion #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); private Guid OcId { get { if (UserId != null || UserId != "") return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!)); else return Guid.Empty; } } #endregion #region " Methods " /// /// แสดงปีเป็นปีพุทธศักราช โดยดึงจากข้อมูลที่มีในระบบ /// /// /// ถ้าไม่มีข้อมูลเลยจะ default ปีปัจจุบันให้ 1 รายการ /// /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("fiscal-year")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetFiscalAsync() { var data = await _repository.GetAllAsync(); if (data != null) { var _data = data.GroupBy(x => x.CommandYear).Select(x => new { Id = x.FirstOrDefault().CommandYear.ToInteger().ToCeYear(), Name = x.FirstOrDefault().CommandYear.ToInteger().ToThaiYear(), }).ToList(); if (_data == null || _data.Count == 0) { _data!.Add(new { Id = DateTime.Now.Year, Name = DateTime.Now.Year.ToThaiYear() }); } return Success(_data); } return Success(data); } /// /// แสดงประเภทคำสั่ง โดยดึงจากข้อมูลที่มีในระบบ /// /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("order-type")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandTypeAsync() { try { var data = (await _commandTypeRepository.GetAllAsync()).OrderBy(x => x.CommandCode); return Success(data); } catch { throw; } } /// /// ตรวจสอบความพร้อมในการออกคำสั่ง /// /// Record Id ของคำสั่ง /// /// ค่า Y = พร้อมออกคำสั่ง, N = ยังไม่พร้อม /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("ready/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> CheckReadyToExcecuteAsync(Guid orderId) { try { var command = await _repository.GetByIdAsync(orderId); if (command == null) throw new Exception(GlobalMessages.CommandNotFound); var cover = command.Documents.FirstOrDefault(x => x.Category == GlobalConstants.TYPE_COVER); var attatchment = command.Documents.FirstOrDefault(x => x.Category == GlobalConstants.TYPE_ATTACHMENT); if (command.CommandNo != "" && command.CommandYear != null && command.CommandExcecuteDate != null && cover != null && attatchment != null) { return Success(new { result = "Y" }); } else return Success(new { result = "N" }); } catch { throw; } } /// /// เปลี่ยน status ของคำสั่งไปขั้นตอนถัดไป /// /// Record Id ของคำสั่ง /// /// ค่า Y = พร้อมออกคำสั่ง, N = ยังไม่พร้อม /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("next/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GoToNextState(Guid orderId) { try { await _repository.GotoNextStateAsync(orderId); return Success(); } catch { throw; } } /// /// เปลี่ยน status ของคำสั่งไปขั้นตอนก่อนหน้า /// /// Record Id ของคำสั่ง /// /// ค่า Y = พร้อมออกคำสั่ง, N = ยังไม่พร้อม /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("prev/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GoToPrevState(Guid orderId) { try { await _repository.GotoPrevStateAsync(orderId); return Success(); } catch { throw; } } /// /// PM7-19 : หน้าจอรายการออกคำสั่ง /// /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetAllAsync() { try { var data = (await _repository.GetAllAsync()) .Select(d => new { OrderId = d.Id, OrderName = d.CommandSubject, OrderNo = d.CommandNo, FiscalYear = d.CommandYear, OrderDate = d.CommandAffectDate, OrderByOrganization = d.IssuerOrganizationName, OrderBy = d.IssuerOrganizationId, signatoryBy = d.AuthorizedUserFullName, signatoryPosition = d.AuthorizedPosition, OrderStatusValue = d.CommandStatusId, OrderStatusName = d.CommandStatus.Name, OrderTypeValue = d.CommandTypeId, OrderTypeName = d.CommandType.Name }).ToList(); return Success(data); } catch { throw; } } /// /// PM7-20 : ลบรายการคำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> DeleteAsync(Guid orderId) { try { var data = await _repository.GetByIdAsync(orderId); if (data == null) throw new Exception(GlobalMessages.DataNotFound); await _repository.DeleteAsync(data!); return Success(data); } catch { throw; } } /// /// PM7-21 : รายละเอียดการออกคำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("detail/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetByIdAsync(Guid orderId) { try { var data = await _repository.GetByIdAsync(orderId); if (data == null) throw new Exception(GlobalMessages.DataNotFound); var result = new { orderId = orderId, orderTypeValue = data.CommandTypeId, orderTitle = data.CommandSubject, orderNo = data.CommandNo, orderYear = data.CommandYear, orderDate = data.CommandAffectDate, orderBy = data.IssuerOrganizationId, signatoryBy = data.AuthorizedUserFullName, signatoryPosition = data.AuthorizedPosition, examRound = data.Placement.Id, registerPosition = "", conclusionRegisterNo = data.ConclusionRegisterNo, conclusionRegisterDate = data.ConclusionRegisterDate, conclusionResultNo = data.ConclusionResultNo, conclusionResultDate = data.ConclusionResultDate, }; return Success(result); } catch { throw; } } /// /// PM7-22 : สร้างข้อมูลรายละเอียดการออกคำสั่ง /// /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("detail")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PostAsync([FromBody] CreateCommandRequest req) { try { var inserted = new Domain.Models.Commands.Core.Command { CommandNo = req.orderNo.ToString(), CommandYear = req.orderYear.ToString(), CommandSubject = req.orderTitle, PositionName = req.registerPosition, CommandTypeId = req.orderTypeValue, IssuerOrganizationId = req.orderBy, AuthorizedUserFullName = req.signatoryBy, AuthorizedPosition = req.signatoryPosition, PlacementId = req.examRound, ConclusionRegisterNo = req.conclusionRegisterNo, ConclusionRegisterDate = req.conclusionRegisterDate, ConclusionResultNo = req.conclusionResultNo, ConclusionResultDate = req.conclusionResultDate, CommandAffectDate = req.orderDate, OwnerGovId = OcId }; var result = await _repository.AddAsync(inserted); return Success(result); } catch { throw; } } /// /// PM7-23 : แก้ไขข้อมูลรายละเอียดการออกคำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("detail/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PutAsync(Guid orderId, [FromBody] CreateCommandRequest req) { try { var order = await _repository.GetByIdAsync(orderId); if (order == null) throw new Exception(GlobalMessages.CommandNotFound); var placement = await _placementRepository.GetByIdAsync(req.examRound); var commandType = await _commandTypeRepository.GetByIdAsync(req.orderTypeValue); var status = await _commandStatusRepository.GetByIdAsync(order.CommandStatusId); order.CommandNo = req.orderNo.ToString(); order.CommandYear = req.orderYear.ToString(); order.CommandSubject = req.orderTitle; order.PositionName = req.registerPosition; order.CommandType = commandType!; order.IssuerOrganizationId = req.orderBy; order.AuthorizedUserFullName = req.signatoryBy; order.AuthorizedPosition = req.signatoryPosition; order.Placement = placement!; order.ConclusionRegisterNo = req.conclusionRegisterNo; order.ConclusionRegisterDate = req.conclusionRegisterDate; order.ConclusionResultNo = req.conclusionResultNo; order.ConclusionResultDate = req.conclusionResultDate; order.CommandStatus = status!; order.CommandAffectDate = req.orderDate; var result = await _repository.UpdateAsync(order); return Success(result); } catch { throw; } } /// /// PM7-24 : dropdown รอบการสอบ หน้ารายละเอียดการออกคำสั่ง /// /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("detail/exam-round")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetExamRoundAsync() { try { var data = (await _placementRepository.GetAllAsync()) .Select(x => new { examRoundValue = x.Id, examRoundName = $"{x.Name} ครั้งที่ {x.Round}/{x.Year.ToThaiYear()}" }) .ToList(); return Success(data); } catch { throw; } } /// /// PM7-26 : ข้อมูลเลือกรายชื่อออกคำสั่ง ** ยังไม่ได้กรองหน่วยงาน ** ** แสดงรายการจากระบบบรรจุ ** /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("persons/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandReceiverAsync(Guid orderId) { try { // TODO : หาค่า Education มาแสดง var existed = await _repository.GetReceiverByCommmandIdAsync(orderId); var receivers = (await _repository.GetReceiverForCommandAsync(orderId)) .OrderBy(x => x.Sequence) .Select(r => new CommandReceiverResponse { RefRecordId = r.RefPlacementProfileId!.Value, PersonalId = r.Id, Sequence = r.Sequence, IdCard = r.CitizenId, Name = $"{r.Prefix!}{r.FirstName!} {r.LastName!}", SelectStatus = existed.FirstOrDefault(x => x.RefPlacementProfileId!.Value == r.RefPlacementProfileId!.Value) != null, Education = "" // ยังหาไม่เจอว่าอยุ่ field ไหน }).ToList(); foreach (var r in receivers) { var salary = await _repository.GetPlacementSalaryAsync(r.RefRecordId); r.SalaryAmount = salary.SalaryAmount; r.PositionSalaryAmount = salary.PositionSalaryAmount; r.MonthSalaryAmount = salary.MonthSalaryAmount; } return Success(receivers); } catch { throw; } } /// /// ข้อมูลเลือกรายชื่อออกคำสั่ง ** ที่ได้เลือกเอาไว้แล้ว ** /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("persons-selected/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandSelectReceiverAsync(Guid orderId) { try { var receivers = (await _repository.GetReceiverByCommmandIdAsync(orderId)) .OrderBy(x => x.Sequence) .Select(r => new CommandReceiverResponse { RefRecordId = r.RefPlacementProfileId!.Value, PersonalId = r.Id, Sequence = r.Sequence, IdCard = r.CitizenId, Name = $"{r.Prefix!}{r.FirstName!} {r.LastName!}", SelectStatus = true, Education = "" // ยังหาไม่เจอว่าอยุ่ field ไหน }).ToList(); foreach (var r in receivers) { var salary = await _repository.GetPlacementSalaryAsync(r.RefRecordId); r.SalaryAmount = salary.SalaryAmount; r.PositionSalaryAmount = salary.PositionSalaryAmount; r.MonthSalaryAmount = salary.MonthSalaryAmount; } return Success(receivers); } catch { throw; } } /// /// บันทึกข้อมูลเลือกรายชื่อออกคำสั่ง ** ที่ได้เลือกเอาไว้แล้ว ** /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("persons/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> SaveCommandSelectReceiverAsync(Guid orderId, [FromBody] List selected) { try { await _repository.SaveSelectedReceiverAsync(orderId, selected); return Success(); } catch { throw; } } /// /// PM7-27 : ลบข้อมูลเลือกรายชื่อออกคำสั่ง /// /// /// ** ยังไม่ได้กรองหน่วยงาน ** /// /// Record Id ของผู้รับคำสั่งในรายการบัญชีแนบท้าย /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("persons/{personalId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> DeleteCommandReceiverAsync(Guid personalId) { try { var receiver = await _repository.DeleteCommandReceiverAsync(personalId); return Success(receiver); } catch { throw; } } /// /// PM7-28 : ข้อมูลเลือกรายชื่อส่งสำเนาคำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("copy-order/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandDeploymentAsync(Guid orderId) { try { var deployments = (await _repository.GetDeploymentByCommandIdAsync(orderId)) .Select(x => new { personalId = x.Id, sequence = x.Sequence, selectStatus = true, idCard = x.CitizenId, name = $"{x.Prefix}{x.FirstName} {x.LastName}", position = x.PositionName, unit = x.OrganizationName, emailChannel = x.IsSendMail, inboxChannel = x.IsSendInbox }) .ToList(); return Success(deployments); } catch { throw; } } /// /// PM7-30 : popup - เลือกรายชื่อส่งสำเนาคำสั่ง /// /// Record Id ของหน่วยงาน /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("copy-order/persons/{organizationId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetPeopleByOrganizationIdAsync(Guid organizationId) { try { var people = (await _repository.GetProfileByOrganizationIdAsync(organizationId)) .Select(x => new { profileId = x.Profile!.Id, idCard = x.Profile!.CitizenId, name = $"{x.Profile!.Prefix!.Name}{x.Profile!.FirstName} {x.Profile!.LastName}", position = x.OrganizationPosition!.PositionMaster!.PositionPath!.Name, unit = x.OrganizationPosition!.Organization!.OrganizationOrganization!.Name, prefixId = x.Profile!.Prefix!.Id, firstName = x.Profile!.FirstName, lastName = x.Profile!.LastName }) .ToList(); return Success(people); } catch { throw; } } /// /// PM7-31 : เพิ่มรายชื่อคนที่ต้องการส่งสำเนาคำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("copy-order/persons/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> CreateCommandDeploymentAsync(Guid orderId, [FromBody] List req) { try { // transform req var deploys = new List(); foreach (var p in req) { var prefix = await _prefixRepository.GetByIdAsync(p.PrefixId); deploys.Add(new CommandDeployment { CitizenId = p.IdCard, Prefix = prefix.Name, FirstName = p.FirstName, LastName = p.LastName, OrganizationName = p.Unit, PositionName = p.Position }); } await _repository.CreateCommandDeploymentAsync(orderId, deploys); return Success(); } catch { throw; } } /// /// PM7-32 : บันทึกช่องทางการส่งสำเนาคำสั่ง /// /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("copy-order")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> UpdateCommandDeploymentAsync([FromBody] List req) { try { // transform var deploys = new List(); foreach (var p in req) { var updated = await _repository.GetCommandDeploymentById(p.PersonalId); updated!.IsSendInbox = p.InboxChannel; updated!.IsSendMail = p.EmailChannel; deploys.Add(updated); } await _repository.UpdatCommandDeploymentAsync(deploys); return Success(); } catch { throw; } } /// /// PM7-33 : ลบรายชื่อคนที่ต้องการส่งสำเนาคำสั่ง /// /// Record Id ของผู้รับสำเนาคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("copy-order/{personalId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> DeleteCommandDeloymentAsync(Guid personalId) { try { await _repository.DeleteCommandDeploymentAsync(personalId); return Success(); } catch { throw; } } #region " Documents " /// /// PM7-34 : ข้อมูลรายละเอียดคำสั่งและแนบท้าย /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("attachment/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandAttatchmentAsync(Guid orderId) { try { var command = await _repository.GetByIdAsync(orderId); if (command == null) throw new Exception(GlobalMessages.CommandNotFound); var documents = await _repository.GetCommandDocumentAsync(orderId); var cover = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_COVER).FirstOrDefault(); var attach = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_ATTACHMENT).FirstOrDefault(); var result = new { orderNo = command.CommandNo, orderYear = command.CommandYear, signDate = command.CommandExcecuteDate, orderFileUrl = cover == null ? null : await _documentService.ImagesPath(cover.Document.Id), attachmentFileUrl = attach == null ? null : await _documentService.ImagesPath(attach.Document.Id), }; return Success(result); } catch { throw; } } /// /// PM7-35 : อัปโหลดไฟล์คำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("attachment/order-file/{orderId}"), DisableRequestSizeLimit] [Consumes("multipart/form-data")] [SwaggerOperation( Summary = "Upload a file", Description = "Upload a file using multipart/form-data", OperationId = "UploadCommandCoverAsync" //Tags = new[] { "File" } )] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> UploadCommandCoverAsync(Guid orderId) { try { // check upload file if (Request.Form.Files == null || Request.Form.Files.Count == 0) { return Error(GlobalMessages.NoFileToUpload); } var file = Request.Form.Files[0]; // get exit file var docs = await _repository.GetExistDocument(orderId, GlobalConstants.TYPE_COVER); // delete exist document from s3 foreach (var doc in docs) { await _documentService.DeleteFileAsync(doc.Document.Id); } // upload new document to s3 var cover = await _documentService.UploadFileAsync(file); // create new CommandDocumentEntity var commandDoc = new CommandDocument { Category = GlobalConstants.TYPE_COVER, Document = cover, }; // send to repo to save in database await _repository.UploadDocument(orderId, GlobalConstants.TYPE_COVER, commandDoc); return Success(); } catch { throw; } } /// /// PM7-36 : อัปโหลดไฟล์เอกสารแนบท้าย /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("attachment/file/{orderId}"), DisableRequestSizeLimit] [Consumes("multipart/form-data")] [SwaggerOperation( Summary = "Upload a file", Description = "Upload a file using multipart/form-data", OperationId = "UploadCommandAttachmentAsync" //Tags = new[] { "File" } )] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> UploadCommandAttachmentAsync(Guid orderId) { try { // check upload file if (Request.Form.Files == null || Request.Form.Files.Count == 0) { return Error(GlobalMessages.NoFileToUpload); } var file = Request.Form.Files[0]; // get exit file var docs = await _repository.GetExistDocument(orderId, GlobalConstants.TYPE_ATTACHMENT); // delete exist document from s3 foreach (var doc in docs) { await _documentService.DeleteFileAsync(doc.Document.Id); } // upload new document to s3 var cover = await _documentService.UploadFileAsync(file); // create new CommandDocumentEntity var commandDoc = new CommandDocument { Category = GlobalConstants.TYPE_ATTACHMENT, Document = cover, }; // send to repo to save in database await _repository.UploadDocument(orderId, GlobalConstants.TYPE_ATTACHMENT, commandDoc); return Success(); } catch { throw; } } #endregion /// /// PM7-37 : บันทึกข้อมูลคำสั่งและแนบท้าย /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("attachment/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> UpdateCommandExecuteAsync(Guid orderId, [FromBody] UpdateCommandExecuteRequest req) { try { await _repository.UpdateCommandInfoAsync(orderId, req.OrderNo, req.OrderYear, req.SignDate); return Success(); } catch { throw; } } /// /// อ่านข้อมูลเงินเดือนสำหรับผู้บรรจุ จากข้อมูลระบบสรรหา /// /// Record Id ของผู้รับคำสั่งในบัญชีแนบท้าย /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("salary/{personalId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetPlacementSalaryAsync(Guid personalId) { try { var record = await _repository.GetCommandReceiverAsync(personalId); var data = await _repository.GetPlacementSalaryAsync(record!.RefPlacementProfileId!.Value); return Success(data); } catch { throw; } } /// /// บันทึกข้อมูลเงินเดือนสำหรับผู้บรรจุ /// /// Record Id ของผู้รับคำสั่งในบัญชีแนบท้าย /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("salary/{personalId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> UpdatePlacementSalaryAsync(Guid personalId, [FromBody] UpdatePlacementSalaryRequest req) { try { var receiver = await _repository.GetCommandReceiverAsync(personalId); if (receiver == null) throw new Exception(GlobalMessages.DataNotFound); await _repository.UpdatePlacementSalaryAsync(personalId, req); return Success(); } catch { throw; } } /// /// สลับลำดับข้อมูลในบัญชีแนบท้ายขึ้น /// /// Record Id ของผู้รับคำสั่งในบัญชีแนบท้าย /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("swap/up/{personalId}")] public async Task> SwapUpReceiverOrderAsync(Guid personalId) { try { await _repository.SwapReceiverOrderAsync(personalId, "up"); return Success(); } catch { throw; } } /// /// สลับลำดับข้อมูลในบัญชีแนบท้ายลง /// /// Record Id ของผู้รับคำสั่งในบัญชีแนบท้าย /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("swap/down/{personalId}")] public async Task> SwapDownReceiverOrderAsync(Guid personalId) { try { await _repository.SwapReceiverOrderAsync(personalId, "down"); return Success(); } catch { throw; } } /// /// ออกคำสั่ง /// /// Record Id ของคำสั่ง /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("execute/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> ExecuteCommandAsync(Guid orderId) { try { await _repository.ExecuteCommandAsync(orderId); return Success(); } catch { throw; } } #endregion } }