Files
mapo-core/MP.Data/Services/SharedMemService.cs
T
Samuele Locatelli 0d32c8e1d4 bozza sel macchine
2023-10-04 19:29:20 +02:00

173 lines
5.3 KiB
C#

using MP.Data.DatabaseModels;
using NLog;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
namespace MP.Data.Services
{
public class SharedMemService : BaseServ
{
#region Public Properties
/// <summary>
/// List configurazione attiva da tab DB
/// </summary>
public List<ConfigModel> DbConfig { get; set; } = new List<ConfigModel>();
/// <summary>
/// Dizionario configurazione attiva da tab DB
/// </summary>
public Dictionary<string, string> DictConfig { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Dizionario macchine (shared)
/// </summary>
public Dictionary<string, string> DictMacchine { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Dizionario macchine con info multi(bool)
/// </summary>
public Dictionary<string, int> DictMacchMulti { get; set; } = new Dictionary<string, int>();
/// <summary>
/// List configurazione attiva da tab DB
/// </summary>
public List<VMSFDModel> ListMSFD { get; set; } = new List<VMSFDModel>();
/// <summary>
/// Dizionario Menu
/// </summary>
public Dictionary<string, List<LinkMenu>> DictMenu { get; set; } = new Dictionary<string, List<LinkMenu>>();
public bool MenuOk
{
get => AllMenuData.Count > 0;
}
#endregion Public Properties
#region Public Methods
/// <summary>
/// Restituisce il livello pagina dato URL
/// - se contiene ?IdxMacc --&gt; T2D (detail)
/// - altrimenti --&gt; T2H
/// </summary>
/// <param name="fullUrl"></param>
/// <returns></returns>
public string PageLevel(string fullUrl)
{
string answ = "T2H";
var urlPart = fullUrl.Split("/");
string pageUrl = urlPart.Last().ToLower();
// cerco nell'elenco...
if (AllMenuData.Count > 0)
{
var sRec = AllMenuData
.Where(x => x.NavigateUrl.ToLower() == pageUrl)
.FirstOrDefault();
if (sRec != null)
{
answ = sRec.TipoLink;
}
}
return answ;
}
/// <summary>
/// Reset cache memory
/// </summary>
public void ClearCache()
{
DictMacchine = new Dictionary<string, string>();
DictMenu = new Dictionary<string, List<LinkMenu>>();
DbConfig = new List<ConfigModel>();
DictConfig = new Dictionary<string, string>();
Log.Info("SharedMemService | Cache resetted!");
}
public void SetConfig(List<ConfigModel>? newConfList)
{
DbConfig = newConfList ?? new List<ConfigModel>();
// salvo dizionario
DictConfig = DbConfig.ToDictionary(x => x.Chiave, x => x.Valore);
Log.Info("SharedMemService | SetConfig executed!");
}
public void SetMsfd(List<VMSFDModel> newList)
{
ListMSFD = newList ?? new List<VMSFDModel>();
// salvo dizionario
DictMacchMulti = ListMSFD.ToDictionary(x => x.IdxMacchina, x => x.Multi);
Log.Info("SharedMemService | SetMsfd executed!");
}
/// <summary>
/// Effettua setup memorie menu recuperando dati dal DB + popolando dizionario x livelli
/// </summary>
public void SetupMenu(List<LinkMenu> AllData)
{
// svuoto i valori in essere
DictMenu.Clear();
// salvo nuovo set
AllMenuData = AllData;
// ciclo x recuperare i distinti TIPI di menu...
var tList = AllMenuData
.Select(x => x.TipoLink)
.Distinct()
.ToList();
// per ogni tipo --> ciclo!
foreach (var item in tList)
{
var currMenu = AllMenuData
.Where(x => x.TipoLink == item)
.ToList();
if (!DictMenu.ContainsKey(item))
{
DictMenu.Add(item, currMenu);
}
}
Log.Info("SharedMemService | SetupMenu executed!");
}
public string GetConf(string chiave)
{
string answ = "";
if (DictConfig.ContainsKey(chiave))
{
answ = DictConfig[chiave];
}
return answ;
}
public int GetConfInt(string chiave)
{
int answ = 0;
int.TryParse(GetConf(chiave), out answ);
return answ;
}
public bool GetConfBool(string chiave)
{
bool answ = false;
bool.TryParse(GetConf(chiave), out answ);
return answ;
}
#endregion Public Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Properties
private List<LinkMenu> AllMenuData { get; set; } = new List<LinkMenu>();
#endregion Private Properties
}
}