using Microsoft.AspNetCore.Components; using MP.Data.DbModels; using MP.Data.Services; using NLog; using static EgwCoreLib.Utils.DtUtils; namespace MP_TAB3.Components { public partial class ProdConfMan : IDisposable { #region Public Properties [Parameter] public EventCallback E_MachSel { get; set; } [Parameter] public EventCallback E_Updated { get; set; } [Parameter] public string IdxMacchSub { get; set; } = ""; [Parameter] public MappaStatoExplModel? RecMSE { get; set; } = null; #endregion Public Properties #region Public Methods public void Dispose() { ListComplete.Clear(); ListPaged.Clear(); ListaOdl.Clear(); ListaOper.Clear(); DictOpr.Clear(); } #endregion Public Methods #region Protected Properties protected List ListComplete { get; set; } = new List(); protected List ListPaged { get; set; } = new List(); [Inject] protected MessageService MServ { get; set; } = null!; [Inject] protected SharedMemService SMServ { get; set; } = null!; [Inject] protected TabDataService TabDServ { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected override async Task OnInitializedAsync() { await Task.Delay(1); InitConfig(); await ReloadBaseList(); if (RecMSE != null) { IdxMaccSel = RecMSE.IdxMacchina; isMulti = SMServ.DictMacchMulti[IdxMaccSel] == 1; if (isMulti) { var idxMSel = MServ.UserPrefGet(IdxMaccSel); if (!string.IsNullOrEmpty(idxMSel)) { IdxMaccSel = idxMSel; } } await setupPeriodo(); await ReloadData(); } } protected async Task SetMacc(string selIdxMacc) { isProcessing = true; await Task.Delay(1); IdxMaccSel = selIdxMacc; await ReloadData(); await Task.Delay(1); isProcessing = false; await E_MachSel.InvokeAsync(selIdxMacc); } protected void SetNumRec(int newNum) { NumRecPage = newNum; UpdateTable(); } protected async Task SetOdl(int selIdxOdl) { isProcessing = true; IdxOdl = selIdxOdl; // se ho odl --> imposto periodo if (IdxOdl > 0) { var recOdl = GetOdl(IdxOdl); if (recOdl != null && recOdl.IdxOdl == IdxOdl) { DateTime fine = DateTime.Today.AddDays(1); DateTime inizio = fine.AddMonths(-1); CurrPeriodo = new Periodo(recOdl.DataInizio ?? inizio.AddDays(-1), recOdl.DataFine ?? fine); } } // altrimenti reset.. else { await setupPeriodo(); } await ReloadData(); isProcessing = false; //await Task.Delay(1); } protected void SetPage(int newNum) { PageNum = newNum; UpdateTable(); } protected async Task SetPeriodo(Periodo newPeriodo) { CurrPeriodo = newPeriodo; await ReloadData(); } protected void UpdateTable() { // esegue paginazione if (TotalCount > NumRecPage) { ListPaged = ListComplete .OrderByDescending(x => x.DataTo) .Skip((PageNum - 1) * NumRecPage) .Take(NumRecPage) .ToList(); } else { ListPaged = ListComplete .OrderByDescending(x => x.DataTo) .ToList(); } } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private Dictionary DictOpr = new Dictionary(); private bool isMulti = false; private bool isProcessing = false; private List ListaOdl = new List(); private List ListaOper = new List(); private int NumRecPage = 10; private int PageNum = 1; /// /// Indica se mostrare descrizione articolo su 3° riga /// private bool ShowArtDescr = false; /// /// Indica se mostrare CodExt (commessa) su 3° riga /// private bool ShowExtCode = false; /// /// Indica se partire da ultimo ODL x definire ultimo periodo... /// private bool ShowLastOdl = false; /// /// Indica se mostrare rilavorati in conferma produzione /// private bool ShowRilav = false; private int TotalCount = 0; private bool useOdl = false; #endregion Private Fields #region Private Properties private Periodo CurrPeriodo { get; set; } = new Periodo(); private string IdxMaccAltra { get; set; } = ""; /// /// Restituisce il codice IdxMacchina dell'altra tavola (se multi) altrimenti la stessa macchina... /// private string idxMaccAltraTav { get { string answ = ""; if (RecMSE != null) { try { // verifico se SIA una tavola (ha char "#") int iSharp = IdxMaccMain.IndexOf('#'); if (iSharp > 0) { // ora verifico SE ALTRA TAVOLA ha ODL... string nomeTav = IdxMaccMain.Substring(iSharp); string altraTav = nomeTav.Substring(0, nomeTav.Length - 1); altraTav += nomeTav.EndsWith("1") ? "2" : "1"; // sistemo nome answ = IdxMaccMain.Replace(nomeTav, altraTav); } } catch { } } return answ; } } /// /// Macchina selezionata MAIN /// private string IdxMaccMain { get => RecMSE != null ? RecMSE.IdxMacchina : ""; } private string IdxMaccSel { get; set; } = ""; private int IdxOdl { get; set; } = 0; private string selMessage { get => useOdl ? "Periodo da ODL" : "Periodo Libero"; } private bool UseOdl { get => useOdl; set { useOdl = value; if (!value) { IdxOdl = 0; } } } #endregion Private Properties #region Private Methods private ODLExpModel ActiveOdl(DateTime dtRif) { var recOdl = ListaOdl .Where(x => x.DataInizio <= dtRif && (x.DataFine >= dtRif || x.DataFine == null)) .OrderByDescending(x => x.DataInizio) .FirstOrDefault(); return recOdl ?? new ODLExpModel(); } private ODLExpModel GetOdl(int idxOdl) { var recOdl = ListaOdl .Where(x => x.IdxOdl == idxOdl) .FirstOrDefault(); return recOdl ?? new ODLExpModel(); } /// /// Init da valori configurazione /// private void InitConfig() { TabDServ.ConfigGetVal("TAB_confProdPeriodLastODL", ref ShowLastOdl); TabDServ.ConfigGetVal("TAB_confProdShowRilav", ref ShowRilav); TabDServ.ConfigGetVal("TAB_confProdShowArtDescr", ref ShowArtDescr); TabDServ.ConfigGetVal("TAB_confProdShowExtCode", ref ShowExtCode); } private string OperDto(int matr) { string answ = $"[{matr}]"; // cerco in dizionario if (DictOpr.ContainsKey(matr)) { answ = DictOpr[matr]; } else { // altrimenti cerco da lista e inserisco in dizionario var recOpr = ListaOper.Where(x => x.MatrOpr == matr).FirstOrDefault(); if (recOpr != null) { answ = $"{recOpr.Cognome} {recOpr.Nome}"; } DictOpr.Add(matr, answ); } return answ; } /// /// Rileggo anagrafiche di base /// /// private async Task ReloadBaseList() { ListaOper = await TabDServ.ElencoOperatori(); } /// /// Aggiorno valori produzione alla data richiesta... /// /// private async Task ReloadData() { isProcessing = true; await Task.Delay(1); DateTime dtEnd = DateTime.Today.AddDays(1); DateTime dtStart = dtEnd.AddMonths(-3); if (!string.IsNullOrEmpty(IdxMaccSel)) { ListaOdl = await TabDServ.OdlListByMaccPeriodo(IdxMaccSel, dtStart, dtEnd); // se vuoto prendo 1 anno... int maxTry = 3; while (ListaOdl.Count < 1 && maxTry > 0) { dtStart = dtStart.AddMonths(-3); ListaOdl = await TabDServ.OdlListByMaccPeriodo(IdxMaccSel, dtStart, dtEnd); maxTry--; } ListComplete = await TabDServ.ElencoConfProdFiltAsync(IdxMaccSel, CurrPeriodo.Inizio, CurrPeriodo.Fine); TotalCount = ListComplete.Count; // esegue paginazione UpdateTable(); } isProcessing = false; } private async Task setupPeriodo() { bool doSet = true; if (ShowLastOdl) { UseOdl = true; var LastODL = await TabDServ.VSOdlGetLastByMacc(IdxMaccSel, 1); if (LastODL != null) { var recOdl = LastODL .Where(x => x.value > 0) .FirstOrDefault(); if (recOdl != null) { var idxOdlLast = recOdl.value; await SetOdl(idxOdlLast); doSet = false; } } } // controllo di nuovo se impostare priodo if (doSet) { DateTime fine = DateTime.Today.AddDays(1); DateTime inizio = fine.AddMonths(-1); CurrPeriodo = new Periodo(inizio, fine); } } #endregion Private Methods } }