using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data; using MP.Data.DatabaseModels; using MP.SPEC.Data; using MP.SPEC.Services; using NLog; namespace MP.SPEC.Components { public partial class ListPODL : IDisposable { #region Public Properties [Parameter] public SelectXdlParams actFilter { get; set; } = new SelectXdlParams(); [Parameter] public string padCodXdl { get; set; } = "0000"; [Parameter] public EventCallback PagerResetReq { get; set; } [Parameter] public EventCallback RecordSel { get; set; } [Parameter] public EventCallback updateRecordCount { get; set; } #endregion Public Properties #region Public Methods public string checkSelect(PODLExpModel record) { string answ = ""; if (currRecord != null) { try { answ = ((currRecord.IdxMacchina == record.IdxMacchina) && (currRecord.CodArticolo == record.CodArticolo) && (currRecord.CodFase == record.CodFase)) ? "table-info" : ""; } catch { } } return answ; } public void Dispose() { currRecord = null; SearchRecords = null; ListRecords = null; ListStati = null; GC.Collect(); } #endregion Public Methods #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected MpDataService MDService { get; set; } = null!; [Inject] protected IOApiService MpIoApiCall { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected async Task cloneRecord(PODLExpModel selRec) { currRecord = null; // clono resettando ODL var clonedRec = Utils.POdlExt.clone(selRec, true); currRecord = clonedRec; await RecordSel.InvokeAsync(clonedRec); header = "Duplica PODL"; } /// /// Eliminazione record selezionato (previa conferma) /// /// /// protected async Task deleteRecord(PODLExpModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Eliminazione Record: sei sicuro di voler procedere?")) return; await Task.Delay(1); var done = await MDService.PODLDeleteRecord(selRec); await callSyncDb(selRec.IdxMacchina); currRecord = null; await reloadData(); await Task.Delay(1); } protected override async Task OnInitializedAsync() { //await FilterChanged.InvokeAsync(actFilter); ListStati = await MDService.AnagStatiComm(); } protected override async Task OnParametersSetAsync() { if (!lastFilter.Equals(actFilter)) { lastFilter = actFilter.clone(); await reloadData(); } } protected async void OnSeachUpdated() { await InvokeAsync(() => { PagerResetReq.InvokeAsync(true); //currPage = 1; Task task = UpdateData(); StateHasChanged(); }); } protected bool POdlDelEnabled(int idxOdl) { return idxOdl == 0; } protected async Task resetSel() { currRecord = null; await RecordSel.InvokeAsync(null); } protected async Task selRecord(PODLExpModel? selRec) { currRecord = selRec; header = "Modifica PODL"; await RecordSel.InvokeAsync(selRec); } protected async Task startOdl(PODLExpModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Sei sicuro di voler avviare PODL selezionato?")) return; if (selRec != null) { int idxEvento = 0; string evMess = ""; // verifico ancora NON ci sia ODL corrente/aperto if (canStartOdl(selRec.IdxMacchina)) { await callStartSetup(selRec.IdxMacchina); await Task.Delay(1); // chiamo stored stp_ODL_inizioSetupPromessa e recupero ODL corrente bool fatto = await MDService.POdlDoSetup(selRec); if (fatto) { var currPOdl = await MDService.PODL_getByKey(selRec.IdxPromessa); var newOdl = await MDService.OdlGetByKey(currPOdl.IdxOdl); // registro evento... idxEvento = 2; evMess = $"Inizio Setup | PODL {selRec.IdxPromessa}"; processaEvento(selRec.IdxMacchina, idxEvento, evMess, newOdl.IdxOdl, newOdl.CodArticolo); // aspetto 1 sec await Task.Delay(1000); // registro inizio produzione idxEvento = 2; evMess = $"Registrata inizio Produzione | PODL {selRec.IdxPromessa} | ODL {newOdl.IdxOdl} | ART {newOdl.CodArticolo}"; processaEvento(selRec.IdxMacchina, idxEvento, evMess, newOdl.IdxOdl, newOdl.CodArticolo); // chiamo task x IOB await callForceUpdate(selRec.IdxMacchina); await Task.Delay(1); await callForceUpdate(selRec.IdxMacchina); await Task.Delay(1); await callSyncDb(selRec.IdxMacchina); await Task.Delay(1); // svuoto memorie pagina... await MDService.FlushRedisCache(); NavManager.NavigateTo(NavManager.Uri, true); } } } } protected async Task UpdateData() { currRecord = null; await reloadData(); } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private PODLExpModel? currRecord = null; private List? ListRecords; private List? ListStati; /// /// scadenza validità lista ODL correnti /// private DateTime odlCurrExp = DateTime.Now.AddMinutes(-1); /// /// Elenco ODL correnti... /// private List odlCurrList = new List(); private List? SearchRecords; #endregion Private Fields #region Private Properties private DateTime selDtStart { get => actFilter.DtStart; set { if (!actFilter.DtStart.Equals(value)) { actFilter.DtStart = value; currPage = 1; } } } private DateTime selDtEnd { get => actFilter.DtEnd; set { if (!actFilter.DtEnd.Equals(value)) { actFilter.DtEnd = value; currPage = 1; } } } private int _totalCount { get; set; } = 0; private int currPage { get => actFilter.CurrPage; set => actFilter.CurrPage = value; } private bool hasOdl { get => actFilter.HasOdl; set => actFilter.HasOdl = value; } private bool isLoading { get; set; } = false; private SelectXdlParams lastFilter { get; set; } = new SelectXdlParams() { CurrPage = -1 }; private string macchina { get => actFilter.IdxMacchina; set => actFilter.IdxMacchina = value; } private int numRecord { get => actFilter.NumRec; set => actFilter.NumRec = value; } private string reparto { get => actFilter.CodReparto; set => actFilter.CodReparto = value; } private string SearchVal { get => string.IsNullOrEmpty(actFilter.SearchVal) ? "*" : actFilter.SearchVal; } private string StatoSel { get => actFilter.CodFase; set => actFilter.CodFase = value; } private int totalCount { get => _totalCount; set { if (_totalCount != value) { _totalCount = value; updateRecordCount.InvokeAsync(value); } } } #endregion Private Properties #region Private Methods /// /// Chiama metodo x chiedere sync DB /// /// /// private async Task addTask2Exe(string idxMacc, string taskName, string taskVal) { // compongo URL e chiamo string restUrl = $"IOB/addTask2Exe/{idxMacc}?taskName={taskName}&taskVal={taskVal}"; try { var response = await MpIoApiCall.callMpIoUrlGet(restUrl); } catch (Exception exc) { Log.Error($"Errore durante chiamata: {Environment.NewLine}{exc}"); } } /// /// Chiama metodo x chiedere force Update /// /// /// private async Task callForceUpdate(string IdxMacc) { // chiamo aggiunta task SyncDb... await addTask2Exe(IdxMacc, "ForceUpdate", $"SPEC|TS:{DateTime.Now:yyMMddHHmmss}"); } /// /// Chiama metodo x indicare inizio setup /// /// /// private async Task callStartSetup(string IdxMacc) { // chiamo evento inizio setup await addTask2Exe(IdxMacc, "startSetup", $"SPEC|TS:{DateTime.Now:yyMMddHHmmss}"); } /// /// Chiama metodo x chiedere sync DB /// /// /// private async Task callSyncDb(string IdxMacc) { // chiamo aggiunta task SyncDb... await addTask2Exe(IdxMacc, "syncDbData", ""); } /// /// verifica se sia avviabile ODL x macchina /// /// /// private bool canStartOdl(string idxMacchina) { // controllo se lista scaduta... bool answ = false; DateTime adesso = DateTime.Now; if (adesso > odlCurrExp || odlCurrList == null || odlCurrList.Count == 0) { odlCurrList = MDService.OdlGetCurrent(); odlCurrExp = adesso.AddSeconds(2); } answ = !odlCurrList.Contains(idxMacchina); return answ; } protected string header { get => actFilter.Header; set => actFilter.Header = value; } /// /// processa evento richiesto /// /// /// /// /// private async void processaEvento(string idxMacc, int idxEvento, string userMsg, int idxODL, string codArticolo) { // se manca codart calcolo... if (string.IsNullOrEmpty(codArticolo)) { var currOdl = await MDService.OdlGetByKey(idxODL); if (currOdl != null) { codArticolo = currOdl.CodArticolo; } } // scrivo evento scriviRigaEventoBarcode EventListModel newRec = new EventListModel() { IdxMacchina = idxMacc, InizioStato = DateTime.Now, IdxTipo = idxEvento, CodArticolo = codArticolo, MatrOpr = 0, pallet = "", Value = userMsg }; await MDService.EvListInsert(newRec); } private async Task reloadData() { ListRecords = null; isLoading = true; SearchRecords = await MDService.ListPODLFilt(hasOdl, StatoSel, macchina, reparto, selDtStart, selDtEnd); totalCount = SearchRecords.Count; ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); await Task.Delay(1); await InvokeAsync(() => StateHasChanged()); //await updateRecordCount.InvokeAsync(totalCount); isLoading = false; } 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; } #endregion Private Methods } }