From 1a7be19e33e82c1a7dbc11887bd01acff7a1d79c Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Tue, 8 Aug 2023 10:19:35 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=20api=20?= =?UTF-8?q?=E0=B8=95=E0=B8=B2=E0=B8=A1=E0=B8=97=E0=B8=B5=E0=B9=88=E0=B8=84?= =?UTF-8?q?=E0=B8=B8=E0=B8=A2=E0=B9=83=E0=B8=AB=E0=B8=A1=E0=B9=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CommandRepository.cs | 106 +++++++++++++++--- .../Controllers/OrderController.cs | 88 ++++++++++++++- 2 files changed, 174 insertions(+), 20 deletions(-) diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index be7817a9..9af177c5 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -37,15 +37,21 @@ namespace BMA.EHR.Application.Repositories.Commands #region " Private " - private async Task GetReceiverForByCommndTypeAsync(Command command) + private async Task> GetReceiverForByCommndTypeAsync(Command command) { - switch (command.CommandType.Category.Trim().ToUpper()) + var result = new List(); + + switch (command.CommandType.CommandCode.Trim().ToUpper()) { case "C-PM-01": - await GetReceiver01(command); + case "C-PM-02": + case "C-PM-03": + result = await GetReceiver01(command); break; default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement); } + + return result; } /// @@ -53,10 +59,13 @@ namespace BMA.EHR.Application.Repositories.Commands /// /// /// - private async Task GetReceiver01(Command command) + private async Task> GetReceiver01(Command command) { try { + var result = new List(); + + // 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน var otherCommandReceivers = await _dbContext.Set() .Include(x => x.Command) @@ -70,7 +79,7 @@ namespace BMA.EHR.Application.Repositories.Commands var appointPeople = await _dbContext.Set() .Include(x => x.Prefix) .Include(x => x.OrganizationPosition) - .ThenInclude(x => x.Organization) + .ThenInclude(x => x!.Organization) //.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId) .Where(x => !otherCommandReceivers.Contains(x.CitizenId!)) .Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN") @@ -94,9 +103,10 @@ namespace BMA.EHR.Application.Repositories.Commands }; seq++; - command.Receivers.Add(receiver); + result.Add(receiver); } - await _dbContext.SaveChangesAsync(); + + return result; } catch { @@ -151,10 +161,80 @@ namespace BMA.EHR.Application.Repositories.Commands #region " Command Receiver " + public async Task SaveSelectedReceiverAsync(Guid id, List selected) + { + try + { + var command = await _dbContext.Set() + .Include(x => x.Receivers) + .Include(x => x.CommandType) + .FirstOrDefaultAsync(x => x.Id == id); + + if (command == null) + throw new Exception(GlobalMessages.CommandNotFound); + + var appointPeople = await _dbContext.Set() + .Include(x => x.Prefix) + .Include(x => x.OrganizationPosition) + .ThenInclude(x => x!.Organization) + //.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId) + .Where(x => selected.Contains(x.Id)) + .OrderBy(x => x.ExamNumber) + .ToListAsync(); + + _dbContext.Set().RemoveRange(command.Receivers); + await _dbContext.SaveChangesAsync(); + + var seq = 1; + foreach (var item in appointPeople) + { + + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = item.CitizenId!, + Prefix = item.Prefix!.Name, + FirstName = item.Firstname!, + LastName = item.Lastname!, + RefPlacementProfileId = item.Id + }; + seq++; + + command.Receivers.Add(receiver); + } + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + + public async Task> GetReceiverForCommandAsync(Guid id) + { + try + { + var command = await _dbContext.Set() + .Include(x => x.Receivers) + .Include(x => x.CommandType) + .FirstOrDefaultAsync(x => x.Id == id); + + if (command == null) + throw new Exception(GlobalMessages.CommandNotFound); + else + return await GetReceiverForByCommndTypeAsync(command); + } + catch + { + throw; + } + } + public async Task> GetReceiverByCommmandIdAsync(Guid Id) { try { + // ปรับใหม่ให้อ่านจาก database ล้วนๆ var command = await _dbContext.Set() .Include(x => x.Receivers) .Include(x => x.CommandType) @@ -170,10 +250,8 @@ namespace BMA.EHR.Application.Repositories.Commands } else { - await GetReceiverForByCommndTypeAsync(command); - - // query for new list - return command.Receivers!; + // returrn empty list + return new List(); } } } @@ -711,14 +789,14 @@ namespace BMA.EHR.Application.Repositories.Commands try { var data = await _dbContext.Set().AsQueryable() - .Include(x => x.OrganizationAgency) - .Include(x => x.OrganizationGovernmentAgency) + //.Include(x => x.OrganizationAgency) + //.Include(x => x.OrganizationGovernmentAgency) .FirstOrDefaultAsync(o => o.Id == ocId); if (data == null) throw new Exception(GlobalMessages.OrganizationNotFound); - return data.OrganizationAgency!.Id; + return data.OrganizationAgencyId!.Value; } catch { diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index 6008cb04..4ef88b96 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.Application.Requests.Commands; using BMA.EHR.Command.Service.Requests; @@ -493,7 +494,7 @@ namespace BMA.EHR.Command.Service.Controllers } /// - /// PM7-26 : ข้อมูลเลือกรายชื่อออกคำสั่ง + /// PM7-26 : ข้อมูลเลือกรายชื่อออกคำสั่ง ** ยังไม่ได้กรองหน่วยงาน ** ** แสดงรายการจากระบบบรรจุ ** /// /// Record Id ของคำสั่ง /// @@ -509,8 +510,57 @@ namespace BMA.EHR.Command.Service.Controllers try { // TODO : หาค่า Education มาแสดง - var receivers = (await _repository.GetReceiverByCommmandIdAsync(orderId)) + 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, @@ -540,7 +590,33 @@ namespace BMA.EHR.Command.Service.Controllers } /// - /// PM7-27 : ข้อมูลเลือกรายชื่อออกคำสั่ง ** ยังไม่ได้กรองหน่วยงาน ** + /// บันทึกข้อมูลเลือกรายชื่อออกคำสั่ง ** ที่ได้เลือกเอาไว้แล้ว ** + /// + /// 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 : ลบข้อมูลเลือกรายชื่อออกคำสั่ง /// /// /// ** ยังไม่ได้กรองหน่วยงาน ** @@ -935,8 +1011,8 @@ namespace BMA.EHR.Command.Service.Controllers { try { - var record = await _repository.GetCommandReceiverAsync(personalId); - + var record = await _repository.GetCommandReceiverAsync(personalId); + var data = await _repository.GetPlacementSalaryAsync(record!.RefPlacementProfileId!.Value); return Success(data);