เพิ่ม api

1. ตรวจสอบสถานะความพร้อมในการออกคำสั่ง
2. เปลี่ยนสถานะไป state ถัดไป
3. เปลี่ยนสถานะไป state ก่อนหน้า
This commit is contained in:
Suphonchai Phoonsawat 2023-08-04 10:40:09 +07:00
parent e8607f76fa
commit ab7135fcbf
4 changed files with 194 additions and 11 deletions

View file

@ -1,5 +1,4 @@
using Amazon.S3.Model.Internal.MarshallTransformations;
using BMA.EHR.Application.Common.Interfaces;
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;
@ -39,6 +38,9 @@ namespace BMA.EHR.Application.Repositories.Commands
return await _dbContext.Set<Command>()
.Include(x => x.Placement)
.Include(x => x.CommandType)
.Include(x => x.Documents)
.Include(x => x.Receivers)
.Include(x => x.CommandStatus)
.FirstOrDefaultAsync(x => x.Id == id);
}
@ -47,10 +49,20 @@ namespace BMA.EHR.Application.Repositories.Commands
var status = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == 1);
command.CommandStatus = status!;
_dbContext.Attatch(status!);
return await base.AddAsync(command);
}
public override async Task<Command> UpdateAsync(Command entity)
{
// attatch
_dbContext.Attatch(entity.CommandStatus);
_dbContext.Attatch(entity.CommandType);
_dbContext.Attatch(entity.Placement);
return await base.UpdateAsync(entity);
}
#endregion
public async Task<List<CommandReceiver>> GetReceiverByCommmandIdAsync(Guid Id)
@ -343,12 +355,64 @@ namespace BMA.EHR.Application.Repositories.Commands
#endregion
#region " Change Command Status "
public async Task GotoNextStateAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>().Include(c => c.CommandStatus).FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
var notProcess = new int[] { 4, 5 };
if (!notProcess.Contains(command.CommandStatus.Sequence))
{
var nextStatus = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == command.CommandStatus.Sequence + 1);
command.CommandStatus = nextStatus!;
_dbContext.Attatch(nextStatus!);
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
public async Task GotoPrevStateAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>().Include(c => c.CommandStatus).FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
var notProcess = new int[] { 1, 5 };
if (!notProcess.Contains(command.CommandStatus.Sequence))
{
var nextStatus = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == command.CommandStatus.Sequence - 1);
command.CommandStatus = nextStatus!;
_dbContext.Attatch(nextStatus!);
await _dbContext.SaveChangesAsync();
}
}
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)
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
command.CommandExcecuteDate = signDate;