Files
2025-09-09 11:49:43 +02:00

178 lines
5.0 KiB
C#

using global::Microsoft.AspNetCore.Components;
using MP.Data.DbModels;
using MP.Data.Services;
using NLog;
using System.Reflection.Metadata;
using static MongoDB.Driver.WriteConcern;
namespace MP_TAB3.Components
{
public partial class MachSel : IDisposable
{
#region Public Properties
[Parameter]
public EventCallback<string> E_MachSel { get; set; }
[Parameter]
public MappaStatoExplModel? RecMSE { get; set; } = null;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
TDataService.DataInvalidated -= TDataService_DataInvalidated;
}
#endregion Public Methods
#region Protected Properties
protected string idxMaccCurr
{
get
{
string answ = "";
if (RecMSE != null)
{
answ = RecMSE?.IdxMacchina ?? "";
}
return answ;
}
}
protected string IdxMaccSel
{
get => idxMaccSel;
set
{
if (idxMaccSel != value)
{
idxMaccSel = value;
MsgServ.UserPrefSet(idxMaccCurr, value);
}
}
}
protected Dictionary<string, string>? ListMacchine { get; set; } = null;
[Inject]
protected MessageService MsgServ { get; set; } = null!;
[Inject]
protected SharedMemService SMServ { get; set; } = null!;
[Inject]
protected TabDataService TDataService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override void OnInitialized()
{
//base.OnInitialized();
firstInit = true;
TDataService.DataInvalidated += TDataService_DataInvalidated;
}
protected override async Task OnParametersSetAsync()
{
try
{
// inizilamente riporto macchina corrente da MSE
if (RecMSE == null)
{
IdxMaccSel = "";
Log.Error("Impossibile selezionare: recMSE nullo");
}
else
{
if (string.IsNullOrEmpty(idxMaccSel))
{
await ReloadSelector(false);
}
}
}
catch (Exception exc)
{
Log.Error($"Errore in MachSel.OnParametersSetAsync{Environment.NewLine}{exc}");
}
await Task.Delay(1);
}
#endregion Protected Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private bool firstInit = true;
private bool isMulti = false;
#endregion Private Fields
#region Private Properties
private string idxMaccSel { get; set; } = "";
#endregion Private Properties
#region Private Methods
private async Task OnSelectionChanged(ChangeEventArgs e)
{
var newValue = e.Value?.ToString();
if (newValue != idxMaccSel)
{
IdxMaccSel = newValue;
await E_MachSel.InvokeAsync(newValue);
}
}
private async Task ReloadSelector(bool doReload)
{
if (RecMSE != null && firstInit)
{
firstInit = false;
// verifico se la macchina sia configurata tra le MSFD...
if (SMServ.DictMacchMulti.ContainsKey(RecMSE.IdxMacchina))
{
isMulti = SMServ.DictMacchMulti[RecMSE.IdxMacchina] == 1;
}
if (isMulti)
{
var listMulti = await TDataService.VMSFDGetAll(doReload);
ListMacchine = listMulti
.Where(x => x.IdxMacchina.Contains($"{RecMSE.IdxMacchina}#"))
.ToDictionary(x => x.IdxMacchina, x => x.CodMaccArticolo);
if (ListMacchine.Count > 0)
{
// cerco se ho in storage la macchina selezionata...
var idxMSel = MsgServ.UserPrefGet(idxMaccCurr);
if (!string.IsNullOrEmpty(idxMSel))
{
IdxMaccSel = idxMSel;
await E_MachSel.InvokeAsync(idxMSel);
}
else
{
IdxMaccSel = ListMacchine.FirstOrDefault().Key;
}
}
}
}
}
private async void TDataService_DataInvalidated(object? sender, EventArgs e)
{
await ReloadSelector(true);
}
#endregion Private Methods
}
}