Files
lux/Lux.UI/Components/Compo/Config/ProfileMan.razor.cs
T
2026-03-24 12:55:16 +01:00

364 lines
11 KiB
C#

using EgwCoreLib.Lux.Core.RestPayload;
using EgwCoreLib.Lux.Data.DbModel.Config;
using EgwCoreLib.Lux.Data.Services;
using EgwCoreLib.Lux.Data.Services.Config;
using EgwCoreLib.Lux.Data.Services.General;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Lux.UI.Components.Compo.Config
{
public partial class ProfileMan : IDisposable
{
#region Public Methods
/// <summary>
/// Dispose sottoscrizione canale
/// </summary>
public void Dispose()
{
DLService.PipeProfList.EA_NewMessage -= PipeProfList_EA_NewMessage;
}
#endregion Public Methods
#region Protected Methods
protected override void OnInitialized()
{
apiUrl = Config.GetValue<string>("ServerConf:Prog.ApiUrl") ?? "";
genericBasePath = Config.GetValue<string>("ServerConf:GenericBaseUrl") ?? "";
calcTag = Config.GetValue<string>("ServerConf:CalcTag") ?? "calc";
DLService.PipeProfList.EA_NewMessage += PipeProfList_EA_NewMessage;
}
protected override async Task OnParametersSetAsync()
{
await ReloadDataAsync();
UpdateTable();
}
#endregion Protected Methods
#region Private Fields
private List<ProfileModel> AllRecords = new();
private List<string> AllRecordsRedis = new();
private string apiUrl = "";
private string calcTag = "calc";
private EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS cEnvir = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW;
/// <summary>
/// Per eventuale gestione multi-profilo su multi tenant
/// </summary>
private string confProf = "Default";
private ProfileModel? currDispl = null;
private int currPage = 1;
private string DetReq = "";
private ProfileModel? EditRecord = null;
private string genericBasePath = "generic";
private bool isLoading = false;
private List<ProfileModel> ListRecords = new();
private int numRecord = 5;
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
private string btnResetCss
{
get => string.IsNullOrEmpty(searchVal) ? "btn-outline-light" : "btn-primary";
}
[Inject]
private IConfigDataService CDService { get; set; } = null!;
[Inject]
private IConfiguration Config { get; set; } = null!;
[Inject]
private IConfProfileService CPService { get; set; } = null!;
[Inject]
private ICalcRuidService CRService { get; set; } = null!;
[Inject]
private ICalcRequestService CService { get; set; } = null!;
private string cssBtnThresh
{
get => ListRecords != null && ListRecords.Count > 0 ? "btn-info" : "btn-secondary disabled";
}
[Inject]
private IDataLayerServices DLService { get; set; } = null!;
[Inject]
private IJSRuntime JSRuntime { get; set; } = null!;
private string searchVal { get; set; } = string.Empty;
private string SearchVal
{
get => searchVal;
set
{
if (searchVal != value)
{
searchVal = value;
_ = FullUpdate();
}
}
}
#endregion Private Properties
#region Private Methods
/// <summary>
/// Effettua vera richiesta elenco profili gestiti
/// </summary>
/// <returns></returns>
private async Task callRefreshProfList()
{
Dictionary<string, string> DictExec = new Dictionary<string, string>();
var cMode = Egw.Window.Data.Enums.QuestionModes.CONFIG;
var cSubMode = Egw.Window.Data.Enums.QuestionConfSubModes.PROFILELIST;
// compongo righiesta
string reqUid = "Default";
DictExec.Add("Mode", $"{(int)cMode}");
DictExec.Add("UID", reqUid);
// creo registrazione richiesta...
var ruid = await CRService.AddRequestAsync($"{cEnvir}", $"{cMode}-{cSubMode}", reqUid);
// aggiungo RUID effettivo
DictExec.Add("RUID", ruid);
DictExec.Add("SubMode", $"{(int)cSubMode}");
CalcRequestDTO req = new CalcRequestDTO()
{
EnvType = cEnvir,
DictExec = DictExec
};
// svuotiamo cache dati...
AllRecordsRedis = new List<string>();
AllRecords = new List<ProfileModel>();
isLoading = true;
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await CService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{reqUid}", req);
}
/// <summary>
/// Effettua richiesta delle soglie dati i profili
/// </summary>
/// <returns></returns>
private async Task callRefreshProfThresh()
{
Dictionary<string, string> DictExec = new Dictionary<string, string>();
var cMode = Egw.Window.Data.Enums.QuestionModes.CONFIG;
var cSubMode = Egw.Window.Data.Enums.QuestionConfSubModes.PROFILEDATA;
DictExec.Add("UID", "");
DictExec.Add("RUID", "");
DictExec.Add("ProfileName", "");
DictExec.Add("Mode", $"{(int)cMode}");
DictExec.Add("SubMode", $"{(int)cSubMode}");
// ciclo x tutti i profili...
foreach (var profile in ListRecords)
{
string reqUid = profile.Code;
DictExec["UID"] = reqUid;
DictExec["ProfileName"] = reqUid;
// creo registrazione richiesta...
var ruid = await CRService.AddRequestAsync($"{cEnvir}", $"{cMode}-{cSubMode}", reqUid);
// aggiungo RUID effettivo
DictExec["RUID"] = ruid;
CalcRequestDTO req = new CalcRequestDTO()
{
EnvType = cEnvir,
DictExec = DictExec
};
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await CService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{reqUid}", req);
}
}
private string checkSel(ProfileModel curRec)
{
string answ = "";
if (EditRecord != null)
{
answ = curRec.Code == EditRecord.Code ? "table-info" : "";
}
return answ;
}
private void DoCloseDetail(bool args)
{
DetReq = "";
currDispl = null;
}
/// <summary>
/// Eliminazione record
/// </summary>
/// <param name="selRec"></param>
private async Task DoDelete(ProfileModel selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler eliminare il record? Dettagli: {selRec.Code} | {selRec.Description}"))
return;
isLoading = true;
await InvokeAsync(StateHasChanged);
// esegue eliminazione del record...
await CPService.DeleteAsync(selRec);
// segnalo update x reload
await ReloadDataAsync();
UpdateTable();
}
/// <summary>
/// Edit record selezionato
/// </summary>
/// <param name="curRec"></param>
private void DoEdit(ProfileModel curRec)
{
EditRecord = curRec;
}
/// <summary>
/// Richiesta update elenco HW preferiti/configurati
/// </summary>
private Task DoReqUpdate()
{
// chiamata richiesta update da calc
return callRefreshProfList();
}
private Task DoReqUpdateThresh()
{
// chiamata richiesta update da calc
return callRefreshProfThresh();
}
/// <summary>
/// Update soglie da elenco profili
/// </summary>
/// <returns></returns>
/// <summary>
/// Reset selezione
/// </summary>
private void DoReset()
{
EditRecord = null;
}
/// <summary>
/// Salvataggio record
/// </summary>
/// <param name="curRec"></param>
private async Task DoSave(ProfileModel curRec)
{
await CPService.UpsertAsync(curRec);
EditRecord = null;
await ReloadDataAsync();
UpdateTable();
}
private async Task FullUpdate()
{
await ReloadDataAsync();
UpdateTable();
}
/// <summary>
/// Ricevuto update da Calc x elenco profili: aggiorno!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void PipeProfList_EA_NewMessage(object? sender, EventArgs e)
{
await FullUpdate();
await InvokeAsync(StateHasChanged);
}
private async Task ReloadDataAsync()
{
isLoading = true;
AllRecords = await CPService.GetAllAsync();
// se ho ricerca testuale faccio filtro ulteriore...
if (string.IsNullOrEmpty(SearchVal))
{
AllRecords = AllRecords
.OrderBy(x => x.Code)
.ToList();
}
else
{
AllRecords = AllRecords
.Where(x => x.Code.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) || x.Description.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase))
.OrderBy(x => x.Code)
.ToList();
}
totalCount = AllRecords.Count;
isLoading = false;
}
private void ResetSearch()
{
SearchVal = "";
}
private void SaveNumRec(int newNum)
{
numRecord = newNum;
UpdateTable();
}
private void SavePage(int newNum)
{
currPage = newNum;
UpdateTable();
}
private void ShowData(ProfileModel currRec)
{
DetReq = "Data";
currDispl = currRec;
}
private void ShowThresh(ProfileModel currRec)
{
DetReq = "Threshold";
currDispl = currRec;
}
/// <summary>
/// Filtro e paginazione
/// </summary>
private void UpdateTable()
{
// fix paginazione
ListRecords = AllRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
isLoading = false;
}
#endregion Private Methods
}
}