Files
gpw_next/GPW.CORE.WRKLOG/Shared/LoginDisplay.razor.cs
T
2024-03-05 17:15:28 +01:00

297 lines
9.2 KiB
C#

using GPW.CORE.Data;
using GPW.CORE.Data.DbModels;
using GPW.CORE.WRKLOG.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Newtonsoft.Json;
namespace GPW.CORE.WRKLOG.Shared
{
public partial class LoginDisplay : IDisposable
{
#region Public Methods
public void Dispose()
{
GDataServ.mPipeTimb.EA_NewMessage -= MPipeTimb_EA_NewMessage;
GDataServ.mPipeRich.EA_NewMessage -= MPipeRich_EA_NewMessage;
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IConfiguration config { get; set; } = null!;
protected string cssActive { get => AppMServ.IsActive ? "text-success" : "text-danger"; }
protected string cssLicOk { get => AppMServ.PayloadOk ? "text-success" : "text-danger"; }
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Verifica informazioni condivise applicazione
/// </summary>
/// <returns></returns>
protected async Task<bool> checkActivations()
{
bool allOk = false;
if (LicServ.ValidData)
{
// se non fosse tutto ok
if (!LicServ.HasActivData)
{
// chiamo refresh licenze da remoto
allOk = await LicServ.RefreshLicense();
}
}
await Task.Delay(1);
return allOk;
}
/// <summary>
/// Verifica informazioni condivise applicazione
/// </summary>
/// <returns></returns>
protected async Task<bool> checkSharedInfo()
{
bool allOk = false;
if (!LicServ.ValidData)
{
// salvo cod app da Conf...
LicServ.Applicazione = GDataServ.CodApp;
// effettuo lettura dati preliminari da AKV (eventualmente in cache...)
List<AnagKeyValueModel>? rawAkvList = await GDataServ.AKVList();
if (rawAkvList != null)
{
LicServ.AKVList = rawAkvList;
allOk = LicServ.InitAkv();
}
}
await Task.Delay(1);
return allOk;
}
/// <summary>
/// Verifica info specifiche utente
/// </summary>
/// <returns></returns>
protected async Task<bool> checkUserLicense()
{
bool answ = false;
// verifico utente attivo
answ = AppMServ.IsActive;
if (answ)
{
var rawActList = LicServ.ActivList;
// recupero hash utente
string hashImpiego = AppMServ.HashDip;
// confronto con elenco attivazioni dello shared service...
var foundRec = rawActList.Where(x => x.CodImpiego == hashImpiego);
answ = foundRec.Count() > 0;
// salvo status payloadOk
AppMServ.PayloadOk = answ;
}
await Task.Delay(1);
return answ;
}
protected async Task DoReloadUtente()
{
AppMServ.RigaDip = null;
await Task.Delay(1);
await LoadUtente();
// verifico attivazione dipendente...
await checkUserLicense();
}
protected async Task DoVerifyActiv()
{
AppMServ.PayloadOk = false;
await Task.Delay(50);
await VerifyActiv();
}
protected async Task LoadUtente()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
// verifico se ho utente loggato...
if (user.Identity != null && user.Identity.IsAuthenticated)
{
userName = $"{user.Identity.Name}";
// cerco su DB
DipendentiModel? rigaDip = ListaDip.Where(x => $"{x.Dominio.Trim()}\\{x.Utente.Trim()}".ToLower() == userName.Trim().ToLower()).FirstOrDefault();
if (rigaDip != null)
{
// salvo riga info dipendente
AppMServ.RigaDip = rigaDip;
userName = $"{rigaDip.Cognome} {rigaDip.Nome}";
idxDipendente = rigaDip.IdxDipendente;
}
}
else
{
userName = "N.A.";
}
}
protected override async Task OnInitializedAsync()
{
// aggiungo gestione notifica messaggi timbrature...
GDataServ.mPipeTimb.EA_NewMessage += MPipeTimb_EA_NewMessage;
GDataServ.mPipeRich.EA_NewMessage += MPipeRich_EA_NewMessage;
// leggo lista dip
ListaDip = await GDataServ.DipendentiGetAll();
numTot = ListaDip.Count(x => x.Attivo == true);
//effettuo eventuale recupero dati dipendente
if (AppMServ.RigaDip == null)
{
await LoadUtente();
}
// verifica attivazione
await VerifyActiv();
// verifica rpesenza personale
await VerifPresenze(false);
}
protected async Task VerifyActiv()
{
// preliminarmente verifica shared info
await checkSharedInfo();
// refresh attivazioni se necessario
await checkActivations();
// verifico e salvo dati attivazione dipendente...
await checkUserLicense();
// verifica admin
checkAdmin();
}
#endregion Protected Methods
#region Private Fields
private int idxDipendente = 0;
private bool isAdmin = false;
private List<DipendentiModel> ListaDip = new List<DipendentiModel>();
private List<StatsDayPresModel> ListaPres = new List<StatsDayPresModel>();
private List<string> ListIdxAdmin = new List<string>();
private int numPres = 0;
private int numTot = 0;
private string userName = "";
#endregion Private Fields
#region Private Properties
private string lblDisplay { get; set; } = "";
[Inject]
private NavigationManager NavManager { get; set; } = null!;
#endregion Private Properties
#region Private Methods
private void checkAdmin()
{
// leggo elenco admin
string rawData = config.GetValue<string>("ServerConf:AdmList");
if (!string.IsNullOrEmpty(rawData))
{
ListIdxAdmin = rawData.Split(',').ToList();
}
isAdmin = ListIdxAdmin.Contains($"{idxDipendente}");
}
private async void MPipeRich_EA_NewMessage(object? sender, EventArgs e)
{
await processMessage(e);
}
private async void MPipeTimb_EA_NewMessage(object? sender, EventArgs e)
{
await processMessage(e);
}
private async Task processMessage(EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly List<string> --> allarmi
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
try
{
TimbratureModel? timbData = JsonConvert.DeserializeObject<TimbratureModel>(currArgs.newMessage);
if (timbData != null)
{
// solo se admin
if (isAdmin)
{
updateDisplay(timbData);
await InvokeAsync(StateHasChanged);
await ResetMessage(6);
}
}
}
catch
{ }
}
}
/// <summary>
/// Chiama reset messaggio dopo un periodo di sec richiesto...
/// </summary>
/// <returns></returns>
private async Task ResetMessage(int numSec)
{
// chiudo visualizzazione stato update
await Task.Delay(numSec * 1000);
lblDisplay = "";
await VerifPresenze(true);
await InvokeAsync(StateHasChanged);
}
private void updateDisplay(TimbratureModel timbData)
{
string tipo = timbData.Entrata ?? true ? "IN" : "OUT";
string dipData = $"Dip: {timbData.IdxDipendente}";
//cerco dip...
var currDip = ListaDip.Find(x => x.IdxDipendente == timbData.IdxDipendente);
if (currDip != null)
{
dipData = $"{currDip.Cognome} {currDip.Nome} ({timbData.IdxDipendente})";
}
lblDisplay = $"{tipo} | {dipData} | {timbData.DataOra:HH:mm:ss}";
}
private async Task VerifPresenze(bool resetCache)
{
if (resetCache)
{
await GDataServ.FlushRedisCache();
}
ListaPres = await GDataServ.PresenzeDay(DateTime.Now);
numPres = ListaPres.Sum(x => x.IsPresent);
}
#endregion Private Methods
}
}