Files

238 lines
6.9 KiB
C#

using GPW.CORE.Data.DbModels;
using GPW.CORE.Data.DTO;
using GPW.CORE.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using System.Runtime.CompilerServices;
namespace GPW.CORE.ADM.Components.Compo
{
public partial class DipendentiEdit
{
#region Public Enums
/// <summary>
/// Modalità Edit
/// </summary>
public enum EditMode
{
None = 0,
KeyRegen,
ResetRequest,
FullEdit
}
#endregion Public Enums
#region Public Properties
[Parameter]
public EditMode CurrMode { get; set; } = EditMode.None;
[Parameter]
public DipendentiModel? CurrRecord { get; set; } = null;
[Parameter]
public EventCallback<DipendentiModel> EC_Activate { get; set; }
[Parameter]
public EventCallback<DipendentiModel> EC_Reissue { get; set; }
[Parameter]
public EventCallback<DipendentiModel> EC_Release { get; set; }
[Parameter]
public EventCallback<DipendentiModel> EC_Resync { get; set; }
[Parameter]
public EventCallback<DipendentiModel> EC_update { get; set; }
[Parameter]
public List<DipendentiModel> ListDipendenti { get; set; } = null!;
[Parameter]
public List<AnagOrariModel> ListOrari { get; set; } = null!;
#endregion Public Properties
#region Protected Fields
protected bool CheckActivation = false;
protected bool CheckActivationUnlocked = false;
protected string CodImpiego = "";
protected int IdxSubLic = 0;
protected bool LicenzeDisponibili = false;
protected bool LicenzeOk = false;
protected bool LockExpired = false;
protected int NumDipAct = 0;
protected DateTime UnlockDateLic = DateTime.Today.AddMonths(3);
#endregion Protected Fields
#region Protected Properties
[Inject]
protected MessageService AppMServ { get; set; } = null!;
[Inject]
protected LicenseService LicServ { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Attiva + Assegna Licenza
/// </summary>
/// <returns></returns>
protected async Task DoActivate()
{
await EC_Activate.InvokeAsync(CurrRecord);
}
protected async Task DoCancel()
{
await EC_update.InvokeAsync(CurrRecord);
}
/// <summary>
/// Assegna Licenza
/// </summary>
/// <returns></returns>
protected async Task DoReissue()
{
await EC_Reissue.InvokeAsync(CurrRecord);
}
/// <summary>
/// Rilascia Licenza
/// </summary>
/// <returns></returns>
protected async Task DoRelease()
{
await EC_Release.InvokeAsync(CurrRecord);
}
/// <summary>
/// Richiesta Rilascio Licenza
/// </summary>
/// <returns></returns>
protected async Task DoReqUnlock()
{
// invio richiesta sblocco licenza prima della scadenza naturale lock
//await EC_Release.InvokeAsync(CurrRecord);
await Task.Delay(1);
}
/// <summary>
/// Genera una nuova chiave utente + riassegna Licenza
/// </summary>
/// <returns></returns>
protected async Task DoResync()
{
await EC_Resync.InvokeAsync(CurrRecord);
}
protected async Task DoSave()
{
bool fatto = false;
if (CurrRecord != null)
{
DateTime adesso = DateTime.Now;
// chiama resync dati licenza (cod impiego / codAuth)
fatto = LicServ.SendTicketReq(ReqReset.Richiedente.Trim(), ReqReset.Email.Trim(), ReqReset.Telefono.Trim(), ReqReset.Causale.Trim(), CodImpiego, IdxSubLic);
// chiudo
LicServ.ResetTicketCache();
// reset richiesta
ReqReset = new DatiReqResetDTO();
#if false
// evento reset
raiseReset();
#endif
await EC_update.InvokeAsync(CurrRecord);
}
}
protected override void OnParametersSet()
{
DoActivCheck();
}
#endregion Protected Methods
#region Private Fields
private DatiReqResetDTO ReqReset = new DatiReqResetDTO();
#endregion Private Fields
#region Private Methods
/// <summary>
/// Effettua verifiche attivazioni e dati vari
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void DoActivCheck()
{
if (ListDipendenti != null && ListDipendenti.Count > 0)
{
NumDipAct = ListDipendenti.Where(x => x.Attivo ?? false).Count();
if (CurrRecord != null)
{
// calcolo CodImpiego
CodImpiego = LicServ.HashDip(CurrRecord);
UnlockDateLic = LockExpiry();
LockExpired = UnlockDateLic <= DateTime.Today.AddDays(1);
var onlineActInfo = LicServ.GetOnlineActivationInfo(CodImpiego);
if (onlineActInfo != null)
{
// verifico unlock licenza online...
#if false
CheckActivationUnlocked = LicServ.CheckActivationUnlocked(CodImpiego);
#endif
CheckActivation = onlineActInfo.CodImpiego == CodImpiego;
CheckActivationUnlocked = onlineActInfo.VetoUnlock <= DateTime.Today || string.IsNullOrEmpty(onlineActInfo.CodImpiego);
IdxSubLic = onlineActInfo.IdxSubLic;
}
// verifico licenze
LicenzeOk = NumDipAct <= LicServ.ActivList.Count();
// verifico licenze disponibili
LicenzeDisponibili = LicServ.AppLicense.NumLicenze > NumDipAct;
}
}
}
/// <summary>
/// Restitusice la data di unlock del dipendente
/// </summary>
private DateTime LockExpiry()
{
DateTime cessato = CurrRecord.DataCessazione ?? DateTime.Today.AddDays(-1);
bool isAttivo = CurrRecord.Attivo ?? false;
DateTime answ = isAttivo ? DateTime.Today.AddDays(90) : cessato;
// verifico SE sia disponibile licenza...
var activationsList = LicServ.ActivList;
var currAct = activationsList.Where(x => x.CodImpiego == CodImpiego).FirstOrDefault();
// se trovo record
if (currAct != null)
{
answ = currAct.VetoUnlock;
}
return answ;
}
#endregion Private Methods
}
}