diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index c8c9e2e5..b69f52db 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1,5 +1,4 @@ -using Amazon.S3.Model.Internal.MarshallTransformations; -using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Models.Commands.Core; using BMA.EHR.Domain.Models.Organizations; using BMA.EHR.Domain.Models.Placement; @@ -39,6 +38,9 @@ namespace BMA.EHR.Application.Repositories.Commands return await _dbContext.Set() .Include(x => x.Placement) .Include(x => x.CommandType) + .Include(x => x.Documents) + .Include(x => x.Receivers) + .Include(x => x.CommandStatus) .FirstOrDefaultAsync(x => x.Id == id); } @@ -47,10 +49,20 @@ namespace BMA.EHR.Application.Repositories.Commands var status = await _dbContext.Set().FirstOrDefaultAsync(c => c.Sequence == 1); command.CommandStatus = status!; _dbContext.Attatch(status!); - + return await base.AddAsync(command); } + public override async Task UpdateAsync(Command entity) + { + // attatch + _dbContext.Attatch(entity.CommandStatus); + _dbContext.Attatch(entity.CommandType); + _dbContext.Attatch(entity.Placement); + + return await base.UpdateAsync(entity); + } + #endregion public async Task> GetReceiverByCommmandIdAsync(Guid Id) @@ -343,12 +355,64 @@ namespace BMA.EHR.Application.Repositories.Commands #endregion + #region " Change Command Status " + + public async Task GotoNextStateAsync(Guid id) + { + try + { + var command = await _dbContext.Set().Include(c => c.CommandStatus).FirstOrDefaultAsync(x => x.Id == id); + if (command == null) + throw new Exception(GlobalMessages.CommandNotFound); + + var notProcess = new int[] { 4, 5 }; + + if (!notProcess.Contains(command.CommandStatus.Sequence)) + { + var nextStatus = await _dbContext.Set().FirstOrDefaultAsync(c => c.Sequence == command.CommandStatus.Sequence + 1); + command.CommandStatus = nextStatus!; + _dbContext.Attatch(nextStatus!); + await _dbContext.SaveChangesAsync(); + } + } + catch + { + throw; + } + } + + public async Task GotoPrevStateAsync(Guid id) + { + try + { + var command = await _dbContext.Set().Include(c => c.CommandStatus).FirstOrDefaultAsync(x => x.Id == id); + if (command == null) + throw new Exception(GlobalMessages.CommandNotFound); + + var notProcess = new int[] { 1, 5 }; + + if (!notProcess.Contains(command.CommandStatus.Sequence)) + { + var nextStatus = await _dbContext.Set().FirstOrDefaultAsync(c => c.Sequence == command.CommandStatus.Sequence - 1); + command.CommandStatus = nextStatus!; + _dbContext.Attatch(nextStatus!); + await _dbContext.SaveChangesAsync(); + } + } + catch + { + throw; + } + } + + #endregion + public async Task UpdateCommandInfo(Guid id, string orderNo, string orderYear, DateTime signDate) { try { var command = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == id); - if(command == null) + if (command == null) throw new Exception(GlobalMessages.CommandNotFound); command.CommandExcecuteDate = signDate; diff --git a/BMA.EHR.Command.Service/BMA.EHR.Command.Service.csproj b/BMA.EHR.Command.Service/BMA.EHR.Command.Service.csproj index 2a444d61..96d01d2d 100644 --- a/BMA.EHR.Command.Service/BMA.EHR.Command.Service.csproj +++ b/BMA.EHR.Command.Service/BMA.EHR.Command.Service.csproj @@ -39,6 +39,18 @@ + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index 6e2ea314..1d19f681 100644 --- a/BMA.EHR.Command.Service/Controllers/OrderController.cs +++ b/BMA.EHR.Command.Service/Controllers/OrderController.cs @@ -1,4 +1,5 @@ -using BMA.EHR.Application.Repositories; +using Amazon.S3.Model.Internal.MarshallTransformations; +using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Command.Service.Requests; using BMA.EHR.Domain.Common; @@ -33,6 +34,8 @@ namespace BMA.EHR.Command.Service.Controllers private readonly PrefixRepository _prefixRepository; private readonly CommandTypeRepository _commandTypeRepository; + + #endregion #region " Constuctor and Destructor " @@ -93,7 +96,7 @@ namespace BMA.EHR.Command.Service.Controllers Name = x.FirstOrDefault().CommandYear.ToInteger().ToThaiYear(), }).ToList(); - if(_data == null || _data.Count == 0) + if (_data == null || _data.Count == 0) { _data!.Add(new { @@ -133,6 +136,102 @@ namespace BMA.EHR.Command.Service.Controllers } } + /// + /// ตรวจสอบความพร้อมในการออกคำสั่ง + /// + /// 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_ATTATCHMENT); + + 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 : หน้าจอรายการออกคำสั่ง /// @@ -584,17 +683,17 @@ namespace BMA.EHR.Command.Service.Controllers try { var command = await _repository.GetByIdAsync(orderId); - if(command == null) + if (command == null) throw new Exception(GlobalMessages.CommandNotFound); var documents = await _repository.GetCommandDocumentAsync(orderId); - var cover = documents.Where(x => x.Category.Trim().ToLower() == "cover").FirstOrDefault(); - var attach = documents.Where(x => x.Category.Trim().ToLower() == "attachment").FirstOrDefault(); + var cover = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_COVER).FirstOrDefault(); + var attach = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_ATTATCHMENT).FirstOrDefault(); var result = new { - orderNo=command.CommandNo, - orderYear=command.CommandYear, + orderNo = command.CommandNo, + orderYear = command.CommandYear, signDate = command.CommandExcecuteDate, orderFileUrl = cover == null ? null : _documentService.ImagesPath(cover.Document.ObjectRefId), attachmentFileUrl = attach == null ? null : _documentService.ImagesPath(attach.Document.ObjectRefId), diff --git a/BMA.EHR.Domain/Shared/GlobalConstants.cs b/BMA.EHR.Domain/Shared/GlobalConstants.cs new file mode 100644 index 00000000..d89f50ec --- /dev/null +++ b/BMA.EHR.Domain/Shared/GlobalConstants.cs @@ -0,0 +1,8 @@ +namespace BMA.EHR.Domain.Shared +{ + public class GlobalConstants + { + public static readonly string TYPE_ATTATCHMENT = "attachment"; + public static readonly string TYPE_COVER = "cover"; + } +}