using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Core.DTO; using MP.Data.DbModels; using MP.SPEC.Data; using Newtonsoft.Json; namespace MP.SPEC.Components { public partial class ListDossiers : IDisposable { #region Public Properties [Parameter] public EventCallback RecordSel { get; set; } [Parameter] public EventCallback RecordSelFlux { get; set; } [Parameter] public SelectDossierParams SelFilter { get; set; } = null!; [Parameter] public EventCallback TotRecordChanged { get; set; } #endregion Public Properties #region Public Methods public string checkSelect(DossierModel recordSel) { string answ = ""; if (currRecord != null) { try { answ = (currRecord.IdxMacchina == recordSel.IdxMacchina && currRecord.DtRif == recordSel.DtRif) ? "table-info" : ""; } catch { } } return answ; } public string checkSelPar(FluxLogDTO recordSel) { string answ = ""; if (currFluxLogDto != null) { try { answ = (currFluxLogDto.CodFlux == recordSel.CodFlux && currFluxLogDto.dtEvento == recordSel.dtEvento) ? "table-info" : ""; } catch { } } return answ; } public void Dispose() { currRecord = null; SearchRecords = null; ListRecords = null; GC.Collect(); } public async Task flushCache() { await MDService.ForceFlushRedisCache(); await MDService.ForceFlushFusionCacheAsync(); // rimando a pagina corrente NavManager.NavigateTo(NavManager.Uri, true); } #endregion Public Methods #region Protected Fields protected string giacenzeConf = "false"; protected string selAzienda = "*"; #endregion Protected Fields #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected MpDataService MDService { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected async Task cancel() { var alert = await JSRuntime.InvokeAsync("confirm", "Confermi di voler annullare TUTTE le modifiche? i dati saranno ricaricati."); if (alert) { currFluxLogDto = null; isEditing = false; await Task.Delay(1); if (currRecord != null) { listaFlux = MDService.FluxLogDtoConvert(currRecord.Valore); } StateHasChanged(); } } protected async Task cancelNewDoss() { var alert = await JSRuntime.InvokeAsync("confirm", "Confermi di voler annullare l'aggiunta di un nuovo dossier? i dati saranno ricaricati."); if (alert) { currRecordClone = null; isEditing = false; await Task.Delay(1); StateHasChanged(); } } protected async Task cloneRecord(DossierModel selRec) { // creo record duplicato... DossierModel newRec = new DossierModel() { //IdxDossier = 0, DataType = selRec.DataType, KeyRichiesta = selRec.KeyRichiesta, DtRif = DateTime.Now, IdxMacchina = selRec.IdxMacchina, CodArticolo = selRec.CodArticolo, IdxODL = 0, Valore = selRec.Valore }; currRecordClone = newRec; await Task.Delay(1); } /// /// Eliminazione record selezionato (previa conferma) /// /// /// protected async Task DoDelete(DossierModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Eliminazione Dossier: sei sicuro di voler procedere?")) return; var done = await MDService.DossiersDeleteRecordAsync(selRec); currRecord = null; await ReloadData(true); visualizzaFlux = true; } protected async Task editRecord(FluxLogDTO selRec) { currFluxLogDto = selRec; // indico record selezionato await RecordSelFlux.InvokeAsync(selRec); } protected async Task newDossier(DossierModel selRec) { var alert = await JSRuntime.InvokeAsync("confirm", "Confermi di voler creare un nuovo Dossier per l'impianto/articolo selezionato?"); if (alert) { await Task.Delay(1); if (currRecordClone != null) { // serializzo valore x flux log... DossierFluxLogDTO? valoreDeserializzato = JsonConvert.DeserializeObject(selRec.Valore); if (valoreDeserializzato != null) { listaFlux = valoreDeserializzato .ODL .OrderBy(x => x.CodFlux) .ToList(); } if (listaFlux != null) { foreach (var item in listaFlux) { item.IdxMacchina = selRec.IdxMacchina; item.Valore = "0"; item.ValoreEdit = "0"; item.dtEvento = DateTime.Now; } } DossierFluxLogDTO updatedResult = new DossierFluxLogDTO() { ODL = listaFlux }; string newVal = JsonConvert.SerializeObject(updatedResult); // preparo x insert currRecordClone.DtRif = DateTime.Now; currRecordClone.IdxMacchina = selRec.IdxMacchina; currRecordClone.CodArticolo = selRec.CodArticolo; currRecordClone.KeyRichiesta = selRec.KeyRichiesta; currRecordClone.Valore = newVal; // METODO PER INSERT DOSSIER + FLUX await MDService.DossiersInsert(currRecordClone); //await ReloadDataAsync(true); currRecordClone = null; isEditing = false; await Task.Delay(1); await flushCache(); } return; } else { currRecordClone = null; await Task.Delay(1); NavManager.NavigateTo(NavManager.Uri, true); } } protected override async Task OnInitializedAsync() { await MDService.ConfigResetCacheAsync(); ListGruppiFase = await MDService.ElencoGruppiFaseAsync(); ListStati = await MDService.AnagStatiCommAsync(); selAzienda = await MDService.ConfigTryGetAsync("AZIENDA"); giacenzeConf = await MDService.ConfigTryGetAsync("SPEC_ShowGiacenze"); ListMacchine = await MDService.MacchineGetFiltAsync("*"); await ReloadData(true); } protected override async Task OnParametersSetAsync() { if (!lastFilter.Equals(SelFilter)) { lastFilter = SelFilter.clone(); await ReloadData(true); } } protected async void OnSeachUpdated() { await InvokeAsync(() => { currPage = 1; StateHasChanged(); }); } protected async Task selRecord(DossierModel selRec) { currRecord = selRec; await RecordSel.InvokeAsync(selRec); listaFlux = MDService.FluxLogDtoConvert(selRec.Valore); await toggleTableFlux(); } protected async Task update(FluxLogDTO selRec) { var alert = await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare TUTTE le modifiche? queste saranno parte del dossier inviato all'impianto"); if (alert) { if (currRecord != null) { // serializzo valore x flux log... DossierFluxLogDTO updatedResult = new DossierFluxLogDTO() { ODL = listaFlux }; string newVal = JsonConvert.SerializeObject(updatedResult); currRecord.Valore = newVal; // METODO PER UPDATE FLUX await MDService.DossiersUpdateValoreAsync(currRecord); currFluxLogDto = null; isEditing = false; await InvokeAsync(StateHasChanged); } return; } else { currFluxLogDto = null; await Task.Delay(1); // rimando a pagina corrente NavManager.NavigateTo(NavManager.Uri, true); } } protected async Task UpdateData() { currRecord = null; await ReloadData(true); } #endregion Protected Methods #region Private Fields private int _totalCount = 0; private FluxLogDTO? currFluxLogDto = null; private DossierModel? currRecord = null; private DossierModel? currRecordClone = null; private List? ListGruppiFase; private List? ListMacchine; private List? ListRecords; private List? ListStati; /// /// Ricerca articoli /// private string SearchArt = ""; private List? SearchRecords; #endregion Private Fields #region Private Properties private int currPage { get => SelFilter.CurrPage; set => SelFilter.CurrPage = value; } private bool isEditing { get => SelFilter.isEditing; set => SelFilter.isEditing = value; } private bool isLoading { get; set; } = false; private SelectDossierParams lastFilter { get; set; } = new SelectDossierParams() { CurrPage = -1 }; private List? listaFlux { get; set; } = null; private int MaxRec { get => SelFilter.MaxRecord; } private int MaxRecord { get => SelFilter.MaxRecord; } [Inject] private NavigationManager NavManager { get; set; } = null!; private int numRecord { get => SelFilter.NumRec; set => SelFilter.NumRec = value; } private string SelArticolo { get => SelFilter.CodArticolo; } private DateTime SelDtEnd { get => SelFilter.DtEnd; } private DateTime SelDtStart { get => SelFilter.DtStart; } private string SelMacchina { get => SelFilter.IdxMacchina; } private int totalCount { get => _totalCount; set { if (_totalCount != value) { _totalCount = value; TotRecordChanged.InvokeAsync(value); } } } private bool visualizzaFlux { get; set; } = true; #endregion Private Properties #region Private Methods private async Task closeTableFlux() { currFluxLogDto = null; currRecord = null; currRecordClone = null; visualizzaFlux = true; isEditing = false; await RecordSelFlux.InvokeAsync(currFluxLogDto); await Task.Delay(1); } private string css() { string answ = ""; if (isEditing) { answ = "visible"; } else { answ = "hidden"; } return answ; } private void enableEditing() { isEditing = true; } private async Task ReloadData(bool setChanged) { isLoading = true; SearchRecords = await MDService.DossiersGetLastFiltAsync(SelMacchina, SelArticolo, SelDtStart, SelDtEnd, MaxRec); totalCount = SearchRecords.Count; ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); await Task.Delay(1); if (setChanged) { await InvokeAsync(() => StateHasChanged()); } isLoading = false; } private async Task toggleTableFlux() { visualizzaFlux = false; await Task.Delay(1); } private string tradFase(string codFase) { string answ = codFase; if (ListStati != null && ListStati.Count > 0) { var recSel = ListStati.FirstOrDefault(x => x.value == codFase); if (recSel != null) { answ = recSel.label; } } return answ; } private string traduci(string lemma) { var answ = MDService.Traduci(lemma, "IT"); return answ; } #endregion Private Methods } }