using System.Data; using Dapper; using SCM.Application.Common; using SCM.Domain.Materiais; namespace SCM.Infrastructure.Persistence.Repositories; public sealed class DeliveryOrderRepository : IDeliveryOrderRepository { private readonly IUnitOfWork _uow; public DeliveryOrderRepository(IUnitOfWork uow) => _uow = uow; private IDbConnection Conn => _uow.Connection; private IDbTransaction? Tx => _uow.CurrentTransaction; private const string Columns = """ id, obra_id, nota_fiscal, item_recebido, ordem_compra, quantidade, data_recebimento, unidade_medida_id, book_material_id, tipo_material_macro_id, peso, comprimento, largura, espessura, diametro, generico1, generico2, generico3, generico4, generico5 """; public async Task> GetByObraAsync( Guid obraId, string? notaFiscal = null, Guid? tipoMaterialMacroId = null, CancellationToken ct = default) { const string sql = $""" SELECT {Columns} FROM delivery_orders WHERE obra_id = @ObraId AND (@NotaFiscal::text IS NULL OR nota_fiscal ILIKE '%' || @NotaFiscal || '%') AND (@TipoMacroId::uuid IS NULL OR tipo_material_macro_id = @TipoMacroId) ORDER BY nota_fiscal, item_recebido """; var rows = await Conn.QueryAsync(new CommandDefinition( sql, new { ObraId = obraId, NotaFiscal = notaFiscal, TipoMacroId = tipoMaterialMacroId }, Tx, cancellationToken: ct)); return rows.ToList(); } public async Task GetByIdAsync(Guid id, CancellationToken ct = default) { const string sql = $"SELECT {Columns} FROM delivery_orders WHERE id = @Id"; return await Conn.QueryFirstOrDefaultAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); } public async Task ParExistsAsync(Guid obraId, string notaFiscal, string itemRecebido, Guid? excludeId = null, CancellationToken ct = default) { const string sql = """ SELECT COUNT(*) FROM delivery_orders WHERE obra_id = @ObraId AND lower(nota_fiscal) = lower(@NotaFiscal) AND lower(item_recebido) = lower(@ItemRecebido) AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) """; var count = await Conn.ExecuteScalarAsync(new CommandDefinition( sql, new { ObraId = obraId, NotaFiscal = notaFiscal, ItemRecebido = itemRecebido, ExcludeId = excludeId }, Tx, cancellationToken: ct)); return count > 0; } public async Task ObraExistsAsync(Guid obraId, CancellationToken ct = default) => await ExisteAsync("obras", obraId, ct); public async Task UnidadeExistsAsync(Guid unidadeMedidaId, CancellationToken ct = default) => await ExisteAsync("unidades_medida", unidadeMedidaId, ct); public async Task BookMaterialExistsAsync(Guid bookMaterialId, CancellationToken ct = default) => await ExisteAsync("tipos_material", bookMaterialId, ct); public async Task GetAgrupadorDoTipoMacroAsync(Guid tipoMaterialMacroId, CancellationToken ct = default) { const string sql = "SELECT agrupador_id FROM tipos_material_macro WHERE id = @Id"; var rows = (await Conn.QueryAsync( new CommandDefinition(sql, new { Id = tipoMaterialMacroId }, Tx, cancellationToken: ct))).ToList(); if (rows.Count == 0) return null; // tipo macro não existe return rows[0] ?? Guid.Empty; // existe; Empty = sem agrupador } public async Task ContarVinculosRirAsync(Guid deliveryOrderId, CancellationToken ct = default) { const string sql = "SELECT COUNT(*) FROM rir_itens WHERE delivery_order_id = @Id"; return await Conn.ExecuteScalarAsync( new CommandDefinition(sql, new { Id = deliveryOrderId }, Tx, cancellationToken: ct)); } public async Task AddAsync(DeliveryOrder d, CancellationToken ct = default) { const string sql = """ INSERT INTO delivery_orders (id, obra_id, nota_fiscal, item_recebido, ordem_compra, quantidade, data_recebimento, unidade_medida_id, book_material_id, tipo_material_macro_id, peso, comprimento, largura, espessura, diametro, generico1, generico2, generico3, generico4, generico5) VALUES (@Id, @ObraId, @NotaFiscal, @ItemRecebido, @OrdemCompra, @Quantidade, @DataRecebimento, @UnidadeMedidaId, @BookMaterialId, @TipoMaterialMacroId, @Peso, @Comprimento, @Largura, @Espessura, @Diametro, @Generico1, @Generico2, @Generico3, @Generico4, @Generico5) """; await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(d), Tx, cancellationToken: ct)); } public async Task UpdateAsync(DeliveryOrder d, CancellationToken ct = default) { const string sql = """ UPDATE delivery_orders SET nota_fiscal = @NotaFiscal, item_recebido = @ItemRecebido, ordem_compra = @OrdemCompra, quantidade = @Quantidade, data_recebimento = @DataRecebimento, unidade_medida_id = @UnidadeMedidaId, book_material_id = @BookMaterialId, tipo_material_macro_id = @TipoMaterialMacroId, peso = @Peso, comprimento = @Comprimento, largura = @Largura, espessura = @Espessura, diametro = @Diametro, generico1 = @Generico1, generico2 = @Generico2, generico3 = @Generico3, generico4 = @Generico4, generico5 = @Generico5 WHERE id = @Id """; await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(d), Tx, cancellationToken: ct)); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { const string sql = "DELETE FROM delivery_orders WHERE id = @Id"; var affected = await Conn.ExecuteAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); return affected > 0; } private async Task ExisteAsync(string tabela, Guid id, CancellationToken ct) { var sql = $"SELECT COUNT(*) FROM {tabela} WHERE id = @Id"; var count = await Conn.ExecuteScalarAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); return count > 0; } private static object ToParameters(DeliveryOrder d) => new { d.Id, d.ObraId, d.NotaFiscal, d.ItemRecebido, d.OrdemCompra, d.Quantidade, d.DataRecebimento, d.UnidadeMedidaId, d.BookMaterialId, d.TipoMaterialMacroId, d.Peso, d.Comprimento, d.Largura, d.Espessura, d.Diametro, d.Generico1, d.Generico2, d.Generico3, d.Generico4, d.Generico5 }; }