Files
2026-05-04 14:56:56 +02:00

327 lines
9.0 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DbModels;
using MP.Data.Services;
using MP.Stats.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Stats.Pages
{
public partial class Energy : ComponentBase, IDisposable
{
#region Public Methods
public void Dispose()
{
MServ.EA_SearchUpdated -= OnSeachUpdated;
ListRecords.Clear();
SearchRecords.Clear();
}
public async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
Task task = UpdateData();
StateHasChanged();
});
}
#endregion Public Methods
#region Protected Fields
protected string fileName = "ODL_Energy.csv";
protected List<AutocompleteModel> ListMacchine = new List<AutocompleteModel>();
protected int NumMacc = 0;
protected int TotalCount = 0;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; }
/// <summary>
/// Verifica validit selezione macchine
/// </summary>
protected bool MaccSelValid
{
get => currFilter.MaccSelValid;
}
[Inject]
protected Data.MessageService MServ { get; set; }
[Inject]
protected NavigationManager NavManager { get; set; }
[Inject]
protected MpStatsService StatService { get; set; }
[Inject]
protected TranslateSrv TradService { get; set; }
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Verifica: se nullo il valore pz totali --> current --> indico non confermato
/// </summary>
/// <param name="testRec"></param>
/// <returns></returns>
protected string CheckCurrent(OdlEnergyModel testRec)
{
string answ = "";
if (testRec != null)
{
try
{
answ = (testRec.TotCount01 + testRec.TotCount02 + testRec.TotCount03) == 0 || testRec.DataFine == null ? "text-secondary" : "";
//answ = (testRec.TotCount01 + testRec.TotCount02 + testRec.TotCount03) == 0 || testRec.DataFine == null ? "table-info opacity-75" : "";
}
catch
{ }
}
return answ;
}
protected async Task DoFilter(SelectData newFilter)
{
clearFile();
SearchRecords = null;
ListRecords = null;
currFilter = newFilter;
await ReloadData();
}
/// <summary>
/// recupero UM del flusso indicato
/// </summary>
/// <param name="CodFlux"></param>
/// <returns></returns>
protected string GetUM(string CodFlux)
{
return StatService.FluxGetUM(CodFlux);
}
protected override async Task OnInitializedAsync()
{
isLoading = true;
clearFile();
MServ.ShowSearch = false;
MServ.PageName = "ENERGY";
MServ.PageIcon = "fa fa-chart-column";
MServ.EA_SearchUpdated += OnSeachUpdated;
await LoadConfData();
await ReloadData();
isLoading = false;
}
protected void ResetData()
{
clearFile();
StatService.rollBackEdit(currRecord);
currRecord = null;
}
protected async Task ResetFilter(SelectData newFilter)
{
clearFile();
currRecord = null;
SearchRecords = null;
ListRecords = null;
currFilter = SelectData.Init(5, 7);
await ReloadData();
}
protected decimal righDiv(decimal num, decimal den)
{
if (den == 0)
{
den = 1;
}
decimal answ = num / den;
return answ;
}
protected void SetNumRec(int newNum)
{
numRecord = newNum;
DisplayData();
}
protected void SetPage(int newNum)
{
currPage = newNum;
DisplayData();
}
/// <summary>
/// Traduzione lemma richeisto (lingua default="IT")
/// </summary>
/// <param name="Lemma"></param>
/// <returns></returns>
protected string Traduci(string Lemma)
{
return TradService.Traduci(Lemma);
}
protected async Task UpdateData()
{
currRecord = null;
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private List<ConfigModel> ConfigList;
private OdlEnergyModel currRecord = null;
private bool DynMode = true;
private List<OdlEnergyModel> ListRecords;
private int MaxDisplay = 20;
private int numCount = 1;
private int numTotEn = 1;
private List<OdlEnergyModel> SearchRecords;
#endregion Private Fields
#region Private Properties
private SelectData currFilter
{
get
{
return MServ.EnergyOdl_Filter;
}
set
{
MServ.EnergyOdl_Filter = value;
}
}
private int currPage { get; set; } = 1;
private string fullPath
{
get => $"{Directory.GetCurrentDirectory()}\\temp\\{fileName}";
}
private bool isLoading { get; set; } = false;
private int numRecord { get; set; } = 10;
#endregion Private Properties
#region Private Methods
private async void clearFile()
{
await Task.Run(() => File.Delete(fullPath));
}
private string confVal(string chiave)
{
string answ = "";
if (ConfigList != null && ConfigList.Count > 0)
{
var searchRec = ConfigList.First(x => x.Chiave == chiave);
if (searchRec != null)
{
answ = searchRec.Valore;
}
}
return answ;
}
private void DisplayData()
{
ListRecords = SearchRecords
.OrderByDescending(x => x.DataInizio)
.ThenBy(x => x.IdxMacchina)
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
private async Task ExportCsv()
{
isLoading = true;
// salvo davvero!
await Egw.Core.Utils.SaveToCsv(SearchRecords, fullPath, ';');
isLoading = false;
}
private async Task LoadConfData()
{
isLoading = true;
ConfigList = await StatService.ConfigGetAll();
ListMacchine = await StatService.MachineList(true);
NumMacc = ListMacchine.Count();
DynMode = confVal("STATS_EnergyMode") == "Dynamic";
int.TryParse(confVal("STATS_EnergyNumCount"), out numCount);
int.TryParse(confVal("STATS_EnergyNumTotEn"), out numTotEn);
int.TryParse(confVal("STATS_EnergyMaxSelect"), out MaxDisplay);
}
private async Task ReloadData()
{
isLoading = true;
if (SearchRecords != null)
{
SearchRecords.Clear();
}
else
{
SearchRecords = new List<OdlEnergyModel>();
}
// se seleziona tutte...
if (currFilter.ListIdxMaccSel != null && currFilter.ListIdxMaccSel.Count > 0)
{
foreach (var idxMacc in currFilter.ListIdxMaccSel)
{
currFilter.IdxMacchina = idxMacc;
var tempRec = await StatService.StatOdlEnergyGetAll(currFilter, MServ.SearchVal);
SearchRecords.AddRange(tempRec);
tempRec = null;
}
}
TotalCount = SearchRecords.Count;
DisplayData();
isLoading = false;
}
/// <summary>
/// Elimina da dict macchina indicata
/// </summary>
/// <param name="idxMacc"></param>
private async Task RemoveMachine(string idxMacc)
{
// in primis se contiene spazio --> prendo prima aprte che idxMacc...
if (idxMacc.Contains(" "))
{
var sVals = idxMacc.Split(' ');
idxMacc = sVals[0];
}
if (currFilter.ListIdxMaccSel.Contains(idxMacc))
{
currFilter.ListIdxMaccSel.Remove(idxMacc);
GC.Collect();
await ReloadData();
}
}
#endregion Private Methods
}
}