Spaces:
Runtime error
Runtime error
File size: 3,365 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 | using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.Planejamento;
namespace SCM.Infrastructure.Persistence.Repositories;
public sealed class FolhaTarefaItemRepository : IFolhaTarefaItemRepository
{
private readonly IUnitOfWork _uow;
public FolhaTarefaItemRepository(IUnitOfWork uow) => _uow = uow;
private IDbConnection Conn => _uow.Connection;
private IDbTransaction? Tx => _uow.CurrentTransaction;
private const string Columns = """
id, folha_tarefa_id, item_programado_id, qt_programada, qt_programada_prox,
prioridade, observacao, data_prev_ini, data_prev_fim, data_prev_ini_prox, data_prev_fim_prox
""";
public async Task<IReadOnlyList<FolhaTarefaItem>> GetByFolhaTarefaAsync(Guid folhaTarefaId, CancellationToken ct = default)
{
const string sql = $"SELECT {Columns} FROM folha_tarefa_itens WHERE folha_tarefa_id = @FtId ORDER BY prioridade, id";
var rows = await Conn.QueryAsync<FolhaTarefaItem>(
new CommandDefinition(sql, new { FtId = folhaTarefaId }, Tx, cancellationToken: ct));
return rows.ToList();
}
public async Task<decimal?> GetQuantidadePrevistaAsync(Guid itemProgramadoId, CancellationToken ct = default)
{
const string sql = "SELECT quantidade FROM itens_programados WHERE id = @Id";
var rows = (await Conn.QueryAsync<decimal>(
new CommandDefinition(sql, new { Id = itemProgramadoId }, Tx, cancellationToken: ct))).ToList();
return rows.Count == 0 ? null : rows[0];
}
public async Task<decimal> GetTotalProgramadoOutrasFtsAsync(Guid itemProgramadoId, Guid folhaTarefaId, CancellationToken ct = default)
{
const string sql = """
SELECT COALESCE(SUM(qt_programada + qt_programada_prox), 0)
FROM folha_tarefa_itens
WHERE item_programado_id = @ItemId AND folha_tarefa_id <> @FtId
""";
return await Conn.ExecuteScalarAsync<decimal>(new CommandDefinition(
sql, new { ItemId = itemProgramadoId, FtId = folhaTarefaId }, Tx, cancellationToken: ct));
}
public async Task ReplaceAsync(Guid folhaTarefaId, IReadOnlyList<FolhaTarefaItem> itens, CancellationToken ct = default)
{
const string sqlDel = "DELETE FROM folha_tarefa_itens WHERE folha_tarefa_id = @FtId";
await Conn.ExecuteAsync(new CommandDefinition(sqlDel, new { FtId = folhaTarefaId }, Tx, cancellationToken: ct));
const string sqlIns = """
INSERT INTO folha_tarefa_itens
(id, folha_tarefa_id, item_programado_id, qt_programada, qt_programada_prox,
prioridade, observacao, data_prev_ini, data_prev_fim, data_prev_ini_prox, data_prev_fim_prox)
VALUES
(@Id, @FolhaTarefaId, @ItemProgramadoId, @QtProgramada, @QtProgramadaProx,
@Prioridade, @Observacao, @DataPrevIni, @DataPrevFim, @DataPrevIniProx, @DataPrevFimProx)
""";
foreach (var i in itens)
await Conn.ExecuteAsync(new CommandDefinition(sqlIns, new
{
i.Id, i.FolhaTarefaId, i.ItemProgramadoId, i.QtProgramada, i.QtProgramadaProx,
i.Prioridade, i.Observacao, i.DataPrevIni, i.DataPrevFim, i.DataPrevIniProx, i.DataPrevFimProx
}, Tx, cancellationToken: ct));
}
}
|