c9620eaf95
- 2 viste di partenza - gestione cache ottimizzata - gestione calcolo ottimizzata
244 lines
7.7 KiB
C#
244 lines
7.7 KiB
C#
using EgwCoreLib.Razor.Data;
|
|
using Microsoft.AspNetCore.Components;
|
|
using NLog;
|
|
using NLog.Fluent;
|
|
using SHERPA.BBM.CORE.DTO;
|
|
using SHERPA.BBM.UI.Data;
|
|
using System.Diagnostics;
|
|
using static EgwCoreLib.Razor.Data.chartJsData;
|
|
|
|
namespace SHERPA.BBM.UI.Components
|
|
{
|
|
public partial class MonthStats
|
|
{
|
|
#region Public Enums
|
|
|
|
public enum StatAvail
|
|
{
|
|
Trattato
|
|
, Ordinato
|
|
, Fatturato
|
|
//, Incassato
|
|
}
|
|
|
|
#endregion Public Enums
|
|
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public StatAvail StatReq
|
|
{
|
|
get => statReq;
|
|
set
|
|
{
|
|
if (statReq != value)
|
|
{
|
|
statReq = value;
|
|
// reload async/sync...
|
|
var pUpd = Task.Run(async () => await ReloadData());
|
|
pUpd.Wait();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Properties
|
|
|
|
[Inject]
|
|
protected BBM_EFService BBMService { get; set; } = null!;
|
|
|
|
protected List<string>? ChartLabels { get; set; } = null;
|
|
|
|
protected string ChartTitle
|
|
{
|
|
get => ShowCharts ? "Nascondi Grafici" : "Mostra Grafici";
|
|
}
|
|
|
|
protected string CssToggleChart
|
|
{
|
|
get => ShowCharts ? "btn-secondary" : "btn-primary";
|
|
}
|
|
|
|
protected string cumSumMsg
|
|
{
|
|
get => showCumSum ? "Cumulato" : "Puntuale";
|
|
}
|
|
|
|
protected List<chartJsData.chartJsDataSetXY>? MonthlyDS { get; set; } = null;
|
|
|
|
protected bool ShowCharts { get; set; } = false;
|
|
|
|
protected bool ShowCumSum
|
|
{
|
|
get => showCumSum;
|
|
set
|
|
{
|
|
if (showCumSum != value)
|
|
{
|
|
showCumSum = value;
|
|
// reload async/sync...
|
|
var pUpd = Task.Run(async () => await ReloadData());
|
|
pUpd.Wait();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task ToggleGraph()
|
|
{
|
|
ShowCharts = !ShowCharts;
|
|
await ReloadData();
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private bool showCumSum = true;
|
|
|
|
private List<int> yearsList = new List<int>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private Dictionary<int, List<MonthStatDTO>>? MonthStatRecords { get; set; } = null;
|
|
private StatAvail statReq { get; set; } = StatAvail.Ordinato;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
MonthStatRecords = new Dictionary<int, List<MonthStatDTO>>();
|
|
MonthlyDS = new List<chartJsData.chartJsDataSetXY>();
|
|
ChartLabels = new List<string>();
|
|
// fare recupero anni x filtro...
|
|
yearsList = await BBMService.NegotYears();
|
|
// preparo stats annuali
|
|
List<MonthStatDTO> yearData = new List<MonthStatDTO>();
|
|
// predispongo colori: anno corrente è verde, gli altri sono azzurri e più trasparenti
|
|
// man mano vanno indietro
|
|
string greenBordCol = "rgba(11, 200, 44, 0.9)";
|
|
string greenBackCol = "rgba(33, 222, 66, 0.1)";
|
|
string cBordCol = "";
|
|
string cBackCol = "";
|
|
int anno = DateTime.Today.Year;
|
|
int delta = 1;
|
|
int num = 10;
|
|
int numBord = 7;
|
|
int numBack = 4;
|
|
int numYears = yearsList.Count;
|
|
int deltaCol = 10;
|
|
int rCol = 10;
|
|
int gCol = 22;
|
|
int bCol = 245;
|
|
string alpha = "";
|
|
if (ShowCharts)
|
|
{
|
|
for (int iMese = 1; iMese <= 12; iMese++)
|
|
{
|
|
ChartLabels.Add($"{iMese:00}");
|
|
}
|
|
}
|
|
foreach (var i in yearsList)
|
|
{
|
|
switch (statReq)
|
|
{
|
|
case StatAvail.Trattato:
|
|
if (showCumSum)
|
|
{
|
|
yearData = await BBMService.MonthCumSumTrattato(i);
|
|
}
|
|
else
|
|
{
|
|
yearData = await BBMService.MonthDetailTrattato(i);
|
|
}
|
|
break;
|
|
|
|
case StatAvail.Fatturato:
|
|
if (showCumSum)
|
|
{
|
|
yearData = await BBMService.MonthCumSumImporto(i);
|
|
}
|
|
else
|
|
{
|
|
yearData = await BBMService.MonthDetailImporto(i);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
case StatAvail.Ordinato:
|
|
if (showCumSum)
|
|
{
|
|
yearData = await BBMService.MonthCumSumOrdinato(i);
|
|
}
|
|
else
|
|
{
|
|
yearData = await BBMService.MonthDetailOrdinato(i);
|
|
}
|
|
break;
|
|
}
|
|
MonthStatRecords.Add(i, yearData);
|
|
// verifico se servono dati x grafici...
|
|
if (ShowCharts)
|
|
{
|
|
List<chartJsXY> currData = yearData.Select(x => new chartJsXY
|
|
{
|
|
x = x.Month,
|
|
y = (int)x.Value
|
|
}).ToList();
|
|
|
|
// in base all'anno decido i colori...
|
|
if (i == anno)
|
|
{
|
|
delta = 0;
|
|
cBordCol = greenBordCol;
|
|
cBackCol = greenBackCol;
|
|
}
|
|
else
|
|
{
|
|
// ogni anno è 0.1 meno...
|
|
delta = anno - i;
|
|
num = numBord - delta > 1 ? numBord - delta : 1;
|
|
alpha = $"{(float)(num) / 10:N1}".Replace(",", ".");
|
|
rCol = deltaCol + (255 - deltaCol) * delta / numYears;
|
|
bCol = 255 - (255 - deltaCol) * delta / numYears;
|
|
cBordCol = $"rgba({rCol},{gCol},{bCol},{alpha})";
|
|
num = numBack - delta > 1 ? numBack - delta : 1;
|
|
alpha = $"{(float)(num) / 10:N1}".Replace(",", ".");
|
|
cBackCol = $"rgba({rCol},{gCol},{bCol},{alpha})";
|
|
}
|
|
chartJsData.chartJsDataSetXY currChart = new chartJsData.chartJsDataSetXY()
|
|
{
|
|
label = $"{i}",
|
|
data = currData,
|
|
borderColor = cBordCol,
|
|
backgroundColor = cBackCol,
|
|
lineTension = 0,
|
|
stepped = false,
|
|
fill = delta == 0 ? "start" : "false"
|
|
};
|
|
MonthlyDS.Add(currChart);
|
|
}
|
|
}
|
|
sw.Stop();
|
|
Log.Info($"Eseguito MonthStats.ReloadData in {sw.Elapsed.TotalMilliseconds}ms");
|
|
}
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |