415 lines
16 KiB
C#
415 lines
16 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 LiMan.GLS;
|
|
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, IConnectionMultiplexer redisConnMult, IEmailSender emailSender) : base(configuration, redisConnMult, emailSender)
|
|
{
|
|
// 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()
|
|
{
|
|
string source = "DB";
|
|
List<GLS.DatabaseModels.AnagApplicazioni>? dbResult = new List<GLS.DatabaseModels.AnagApplicazioni>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:GLS:Applicazioni";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.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>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ApplicazioniGLSGetAll | {source} in: {sw.Elapsed.TotalMilliseconds:N2} 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 FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniGLSUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<bool> DbForceMigrate()
|
|
{
|
|
return await Task.FromResult(dbControllerGLS.DbForceMigrate());
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
base.Dispose();
|
|
// Clear database controller
|
|
dbControllerGLS.Dispose();
|
|
}
|
|
|
|
public async Task<List<GLS.DatabaseModels.AnagInstallazioni>> InstallazioniGLSGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<GLS.DatabaseModels.AnagInstallazioni> dbResult = new List<GLS.DatabaseModels.AnagInstallazioni>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:GLS:Installazioni:List";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.AnagInstallazioni>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<GLS.DatabaseModels.AnagInstallazioni>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbControllerGLS.GetInstallazioni();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<GLS.DatabaseModels.AnagInstallazioni>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"InstallazioniGLSGetAll | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during InstallazioniGLSGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
|
|
#if false
|
|
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);
|
|
#endif
|
|
}
|
|
|
|
public async Task<bool> InstallazioniGLSUpdate(GLS.DatabaseModels.AnagInstallazioni currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateInstallazioni(currItem);
|
|
await FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in InstallazioniGLSUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
/// <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 = dbController.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 = dbController.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 = dbController.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 = dbController.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 = dbController.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()
|
|
{
|
|
string source = "DB";
|
|
List<GLS.DatabaseModels.LicenzeAttive> dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:GLS:Licenze:List";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
string rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.LicenzeAttive>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbControllerGLS.GetLicenze();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"LicenzeGLSGetAll | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during LicenzeGLSGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
|
|
#if false
|
|
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);
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera licenze SENZA cache
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<GLS.DatabaseModels.LicenzeAttive>> LicenzeGLSGetFilt(SelectGLS CurrFilter)
|
|
{
|
|
Stopwatch sw = new Stopwatch();
|
|
List<GLS.DatabaseModels.LicenzeAttive> dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
sw.Start();
|
|
dbResult = dbControllerGLS.GetLicenzeFilt(CurrFilter.OnlyActive, CurrFilter.OnlyUnlock, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel);
|
|
sw.Stop();
|
|
Log.Trace($"Effettuata lettura da DB per LicenzeGLSGetFilt: {sw.Elapsed.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 void rollBackEditGLS(object item)
|
|
{
|
|
dbControllerGLS.rollBackEntity(item);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected new static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Protected Fields
|
|
}
|
|
} |