878 lines
35 KiB
C#
878 lines
35 KiB
C#
using Core;
|
|
using Core.DTO;
|
|
using Core.Models;
|
|
using Liman.CadCam.DbModel;
|
|
using LiMan.DB;
|
|
using LiMan.DB.Controllers;
|
|
using LiMan.DB.DBModels;
|
|
using LiMan.DB.DTO;
|
|
using LiMan.DB.Services;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using Org.BouncyCastle.Asn1.Pkcs;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Runtime;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static Core.Enum;
|
|
|
|
namespace LiMan.UI.Data
|
|
{
|
|
public class LiManDataService : CommonDataServices
|
|
{
|
|
#region Public Fields
|
|
|
|
public static GLS.Controllers.LicManController dbControllerGLS;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public LiManDataService(IConfiguration configuration, IMemoryCache memoryCache, IDistributedCache distributedCache, IConnectionMultiplexer redisConnMult, IEmailSender emailSender) : base(configuration, redisConnMult, emailSender)
|
|
{
|
|
// conf cache
|
|
this.memoryCache = memoryCache;
|
|
this.distributedCache = distributedCache;
|
|
// conf DB
|
|
string connStrGLS = _configuration.GetConnectionString("LiMan.GLS");
|
|
if (string.IsNullOrEmpty(connStrGLS))
|
|
{
|
|
Log.Error("Empty ConnString for LiMan.GLS!");
|
|
}
|
|
else
|
|
{
|
|
dbControllerGLS = new LiMan.GLS.Controllers.LicManController(configuration);
|
|
Log.Info("DbController for LiMan.GLS OK");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<List<GLS.DatabaseModels.AnagApplicazioni>> ApplicazioniGLSGetAll()
|
|
{
|
|
#if false
|
|
List<GLS.DatabaseModels.AnagApplicazioni> dbResult = new List<GLS.DatabaseModels.AnagApplicazioni>();
|
|
string cacheKey = mHash("GLS:Applicazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.AnagApplicazioni>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.GetApplicazioni();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per ApplicazioniGLSGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
#endif
|
|
string source = "DB";
|
|
List<GLS.DatabaseModels.AnagApplicazioni>? dbResult = new List<GLS.DatabaseModels.AnagApplicazioni>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:GLS:Applicazioni";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.AnagApplicazioni>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<GLS.DatabaseModels.AnagApplicazioni>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbControllerGLS.GetApplicazioni();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<GLS.DatabaseModels.AnagApplicazioni>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ApplicazioniGLSGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ApplicazioniGLSGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<bool> ApplicazioniGLSUpdate(GLS.DatabaseModels.AnagApplicazioni currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateApplicazioni(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniGLSUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<bool> ApplicazioniHasChild(string CodApp)
|
|
{
|
|
bool dbResult = false;
|
|
string cacheKey = mHash($"Next:Applicazioni:hasChild:{CodApp}");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<bool>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.ApplicazioniHasChild(CodApp);
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per ApplicazioniHasChild: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina il record indicato (se non ci sono record correlati...)
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ApplicazioniNextDelete(ApplicativoModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
// controllo NON ci siano record figli...
|
|
bool hasCHild = dbControllerNext.ApplicazioniHasChild(currItem.CodApp);
|
|
if (!hasCHild)
|
|
{
|
|
done = dbControllerNext.ApplicazioniNextDelete(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniNextDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public async Task<List<ApplicativoModel>> ApplicazioniNextGetAll()
|
|
{
|
|
List<ApplicativoModel> dbResult = new List<ApplicativoModel>();
|
|
string cacheKey = mHash("Next:Applicazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<ApplicativoModel>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.GetApplicazioni();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per ApplicazioniNextGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> ApplicazioniNextUpdate(ApplicativoModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerNext.ApplicazioniNextUpdate(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniNextUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua sblocco di una licenza impostando data veto a oggi
|
|
/// </summary>
|
|
/// <param name="idxSubLic"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AttivazioneUnlock(int idxSubLic)
|
|
{
|
|
bool fatto = dbControllerNext.AttivazioniUnlock(idxSubLic);
|
|
await Task.Delay(1);
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera attivazioni data licenza
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<SubLicenzaModel>> AttivazioniGetByLic(int IdxLic)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<SubLicenzaModel> dbResult = new List<SubLicenzaModel>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.AttivazioniGetByLic(IdxLic);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniGetByLic: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> DbForceMigrate()
|
|
{
|
|
return await Task.FromResult(dbControllerGLS.DbForceMigrate());
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
base.Dispose();
|
|
// Clear database controller
|
|
dbControllerGLS.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco file registrati dato ticket id
|
|
/// </summary>
|
|
/// <param name="idxTicket">Identificativo del ticket</param>
|
|
/// <returns></returns>
|
|
public async Task<List<FileAttachModel>> FileGetFilt(int idxTicket)
|
|
{
|
|
List<FileAttachModel> dbResult = new List<FileAttachModel>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.FileGetFilt(idxTicket);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per FileGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<GLS.DatabaseModels.AnagInstallazioni>> InstallazioniGLSGetAll()
|
|
{
|
|
List<GLS.DatabaseModels.AnagInstallazioni> dbResult = new List<GLS.DatabaseModels.AnagInstallazioni>();
|
|
string cacheKey = mHash("GLS:Installazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.AnagInstallazioni>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.GetInstallazioni();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per InstallazioniGLSGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> InstallazioniGLSUpdate(GLS.DatabaseModels.AnagInstallazioni currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateInstallazioni(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in InstallazioniGLSUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<InstallazioneModel>> InstallazioniNextGetAll()
|
|
{
|
|
List<InstallazioneModel> dbResult = new List<InstallazioneModel>();
|
|
string cacheKey = mHash("Next:Installazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<InstallazioneModel>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.GetInstallazioni();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per InstallazioniNextGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> InstallazioniNextUpdate(InstallazioneModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerNext.UpsertInstallazione(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in InstallazioniNextUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera info statistiche installazione dato periodo riferimento, da cache o da db
|
|
/// </summary>
|
|
/// <param name="dtStart"></param>
|
|
/// <param name="dtEnd"></param>
|
|
/// <param name="CodInst">Filtro Cliente (CodInstall)</param>
|
|
/// <param name="TipoApp">Filtro TipoApp</param>
|
|
public async Task<InstallStatusDTO> InstallStatusGetInfo(DateTime dtStart, DateTime dtEnd, string CodInst, string TipoApp)
|
|
{
|
|
string source = "DB";
|
|
InstallStatusDTO dbResult = new InstallStatusDTO();
|
|
try
|
|
{
|
|
string filtCli = string.IsNullOrEmpty(CodInst) ? "ALL-INSTALL" : $"{CodInst}";
|
|
string filtTipo = string.IsNullOrEmpty(TipoApp) ? "ALL-TIPO" : $"{TipoApp}";
|
|
string currKey = $"{Const.rKeyConfig}:InstVerSta:{filtTipo}:{filtCli}:{dtStart:yyyyMMdd-HHmmss}:{dtEnd:yyyyMMdd-HHmmss}";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<InstallStatusDTO>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new InstallStatusDTO();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbControllerNext.InstallStatusGetInfo(dtStart, dtEnd, CodInst, TipoApp);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new InstallStatusDTO();
|
|
}
|
|
sw.Stop();
|
|
TimeSpan ts = sw.Elapsed;
|
|
Log.Debug($"InstallStatusGetInfo | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during InstallStatusGetInfo:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// invalida tutta la cache in caso di update
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task InvalidateAllCache()
|
|
{
|
|
await distributedCache.RemoveAsync(mHash("GLS:Applicazioni"));
|
|
await distributedCache.RemoveAsync(mHash("Next:Applicazioni"));
|
|
await distributedCache.RemoveAsync(mHash("GLS:Installazioni"));
|
|
await distributedCache.RemoveAsync(mHash("Next:Installazioni"));
|
|
await distributedCache.RemoveAsync(mHash("GLS:Licenze"));
|
|
await distributedCache.RemoveAsync(mHash("Next:Licenze"));
|
|
//await distributedCache.RemoveAsync(mHash("SUPPL:List"));
|
|
//await distributedCache.RemoveAsync(mHash("TRANSP:List"));
|
|
//await distributedCache.RemoveAsync(mHash("WEEKPLAN:List"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trasferisce una licenza da GLS a Next come LOG di una licenza scaduta
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> LicenzaLogGlsNext(GLS.DatabaseModels.LicenzeAttive currItem, int IdxLicNext)
|
|
{
|
|
bool done = false;
|
|
|
|
var currLicenza = dbControllerNext.GetLicenza(IdxLicNext);
|
|
// step 1: converto licenza, faccio upsert
|
|
var logLicNext = new LogLicenzaModel()
|
|
{
|
|
CodApp = currItem.ApplicativoNavigation.Applicativo,
|
|
CodInst = currItem.InstallazioneNavigation.Installazione,
|
|
Chiave = currItem.Licenza,
|
|
NumLicenze = currItem.NumLicenze,
|
|
Scadenza = currItem.Scadenza,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione,
|
|
Tipo = TipoLicenza.GLS,
|
|
IdxLic = IdxLicNext,
|
|
Locked = true
|
|
};
|
|
bool step1 = dbControllerNext.UpsertLogLic(logLicNext);
|
|
|
|
if (currLicenza != null && step1)
|
|
{
|
|
// step 2: disattivo vecchia licenza
|
|
currItem.Locked = true;
|
|
done = dbControllerGLS.UpdateLicenze(currItem);
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trasferisce una licenza da GLS a Next
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> LicenzaTransferGlsNext(GLS.DatabaseModels.LicenzeAttive currItem)
|
|
{
|
|
bool done = false;
|
|
// step 1: controllo applicazione esistente
|
|
ApplicativoModel appNext = new ApplicativoModel()
|
|
{
|
|
CodApp = currItem.ApplicativoNavigation.Applicativo,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione
|
|
};
|
|
bool step1 = dbControllerNext.ApplicazioniNextUpdate(appNext);
|
|
|
|
// step 2: controllo installazione esistente
|
|
var instNext = new InstallazioneModel()
|
|
{
|
|
Cliente = currItem.InstallazioneNavigation.Installazione,
|
|
CodInst = currItem.InstallazioneNavigation.Installazione,
|
|
Contatto = currItem.InstallazioneNavigation.Contatto,
|
|
Email = currItem.InstallazioneNavigation.Email,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione
|
|
};
|
|
bool step2 = dbControllerNext.UpsertInstallazione(instNext);
|
|
|
|
// step 3: converto licenza, faccio upsert
|
|
var licNext = new LicenzaModel()
|
|
{
|
|
CodApp = currItem.ApplicativoNavigation.Applicativo,
|
|
CodInst = currItem.InstallazioneNavigation.Installazione,
|
|
Chiave = currItem.Licenza,
|
|
NumLicenze = currItem.NumLicenze,
|
|
Scadenza = currItem.Scadenza,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione,
|
|
Tipo = TipoLicenza.GLS,
|
|
Enigma = "",
|
|
Payload = "",
|
|
DataEnigma = DateTime.Now
|
|
};
|
|
bool step3 = dbControllerNext.UpsertLicenza(licNext);
|
|
|
|
if (step1 && step2 && step3)
|
|
{
|
|
// step 4: disattivo vecchia licenza
|
|
currItem.Locked = true;
|
|
done = dbControllerGLS.UpdateLicenze(currItem);
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<GLS.DatabaseModels.LicenzeAttive>> LicenzeGLSGetAll()
|
|
{
|
|
List<GLS.DatabaseModels.LicenzeAttive> dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
string cacheKey = mHash("GLS:Licenze");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.LicenzeAttive>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.GetLicenze();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per LicenzeGLSGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera licenze SENZA cache
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<GLS.DatabaseModels.LicenzeAttive>> LicenzeGLSGetFilt(SelectGLS CurrFilter)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<GLS.DatabaseModels.LicenzeAttive> dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.GetLicenzeFilt(CurrFilter.OnlyActive, CurrFilter.OnlyUnlock, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per LicenzeGLSGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> LicenzeGLSUpdate(GLS.DatabaseModels.LicenzeAttive currItem)
|
|
{
|
|
bool done = false;
|
|
#if false
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateApplicazioni(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
#endif
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<LicenzaModel>> LicenzeNextGetAll()
|
|
{
|
|
List<LicenzaModel> dbResult = new List<LicenzaModel>();
|
|
string cacheKey = mHash("Next:Licenze");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<LicenzaModel>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.GetLicenze();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per LicenzeNextGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera licenze SENZA cache
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<LicenzaModel>> LicenzeNextGetFilt(SelectNext CurrFilter)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<LicenzaModel> dbResult = new List<LicenzaModel>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.GetLicenzeFilt(CurrFilter.OnlyActive, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per LicenzeNextGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> LicenzeNextUpdate(LicenzaModel currItem)
|
|
{
|
|
bool done = false;
|
|
|
|
// chiamo Log licenza + update insieme
|
|
try
|
|
{
|
|
done = dbControllerNext.UpsertLicenza(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upsert record Release applicazione
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ReleaseUpsert(ReleaseModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = await dbControllerNext.ReleaseUpsert(currItem);
|
|
await InvalidateAllCache();
|
|
await FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ReleaseUpsert:{Environment.NewLine}{exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void rollBackEditGLS(object item)
|
|
{
|
|
dbControllerGLS.rollBackEntity(item);
|
|
}
|
|
|
|
public async Task<bool> SendEmail(string destEmail, string oggetto, string corpo)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
await _emailSender.SendEmailAsync(destEmail, oggetto, corpo);
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Statistiche del LOG chiamate all'API dato filtro
|
|
/// </summary>
|
|
/// <param name="DateFrom">Data minima</param>
|
|
/// <param name="DateTo">DataMax</param>
|
|
/// <param name="SearchVal">Valore cercato, se "" è tutti</param>
|
|
/// <returns></returns>
|
|
public async Task<List<StatsCallModel>> StatsLogCallGetFilt(DateTime DateFrom, DateTime DateTo, string SearchVal = "")
|
|
{
|
|
List<StatsCallModel> dbResult = new List<StatsCallModel>();
|
|
string cacheKey = mHash($"StatslogCall:{DateFrom:yyyyMMdd}:{DateTo:yyyyMMdd}");
|
|
if (!string.IsNullOrEmpty(SearchVal))
|
|
{
|
|
cacheKey += $":{SearchVal}";
|
|
}
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<StatsCallModel>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
var rawResult = dbControllerNext.StatsLogCallGetFilt(DateFrom, DateTo, SearchVal);
|
|
dbResult = rawResult
|
|
.OrderByDescending(x => x.YearRef)
|
|
.ThenByDescending(x => x.TotCall)
|
|
.ToList();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB + caching per StatsLogCallGetFilt: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera elenco Tickets filtrato
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TicketDTO>> TicketsGetAll()
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.TicketGetAll(false, 1000);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TicketsGetAll: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera elenco Tickets filtrato
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TicketDTO>> TicketsGetFilt(SelectNext CurrFilter)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.TicketGetFiltAllLic(CurrFilter.OnlyActive, Core.Enum.TipologiaTicket.ND, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel, 1000);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TicketsGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricerca ticket filtrati
|
|
/// </summary>
|
|
/// <param name="onlyOpen"></param>
|
|
/// <param name="CodApp"></param>
|
|
/// <param name="CodInst"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TicketDTO>> TicketsGetFilt(bool onlyOpen, string CodApp, string CodInst)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.TicketGetFilt(onlyOpen, CodApp, CodInst);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TicketsGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamentos tato ticket
|
|
/// </summary>
|
|
/// <param name="IdxTicket"></param>
|
|
/// <param name="NewStatus"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> TicketUpdateState(int IdxTicket, StatoRichiesta NewStatus)
|
|
{
|
|
bool fatto = false;
|
|
// inserimento!
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
fatto = dbControllerNext.TicketUpdateState(IdxTicket, NewStatus);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata update con TicketUpdateState: {ts.TotalMilliseconds} ms");
|
|
|
|
// restituisce elenco
|
|
return await Task.FromResult(fatto);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Internal Methods
|
|
|
|
/// <summary>
|
|
/// Effettua sblocco di una licenza impostando data veto a oggi
|
|
/// </summary>
|
|
/// <param name="idxSubLic"></param>
|
|
/// <returns></returns>
|
|
internal async Task<bool> AttivazioneDelete(int idxSubLic)
|
|
{
|
|
bool fatto = dbControllerNext.AttivazioniDelete(idxSubLic);
|
|
await Task.Delay(1);
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Internal Methods
|
|
|
|
#region Protected Methods
|
|
|
|
protected string getCacheKey(string TableName, SelectData CurrFilter)
|
|
{
|
|
string answ = $"{TableName}:D_{CurrFilter.DateStart:yyyyMMddHHmm}_{CurrFilter.DateEnd:yyyyMMddHHmm}";
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private readonly IDistributedCache distributedCache;
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
/// <summary>
|
|
/// Durata assoluta massima della cache IN SECONDI
|
|
/// </summary>
|
|
private int chAbsExp = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata della cache IN SECONDI in modalità inattiva (non acceduta) prima di venire
|
|
/// rimossa NON estende oltre il tempo massimo di validità della cache (chAbsExp)
|
|
/// </summary>
|
|
private int chSliExp = 60 * 1;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina,
|
|
/// StateMachineIngressi, ...)
|
|
/// </summary>
|
|
/// <param name="dataType"></param>
|
|
/// <returns></returns>
|
|
private static string mHash(string dataType)
|
|
{
|
|
return $"DATA:{dataType}";
|
|
}
|
|
|
|
private DistributedCacheEntryOptions cacheOpt(bool fastCache)
|
|
{
|
|
var numSecAbsExp = fastCache ? chAbsExp : chAbsExp * 10;
|
|
var numSecSliExp = fastCache ? chSliExp : chSliExp * 10;
|
|
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |