using Microsoft.AspNetCore.Components; using MP.Data.MgModels; using MP.SPEC.Data; using NLog; namespace MP.SPEC.Components { public partial class RecipeConfMan { #region Public Properties [Parameter] public EventCallback CancelEvent { get; set; } [Parameter] public int IdxPODL { get; set; } = 0; [Parameter] public string RecipePath { get; set; } = ""; #endregion Public Properties #region Protected Fields protected bool isLoading = false; #endregion Protected Fields #region Protected Properties protected RecipeModel? CurrRecipe { get; set; } = null; protected RecipeModel? OrigRecipe { get; set; } = null; [Inject] protected MpDataService MDService { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected async Task CancelHeadData() { await ReloadData(); } protected Dictionary GetHeadListByType(string DictType) { Dictionary answ = new Dictionary(); // in primis il "selezionare" answ.Add("", "--- Selezionare ---"); // cerco tipo in enums... if (CurrRecipe != null && CurrRecipe.HeadConf.EnumVal.ContainsKey(DictType)) { foreach (var item in CurrRecipe.HeadConf.EnumVal[DictType]) { answ.Add(item.Key, item.Value); } } return answ; } protected Dictionary GetRowListByType(string DictType) { Dictionary answ = new Dictionary(); // in primis il "selezionare" answ.Add("", "--- Selezionare ---"); // cerco tipo in enums... if (CurrRecipe != null && CurrRecipe.RowsConf.EnumVal.ContainsKey(DictType)) { foreach (var item in CurrRecipe.RowsConf.EnumVal[DictType]) { answ.Add(item.Key, item.Value); } } return answ; } protected override async Task OnParametersSetAsync() { await ReloadData(); } protected async Task SaveHeadData() { if (CurrRecipe != null) { await MDService.RecipeSetByPODL(CurrRecipe); await ReloadData(); } } #endregion Protected Methods #region Private Fields private bool showHead = true; private bool showAllRows = false; private Dictionary showRows = new Dictionary(); #endregion Private Fields #region Private Methods private async Task ReloadData() { await Task.Delay(1); isLoading = true; if (IdxPODL != 0 && !string.IsNullOrEmpty(RecipePath)) { CurrRecipe = new RecipeModel(); // effettua ricerca ricetta su MongoDb CurrRecipe = await MDService.RecipeGetByPODL(IdxPODL); // se non trova crea nuova... if (CurrRecipe == null) { Dictionary CalcArgs = await getCalcArgs(); CurrRecipe = MDService.InitRecipe(RecipePath, IdxPODL, CalcArgs); // la salvo... await MDService.RecipeSetByPODL(CurrRecipe); } // rileggo la default OrigRecipe = await MDService.RecipeGetByPODL(IdxPODL); fixRowsShowStatus(); } await Task.Delay(1); isLoading = false; } private void fixRowsShowStatus() { // sistemo lista bool x righe showRows = new Dictionary(); if (CurrRecipe != null) { for (int i = 1; i <= CurrRecipe.RowsVal.Count; i++) { showRows.Add($"{i}", false); } } } /// /// Prepara Dict args calcolati x creazione ricetta /// /// private async Task> getCalcArgs() { // preparo dizionario valori calcolati Dictionary CalcArgs = new Dictionary(); // aggiungo dati PODL CalcArgs.Add("IdxPODL", $"{IdxPODL}"); CalcArgs.Add("CodePODL", $"PODL{IdxPODL:00000000}"); // recupero altri dati da PODL var rowPodl = await MDService.POdlGetByKey(IdxPODL); if (rowPodl != null) { CalcArgs.Add("CodArticolo", rowPodl.CodArticolo); CalcArgs.Add("DescArticolo", rowPodl.ArticoloNav.DescArticolo); } return CalcArgs; } private bool needSave { get { bool answ = false; if (CurrRecipe != null && OrigRecipe != null) { answ = !(CurrRecipe.HeadVal.SequenceEqual(OrigRecipe.HeadVal)); if (!answ) { // verifico nu righe... answ = (CurrRecipe.RowsVal.Count != OrigRecipe.RowsVal.Count); if (!answ) { try { foreach (var item in CurrRecipe.RowsVal) { answ = !(item.Value.SequenceEqual(OrigRecipe.RowsVal[item.Key])); if (answ) { break; } } } catch (Exception exc) { Log.Error($"Error in needSave:{Environment.NewLine}{exc}"); answ = true; } } } } return answ; } } private static Logger Log = LogManager.GetCurrentClassLogger(); private async Task addRow() { isLoading = true; if (CurrRecipe != null) { // preparo dizionario valori calcolati Dictionary CalcArgs = await getCalcArgs(); int newNumRow = CurrRecipe.RowsVal.Count + 1; CalcArgs.Add("RowNum", $"{newNumRow}"); CalcArgs.Add("RowTot", $"{newNumRow}"); // metodo x avere nuova var rowElem = CurrRecipe.getNewRow(CalcArgs); // effettuo aggiunta riga... CurrRecipe.RowsVal.Add($"{newNumRow}", rowElem); fixRowsShowStatus(); } isLoading = false; await Task.Delay(1); } /// /// Elimino riga (ultima)... /// /// /// private async Task deleteRow(string rowNum) { isLoading = true; if (CurrRecipe != null) { // verifico esista... if (CurrRecipe.RowsVal.ContainsKey(rowNum)) { CurrRecipe.RowsVal.Remove(rowNum); fixRowsShowStatus(); } } isLoading = false; await Task.Delay(1); } private void toggleHead() { showHead = !showHead; } private async Task toggleRows() { showAllRows = !showAllRows; foreach (var item in showRows) { showRows[item.Key] = showAllRows; } await Task.Delay(1); } private async Task toggleRow(string rowNum) { showRows[rowNum] = !showRows[rowNum]; await Task.Delay(1); } #endregion Private Methods } }