Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/ApropriacaoHorasRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ApropriacaoHorasRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/ApropriacaoHorasRepository.cs
-
curl -L -o ApropriacaoHorasRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ApropriacaoHorasRepository.cs
13.5 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Planejamento; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class ApropriacaoHorasRepository : IApropriacaoHorasRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public ApropriacaoHorasRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<ApropriacaoHoras>> GetByFolhaTarefaAsync(Guid folhaTarefaId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, folha_tarefa_id, equipe_id, data FROM apropriacoes_horas WHERE folha_tarefa_id = @FtId ORDER BY data"; | |
| var rows = await Conn.QueryAsync<ApropriacaoHoras>( | |
| new CommandDefinition(sql, new { FtId = folhaTarefaId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<ApropriacaoHoras?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, folha_tarefa_id, equipe_id, data FROM apropriacoes_horas WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<ApropriacaoHoras>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> ExisteNaDataAsync(Guid folhaTarefaId, Guid equipeId, DateOnly data, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM apropriacoes_horas | |
| WHERE folha_tarefa_id = @FtId AND equipe_id = @EquipeId AND data = @Data | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { FtId = folhaTarefaId, EquipeId = equipeId, Data = data }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddComMembrosAsync(ApropriacaoHoras fah, IReadOnlyList<Guid> funcionarioIds, CancellationToken ct = default) | |
| { | |
| const string sqlFah = """ | |
| INSERT INTO apropriacoes_horas (id, folha_tarefa_id, equipe_id, data) | |
| VALUES (@Id, @FolhaTarefaId, @EquipeId, @Data) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlFah, new | |
| { | |
| fah.Id, fah.FolhaTarefaId, fah.EquipeId, fah.Data | |
| }, Tx, cancellationToken: ct)); | |
| const string sqlFunc = """ | |
| INSERT INTO apropriacao_funcionarios (id, apropriacao_horas_id, funcionario_id) | |
| VALUES (@Id, @FahId, @FuncionarioId) | |
| """; | |
| foreach (var fid in funcionarioIds) | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlFunc, new | |
| { | |
| Id = Guid.NewGuid(), FahId = fah.Id, FuncionarioId = fid | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<FahFuncionario>> GetFuncionariosAsync(Guid fahId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT af.id, af.funcionario_id, f.nome, f.matricula | |
| FROM apropriacao_funcionarios af | |
| JOIN funcionarios f ON f.id = af.funcionario_id | |
| WHERE af.apropriacao_horas_id = @FahId | |
| ORDER BY f.nome | |
| """; | |
| var rows = await Conn.QueryAsync<(Guid Id, Guid FuncionarioId, string Nome, string Matricula)>( | |
| new CommandDefinition(sql, new { FahId = fahId }, Tx, cancellationToken: ct)); | |
| return rows.Select(r => new FahFuncionario(r.Id, r.FuncionarioId, r.Nome, r.Matricula)).ToList(); | |
| } | |
| public async Task<Guid?> GetApropFuncionarioIdAsync(Guid fahId, Guid funcionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id FROM apropriacao_funcionarios | |
| WHERE apropriacao_horas_id = @FahId AND funcionario_id = @FuncionarioId | |
| """; | |
| var rows = (await Conn.QueryAsync<Guid>(new CommandDefinition( | |
| sql, new { FahId = fahId, FuncionarioId = funcionarioId }, Tx, cancellationToken: ct))).ToList(); | |
| return rows.Count == 0 ? null : rows[0]; | |
| } | |
| public async Task<int> ContarColetadosAsync(Guid fahId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT | |
| (SELECT COUNT(*) FROM apropriacao_funcionario_atividades a | |
| JOIN apropriacao_funcionarios af ON af.id = a.apropriacao_funcionario_id | |
| WHERE af.apropriacao_horas_id = @FahId) | |
| + | |
| (SELECT COUNT(*) FROM apropriacao_funcionario_motivos m | |
| JOIN apropriacao_funcionarios af ON af.id = m.apropriacao_funcionario_id | |
| WHERE af.apropriacao_horas_id = @FahId) | |
| """; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { FahId = fahId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task ReplaceHorasFuncionarioAsync(Guid apropFuncionarioId, | |
| IReadOnlyList<ApropFuncAtividade> atividades, IReadOnlyList<ApropFuncMotivo> motivos, CancellationToken ct = default) | |
| { | |
| await Conn.ExecuteAsync(new CommandDefinition( | |
| "DELETE FROM apropriacao_funcionario_atividades WHERE apropriacao_funcionario_id = @Id", | |
| new { Id = apropFuncionarioId }, Tx, cancellationToken: ct)); | |
| await Conn.ExecuteAsync(new CommandDefinition( | |
| "DELETE FROM apropriacao_funcionario_motivos WHERE apropriacao_funcionario_id = @Id", | |
| new { Id = apropFuncionarioId }, Tx, cancellationToken: ct)); | |
| const string sqlAtiv = """ | |
| INSERT INTO apropriacao_funcionario_atividades | |
| (id, apropriacao_funcionario_id, sub_atividade_padrao_id, horas_normais, horas_extras) | |
| VALUES (@Id, @ApropriacaoFuncionarioId, @SubAtividadePadraoId, @HorasNormais, @HorasExtras) | |
| """; | |
| foreach (var a in atividades) | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlAtiv, new | |
| { | |
| a.Id, a.ApropriacaoFuncionarioId, a.SubAtividadePadraoId, a.HorasNormais, a.HorasExtras | |
| }, Tx, cancellationToken: ct)); | |
| const string sqlMot = """ | |
| INSERT INTO apropriacao_funcionario_motivos | |
| (id, apropriacao_funcionario_id, motivo_hora_parada_id, sub_atividade_padrao_id, | |
| horas_paradas, horas_extras_paradas, detalhe) | |
| VALUES (@Id, @ApropriacaoFuncionarioId, @MotivoHoraParadaId, @SubAtividadePadraoId, | |
| @HorasParadas, @HorasExtrasParadas, @Detalhe) | |
| """; | |
| foreach (var m in motivos) | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlMot, new | |
| { | |
| m.Id, m.ApropriacaoFuncionarioId, m.MotivoHoraParadaId, m.SubAtividadePadraoId, | |
| m.HorasParadas, m.HorasExtrasParadas, m.Detalhe | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<ApropFuncAtividade>> GetAtividadesAsync(Guid apropFuncionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, apropriacao_funcionario_id, sub_atividade_padrao_id, horas_normais, horas_extras | |
| FROM apropriacao_funcionario_atividades WHERE apropriacao_funcionario_id = @Id | |
| """; | |
| var rows = await Conn.QueryAsync<ApropFuncAtividade>( | |
| new CommandDefinition(sql, new { Id = apropFuncionarioId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<IReadOnlyList<ApropFuncMotivo>> GetMotivosAsync(Guid apropFuncionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, apropriacao_funcionario_id, motivo_hora_parada_id, sub_atividade_padrao_id, | |
| horas_paradas, horas_extras_paradas, detalhe | |
| FROM apropriacao_funcionario_motivos WHERE apropriacao_funcionario_id = @Id | |
| """; | |
| var rows = await Conn.QueryAsync<ApropFuncMotivo>( | |
| new CommandDefinition(sql, new { Id = apropFuncionarioId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<IReadOnlyList<Guid>> GetFuncionarioIdsDaEquipeAsync(Guid equipeId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT funcionario_id FROM equipe_membros WHERE equipe_id = @Id"; | |
| var rows = await Conn.QueryAsync<Guid>( | |
| new CommandDefinition(sql, new { Id = equipeId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<bool> SubAtividadeExistsAsync(Guid subAtivId, CancellationToken ct = default) | |
| => await ExisteAsync("subativs_padrao", subAtivId, ct); | |
| public async Task<bool> MotivoExistsAsync(Guid motivoId, CancellationToken ct = default) | |
| => await ExisteAsync("motivos_hora_parada", motivoId, ct); | |
| public async Task<bool> FuncionarioEstaAtivoAsync(Guid funcionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM funcionarios WHERE id = @Id AND ativo = true"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = funcionarioId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> DeleteAsync(Guid fahId, CancellationToken ct = default) | |
| { | |
| await Conn.ExecuteAsync(new CommandDefinition(""" | |
| DELETE FROM apropriacao_funcionarios WHERE apropriacao_horas_id = @Id | |
| """, new { Id = fahId }, Tx, cancellationToken: ct)); | |
| var affected = await Conn.ExecuteAsync(new CommandDefinition( | |
| "DELETE FROM apropriacoes_horas WHERE id = @Id", new { Id = fahId }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| public async Task AddFuncionarioEmprestadoAsync(Guid fahId, Guid funcionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO apropriacao_funcionarios (id, apropriacao_horas_id, funcionario_id, emprestado) | |
| VALUES (@Id, @FahId, @FuncionarioId, true) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| Id = Guid.NewGuid(), FahId = fahId, FuncionarioId = funcionarioId | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<int> ContarColetadosDoFuncionarioAsync(Guid apropFuncionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT | |
| (SELECT COUNT(*) FROM apropriacao_funcionario_atividades WHERE apropriacao_funcionario_id = @Id) | |
| + | |
| (SELECT COUNT(*) FROM apropriacao_funcionario_motivos WHERE apropriacao_funcionario_id = @Id) | |
| """; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = apropFuncionarioId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> RemoveFuncionarioAsync(Guid apropFuncionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM apropriacao_funcionarios WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = apropFuncionarioId }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| public async Task<string?> GetObservacaoDiaAsync(Guid fahId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT observacao FROM observacoes_equipe_dia WHERE apropriacao_horas_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<string?>( | |
| new CommandDefinition(sql, new { Id = fahId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpsertObservacaoDiaAsync(Guid fahId, string observacao, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO observacoes_equipe_dia (id, apropriacao_horas_id, observacao) | |
| VALUES (@Id, @FahId, @Observacao) | |
| ON CONFLICT (apropriacao_horas_id) DO UPDATE SET observacao = @Observacao | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| Id = Guid.NewGuid(), FahId = fahId, Observacao = observacao | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<MotivoHoraParada>> GetMotivosCatalogoAsync(CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, descricao FROM motivos_hora_parada ORDER BY descricao"; | |
| var rows = await Conn.QueryAsync<MotivoHoraParada>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task AddMotivoAsync(MotivoHoraParada motivo, CancellationToken ct = default) | |
| { | |
| const string sql = "INSERT INTO motivos_hora_parada (id, descricao) VALUES (@Id, @Descricao)"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new { motivo.Id, motivo.Descricao }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteMotivoAsync(Guid motivoId, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM motivos_hora_parada WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = motivoId }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| 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; | |
| } | |
| } | |