Spaces:
Runtime error
Runtime error
File size: 8,730 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 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;
}
}
|