From 6253d8d2ab36c85a67d21a8305e8fc1404d34cee Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Mon, 28 Aug 2023 16:18:35 +0700 Subject: [PATCH] Get Receiver for Command C-PM-01, C-PM-02 --- .../Commands/CommandRepository.cs | 116 +++++++++++++++--- .../Repositories/GenericRepository.cs | 27 +++- 2 files changed, 119 insertions(+), 24 deletions(-) diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index 26f85397..fdc8be31 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -71,10 +71,11 @@ namespace BMA.EHR.Application.Repositories.Commands switch (command.CommandType.CommandCode.Trim().ToUpper()) { case "C-PM-01": - case "C-PM-02": - case "C-PM-03": result = await GetReceiver01Async(command); break; + case "C-PM-02": + result = await GetReceiver02Async(command); + break; default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement); } @@ -82,9 +83,9 @@ namespace BMA.EHR.Application.Repositories.Commands } /// - /// C-PM-01 + /// C-PM-01 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้สอบแข่งขันได้ /// - /// + /// object ของรายการคำสั่ง /// private async Task> GetReceiver01Async(Command command) { @@ -97,13 +98,15 @@ namespace BMA.EHR.Application.Repositories.Commands var otherCommandReceivers = await _dbContext.Set() .Include(x => x.Command) .ThenInclude(x => x.CommandType) - .Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper().StartsWith("C-PM")) + .Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper().StartsWith("C-PM-01")) .Where(x => x.Command.Id != command.Id) .Select(x => x.CitizenId) .ToListAsync(); // 2. Query var appointPeople = await _dbContext.Set() + .Include(x => x.Placement) + .ThenInclude(x => x.PlacementType) .Include(x => x.Prefix) .Include(x => x.OrganizationPosition) .ThenInclude(x => x!.Organization) @@ -111,6 +114,7 @@ namespace BMA.EHR.Application.Repositories.Commands .Where(x => !otherCommandReceivers.Contains(x.CitizenId!)) .Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN") .Where(x => x.Draft! == true) + .Where(x => x.Placement!.PlacementType!.Name == "สอบแข่งขัน") .OrderBy(x => x.ExamNumber) .ToListAsync(); @@ -118,19 +122,20 @@ namespace BMA.EHR.Application.Repositories.Commands var seq = 1; foreach (var item in appointPeople) { - - var receiver = new CommandReceiver + if (!(await CheckIsActiveOfficer(item.CitizenId!))) { - Sequence = seq, - CitizenId = item.CitizenId!, - Prefix = item.Prefix!.Name, - FirstName = item.Firstname!, - LastName = item.Lastname!, - RefPlacementProfileId = item.Id - }; - seq++; - - result.Add(receiver); + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = item.CitizenId!, + Prefix = item.Prefix!.Name, + FirstName = item.Firstname!, + LastName = item.Lastname!, + RefPlacementProfileId = item.Id + }; + seq++; + result.Add(receiver); + } } return result; @@ -141,6 +146,72 @@ namespace BMA.EHR.Application.Repositories.Commands } } + /// + /// C-PM-02 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้ได้รับคัดเลือก + /// + /// object ของรายการคำสั่ง + /// + private async Task> GetReceiver02Async(Command command) + { + try + { + var result = new List(); + // TODO : ต้องมา list คนตามประเภทอีกครั้งนึง + + // 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน + var otherCommandReceivers = await _dbContext.Set() + .Include(x => x.Command) + .ThenInclude(x => x.CommandType) + .Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-02") + .Where(x => x.Command.Id != command.Id) + .Select(x => x.CitizenId) + .ToListAsync(); + + // 2. Query + var appointPeople = await _dbContext.Set() + .Include(x => x.Placement) + .ThenInclude(x => x.PlacementType) + .Include(x => x.Prefix) + .Include(x => x.OrganizationPosition) + .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") + .Where(x => x.Draft! == true) + .Where(x => x.Placement!.PlacementType!.Name != "สอบแข่งขัน") + .OrderBy(x => x.ExamNumber) + .ToListAsync(); + + // 3. Create new Record + var seq = 1; + foreach (var item in appointPeople) + { + if (!(await CheckIsActiveOfficer(item.CitizenId!))) + { + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = item.CitizenId!, + Prefix = item.Prefix!.Name, + FirstName = item.Firstname!, + LastName = item.Lastname!, + RefPlacementProfileId = item.Id + }; + seq++; + + result.Add(receiver); + } + } + + return result; + } + catch + { + throw; + } + } + + #endregion #region " Execute and Deploy " @@ -151,15 +222,20 @@ namespace BMA.EHR.Application.Repositories.Commands { case "C-PM-01": case "C-PM-02": - case "C-PM-03": - await ExecuteCommand01Async(command); + await ExecuteCommand01_02Async(command); break; default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement); } } - private async Task ExecuteCommand01Async(Command command) + /// + /// C-PM-01 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้สอบแข่งขันได้, + /// C-PM-02 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้ได้รับคัดเลือก + /// + /// object ของรายการคำสั่ง + /// + private async Task ExecuteCommand01_02Async(Command command) { try { diff --git a/BMA.EHR.Application/Repositories/GenericRepository.cs b/BMA.EHR.Application/Repositories/GenericRepository.cs index 29354dc1..6ba9c059 100644 --- a/BMA.EHR.Application/Repositories/GenericRepository.cs +++ b/BMA.EHR.Application/Repositories/GenericRepository.cs @@ -1,5 +1,7 @@ -using BMA.EHR.Application.Common.Interfaces; +using Amazon.S3.Model.Internal.MarshallTransformations; +using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Models.Base; +using BMA.EHR.Domain.Models.HR; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using System.Security.Claims; @@ -13,7 +15,7 @@ namespace BMA.EHR.Application.Repositories private readonly IApplicationDBContext _dbContext; private readonly DbSet _dbSet; private readonly IHttpContextAccessor _httpContextAccessor; - + #endregion #region " Constructor and Destructor " @@ -24,7 +26,7 @@ namespace BMA.EHR.Application.Repositories _dbContext = dbContext; _dbSet = _dbContext.Set(); _httpContextAccessor = httpContextAccessor; - + } #endregion @@ -35,12 +37,29 @@ namespace BMA.EHR.Application.Repositories protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; - protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); + protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); #endregion #region " Methods " + public async Task CheckIsActiveOfficer(string citizenId) + { + try + { + var pf = await _dbContext.Set() + .Where(x => x.CitizenId == citizenId) + .Where(x => x.ProfileType.ToLower().Trim() == "officer" && x.IsActive && !x.IsLeave) + .FirstOrDefaultAsync(); + + return pf != null; + } + catch + { + throw; + } + } + public virtual async Task> GetAllAsync() { return await _dbSet.ToListAsync();