@page "/Estadisticas"
@inject NavigationManager Navigation
@layout ConfiguracionLayout
@using Microsoft.AspNetCore.WebUtilities
@using BlazorBootstrap
@using System.Globalization
@using bdHerramientaCACOA.db
@rendermode InteractiveServer
@inject IJSRuntime JS
@using ClosedXML.Excel
@using Microsoft.EntityFrameworkCore
@if (!string.IsNullOrEmpty(mensajeError))
{
}
@code {
public List listadoHerramientaEstadistica { get; set; } = new List();
public List listadoLicitacionesEstadistica { get; set; } = new List();
public List listadoHerramientaEstadisticaUrban { get; set; } = new List();
public List listadoLicitacionesEstadisticaUrban { get; set; } = new List();
public List listadoHerramientaEstadisticaUsos { get; set; } = new List();
public List listadoLicitacionUsos { get; set; } = new List();
public List listadoHerramientaEstadisticaFactores { get; set; } = new List();
public DateTime? fechaInicio { get; set; } = null;
public DateTime? fechaFinal { get; set; } = null;
public EstadistiaHerraCasaVM estadisticasTotales = new EstadistiaHerraCasaVM();
private int totalProyectosTipo = 0;
private string mensajeError = "";
public tsHerramientasCACOA bd;
List mensajes = new List();
private async Task OnFechaInicioChanged(DateTime? value)
{
fechaInicio = value;
if (UtilidadesCASA.comprobarFechasNulas(fechaInicio, fechaFinal))
{
if (UtilidadesCASA.comprobarFechas((DateTime)fechaInicio, (DateTime)fechaFinal))
{
inicializarListado();
mensajeError = "";
}
else
{
mensajeError = "La fecha final no puede ser menor que la fecha inicial.";
}
}
else
{
inicializarListado();
mensajeError = "";
}
}
public bool IsVisible(bdHerramientaCACOA.db.estadisticas listado)
{
DateTime? ultimaFecha;
if (listado.idSimulacionNavigation.FechaModificacion != null)
{
ultimaFecha = listado.idSimulacionNavigation.FechaModificacion;
}
else
{
ultimaFecha = listado.idSimulacionNavigation.FechaCreacion;
}
if (fechaFinal != null && listado.idSimulacionNavigation.FechaCreacion.Date >= fechaInicio?.Date && ultimaFecha?.Date <= fechaFinal?.Date)
{
return true;
}
return false;
}
private async Task OnFechaFinalChanged(DateTime? value)
{
fechaFinal = value;
if (UtilidadesCASA.comprobarFechasNulas(fechaInicio, fechaFinal))
{
if (UtilidadesCASA.comprobarFechas((DateTime)fechaInicio, (DateTime)fechaFinal))
{
inicializarListado();
mensajeError = "";
}
else
{
mensajeError = "La fecha final no puede ser menor que la fecha inicial.";
}
}
else
{
inicializarListado();
mensajeError = "";
}
}
public async Task generarExcel(byte[] arrayListados)
{
try
{
bool fechaValida = false;
string nombreExcel = "";
if ((fechaInicio == null && fechaFinal == null) || (UtilidadesCASA.comprobarFechasNulas(fechaInicio, fechaFinal) && UtilidadesCASA.comprobarFechas((DateTime)fechaInicio, (DateTime)fechaFinal)))
{
fechaValida = true;
}
if (fechaValida)
{
if (UtilidadesCASA.comprobarFechasNulas(fechaInicio, fechaFinal))
{
nombreExcel = "Simulaciones_" + (fechaInicio ?? DateTime.Now).Date.ToString() + (fechaFinal ?? DateTime.Now).Date.ToString() + ".xlsx";
}
else
{
nombreExcel = "SimulacionesTotales_" + DateTime.Now.ToString() + ".xlsx";
}
var wb = new XLWorkbook();
wb.Properties.Author = "Tecnosis";
wb.Properties.Title = "Datos entre fechas";
wb.Properties.Subject = "";
var ws = wb.Worksheets.Add("Total datos");
var bytes = arrayListados;
await JS.InvokeVoidAsync("descargarExcel", nombreExcel, Convert.ToBase64String(bytes));
mensajes.Add(new ToastMessage
{
Type = ToastType.Primary,
Message = $"Descarga completada.",
});
}
else
{
mensajes.Add(new ToastMessage
{
Type = ToastType.Warning,
Message = $"Fechas incorrectas.",
});
}
}
catch (Exception ex)
{
mensajes.Add(new ToastMessage
{
Type = ToastType.Danger,
Message = $"Error al generar el excel.",
});
throw new Exception("Ocurrió un error en la descarga del excel -> " + ex.Message);
}
}
protected override async Task OnInitializedAsync()
{
bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);
inicializarListado();
}
private void inicializarListado()
{
if (fechaInicio == null)
{
listadoHerramientaEstadistica = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 31).ToList();
listadoLicitacionesEstadistica = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 32).ToList();
listadoHerramientaEstadisticaUrban = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 34).ToList();
listadoLicitacionesEstadisticaUrban = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 35).ToList();
}
else
{
var listadoHerraCASA = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 31).ToList();
var listadoLicitacionCASA = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 32).ToList();
listadoHerramientaEstadistica = listadoHerraCASA.Where(x => (x.idSimulacionNavigation.FechaCreacion >= fechaInicio) && (x.idSimulacionNavigation.FechaModificacion ?? x.idSimulacionNavigation.FechaCreacion).Date <= fechaFinal).ToList();
listadoLicitacionesEstadistica = listadoHerraCASA.Where(x => (x.idSimulacionNavigation.FechaCreacion >= fechaInicio) && (x.idSimulacionNavigation.FechaModificacion ?? x.idSimulacionNavigation.FechaCreacion).Date <= fechaFinal).ToList();
var listadoPorTipoURBAN = bd.estadisticas.Include(x => x.idSimulacionNavigation).Where(x => x.idSimulacionNavigation.idTipoSimulacion == 34 || x.idSimulacionNavigation.idTipoSimulacion == 35).ToList();
listadoHerramientaEstadisticaUrban = listadoPorTipoURBAN.Where(x => (x.idSimulacionNavigation.FechaCreacion >= fechaInicio) && (x.idSimulacionNavigation.FechaModificacion ?? x.idSimulacionNavigation.FechaCreacion).Date <= fechaFinal).ToList();
}
listadoHerramientaEstadisticaUsos = bd.estadisticasusos.AsEnumerable().Where(x => listadoHerramientaEstadistica.Any(y => y.idSimulacion == x.idSimulacion)).ToList();
listadoLicitacionUsos = bd.estadisticasusos.AsEnumerable().Where(x => listadoLicitacionesEstadistica.Any(y => y.idSimulacion == x.idSimulacion)).ToList();
listadoHerramientaEstadisticaFactores = bd.estadisticasfactores.AsEnumerable().Where(x => listadoHerramientaEstadistica.Any(y => y.idSimulacion == x.idsimulacion)).ToList();
}
}