Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/CaracteristicaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 8.73 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/CaracteristicaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/CaracteristicaRepository.cs
-
curl -L -o CaracteristicaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/CaracteristicaRepository.cs
8.73 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Controle; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class CaracteristicaRepository : ICaracteristicaRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public CaracteristicaRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = "id, obra_id, nome, finalidade, tipo_dado"; | |
| public async Task<IReadOnlyList<CaracteristicaObra>> GetByObraAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM caracteristicas_obra WHERE obra_id = @Id ORDER BY nome"; | |
| var rows = await Conn.QueryAsync<CaracteristicaObra>( | |
| new CommandDefinition(sql, new { Id = obraId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<CaracteristicaObra?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM caracteristicas_obra WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<CaracteristicaObra>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> NomeExistsAsync(Guid obraId, string nome, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM caracteristicas_obra | |
| WHERE obra_id = @ObraId AND nome = @Nome AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { ObraId = obraId, Nome = nome, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default) | |
| => ExisteAsync("obras", obraId, ct); | |
| public Task<bool> ItemControleExistsAsync(Guid itemControleId, CancellationToken ct = default) | |
| => ExisteAsync("itens_controle", itemControleId, ct); | |
| public async Task AddAsync(CaracteristicaObra c, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO caracteristicas_obra (id, obra_id, nome, finalidade, tipo_dado) | |
| VALUES (@Id, @ObraId, @Nome, @Finalidade, @TipoDado) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| c.Id, c.ObraId, c.Nome, Finalidade = (int)c.Finalidade, TipoDado = (int)c.TipoDado | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var affected = await Conn.ExecuteAsync(new CommandDefinition( | |
| "DELETE FROM caracteristicas_obra WHERE id = @Id", new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| public async Task<int> ContarValoresAsync(Guid caracteristicaId, CancellationToken ct = default) | |
| { | |
| return await Conn.ExecuteScalarAsync<int>(new CommandDefinition( | |
| "SELECT COUNT(*) FROM caracteristica_valores_ic WHERE caracteristica_id = @Id", | |
| new { Id = caracteristicaId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<CaracteristicaValorIc>> GetValoresDoIcAsync(Guid itemControleId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, caracteristica_id, item_controle_id, valor | |
| FROM caracteristica_valores_ic WHERE item_controle_id = @Id | |
| """; | |
| var rows = await Conn.QueryAsync<CaracteristicaValorIc>( | |
| new CommandDefinition(sql, new { Id = itemControleId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task UpsertValorAsync(CaracteristicaValorIc v, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO caracteristica_valores_ic (id, caracteristica_id, item_controle_id, valor) | |
| VALUES (@Id, @CaracteristicaId, @ItemControleId, @Valor) | |
| ON CONFLICT (caracteristica_id, item_controle_id) DO UPDATE SET valor = @Valor | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| v.Id, v.CaracteristicaId, v.ItemControleId, v.Valor | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> RemoverValorAsync(Guid caracteristicaId, Guid itemControleId, CancellationToken ct = default) | |
| { | |
| var affected = await Conn.ExecuteAsync(new CommandDefinition( | |
| "DELETE FROM caracteristica_valores_ic WHERE caracteristica_id = @C AND item_controle_id = @I", | |
| new { C = caracteristicaId, I = itemControleId }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| public async Task<int> ContarFiltrosEmUsoAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(DISTINCT c.id) FROM caracteristicas_obra c | |
| JOIN caracteristica_valores_ic v ON v.caracteristica_id = c.id | |
| WHERE c.obra_id = @Id AND c.finalidade = 0 | |
| """; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = obraId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<FotoPorFiltro>> CalcularFotoPorFiltroAsync(Guid obraId, DateOnly data, CancellationToken ct = default) | |
| { | |
| // Mesma foto da fatia 24, agrupada pelo valor da característica de filtro do IC | |
| // (ICs sem valor caem no grupo '—'). Cadastrado por grupo = Σ quantidades dos itens. | |
| const string sql = """ | |
| WITH filtro AS ( | |
| SELECT v.item_controle_id, v.valor | |
| FROM caracteristica_valores_ic v | |
| JOIN caracteristicas_obra c ON c.id = v.caracteristica_id | |
| WHERE c.obra_id = @ObraId AND c.finalidade = 0 | |
| ), | |
| ultimas AS ( | |
| SELECT DISTINCT ON (p.item_programado_id) | |
| p.avanco, p.tipo_lib_cq, i.quantidade, COALESCE(f.valor, '—') AS valor_filtro | |
| FROM producoes_controle p | |
| JOIN itens_programados i ON i.id = p.item_programado_id | |
| JOIN itens_controle ic ON ic.id = i.item_controle_id | |
| LEFT JOIN filtro f ON f.item_controle_id = ic.id | |
| WHERE ic.obra_id = @ObraId AND p.data <= @Data AND p.classificacao = 0 | |
| ORDER BY p.item_programado_id, p.data DESC | |
| ), | |
| cadastrado AS ( | |
| SELECT COALESCE(f.valor, '—') AS valor_filtro, SUM(i.quantidade) AS qt | |
| FROM itens_programados i | |
| JOIN itens_controle ic ON ic.id = i.item_controle_id | |
| LEFT JOIN filtro f ON f.item_controle_id = ic.id | |
| WHERE ic.obra_id = @ObraId | |
| GROUP BY COALESCE(f.valor, '—') | |
| ) | |
| SELECT | |
| cad.valor_filtro, | |
| COALESCE(SUM(CASE WHEN u.avanco >= 100 AND u.tipo_lib_cq IN ('I','A','NA','CP') THEN u.quantidade END), 0) AS qt_liberada, | |
| COALESCE(SUM(CASE WHEN u.avanco >= 100 AND u.tipo_lib_cq IS NULL THEN u.quantidade END), 0) AS qt_nao_liberada, | |
| COALESCE(SUM(CASE WHEN u.tipo_lib_cq IS NULL OR u.tipo_lib_cq IN ('I','A','NA','CP') | |
| THEN ROUND(u.avanco / 100 * u.quantidade, 4) END), 0) AS qt_avancada, | |
| cad.qt AS qt_cadastrada, | |
| (COUNT(*) FILTER (WHERE u.avanco >= 100 AND u.tipo_lib_cq IN ('I','A','NA','CP')))::int AS itens_liberados, | |
| (COUNT(*) FILTER (WHERE u.avanco >= 100 AND u.tipo_lib_cq IS NULL))::int AS itens_nao_liberados | |
| FROM cadastrado cad | |
| LEFT JOIN ultimas u ON u.valor_filtro = cad.valor_filtro | |
| GROUP BY cad.valor_filtro, cad.qt | |
| ORDER BY cad.valor_filtro | |
| """; | |
| var rows = await Conn.QueryAsync<FotoPorFiltro>(new CommandDefinition( | |
| sql, new { ObraId = obraId, Data = data }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| 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; | |
| } | |
| } | |