Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/ColetaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 10.4 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ColetaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/ColetaRepository.cs
-
curl -L -o ColetaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ColetaRepository.cs
10.4 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Qualidade; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class ColetaRepository : IColetaRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public ColetaRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<bool> AgenteExistsAsync(Guid agenteId, CancellationToken ct = default) | |
| { | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| "SELECT COUNT(*) FROM agentes WHERE id = @Id", new { Id = agenteId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<Agente?> GetAgenteAsync(Guid agenteId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, codigo, nome, tipo, cpf, email FROM agentes WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<Agente>( | |
| new CommandDefinition(sql, new { Id = agenteId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<ItemProgramadoColeta?> GetItemProgramadoAsync(Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, item_controle_id, descricao, quantidade, subatividade_padrao_id | |
| FROM itens_programados WHERE id = @Id | |
| """; | |
| return await Conn.QueryFirstOrDefaultAsync<ItemProgramadoColeta>( | |
| new CommandDefinition(sql, new { Id = itemProgramadoId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<Programacao>> GetProgramacoesAsync(Guid agenteId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, agente_id, item_programado_id, data_criacao | |
| FROM programacoes WHERE agente_id = @Id ORDER BY data_criacao | |
| """; | |
| var rows = await Conn.QueryAsync<Programacao>( | |
| new CommandDefinition(sql, new { Id = agenteId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<bool> ProgramacaoExistsAsync(Guid agenteId, Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM programacoes WHERE agente_id = @AgenteId AND item_programado_id = @ItemId"; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { AgenteId = agenteId, ItemId = itemProgramadoId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddProgramacaoAsync(Programacao p, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO programacoes (id, agente_id, item_programado_id, data_criacao) | |
| VALUES (@Id, @AgenteId, @ItemProgramadoId, @DataCriacao) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| p.Id, p.AgenteId, p.ItemProgramadoId, p.DataCriacao | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> RemoveProgramacaoAsync(Guid agenteId, Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM programacoes WHERE agente_id = @AgenteId AND item_programado_id = @ItemId"; | |
| var affected = await Conn.ExecuteAsync(new CommandDefinition( | |
| sql, new { AgenteId = agenteId, ItemId = itemProgramadoId }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| public async Task<IReadOnlyList<CampoInspecao>> GetCamposEmUsoAsync( | |
| Guid subAtividadePadraoId, CampoEtapa etapa, CancellationToken ct = default) | |
| { | |
| // A etapa 'Ambas' (2) entra em qualquer fase (regra do legado: filtro por etapa 0=PRD, 1=CQ, 2=Ambas). | |
| const string sql = """ | |
| SELECT id, sub_atividade_padrao_id, nome, tipo, tipo_dado, etapa, | |
| obrigatorio_prd, obrigatorio_cq, em_uso, ordem | |
| FROM campos_inspecao | |
| WHERE sub_atividade_padrao_id = @Id AND em_uso = true AND (etapa = @Etapa OR etapa = 2) | |
| ORDER BY ordem, nome | |
| """; | |
| var rows = await Conn.QueryAsync<CampoInspecao>(new CommandDefinition( | |
| sql, new { Id = subAtividadePadraoId, Etapa = (int)etapa }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task AddColetaAsync(Coleta c, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO coletas (id, agente_id, tipo_agente, origem, data_sincronizacao) | |
| VALUES (@Id, @AgenteId, @TipoAgente, @Origem, @DataSincronizacao) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| c.Id, c.AgenteId, TipoAgente = (int)c.TipoAgente, Origem = (int)c.Origem, c.DataSincronizacao | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddEventoAsync( | |
| EventoColetado e, IReadOnlyList<ValorColetado> valores, IReadOnlyList<EquipeEventoColetado> equipes, | |
| CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO eventos_coletados | |
| (id, coleta_id, item_programado_id, producao_id, data_coleta, qt_prevista, qt_executada, | |
| classificacao, motivo_reapresentacao, executor_id, data_lib_cq, tipo_lib_cq, status, mensagem_log) | |
| VALUES | |
| (@Id, @ColetaId, @ItemProgramadoId, @ProducaoId, @DataColeta, @QtPrevista, @QtExecutada, | |
| @Classificacao, @MotivoReapresentacao, @ExecutorId, @DataLibCq, @TipoLibCq, @Status, @MensagemLog) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| e.Id, e.ColetaId, e.ItemProgramadoId, e.ProducaoId, e.DataColeta, e.QtPrevista, e.QtExecutada, | |
| e.Classificacao, e.MotivoReapresentacao, e.ExecutorId, e.DataLibCq, e.TipoLibCq, | |
| Status = (int)e.Status, e.MensagemLog | |
| }, Tx, cancellationToken: ct)); | |
| const string sqlValor = """ | |
| INSERT INTO valores_coletados (id, evento_coletado_id, campo_inspecao_id, etapa, valor) | |
| VALUES (@Id, @EventoColetadoId, @CampoInspecaoId, @Etapa, @Valor) | |
| """; | |
| foreach (var v in valores) | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlValor, new | |
| { | |
| v.Id, v.EventoColetadoId, v.CampoInspecaoId, Etapa = (int)v.Etapa, v.Valor | |
| }, Tx, cancellationToken: ct)); | |
| const string sqlEquipe = """ | |
| INSERT INTO equipes_evento_coletado (id, evento_coletado_id, equipe_id, quantidade_executada) | |
| VALUES (@Id, @EventoColetadoId, @EquipeId, @QuantidadeExecutada) | |
| """; | |
| foreach (var eq in equipes) | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlEquipe, new | |
| { | |
| eq.Id, eq.EventoColetadoId, eq.EquipeId, eq.QuantidadeExecutada | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddLogAsync(LogSincronizacaoColeta log, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO logs_sincronizacao_coleta (id, agente_id, data, id_evento, mensagem) | |
| VALUES (@Id, @AgenteId, @Data, @IdEvento, @Mensagem) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| log.Id, log.AgenteId, log.Data, log.IdEvento, log.Mensagem | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<Guid?> GetObraIdDoIcAsync(Guid itemControleId, CancellationToken ct = default) | |
| { | |
| return await Conn.ExecuteScalarAsync<Guid?>(new CommandDefinition( | |
| "SELECT obra_id FROM itens_controle WHERE id = @Id", | |
| new { Id = itemControleId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<LogSincronizacaoColeta>> GetLogsAsync(Guid agenteId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, agente_id, data, id_evento, mensagem | |
| FROM logs_sincronizacao_coleta WHERE agente_id = @Id ORDER BY data DESC | |
| """; | |
| var rows = await Conn.QueryAsync<LogSincronizacaoColeta>( | |
| new CommandDefinition(sql, new { Id = agenteId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<IReadOnlyList<ValorColetado>> GetUltimosValoresDoItemAsync(Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| // Valores do evento coletado mais recente (por coleta.data_sincronizacao) do item. | |
| const string sql = """ | |
| SELECT v.id, v.evento_coletado_id, v.campo_inspecao_id, v.etapa, v.valor | |
| FROM valores_coletados v | |
| WHERE v.evento_coletado_id = ( | |
| SELECT e.id FROM eventos_coletados e | |
| JOIN coletas c ON c.id = e.coleta_id | |
| WHERE e.item_programado_id = @Id AND e.status = 1 | |
| ORDER BY c.data_sincronizacao DESC | |
| LIMIT 1 | |
| ) | |
| """; | |
| var rows = await Conn.QueryAsync<ValorColetado>( | |
| new CommandDefinition(sql, new { Id = itemProgramadoId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<bool> EventoExistsAsync(Guid eventoColetadoId, CancellationToken ct = default) | |
| { | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| "SELECT COUNT(*) FROM eventos_coletados WHERE id = @Id", new { Id = eventoColetadoId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task ReplaceValoresAsync(Guid eventoColetadoId, IReadOnlyList<ValorColetado> valores, CancellationToken ct = default) | |
| { | |
| await Conn.ExecuteAsync(new CommandDefinition( | |
| "DELETE FROM valores_coletados WHERE evento_coletado_id = @Id", | |
| new { Id = eventoColetadoId }, Tx, cancellationToken: ct)); | |
| const string sql = """ | |
| INSERT INTO valores_coletados (id, evento_coletado_id, campo_inspecao_id, etapa, valor) | |
| VALUES (@Id, @EventoColetadoId, @CampoInspecaoId, @Etapa, @Valor) | |
| """; | |
| foreach (var v in valores) | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| v.Id, EventoColetadoId = eventoColetadoId, v.CampoInspecaoId, Etapa = (int)v.Etapa, v.Valor | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| } | |