Continuación de CRUDs e inicio de Reportes.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using GestionIntegral.Api.Dtos.Reportes; // Para ExistenciaPapelDto
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GestionIntegral.Api.Data.Repositories.Reportes
|
||||
{
|
||||
public interface IReportesRepository
|
||||
{
|
||||
Task<IEnumerable<ExistenciaPapelDto>> GetExistenciaPapelAsync(DateTime fechaDesde, DateTime fechaHasta, int? idPlanta, bool consolidado);
|
||||
// ... Aquí irán los métodos para otros reportes ...
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Dapper;
|
||||
using GestionIntegral.Api.Dtos.Reportes;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GestionIntegral.Api.Data.Repositories.Reportes
|
||||
{
|
||||
public class ReportesRepository : IReportesRepository
|
||||
{
|
||||
private readonly DbConnectionFactory _dbConnectionFactory;
|
||||
private readonly ILogger<ReportesRepository> _logger;
|
||||
|
||||
public ReportesRepository(DbConnectionFactory dbConnectionFactory, ILogger<ReportesRepository> logger)
|
||||
{
|
||||
_dbConnectionFactory = dbConnectionFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ExistenciaPapelDto>> GetExistenciaPapelAsync(DateTime fechaDesde, DateTime fechaHasta, int? idPlanta, bool consolidado)
|
||||
{
|
||||
string spName = consolidado ? "dbo.SP_ConsumoBobinasConsolidado" : "dbo.SP_ConsumoBobinas";
|
||||
var parameters = new DynamicParameters();
|
||||
|
||||
parameters.Add("FechaDesde", fechaDesde, DbType.Date);
|
||||
parameters.Add("FechaHasta", fechaHasta, DbType.Date); // SP_ConsumoBobinasConsolidado solo usa estas dos
|
||||
|
||||
if (!consolidado)
|
||||
{
|
||||
if (!idPlanta.HasValue)
|
||||
{
|
||||
_logger.LogError("idPlanta es requerido para el reporte de existencia de papel no consolidado.");
|
||||
// Podríamos lanzar una ArgumentNullException o devolver una lista vacía.
|
||||
// Por ahora, el servicio debería validar esto antes.
|
||||
// Para el repositorio, asumimos que si no es consolidado, idPlanta viene.
|
||||
throw new ArgumentNullException(nameof(idPlanta), "El ID de planta es requerido para el reporte no consolidado.");
|
||||
}
|
||||
parameters.Add("@idPlanta", idPlanta.Value, DbType.Int32);
|
||||
|
||||
}
|
||||
// Si los SPs realmente necesitaran @DiasPeriodo, lo calcularíamos así:
|
||||
// int diasPeriodo = (fechaHasta - fechaDesde).Days + 1; // +1 para incluir ambos días
|
||||
// parameters.Add("DiasPeriodo", diasPeriodo, DbType.Int32);
|
||||
|
||||
|
||||
_logger.LogInformation("Ejecutando SP: {SPName} con parámetros: FechaDesde={FechaDesde}, FechaHasta={FechaHasta}, IdPlanta={IdPlanta}, Consolidado={Consolidado}",
|
||||
spName, fechaDesde, fechaHasta, idPlanta, consolidado);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = _dbConnectionFactory.CreateConnection();
|
||||
var result = await connection.QueryAsync<ExistenciaPapelDto>(spName, parameters, commandType: CommandType.StoredProcedure);
|
||||
_logger.LogInformation("SP {SPName} ejecutado, {Count} filas devueltas.", spName, result.Count());
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error al ejecutar SP {SPName} para Existencia de Papel.", spName);
|
||||
return Enumerable.Empty<ExistenciaPapelDto>();
|
||||
}
|
||||
}
|
||||
|
||||
// ... Implementaciones para otros reportes ...
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user