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; } public async void OnSeachUpdated() { await InvokeAsync(() => { Task task = UpdateData(); StateHasChanged(); }); } #endregion Public Methods #region Protected Fields protected string fileName = "ODL_Energy.csv"; #endregion Protected Fields #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } [Inject] protected Data.MessageService MServ { get; set; } [Inject] protected NavigationManager NavManager { get; set; } [Inject] protected MpStatsService StatService { get; set; } protected int totalCount { get { int answ = 0; if (SearchRecords != null) { answ = SearchRecords.Count; } return answ; } } [Inject] protected TranslateSrv TradService { get; set; } #endregion Protected Properties #region Protected Methods protected string checkSelect(int IdxODL) { string answ = ""; if (currRecord != null) { try { answ = (currRecord.IdxOdl == IdxODL) ? "table-info" : ""; } catch { } } return answ; } protected async Task DoFilter(SelectData newFilter) { clearFile(); SearchRecords = null; ListRecords = null; currFilter = newFilter; await ReloadData(); } /// /// recupero UM del flusso indicato /// /// /// protected string GetUM(string CodFlux) { return StatService.FluxGetUM(CodFlux); } protected override async Task OnInitializedAsync() { clearFile(); numRecord = 10; MServ.ShowSearch = false; MServ.PageName = "ENERGY"; MServ.PageIcon = "oi oi-bar-chart"; MServ.EA_SearchUpdated += OnSeachUpdated; await LoadConfData(); await ReloadData(); } 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(); } protected async Task ToggleChart(bool doShow) { ShowCharts = !ShowCharts; if (ShowCharts) { await ReloadData(); } } /// /// Traduzione lemma richeisto (lingua default="IT") /// /// /// 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 ConfigList; private OdlEnergyModel currRecord = null; private bool dynMode = false; private List ListRecords; private int numCount = 1; private int numTotEn = 1; private List SearchRecords; #endregion Private Fields #region Private Properties private SelectData currFilter { get { return MServ.ODL_Filter; } set { MServ.ODL_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; private bool ShowCharts { get; set; } = false; #endregion Private Properties #region Private Methods private async void clearFile() { await Task.Run(() => File.Delete(fullPath)); } private string convVal(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 .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() { ConfigList = await StatService.ConfigGetAll(); dynMode = convVal("STATS_EnergyMode") == "Dynamic"; var sNumCount = convVal("STATS_EnergyNumCount"); int.TryParse(convVal("STATS_EnergyNumCount"), out numCount); int.TryParse(convVal("STATS_EnergyNumTotEn"), out numTotEn); } private async Task ReloadData() { SearchRecords = await StatService.StatOdlEnergyGetAll(currFilter, MServ.SearchVal); DisplayData(); } #endregion Private Methods } }