hrms-api-backend/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs
Suphonchai Phoonsawat e8607f76fa แก้บั้กบันทึกคำสั่งไม่ได้
เพิ่มการใส่ค่าผู้ทำรายการใน GenericRepoository
2023-08-04 09:47:58 +07:00

368 lines
14 KiB
C#

using Amazon.S3.Model.Internal.MarshallTransformations;
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;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Command = BMA.EHR.Domain.Models.Commands.Core.Command;
namespace BMA.EHR.Application.Repositories.Commands
{
public class CommandRepository : GenericRepository<Guid, Command>
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destuctor "
public CommandRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Methods "
#region " Override "
public override async Task<Command?> GetByIdAsync(Guid id)
{
return await _dbContext.Set<Command>()
.Include(x => x.Placement)
.Include(x => x.CommandType)
.FirstOrDefaultAsync(x => x.Id == id);
}
public override async Task<Command> AddAsync(Command command)
{
var status = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == 1);
command.CommandStatus = status!;
_dbContext.Attatch(status!);
return await base.AddAsync(command);
}
#endregion
public async Task<List<CommandReceiver>> GetReceiverByCommmandIdAsync(Guid Id)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Receivers)
.Include(x => x.CommandType)
.FirstOrDefaultAsync(x => x.Id == Id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
{
if (command.Receivers != null || command!.Receivers!.Count > 0)
{
return command.Receivers;
}
else
{
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-01")
.Where(x => x.Id != Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementProfile>()
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x.Organization)
//.Where(x => x.OrganizationPosition.Organization.Id )
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN")
.Where(x => x.Draft! == true)
.OrderBy(x => x.ExamNumber)
.ToListAsync();
// 3. Create new Record
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();
// query for new list
return command.Receivers;
}
}
}
catch
{
throw;
}
}
public async Task<CommandReceiver> DeleteCommandReceiverAsync(Guid personalId)
{
try
{
var deleted = await _dbContext.Set<CommandReceiver>()
.FirstOrDefaultAsync(x => x.Id == personalId);
if (deleted == null)
throw new Exception(GlobalMessages.DataNotFound);
_dbContext.Set<CommandReceiver>().Remove(deleted);
await _dbContext.SaveChangesAsync();
return deleted;
}
catch
{
throw;
}
}
public async Task<List<CommandDeployment>> GetDeploymentByCommandIdAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Deployments)
.FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
{
if (command.Deployments != null || command!.Deployments!.Count > 0)
{
return command.Deployments;
}
else
{
var orgPos = await _dbContext.Set<ProfilePosition>()
.Include(x => x.Profile)
.ThenInclude(x => x!.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.ThenInclude(x => x!.OrganizationOrganization)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionPath)
.Where(x => x.OrganizationPosition!.IsDirector! == true)
.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.FirstOrDefaultAsync();
if (orgPos != null)
{
if (orgPos.Profile != null)
{
var dp = new CommandDeployment
{
Sequence = 1,
ReceiveUserId = orgPos!.Profile!.Id!.ToString("D"),
CitizenId = orgPos!.Profile!.CitizenId!,
Prefix = orgPos!.Profile!.Prefix!.Name,
FirstName = orgPos!.Profile!.FirstName!,
LastName = orgPos!.Profile!.LastName!,
IsSendInbox = true,
IsSendMail = true,
IsSendNotification = true,
OrganizationName = orgPos!.OrganizationPosition!.Organization!.OrganizationOrganization!.Name,
PositionName = orgPos!.OrganizationPosition!.PositionMaster!.PositionPath!.Name
};
command.Deployments.Add(dp);
}
await _dbContext.SaveChangesAsync();
}
// query for new list
return command.Deployments;
}
}
}
catch
{
throw;
}
}
public async Task<List<ProfilePosition>> GetProfileByOrganizationIdAsync(Guid orgId)
{
try
{
var orgProfiles = await _dbContext.Set<ProfilePosition>()
.Include(x => x.Profile)
.ThenInclude(x => x!.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.ThenInclude(x => x!.OrganizationOrganization)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionPath)
.Where(x => x.OrganizationPosition!.Organization!.Id == orgId)
.ToListAsync();
return orgProfiles;
}
catch
{
throw;
}
}
public async Task CreateCommandDeploymentAsync(Guid commandId, List<CommandDeployment> deploy)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Deployments)
.FirstOrDefaultAsync(x => x.Id == commandId);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
{
var lastSeq = 0;
var dep = command.Deployments.OrderByDescending(x => x.Sequence).FirstOrDefault();
if (dep == null) lastSeq = 1;
else lastSeq = dep.Sequence;
foreach (var dep2 in deploy)
{
dep2.Sequence = lastSeq;
lastSeq++;
}
command.Deployments.AddRange(deploy);
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
public async Task UpdatCommandDeploymentAsync(List<CommandDeployment> deploys)
{
try
{
foreach (var dp in deploys)
{
var updated = await _dbContext.Set<CommandDeployment>().FirstOrDefaultAsync(x => x.Id == dp.Id);
if (updated != null)
{
updated.IsSendMail = dp.IsSendMail;
updated.IsSendInbox = dp.IsSendInbox;
}
}
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task DeleteCommandDeploymentAsync(Guid id)
{
try
{
var deleted = await _dbContext.Set<CommandDeployment>().FirstOrDefaultAsync(x => x.Id == id);
if (deleted == null)
throw new Exception(GlobalMessages.DataNotFound);
_dbContext.Set<CommandDeployment>().Remove(deleted);
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<CommandDeployment?> GetCommandDeploymentById(Guid id)
{
try
{
var data = await _dbContext.Set<CommandDeployment>().FirstOrDefaultAsync(x => x.Id == id);
return data;
}
catch
{
throw;
}
}
#region " Documents "
public async Task<List<CommandDocument>> GetCommandDocumentAsync(Guid id)
{
try
{
var docs = await _dbContext.Set<CommandDocument>()
.Include(x => x.Command)
.Include(x => x.Document)
.Where(x => x.Command.Id == id)
.ToListAsync();
return docs;
}
catch
{
throw;
}
}
#endregion
public async Task UpdateCommandInfo(Guid id, string orderNo, string orderYear, DateTime signDate)
{
try
{
var command = await _dbContext.Set<Command>().FirstOrDefaultAsync(x => x.Id == id);
if(command == null)
throw new Exception(GlobalMessages.CommandNotFound);
command.CommandExcecuteDate = signDate;
command.CommandNo = orderNo;
command.CommandYear = orderYear;
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
#endregion
}
}