273 lines
10 KiB
C#
273 lines
10 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiMan.APi.Data
|
|
{
|
|
/// <summary>
|
|
/// Classe astrazione accesso dati
|
|
/// </summary>
|
|
public class ApiDataService : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
private static ILogger<ApiDataService> _logger;
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Fields
|
|
|
|
public static LiMan.DB.Controllers.DbController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="logger"></param>
|
|
public ApiDataService(IConfiguration configuration, ILogger<ApiDataService> logger)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
// conf DB
|
|
string connStrDB = _configuration.GetConnectionString("LiMan.DB");
|
|
if (string.IsNullOrEmpty(connStrDB))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new LiMan.DB.Controllers.DbController(configuration);
|
|
_logger.LogInformation("DbController OK");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Elenco licenze dato cliente
|
|
/// </summary>
|
|
/// <param name="CodInst">Codice Installaizone /Cliente</param>
|
|
/// <param name="CodApp">Codice Applicazione</param>
|
|
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
|
|
/// <returns></returns>
|
|
public async Task<List<DB.DTO.ApplicativoDTO>> ApplicativiSearch(string CodInst, string CodApp, bool HideData)
|
|
{
|
|
List<DB.DTO.ApplicativoDTO> dbResult = new List<DB.DTO.ApplicativoDTO>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
dbResult = dbController.GetApplicativiFilt(true, CodApp, CodInst, HideData);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per ApplicativiByCliente: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco licenze dato cliente
|
|
/// </summary>
|
|
/// <param name="Chiave">Licenza MASTER</param>
|
|
/// <param name="CodImpiego">Codice Impiego licenza</param>
|
|
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
|
|
/// <returns></returns>
|
|
public async Task<DB.DTO.AttivazioneDTO> AttivazioneSearch(string Chiave, string CodImpiego, bool HideData)
|
|
{
|
|
DB.DTO.AttivazioneDTO dbResult = new DB.DTO.AttivazioneDTO();
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
dbResult = dbController.GetAttivazione(Chiave, CodImpiego, HideData);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniSearch: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Attivaizoni da ID Licenza master
|
|
/// </summary>
|
|
/// <param name="IdxLic">Idx Licenza Master</param>
|
|
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
|
|
/// <returns></returns>
|
|
public async Task<List<DB.DTO.AttivazioneDTO>> AttivazioniByLic(int IdxLic, bool HideData)
|
|
{
|
|
List<DB.DTO.AttivazioneDTO> dbResult = new List<DB.DTO.AttivazioneDTO>();
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
dbResult = dbController.GetAttivazioniByLic(IdxLic, HideData);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniByLic: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Attivaizoni da valore Licenza master
|
|
/// </summary>
|
|
/// <param name="MasterKey">Licenza Master</param>
|
|
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
|
|
/// <returns></returns>
|
|
public async Task<List<DB.DTO.AttivazioneDTO>> AttivazioniByMasterKey(string MasterKey, bool HideData)
|
|
{
|
|
List<DB.DTO.AttivazioneDTO> dbResult = new List<DB.DTO.AttivazioneDTO>();
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
var licenza = dbController.GetLicenza(MasterKey);
|
|
if (licenza != null)
|
|
{
|
|
dbResult = dbController.GetAttivazioniByLic(licenza.IdxLic, HideData);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniByLic: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina un Attivaizone
|
|
/// </summary>
|
|
/// <param name="MasterKey">Licenza Master</param>
|
|
/// <param name="ParamDict">Elenco delle attivazioni da eliminare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AttivazioniDelete(string MasterKey, Dictionary<string, string> ParamDict)
|
|
{
|
|
bool answ = false;
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
var licenza = dbController.GetLicenza(MasterKey);
|
|
if (licenza != null)
|
|
{
|
|
answ = dbController.AttivazioniDelete(ParamDict, MasterKey);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniDelete: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina attivaizoni con veto scaduto
|
|
/// </summary>
|
|
/// <param name="MasterKey">Licenza Master</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AttivazioniResetAvail(string MasterKey)
|
|
{
|
|
bool answ = false;
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
var licenza = dbController.GetLicenza(MasterKey);
|
|
if (licenza != null)
|
|
{
|
|
answ = dbController.AttivazioniResetAvail(MasterKey);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniResetAvail: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua registrazione (se possibile) delle licenze indicate dall'elenco codici di impiego indicati
|
|
/// </summary>
|
|
/// <param name="MasterKey">Codice Licenza Master</param>
|
|
/// <param name="ParamDict">Elenco codici impiego (key) + valori in formato dizionari</param>
|
|
/// <param name="DayVeto">Numero giorni x scadenza veto modifica</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<bool> AttivazioniTryAdd(string MasterKey, Dictionary<string, string> ParamDict, int DayVeto)
|
|
{
|
|
bool taskDone = false;
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
taskDone = dbController.AttivazioniTryAdd(MasterKey, ParamDict, DayVeto);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata scrittura + rilettura da DB per AttivazioniTryAdd: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(taskDone);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose classe
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco licenze dato cliente
|
|
/// </summary>
|
|
/// <param name="cliente"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<LiMan.DB.DBModels.LicenzaModel>> LicenzeByCliente(string cliente)
|
|
{
|
|
List<DB.DBModels.LicenzaModel> dbResult = new List<DB.DBModels.LicenzaModel>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
dbResult = dbController.GetLicenzeFilt(true, "", cliente);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per LicenzeByCliente: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco licenze dato cliente
|
|
/// </summary>
|
|
/// <param name="CodInst">Codice Installaizone /Cliente</param>
|
|
/// <param name="CodApp">Codice Applicazione</param>
|
|
/// <param name="Chiave">Chiave Licenza da validare</param>
|
|
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
|
|
/// <returns></returns>
|
|
public async Task<List<DB.DTO.ApplicativoDTO>> LicenzeSearch(string CodInst, string CodApp, string Chiave, bool HideData)
|
|
{
|
|
List<DB.DTO.ApplicativoDTO> dbResult = new List<DB.DTO.ApplicativoDTO>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
dbResult = dbController.GetApplicativiFilt(true, CodApp, CodInst, HideData).Where(x => x.Chiave == Chiave).ToList();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per ApplicativiByCliente: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |