using System.Data; using Dapper; using SCM.Application.Common; using SCM.Domain.Controle; namespace SCM.Infrastructure.Persistence.Repositories; public sealed class PrecedenciaRepository : IPrecedenciaRepository { private readonly IUnitOfWork _uow; public PrecedenciaRepository(IUnitOfWork uow) => _uow = uow; private IDbConnection Conn => _uow.Connection; private IDbTransaction? Tx => _uow.CurrentTransaction; private const string Columns = "id, item_cq_id, item_prd_id, tipo_precedencia"; public async Task> GetByItemCqAsync(Guid itemCqId, CancellationToken ct = default) { const string sql = $"SELECT {Columns} FROM precedencias_item WHERE item_cq_id = @Id"; var rows = await Conn.QueryAsync( new CommandDefinition(sql, new { Id = itemCqId }, Tx, cancellationToken: ct)); return rows.ToList(); } public async Task> GetSucessoresDoPrdAsync(Guid itemPrdId, CancellationToken ct = default) { const string sql = $"SELECT {Columns} FROM precedencias_item WHERE item_prd_id = @Id"; var rows = await Conn.QueryAsync( new CommandDefinition(sql, new { Id = itemPrdId }, Tx, cancellationToken: ct)); return rows.ToList(); } public async Task ItemExistsAsync(Guid itemProgramadoId, CancellationToken ct = default) { var count = await Conn.ExecuteScalarAsync(new CommandDefinition( "SELECT COUNT(*) FROM itens_programados WHERE id = @Id", new { Id = itemProgramadoId }, Tx, cancellationToken: ct)); return count > 0; } public async Task GetTipoAtividadeAsync(Guid itemProgramadoId, CancellationToken ct = default) { var tipo = await Conn.ExecuteScalarAsync(new CommandDefinition( "SELECT tipo_atividade FROM itens_programados WHERE id = @Id", new { Id = itemProgramadoId }, Tx, cancellationToken: ct)); return tipo.HasValue ? (TipoAtividade)tipo.Value : null; } public async Task GetQuantidadeItemAsync(Guid itemProgramadoId, CancellationToken ct = default) { return await Conn.ExecuteScalarAsync(new CommandDefinition( "SELECT COALESCE(quantidade, 0) FROM itens_programados WHERE id = @Id", new { Id = itemProgramadoId }, Tx, cancellationToken: ct)); } public async Task ReplaceAsync(Guid itemCqId, IReadOnlyList precedencias, CancellationToken ct = default) { await Conn.ExecuteAsync(new CommandDefinition( "DELETE FROM precedencias_item WHERE item_cq_id = @Id", new { Id = itemCqId }, Tx, cancellationToken: ct)); const string sql = """ INSERT INTO precedencias_item (id, item_cq_id, item_prd_id, tipo_precedencia) VALUES (@Id, @ItemCqId, @ItemPrdId, @TipoPrecedencia) """; foreach (var p in precedencias) await Conn.ExecuteAsync(new CommandDefinition(sql, new { p.Id, p.ItemCqId, p.ItemPrdId, p.TipoPrecedencia }, Tx, cancellationToken: ct)); } public async Task> GetEstadoPredecessorasAsync(Guid itemCqId, CancellationToken ct = default) { // Produção Normal mais recente de cada PRD predecessora do CQ. const string sql = """ SELECT DISTINCT ON (p.item_programado_id) p.item_programado_id AS item_prd_id, p.qt_prevista, p.qt_executada, p.avanco, p.tipo_lib_cq, p.data FROM producoes_controle p JOIN precedencias_item pr ON pr.item_prd_id = p.item_programado_id WHERE pr.item_cq_id = @Id AND p.classificacao = 0 ORDER BY p.item_programado_id, p.data DESC """; var rows = await Conn.QueryAsync( new CommandDefinition(sql, new { Id = itemCqId }, Tx, cancellationToken: ct)); return rows.ToList(); } }