diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index 4a8718c8..2be308d1 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1,4 +1,5 @@ using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Requests.Commands; using BMA.EHR.Domain.Models.Commands.Core; using BMA.EHR.Domain.Models.MetaData; using BMA.EHR.Domain.Models.Organizations; @@ -7,6 +8,7 @@ using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; +using System.Transactions; using Command = BMA.EHR.Domain.Models.Commands.Core.Command; namespace BMA.EHR.Application.Repositories.Commands @@ -76,6 +78,8 @@ namespace BMA.EHR.Application.Repositories.Commands #endregion + #region " Command Receiver " + public async Task> GetReceiverByCommmandIdAsync(Guid Id) { try @@ -167,6 +171,89 @@ namespace BMA.EHR.Application.Repositories.Commands } } + public async Task GetCommandReceiverAsync(Guid personalId) + { + try + { + var receiver = await _dbContext.Set() + .FirstOrDefaultAsync(x => x.Id == personalId); + + if (receiver == null) + throw new Exception(GlobalMessages.DataNotFound); + + return receiver; + } + catch + { + throw; + } + + + } + + public async Task SwapReceiverOrderAsync(Guid personalId, string direction) + { + try + { + var current = await _dbContext.Set() + .FirstOrDefaultAsync(x => x.Id == personalId); + + if (current == null) + throw new Exception(GlobalMessages.DataNotFound); + + var currentSeq = current.Sequence; + + switch (direction.Trim().ToLower()) + { + case "up": + { + // get prev record + var prev = await _dbContext.Set() + .OrderByDescending(x => x.Sequence) + .Where(x => x.Sequence < currentSeq) + .Take(1) + .FirstOrDefaultAsync(); + + if (prev != null) + { + var prevSeq = prev.Sequence; + + current.Sequence = prevSeq; + prev.Sequence = currentSeq; + await _dbContext.SaveChangesAsync(); + } + break; + } + case "down": + { + // get next record + var next = await _dbContext.Set() + .OrderBy(x => x.Sequence) + .Where(x => x.Sequence > currentSeq) + .Take(1) + .FirstOrDefaultAsync(); + + if (next != null) + { + var nextSeq = next.Sequence; + + current.Sequence = nextSeq; + next.Sequence = currentSeq; + await _dbContext.SaveChangesAsync(); + } + break; + } + default: throw new Exception("Invalid swap direction!"); + } + } + catch + { + throw; + } + } + + #endregion + public async Task> GetDeploymentByCommandIdAsync(Guid id) { try @@ -473,6 +560,28 @@ namespace BMA.EHR.Application.Repositories.Commands #region " Placement " + public async Task UpdatePlacementSalaryAsync(Guid placementProfileId, UpdatePlacementSalaryRequest req) + { + try + { + var placementProfile = await _dbContext.Set() + .FirstOrDefaultAsync(p => p.Id == placementProfileId); + + if (placementProfile == null) + throw new Exception($"Invalid placement profile: {placementProfileId}"); + + placementProfile.Amount = req.SalaryAmount; + placementProfile.PositionSalaryAmount = req.PositionSalaryAmount; + placementProfile.MouthSalaryAmount = req.MonthSalaryAmount; + + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + public async Task> GetPlacementPositionPath(Guid id) { try diff --git a/BMA.EHR.Application/Requests/Commands/UpdatePlacementSalaryRequest.cs b/BMA.EHR.Application/Requests/Commands/UpdatePlacementSalaryRequest.cs new file mode 100644 index 00000000..2aefb92f --- /dev/null +++ b/BMA.EHR.Application/Requests/Commands/UpdatePlacementSalaryRequest.cs @@ -0,0 +1,11 @@ +namespace BMA.EHR.Application.Requests.Commands +{ + public class UpdatePlacementSalaryRequest + { + public double SalaryAmount { get; set; } = 0; + + public double PositionSalaryAmount { get; set; } = 0; + + public double MonthSalaryAmount { get; set; } = 0; + } +} diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index 66aa17f9..d89d1b8c 100644 --- a/BMA.EHR.Command.Service/Controllers/OrderController.cs +++ b/BMA.EHR.Command.Service/Controllers/OrderController.cs @@ -1,6 +1,7 @@ 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.Domain.Common; using BMA.EHR.Domain.Extensions; @@ -889,6 +890,81 @@ namespace BMA.EHR.Command.Service.Controllers } } + /// + /// บันทึกข้อมูลเงินเดือนสำหรับผู้บรรจุ + /// + /// Record Id ของผู้รับคำสั่งในบัญชีแนบท้าย + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPut("salary/{id}")] + [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; + } + } + #endregion } }