Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace JSE.Core.Messages.Integration
{
public class PedidoAutorizadoIntegrationEvent : IntegrationEvent
{
public Guid ClienteId { get; private set; }
public Guid PedidoId { get; private set; }
public IDictionary<Guid, int> Itens { get; private set; }

public PedidoAutorizadoIntegrationEvent(Guid clienteId, Guid pedidoId, IDictionary<Guid, int> itens)
{
ClienteId = clienteId;
PedidoId = pedidoId;
Itens = itens;
}
}
}
2 changes: 1 addition & 1 deletion src/services/JSE.Pedido.API/Application/DTO/PedidoDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class PedidoDTO
{
public Guid Id { get; set; }
public int Codigo { get; set; }

public Guid ClienteId { get; set; }
public int Status { get; set; }
public DateTime Data { get; set; }
public decimal ValorTotal { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public interface IPedidoQueries
{
Task<PedidoDTO> ObterUltimoPedido(Guid clienteId);
Task<IEnumerable<PedidoDTO>> ObterListaPorClienteId(Guid clienteId);
Task<PedidoDTO> ObterPedidosAutorizados();
}

}
31 changes: 31 additions & 0 deletions src/services/JSE.Pedido.API/Application/Queries/PedidoQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ public async Task<IEnumerable<PedidoDTO>> ObterListaPorClienteId(Guid clienteId)
return pedidos.Select(PedidoDTO.ParaPedidoDTO);
}

public async Task<PedidoDTO> ObterPedidosAutorizados()
{
// Correção para pegar todos os itens do pedido e ordernar pelo pedido mais antigo
const string sql = @"SELECT
P.ID as 'PedidoId', P.ID, P.CLIENTEID,
PI.ID as 'PedidoItemId', PI.ID, PI.PRODUTOID, PI.QUANTIDADE
FROM PEDIDOS P
INNER JOIN PEDIDOITEMS PI ON P.ID = PI.PEDIDOID
WHERE P.PEDIDOSTATUS = 1
ORDER BY P.DATACADASTRO";

// Utilizacao do lookup para manter o estado a cada ciclo de registro retornado
var lookup = new Dictionary<Guid, PedidoDTO>();

await _pedidoRepository.ObterConexao().QueryAsync<PedidoDTO, PedidoItemDTO, PedidoDTO>(sql,
(p, pi) =>
{
if (!lookup.TryGetValue(p.Id, out var pedidoDTO))
lookup.Add(p.Id, pedidoDTO = p);

pedidoDTO.PedidoItems ??= new List<PedidoItemDTO>();
pedidoDTO.PedidoItems.Add(pi);

return pedidoDTO;

}, splitOn: "PedidoId,PedidoItemId");

// Obtendo dados o lookup
return lookup.Values.OrderBy(p => p.Data).FirstOrDefault();
}

private PedidoDTO MapearPedido(dynamic result)
{
var pedido = new PedidoDTO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@

using JSE.Core.Messages.Integration;
using JSE.MessageBus;
using JSE.Pedidos.API.Application.Queries;

namespace JSE.Pedidos.API.Services
{
public class PedidoOrquestradorIntegrationHandler : IHostedService, IDisposable
{

private readonly IServiceProvider _serviceProvider;
private readonly ILogger<PedidoOrquestradorIntegrationHandler> _logger;
private Timer _timer;

public PedidoOrquestradorIntegrationHandler(ILogger<PedidoOrquestradorIntegrationHandler> logger)
public PedidoOrquestradorIntegrationHandler(ILogger<PedidoOrquestradorIntegrationHandler> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}

public Task StartAsync(CancellationToken cancellationToken)
Expand All @@ -23,7 +30,22 @@ public Task StartAsync(CancellationToken cancellationToken)

private async void ProcessarPedidos(object state)
{
_logger.LogInformation("Processando Pedidos");
using (var scope = _serviceProvider.CreateScope())
{
var pedidoQueries = scope.ServiceProvider.GetRequiredService<IPedidoQueries>();
var pedido = await pedidoQueries.ObterPedidosAutorizados();

if (pedido == null) return;

var bus = scope.ServiceProvider.GetRequiredService<IMessageBus>();

var pedidoAutorizado = new PedidoAutorizadoIntegrationEvent(pedido.ClienteId, pedido.Id,
pedido.PedidoItems.ToDictionary(p => p.ProdutoId, p => p.Quantidade));

await bus.PublishAsync(pedidoAutorizado);

_logger.LogInformation($"Pedido ID: {pedido.Id} foi encaminhado para baixa no estoque.");
}
}

public Task StopAsync(CancellationToken cancellationToken)
Expand Down
Loading