using System.Data; using Dapper; using SCM.Application.Common; using SCM.Domain.Medicao; namespace SCM.Infrastructure.Persistence.Repositories; public sealed class CriterioEapRepository : ICriterioEapRepository { private readonly IUnitOfWork _uow; public CriterioEapRepository(IUnitOfWork uow) => _uow = uow; private IDbConnection Conn => _uow.Connection; private IDbTransaction? Tx => _uow.CurrentTransaction; public async Task> GetByContratoAsync( Guid contratoId, CriterioEapTipo? tipo = null, CancellationToken ct = default) { const string sql = """ SELECT c.id, c.nome, c.tipo, c.contrato_id, c.obra_id, COUNT(e.id) AS qtd FROM criterios_eap c LEFT JOIN eaps e ON e.criterio_eap_id = c.id WHERE c.contrato_id = @ContratoId AND (@Tipo::int IS NULL OR c.tipo = @Tipo) GROUP BY c.id, c.nome, c.tipo, c.contrato_id, c.obra_id ORDER BY c.nome """; var tipoValue = tipo.HasValue ? (int?)(int)tipo.Value : null; var rows = await Conn.QueryAsync( new CommandDefinition(sql, new { ContratoId = contratoId, Tipo = tipoValue }, Tx, cancellationToken: ct)); return rows .Select(r => new CriterioEapComContagem(r.ToDomain(), r.Qtd)) .ToList(); } public async Task GetByIdAsync(Guid id, CancellationToken ct = default) { const string sql = """ SELECT id, nome, tipo, contrato_id, obra_id FROM criterios_eap WHERE id = @Id """; return await Conn.QueryFirstOrDefaultAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); } public async Task ContratoExistsAsync(Guid contratoId, CancellationToken ct = default) { var count = await Conn.ExecuteScalarAsync(new CommandDefinition( "SELECT COUNT(*) FROM contratos WHERE id = @Id", new { Id = contratoId }, Tx, cancellationToken: ct)); return count > 0; } public async Task ObraExistsAsync(Guid obraId, CancellationToken ct = default) { var count = await Conn.ExecuteScalarAsync(new CommandDefinition( "SELECT COUNT(*) FROM obras WHERE id = @Id", new { Id = obraId }, Tx, cancellationToken: ct)); return count > 0; } public async Task NomeExistsAsync( string nome, Guid contratoId, CriterioEapTipo tipo, Guid? excludeId = null, CancellationToken ct = default) { const string sql = """ SELECT COUNT(*) FROM criterios_eap WHERE nome = @Nome AND contrato_id = @ContratoId AND tipo = @Tipo AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) """; var count = await Conn.ExecuteScalarAsync(new CommandDefinition( sql, new { Nome = nome, ContratoId = contratoId, Tipo = (int)tipo, ExcludeId = excludeId }, Tx, cancellationToken: ct)); return count > 0; } public async Task ContarEapsVinculadasAsync(Guid id, CancellationToken ct = default) { const string sql = "SELECT COUNT(*) FROM eaps WHERE criterio_eap_id = @Id"; return await Conn.ExecuteScalarAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); } public async Task AddAsync(CriterioEap criterio, CancellationToken ct = default) { const string sql = """ INSERT INTO criterios_eap (id, nome, tipo, contrato_id, obra_id) VALUES (@Id, @Nome, @Tipo, @ContratoId, @ObraId) """; await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(criterio), Tx, cancellationToken: ct)); } public async Task UpdateAsync(CriterioEap criterio, CancellationToken ct = default) { const string sql = "UPDATE criterios_eap SET nome = @Nome WHERE id = @Id"; await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(criterio), Tx, cancellationToken: ct)); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { const string sql = "DELETE FROM criterios_eap WHERE id = @Id"; var affected = await Conn.ExecuteAsync( new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); return affected > 0; } private static object ToParameters(CriterioEap c) => new { c.Id, c.Nome, Tipo = (int)c.Tipo, c.ContratoId, c.ObraId }; private sealed class CriterioEapRow { public Guid Id { get; set; } public string Nome { get; set; } = string.Empty; public int Tipo { get; set; } public Guid ContratoId { get; set; } public Guid ObraId { get; set; } public int Qtd { get; set; } public CriterioEap ToDomain() => new() { Id = Id, Nome = Nome, Tipo = (CriterioEapTipo)Tipo, ContratoId = ContratoId, ObraId = ObraId }; } }