ปรับ api ตามที่คุยใหม่

This commit is contained in:
Suphonchai Phoonsawat 2023-08-08 10:19:35 +07:00
parent 7b038f0131
commit 1a7be19e33
2 changed files with 174 additions and 20 deletions

View file

@ -37,15 +37,21 @@ namespace BMA.EHR.Application.Repositories.Commands
#region " Private "
private async Task GetReceiverForByCommndTypeAsync(Command command)
private async Task<List<CommandReceiver>> GetReceiverForByCommndTypeAsync(Command command)
{
switch (command.CommandType.Category.Trim().ToUpper())
var result = new List<CommandReceiver>();
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;
}
/// <summary>
@ -53,10 +59,13 @@ namespace BMA.EHR.Application.Repositories.Commands
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
private async Task GetReceiver01(Command command)
private async Task<List<CommandReceiver>> GetReceiver01(Command command)
{
try
{
var result = new List<CommandReceiver>();
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
@ -70,7 +79,7 @@ namespace BMA.EHR.Application.Repositories.Commands
var appointPeople = await _dbContext.Set<PlacementProfile>()
.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<Guid> selected)
{
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);
var appointPeople = await _dbContext.Set<PlacementProfile>()
.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<CommandReceiver>().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<List<CommandReceiver>> GetReceiverForCommandAsync(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
return await GetReceiverForByCommndTypeAsync(command);
}
catch
{
throw;
}
}
public async Task<List<CommandReceiver>> GetReceiverByCommmandIdAsync(Guid Id)
{
try
{
// ปรับใหม่ให้อ่านจาก database ล้วนๆ
var command = await _dbContext.Set<Command>()
.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<CommandReceiver>();
}
}
}
@ -711,14 +789,14 @@ namespace BMA.EHR.Application.Repositories.Commands
try
{
var data = await _dbContext.Set<OrganizationEntity>().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
{