Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/PrecedenciaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/PrecedenciaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/PrecedenciaRepository.cs
-
curl -L -o PrecedenciaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/PrecedenciaRepository.cs
4.09 kB
| 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<IReadOnlyList<PrecedenciaItem>> GetByItemCqAsync(Guid itemCqId, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM precedencias_item WHERE item_cq_id = @Id"; | |
| var rows = await Conn.QueryAsync<PrecedenciaItem>( | |
| new CommandDefinition(sql, new { Id = itemCqId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<IReadOnlyList<PrecedenciaItem>> GetSucessoresDoPrdAsync(Guid itemPrdId, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM precedencias_item WHERE item_prd_id = @Id"; | |
| var rows = await Conn.QueryAsync<PrecedenciaItem>( | |
| new CommandDefinition(sql, new { Id = itemPrdId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<bool> ItemExistsAsync(Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| "SELECT COUNT(*) FROM itens_programados WHERE id = @Id", | |
| new { Id = itemProgramadoId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<TipoAtividade?> GetTipoAtividadeAsync(Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| var tipo = await Conn.ExecuteScalarAsync<int?>(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<decimal> GetQuantidadeItemAsync(Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| return await Conn.ExecuteScalarAsync<decimal>(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<PrecedenciaItem> 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<IReadOnlyList<PrdEstado>> 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<PrdEstado>( | |
| new CommandDefinition(sql, new { Id = itemCqId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| } | |