1117 lines
39 KiB
C#
1117 lines
39 KiB
C#
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using SHERPA.Data.Controllers;
|
|
using SHERPA.Data.DbModels;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
|
|
namespace SHERPA.AD.Data
|
|
{
|
|
public class SADDataService : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public SADDataService(IConfiguration configuration, ILogger<SADDataService> logger, IConnectionMultiplexer redisConnMult, IEmailSender emailSender)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
// Conf cache
|
|
redisConn = redisConnMult;
|
|
redisDb = this.redisConn.GetDatabase();
|
|
|
|
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
|
|
JSSettings = new JsonSerializerSettings()
|
|
{
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
};
|
|
|
|
// cod app
|
|
CodApp = _configuration["CodApp"];
|
|
// Conf DB
|
|
string connStr = _configuration.GetConnectionString("Sherpa.Fatt");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new SInManController(configuration);
|
|
}
|
|
_logger.LogInformation("Avviata classe GpwDataService");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Aggiorna un record Accounting Document
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AccDocUpdate(AccoDocModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.AccDocUpdate(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in AccDocUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Duplicazione doc selezionato
|
|
/// </summary>
|
|
/// <param name="idxDoc"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AccoDocClone(int idxDoc)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.AccoDocClone(idxDoc);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in AccoDocClone:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eliminazione doc selezionato
|
|
/// </summary>
|
|
/// <param name="item2del"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AccoDocDelete(vDocsExplModel item2del)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.AccoDocDelete(item2del.idxDoc);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in AccoDocDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera AccoDoc (Accounting Document) da Key
|
|
/// </summary>
|
|
/// <param name="DocId"></param>
|
|
/// <returns></returns>
|
|
public async Task<AccoDocModel> AccoDocGetByKey(int DocId)
|
|
{
|
|
string source = "DB";
|
|
AccoDocModel? dbResult = new AccoDocModel();
|
|
string currKey = $"{rKeyDoc}:{DocId}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<AccoDocModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new AccoDocModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AccoDocGetByKey(DocId);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new AccoDocModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AccoDocGetByKey | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista configurazione
|
|
/// </summary>
|
|
/// <param name="dtStart"></param>
|
|
/// <param name="dtEnd"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<ConfigModel>? dbResult = new List<ConfigModel>();
|
|
string currKey = $"{rKeyConfig}:Table";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ConfigModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ConfigModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ConfigGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ConfigModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ConfigGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera una singola chaive di config da cache/DB
|
|
/// </summary>
|
|
/// <param name="chiave"></param>
|
|
/// <returns></returns>
|
|
public async Task<ConfigModel?> ConfigGetKey(string chiave)
|
|
{
|
|
string source = "DB";
|
|
// cerco in cache direttamente la chiave... altrimenti da redis/db
|
|
ConfigModel? keyResult = null;
|
|
string currKey = $"{rKeyConfig}:Dict:{chiave}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<ConfigModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
keyResult = new ConfigModel();
|
|
}
|
|
else
|
|
{
|
|
keyResult = tempResult;
|
|
}
|
|
keyResult = JsonConvert.DeserializeObject<ConfigModel>(rawData);
|
|
}
|
|
else
|
|
{
|
|
var listConfig = await ConfigGetAll();
|
|
keyResult = listConfig.FirstOrDefault(x => x.Chiave == chiave);
|
|
rawData = JsonConvert.SerializeObject(keyResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ConfigGetKey | {chiave} | {source} in: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(keyResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Crea scadenze da elenco fatture
|
|
/// </summary>
|
|
/// <param name="idxDoc"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> CreaScadenze()
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.CreaScadenze();
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in CreaScadenze:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clona un item Customer
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> CustomerClona(CustomerModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.CustomerClona(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in CustomerClona:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina un item Customer
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> CustomerDelete(CustomerModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.CustomerDelete(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in CustomerDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Customers eliminabili
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<CustomerModel>> CustomersDeletable()
|
|
{
|
|
string source = "DB";
|
|
List<CustomerModel>? dbResult = new List<CustomerModel>();
|
|
string currKey = $"{rKeyCustDel}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<CustomerModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<CustomerModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.CustomersDeletable();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<CustomerModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"CustomersDeletable | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Customers
|
|
/// </summary>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<CustomerModel>> CustomersGetAll(string searchVal)
|
|
{
|
|
string source = "DB";
|
|
List<CustomerModel>? dbResult = new List<CustomerModel>();
|
|
string currKey = $"{rKeyCust}:ALL:{searchVal}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<CustomerModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<CustomerModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.CustomersGetAll(searchVal);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<CustomerModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"CustomersGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricerca Customer da idx
|
|
/// </summary>
|
|
/// <param name="idxCli"></param>
|
|
/// <returns></returns>
|
|
public async Task<CustomerModel> CustomersGetByKey(int idxCli)
|
|
{
|
|
string source = "DB";
|
|
CustomerModel? dbResult = new CustomerModel();
|
|
string currKey = $"{rKeyCust}:{idxCli}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<CustomerModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new CustomerModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.CustomersGetByKey(idxCli);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new CustomerModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"CustomersGetByKey | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Customers che richiedono sync tramite stored
|
|
/// </summary>
|
|
/// <param name="tipoDoc">Tipo doc, * = tutti</param>
|
|
/// <param name="anno">Anno doc, 0=tutti</param>
|
|
/// <param name="notUpl">Indica se solo non caricati (1 = NON caricati)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<CustomerModel>> CustomersToSync(string tipoDoc, int anno, bool notUpl)
|
|
{
|
|
string source = "DB";
|
|
List<CustomerModel>? dbResult = new List<CustomerModel>();
|
|
string currKey = $"{rKeySyncCust}:{anno}:{tipoDoc}:{notUpl}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<CustomerModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<CustomerModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.CustomersToSync(tipoDoc, anno, notUpl);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<CustomerModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"CustomersToSync | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un record Cliente
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> CustomerUpdate(CustomerModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.CustomerUpdate(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in CustomerUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
GC.Collect();
|
|
}
|
|
|
|
public async Task<bool> FlushRedisCache()
|
|
{
|
|
await Task.Delay(1);
|
|
RedisValue pattern = new RedisValue($"{redisBaseAddr}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clona un record Riga Doc
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RigaDocClona(RigheFattModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.RigaDocClona(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in RigaDocClona:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina una riga Doc
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RigaDocDelete(RigheFattModel item2Del)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.RigaDocDelete(item2Del.Id);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in RigaDocDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un item RigaDoc cambiando solamente il numero di riga (e spostando un eventuale
|
|
/// altro elemento al suo posto)
|
|
/// </summary>
|
|
/// <param name="delta"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RigaDocMove(RigheFattModel updItem, int delta)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.RigaDocMove(updItem, delta);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in RigaDocUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un record Riga Doc
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RigaDocUpdate(RigheFattModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.RigaDocUpdate(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in RigaDocUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clona un item VAT
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> VATClona(VatModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.VATClona(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in VATClona:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco VAT eliminabili
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<VatModel>> VATDeletable()
|
|
{
|
|
string source = "DB";
|
|
List<VatModel>? dbResult = new List<VatModel>();
|
|
string currKey = $"{rKeyVatDel}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<VatModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<VatModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VATDeletable();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<VatModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VATDeletable | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina un item VAT
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> VATDelete(VatModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.VATDelete(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in VATDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco VAT / CIva (tutti)
|
|
/// </summary>
|
|
/// <param name="tipoDoc">Tipo doc, * = tutti</param>
|
|
/// <param name="anno">Anno doc, 0=tutti</param>
|
|
/// <param name="notUpl">Indica se solo non caricati (1 = NON caricati)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<VatModel>> VATGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<VatModel>? dbResult = new List<VatModel>();
|
|
string currKey = $"{rKeyVatAll}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<VatModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<VatModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VATGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<VatModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VATGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricerca VAT / CIva da idx
|
|
/// </summary>
|
|
/// <param name="idxVat"></param>
|
|
/// <returns></returns>
|
|
public async Task<VatModel> VATGetByKey(int idxVat)
|
|
{
|
|
string source = "DB";
|
|
VatModel? dbResult = new VatModel();
|
|
string currKey = $"{rKeyVat}:{idxVat}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<VatModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new VatModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VATGetByKey(idxVat);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new VatModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VATGetByKey | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco VAT / CIva che richiedono sync tramite stored
|
|
/// </summary>
|
|
/// <param name="tipoDoc">Tipo doc, * = tutti</param>
|
|
/// <param name="anno">Anno doc, 0=tutti</param>
|
|
/// <param name="notUpl">Indica se solo non caricati (1 = NON caricati)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<VatModel>> VATToSync(string tipoDoc, int anno, bool notUpl)
|
|
{
|
|
string source = "DB";
|
|
List<VatModel>? dbResult = new List<VatModel>();
|
|
string currKey = $"{rKeySyncVat}:{anno}:{tipoDoc}:{notUpl}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<VatModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<VatModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VATToSync(tipoDoc, anno, notUpl);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<VatModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VATToSync | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un record VAT / CIva
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> VATUpdate(VatModel updItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.VATUpdate(updItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in VATUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco AccoDoc (Accounting Document) filtrati
|
|
/// </summary>
|
|
/// <param name="cFilt">Filtro complessivo di selezione</param>
|
|
/// <returns></returns>
|
|
public async Task<List<vDocsExplModel>> VDocExplGetFilt(SelectDocExp cFilt)
|
|
{
|
|
string source = "DB";
|
|
List<vDocsExplModel>? dbResult = new List<vDocsExplModel>();
|
|
string currKey = $"{rKeyDocExp}:{cFilt.Anno}:{cFilt.IdxCli}:{cFilt.CodTipo}:{cFilt.IdxGruppo}:{cFilt.RagSoc}:{cFilt.Inizio:yyyyMMdd}:{cFilt.Fine:yyyyMMdd}:{cFilt.Aperta}:{cFilt.SearchVal}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<vDocsExplModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<vDocsExplModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VDocExplGetFilt(cFilt.Anno, cFilt.IdxCli, cFilt.CodTipo, cFilt.IdxGruppo, cFilt.RagSoc, cFilt.Inizio, cFilt.Fine, cFilt.Aperta, cFilt.SearchVal);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<vDocsExplModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VDocExplGetFilt | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<List<vSelCliModel>> VSelCliGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<vSelCliModel>? dbResult = new List<vSelCliModel>();
|
|
string currKey = $"{rKeySelCli}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<vSelCliModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<vSelCliModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VSelCliGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<vSelCliModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VSelCliGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<List<vSelGruppiModel>> VSelGruppiGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<vSelGruppiModel>? dbResult = new List<vSelGruppiModel>();
|
|
string currKey = $"{rKeySelGruppi}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<vSelGruppiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<vSelGruppiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VSelGruppiGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<vSelGruppiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VSelGruppiGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<List<vSelTipoModel>> VSelTipoGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<vSelTipoModel>? dbResult = new List<vSelTipoModel>();
|
|
string currKey = $"{rKeySelTipo}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<vSelTipoModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<vSelTipoModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.VSelTipoGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<vSelTipoModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"VSelTipoGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected const string passPhrase = "322DBEDA-E470-421C-B4BB-B04EE3384A6B";
|
|
protected const string redisBaseAddr = "SHERPA:AD";
|
|
protected const string rKeyAKV = $"{redisBaseAddr}:Cache:AKV";
|
|
protected const string rKeyConfig = $"{redisBaseAddr}:Cache:Config";
|
|
protected const string rKeyCust = $"{redisBaseAddr}:Cache:Cust:List";
|
|
protected const string rKeyCustDel = $"{redisBaseAddr}:Cache:Cust:Del";
|
|
protected const string rKeyDoc = $"{redisBaseAddr}:Cache:Doc";
|
|
protected const string rKeyDocExp = $"{redisBaseAddr}:Cache:DocExpFilt";
|
|
protected const string rKeySelCli = $"{redisBaseAddr}:Cache:vSel:Cli";
|
|
protected const string rKeySelGruppi = $"{redisBaseAddr}:Cache:vSel:Gruppi";
|
|
protected const string rKeySelTipo = $"{redisBaseAddr}:Cache:vSel:Tipo";
|
|
protected const string rKeySyncCust = $"{redisBaseAddr}:Cache:Cust:CloudSync";
|
|
protected const string rKeySyncVat = $"{redisBaseAddr}:Cache:VAT:CloudSync";
|
|
protected const string rKeyVat = $"{redisBaseAddr}:Cache:VAT:List";
|
|
protected const string rKeyVatAll = $"{redisBaseAddr}:Cache:VAT:ALL";
|
|
protected const string rKeyVatDel = $"{redisBaseAddr}:Cache:VAT:Del";
|
|
protected static SInManController dbController = null!;
|
|
protected Random rnd = new Random();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
protected string CodApp { get; set; } = "";
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static ILogger<SADDataService> _logger = null!;
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlLong = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata cache breve IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlShort = 60 * 1;
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
private IConnectionMultiplexer redisConn;
|
|
|
|
//ISubscriber sub = redis.GetSubscriber();
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
private IDatabase redisDb = null!;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan FastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan LongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan UltraLongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
var listEndpoints = redisConn.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = redisConn.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
answ = true;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |