using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data; using MP.Data.DbModels; 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 RecordEdit { 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.IdxPromessa == record.IdxPromessa) ? "table-info" : ""; //answ = ((SelRecord.IdxMacchina == record.IdxMacchina) && (SelRecord.CodArticolo == record.CodArticolo) && (SelRecord.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 Fields protected bool enableForceSync = true; protected bool enableStartPODL = true; protected bool enableStopODL = true; #endregion Protected Fields #region Protected Properties protected string header { get => actFilter.Header; set => actFilter.Header = value; } [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 /// /// Verifica se sia un articolo di tipo "KIT" x mostrare show dettaglio /// /// /// protected bool CheckIsKit(string CodArticolo) { bool answ = false; if (ListArtKit != null && ListArtKit.Count > 0) { answ = ListArtKit.Count(x => x.CodArticolo == CodArticolo) > 0; } return answ; } protected async Task cloneRecord(PODLExpModel selRec) { currRecord = null; // clono resettando ODL var clonedRec = Utils.POdlExt.clone(selRec, true); currRecord = clonedRec; await RecordEdit.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 void doShowRecipeArch(PODLExpModel selRec) { currRecord = selRec; currRecipeArchPath = MDService.MacchineRecipeArchive(selRec.IdxMacchina); showRecipeArch = true; showRecipeConf = false; } protected void doShowRecipeConf(PODLExpModel selRec) { currRecord = selRec; currRecipePath = MDService.MacchineRecipeConf(selRec.IdxMacchina); showRecipeArch = false; showRecipeConf = true; } protected async Task editRecord(PODLExpModel? selRec) { currRecord = selRec; header = "Modifica PODL"; await RecordEdit.InvokeAsync(selRec); } protected void KitToggleDetail(PODLExpModel? recSel) { if (recSel != null) { ListKitTemplate = MDService.TemplateKitFilt(recSel.CodArticolo, ""); ListPOdlKit = MDService.POdlListByKitParent(recSel.IdxPromessa); } else { ListKitTemplate = null; ListPOdlKit = null; } showKitDetail = !showKitDetail; } protected override async Task OnInitializedAsync() { ListStati = await MDService.AnagStatiComm(); ListArtKit = MDService.ArticoliGetByTipo("KIT", "*"); string strMachRecipe = await MDService.ConfigTryGetAsync("MachineWithRecipe"); if (!string.IsNullOrEmpty(strMachRecipe)) { bool.TryParse(strMachRecipe, out MachineWithRecipe); } string SPEC_PODL_gest = await MDService.ConfigTryGetAsync("SPEC_PODL_gest"); if (!string.IsNullOrEmpty(SPEC_PODL_gest)) { bool.TryParse(SPEC_PODL_gest, out enableStartPODL); } string SPEC_ODL_gest = await MDService.ConfigTryGetAsync("SPEC_ODL_gest"); if (!string.IsNullOrEmpty(SPEC_ODL_gest)) { bool.TryParse(SPEC_ODL_gest, out enableStopODL); } string SPEC_XODL_sync = await MDService.ConfigTryGetAsync("SPEC_XODL_sync"); if (!string.IsNullOrEmpty(SPEC_XODL_sync)) { bool.TryParse(SPEC_XODL_sync, out enableForceSync); } } protected override async Task OnParametersSetAsync() { if (!lastFilter.Equals(actFilter)) { lastFilter = actFilter.clone(); await ReloadData(); } } protected async void OnSeachUpdated() { await InvokeAsync(() => { PagerResetReq.InvokeAsync(true); Task task = UpdateData(); StateHasChanged(); }); } protected bool POdlDelEnabled(int idxOdl) { return idxOdl == 0; } protected async Task ReloadData() { ListRecords = null; isLoading = true; SearchRecords = await MDService.POdlListGetFiltAsync(hasOdl, StatoSel, macchina, reparto, selDtStart, selDtEnd); if (totalCount != SearchRecords.Count) { totalCount = SearchRecords.Count; } ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); await Task.Delay(1); await InvokeAsync(() => StateHasChanged()); isLoading = false; } protected async Task resetSel(bool forceUpdate) { currRecord = null; currRecipePath = ""; showRecipeArch = false; showRecipeConf = false; if (forceUpdate) { await ReloadData(); } await RecordEdit.InvokeAsync(null); } protected async Task selRecord(PODLExpModel? selRec) { currRecord = selRec; header = "Dettaglio 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.POdlGetByKey(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 = 1; evMess = $"Registrata inizio Produzione | PODL {selRec.IdxPromessa} | ODL {newOdl.IdxOdl} | ART {newOdl.CodArticolo}"; processaEvento(selRec.IdxMacchina, idxEvento, evMess, newOdl.IdxOdl, newOdl.CodArticolo); // imposto task x setComm, setArt, SetPzComm await callTask2Exe(selRec.IdxMacchina, "setArt", newOdl.CodArticolo); string odlPad = newOdl.IdxOdl.ToString(padCodXdl); await callTask2Exe(selRec.IdxMacchina, "setComm", $"ODL{odlPad}"); await callTask2Exe(selRec.IdxMacchina, "setPzComm", $"{newOdl.NumPezzi}"); // 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(); // svuoto cache MpIoNsCache await MDService.FlushMpIoOdlCache(); // ricarico pagina! 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 string currRecipeArchPath = ""; /// /// Percorso ricetta corrente /// private string currRecipePath = ""; private PODLExpModel? currRecord = null; /// /// Elenco articoli tipo KIT /// private List? ListArtKit; private List? ListKitTemplate = null; private List? ListPOdlKit; private List? ListRecords; /// /// Elenco stati /// private List? ListStati; private bool MachineWithRecipe = false; /// /// scadenza validità lista ODL correnti /// private DateTime odlCurrExp = DateTime.Now.AddMinutes(-1); /// /// Elenco ODL correnti... /// private List odlCurrList = new List(); private List? SearchRecords; private bool showKitDetail = false; private bool showRecipeArch = false; private bool showRecipeConf = false; #endregion Private Fields #region Private Properties private int _totalCount { get; set; } = -1; 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 string mainCss { get => (showRecipeConf || showRecipeArch) ? "col-6" : "col-12"; } 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 DateTime selDtEnd { get => actFilter.DtEnd; set { if (!actFilter.DtEnd.Equals(value)) { actFilter.DtEnd = value; currPage = 1; } } } private DateTime selDtStart { get => actFilter.DtStart; set { if (!actFilter.DtStart.Equals(value)) { actFilter.DtStart = value; currPage = 1; } } } 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", ""); } /// /// Chiama metodo x chiedere registrazione di un Task2Exe a MP-IO /// /// IdxMacchina interessata /// Richiesta da impostare /// Valore da impostare /// private async Task callTask2Exe(string IdxMacc, string KeyReq, string ValReq) { // chiamo aggiunta task SyncDb... await addTask2Exe(IdxMacc, KeyReq, ValReq); } /// /// verifica se sia avviabile ODL x idxMaccSel /// /// /// 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; } /// /// Verifica se la idxMaccSel abbia associata un path x ricette (elenco) /// /// /// private bool machineHasRecipeArch(string idxMacchina) { var recipeArchive = MDService.MacchineRecipeArchive(idxMacchina); return !string.IsNullOrEmpty(recipeArchive); } /// /// Verifica se la idxMaccSel abbia associata una ricetta (template) /// /// /// private bool machineHasRecipeConf(string idxMacchina) { var recipePath = MDService.MacchineRecipeConf(idxMacchina); return !string.IsNullOrEmpty(recipePath); } /// /// 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 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 } }