using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data.DatabaseModels; using MP.SPEC.Data; using MP.SPEC.Services; using System.Reflection.PortableExecutable; using System.Text; namespace MP.SPEC.Components { public partial class ListPODL { #region Public Properties [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(PODLModel 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; } #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 MessageService MsgService { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected async Task cloneRecord(PODLModel selRec) { // creo record duplicato... PODLModel newRec = new PODLModel() { Attivabile = selRec.Attivabile, CodArticolo = selRec.CodArticolo, CodCli = selRec.CodCli, CodGruppo = selRec.CodGruppo, DueDate = selRec.DueDate, IdxMacchina = selRec.IdxMacchina, IdxOdl = selRec.IdxOdl, IdxPromessa = 0, InsertDate = selRec.InsertDate, KeyBCode = selRec.KeyBCode, KeyRichiesta = selRec.KeyRichiesta, Note = $"DUPLICATED - {selRec.Note}", NumPezzi = selRec.NumPezzi, Priorita = selRec.Priorita, PzPallet = selRec.PzPallet, Tcassegnato = selRec.Tcassegnato }; currRecord = selRec; await RecordSel.InvokeAsync(newRec); } protected async Task startOdl(PODLModel selRec) { 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 var newOdl = await MDService.POdlDoSetup(selRec); if (newOdl != null) { // registro evento... idxEvento = 2; evMess = $"Registrata inizio produzione | PODL {selRec.IdxPromessa} | ODL {newOdl.IdxOdl}"; processaEvento(selRec.IdxMacchina, idxEvento, evMess, newOdl.IdxOdl); //aspetto 1 sec // idxEv = 1 //// processo chiusura setup //string evText = "Registrata inizio produzione per ODL {0}"; //StringBuilder sb = new StringBuilder(); //sb.AppendLine(String.Format(evText, idxODLStart)); //processaEvento(idxMacchinaFix, idxEvento, sb.ToString(), idxODLStart); // richiedo refresh su IOB-WIN: // DataLayerObj.addTask4Machine(machine.IdxMacchinaSlave, taskType.setParameter, "ForceUpdate"); await callForceUpdate(selRec.IdxMacchina); await Task.Delay(1); await callForceUpdate(selRec.IdxMacchina); await Task.Delay(1); await callSyncDb(selRec.IdxMacchina); await Task.Delay(1); } } } } /// /// processa evento richiesto /// /// /// /// /// private void processaEvento(string idxMacc, int idxEvento, string userMsg, int idxODL) { // scrivo evento scriviRigaEventoBarcode // fixme todo !!! FARE } /// /// verifica se sia avviabile ODL x macchina /// /// /// private bool canStartOdl(string idxMacchina) { // fare!!! bool answ = idxMacchina.Contains("BAG"); return answ; } /// /// Eliminazione record selezionato (previa conferma) /// /// /// protected async Task deleteRecord(PODLModel 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() { MsgService.EA_PageUpdated += MessageService_EA_PageUpdated; MsgService.EA_SearchUpdated += OnSeachUpdated; MsgService.EA_StatoSearch += MsgService_EA_StatoSearch; ListStati = await MDService.AnagStatiComm(); } protected override async Task OnParametersSetAsync() { 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(PODLModel selRec) { currRecord = selRec; await RecordSel.InvokeAsync(selRec); } protected async Task UpdateData() { currRecord = null; await reloadData(); } #endregion Protected Methods #region Private Fields private PODLModel? currRecord = null; private List? ListRecords; private List? ListStati; private List? SearchRecords; #endregion Private Fields #region Private Properties private int currPage { get => MsgService.currPage; set => MsgService.currPage = value; } private bool isLoading { get; set; } = false; private int numRecord { get => MsgService.numRecord; set => MsgService.numRecord = value; } private string SearchVal { get => string.IsNullOrEmpty(MsgService.SearchVal) ? "*" : MsgService.SearchVal; } private string StatoSel { get => MsgService.StateSel; set => MsgService.StateSel = value; } private int totalCount { get; set; } = 0; #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}"; var response = await MpIoApiCall.callMpIoUrlGet(restUrl); } /// /// Chiama metodo x chiedere sync DB /// /// /// private async Task callSyncDb(string IdxMacc) { // chiamo aggiunta task SyncDb... await addTask2Exe(IdxMacc, "syncDbData", ""); #if false string idxMacc = selRec.IdxMacchina; string restUrl = $"IOB/addTask2Exe/{idxMacc}?taskName=syncDbData&taskVal="; var response = await MpIoApiCall.callMpIoUrlGet(restUrl); #endif } /// /// 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}"); } private async void MessageService_EA_PageUpdated() { await reloadData(); } private async void MsgService_EA_StatoSearch() { await InvokeAsync(() => { PagerResetReq.InvokeAsync(true); //currPage = 1; Task task = UpdateData(); StateHasChanged(); }); } private async Task reloadData() { isLoading = true; SearchRecords = await MDService.ListPODLFilt(SearchVal, StatoSel); 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 } }