Spaces:
Runtime error
Runtime error
File size: 6,999 Bytes
de8b914 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 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<IReadOnlyList<DeliveryOrder>> 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<DeliveryOrder>(new CommandDefinition(
sql, new { ObraId = obraId, NotaFiscal = notaFiscal, TipoMacroId = tipoMaterialMacroId }, Tx, cancellationToken: ct));
return rows.ToList();
}
public async Task<DeliveryOrder?> GetByIdAsync(Guid id, CancellationToken ct = default)
{
const string sql = $"SELECT {Columns} FROM delivery_orders WHERE id = @Id";
return await Conn.QueryFirstOrDefaultAsync<DeliveryOrder>(
new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
}
public async Task<bool> 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<long>(new CommandDefinition(
sql, new { ObraId = obraId, NotaFiscal = notaFiscal, ItemRecebido = itemRecebido, ExcludeId = excludeId }, Tx, cancellationToken: ct));
return count > 0;
}
public async Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default)
=> await ExisteAsync("obras", obraId, ct);
public async Task<bool> UnidadeExistsAsync(Guid unidadeMedidaId, CancellationToken ct = default)
=> await ExisteAsync("unidades_medida", unidadeMedidaId, ct);
public async Task<bool> BookMaterialExistsAsync(Guid bookMaterialId, CancellationToken ct = default)
=> await ExisteAsync("tipos_material", bookMaterialId, ct);
public async Task<Guid?> GetAgrupadorDoTipoMacroAsync(Guid tipoMaterialMacroId, CancellationToken ct = default)
{
const string sql = "SELECT agrupador_id FROM tipos_material_macro WHERE id = @Id";
var rows = (await Conn.QueryAsync<Guid?>(
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<int> ContarVinculosRirAsync(Guid deliveryOrderId, CancellationToken ct = default)
{
const string sql = "SELECT COUNT(*) FROM rir_itens WHERE delivery_order_id = @Id";
return await Conn.ExecuteScalarAsync<int>(
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<bool> 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<bool> ExisteAsync(string tabela, Guid id, CancellationToken ct)
{
var sql = $"SELECT COUNT(*) FROM {tabela} WHERE id = @Id";
var count = await Conn.ExecuteScalarAsync<long>(
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
};
}
|