using Microsoft.AspNetCore.Components; using MP.Data; using MP.SPEC.Data; using Newtonsoft.Json; using NLog; namespace MP.SPEC.Components { public partial class AskCloseOdl : IDisposable { #region Public Methods public void Dispose() { MDService.BroadastMsgPipe.EA_NewMessage -= BroadastMsgPipe_EA_NewMessage; GC.Collect(); } #endregion Public Methods #region Protected Fields protected DateTime lastRec = DateTime.Now.AddMinutes(-1); protected int RefreshPeriod = 2000; #endregion Protected Fields #region Protected Properties protected DisplayAction CurrAction { get; set; } = new DisplayAction(); [Inject] protected MpDataService MDService { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected async Task doCancel() { checkActionNull(); // verifico il tipo di richiesta... if (!string.IsNullOrEmpty(CurrAction.CancelAction)) { switch (CurrAction.CancelAction) { case "DisableAction": case "Reset": default: //// resetto parametro //CurrAction.Parameter = ""; break; } } // eseguo chiusura finale CurrAction.IsActive = false; MDService.ActionSetReqAsync(CurrAction); await Task.Delay(1); } protected async Task doConfirm() { checkActionNull(); bool fatto = false; // verifico il tipo di richiesta... if (!string.IsNullOrEmpty(CurrAction.ConfirmAction)) { switch (CurrAction.ConfirmAction) { case "CloseODL": // chiudo ODL int idxOdl = 0; int.TryParse(CurrAction.Parameter, out idxOdl); if (idxOdl > 0) { DateTime oggi = DateTime.Today; // recupero idxMaccSel x ODL... var elencoOdl = await MDService.OdlListGetFiltAsync(true, "*", "*", "*", "*", DateTime.Today.AddMonths(-1), DateTime.Today.AddDays(1)); var currOdl = elencoOdl.FirstOrDefault(x => x.IdxOdl == idxOdl); if (currOdl != null && currOdl.IdxOdl == idxOdl) { // effettua chiusura sul DB fatto = await MDService.ODLCloseAsync(idxOdl, currOdl.IdxMacchina, 0, true); if (fatto) { Log.Info($"Effettuata chiusura ODL {idxOdl}"); CurrAction.Parameter = ""; // elimino richiesta CurrAction.IsActive = false; } } else { Log.Error($"Errore in doConfirm chiusura ODL: non trovato ODL per idxOdl {idxOdl}"); } } // resetto parametro break; default: break; } } MDService.ActionSetReqAsync(CurrAction); await Task.Delay(1); // se fatto --> ricarico! if (fatto) { // rimando a pagina corrente NavManager.NavigateTo(NavManager.Uri, true); } } protected override async Task OnInitializedAsync() { await reloadData(); MDService.BroadastMsgPipe.EA_NewMessage += BroadastMsgPipe_EA_NewMessage; } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Private Properties [Inject] private NavigationManager NavManager { get; set; } = null!; #endregion Private Properties #region Private Methods private void BroadastMsgPipe_EA_NewMessage(object? sender, EventArgs e) { bool needReload = false; DateTime adesso = DateTime.Now; PubSubEventArgs currArgs = (PubSubEventArgs)e; if (!string.IsNullOrEmpty(currArgs.newMessage)) { lastRec = adesso; try { var result = JsonConvert.DeserializeObject(currArgs.newMessage); if (result != null) { // se da visibile apssa ad hidden --> segnalo serve reload... needReload = (CurrAction.IsActive && !result.IsActive); CurrAction = result; } } catch { } if (needReload) { Task.Delay(500); NavManager.NavigateTo(NavManager.Uri, true); } else { InvokeAsync(() => { StateHasChanged(); }); } } } private void checkActionNull() { if (CurrAction == null) { CurrAction = new DisplayAction() { Topic = "Chiusura ODL", Message = "Rilevato possibile fine operazioni, Vuoi chiudere la commessa?", ShowCancel = true, ShowClose = true, ShowConfirm = true, CancelAction = "DisableAction", ConfirmAction = "CloseODL", DtReq = DateTime.Now, IsActive = true }; } } private async Task reloadData() { CurrAction = await MDService.ActionGetReq(); if (CurrAction != null) { await InvokeAsync(() => StateHasChanged()); } } #endregion Private Methods } }