1671 lines
64 KiB
C#
1671 lines
64 KiB
C#
using EgwCoreLib.Razor.Data;
|
|
using EgwCoreLib.Utils;
|
|
using GPW.CORE.Data;
|
|
using GPW.CORE.Data.DbModels;
|
|
using GPW.CORE.Data.DTO;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.VisualBasic;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
|
|
namespace GPW.CORE.Smart.Data
|
|
{
|
|
public class CoreSmartDataService : IDisposable
|
|
{
|
|
#region Public Fields
|
|
|
|
public static CORE.Data.Controllers.GPWController dbController = null!;
|
|
public bool VetoInsert = false;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public CoreSmartDataService(IConfiguration configuration, ILogger<CoreSmartDataService> logger, IConnectionMultiplexer redisConnMult, IEmailSender emailSender)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
// Conf cache
|
|
redisConn = redisConnMult;// ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
|
//redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
|
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.GetValue<string>("CodApp") ?? "GPW";
|
|
var strVeto = _configuration.GetValue<string>("VetoIns") ?? "false";
|
|
_ = bool.TryParse(strVeto, out VetoInsert);
|
|
// Conf DB
|
|
string connStr = _configuration.GetConnectionString("GPW.DB") ?? "";
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new CORE.Data.Controllers.GPWController(configuration);
|
|
}
|
|
|
|
// init datapipe...
|
|
mPipeTimb = new MessagePipe(redisConn, Const.rPipeChTimb);
|
|
mPipeRich = new MessagePipe(redisConn, Const.rPipeChRich);
|
|
|
|
_logger.LogInformation("Avviata classe CoreSmartDataService");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public string CodApp { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Pipe messaggi richieste
|
|
/// </summary>
|
|
public MessagePipe mPipeRich { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Pipe messaggi timbrature
|
|
/// </summary>
|
|
public MessagePipe mPipeTimb { get; set; } = null!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Elenco AKV
|
|
/// </summary>
|
|
public async Task<List<AnagKeyValueModel>?> AKVList()
|
|
{
|
|
string source = "DB";
|
|
|
|
List<AnagKeyValueModel>? dbResult = new List<AnagKeyValueModel>();
|
|
// cerco da cache
|
|
string currKey = rKeyAKV;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagKeyValueModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagKeyValueModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagKeyValGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagKeyValueModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AKVList | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco fasi (tutte)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagClientiModel>> AnagClientiAll()
|
|
{
|
|
string source = "DB";
|
|
List<AnagClientiModel>? dbResult = new List<AnagClientiModel>();
|
|
string currKey = $"{rKeyCliAll}";
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagClientiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagClientiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagClientiAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagClientiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagClientiAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco fasi (tutte)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagFasiModel>> AnagFasiAll()
|
|
{
|
|
string source = "DB";
|
|
List<AnagFasiModel>? dbResult = new List<AnagFasiModel>();
|
|
string currKey = $"{rKeyFasiAll}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagFasiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagFasiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagFasiAll(false);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagFasiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagFasiAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera info x una specifica FASE
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<AnagFasiModel?> AnagFasiByKey(int idxFase)
|
|
{
|
|
string source = "DB";
|
|
AnagFasiModel? dbResult = new AnagFasiModel();
|
|
// cerco "secca "in redis...
|
|
string currKey = $"{rKeyFasiSearch}:{idxFase}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<AnagFasiModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new AnagFasiModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagFasiByKey(idxFase);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagFasiSearch | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco fasi dato progetto
|
|
/// </summary>
|
|
/// <param name="idxProj"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagFasiModel>?> AnagFasiByProj(int idxProj)
|
|
{
|
|
string source = "DB";
|
|
List<AnagFasiModel>? dbResult = new List<AnagFasiModel>();
|
|
// cerco "secca "in redis...
|
|
string currKey = $"{rKeyFasiByProj}:{idxProj}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagFasiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagFasiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagFasiByProj(idxProj);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagFasiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagFasiByProj | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Giustificativi
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagGiustModel>?> AnagGiust()
|
|
{
|
|
string source = "DB";
|
|
List<AnagGiustModel>? dbResult = new List<AnagGiustModel>();
|
|
// cerco "secca "in redis...
|
|
string currKey = $"{rKeyGiust}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagGiustModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagGiustModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagGiust();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagGiustModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagGiust | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco gruppi abilitati x il dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagGruppiModel>> AnagGruppiUser(int idxDipendente)
|
|
{
|
|
string source = "DB";
|
|
List<AnagGruppiModel>? dbResult = new List<AnagGruppiModel>();
|
|
string currKey = $"{rKeyGrpUser}:{idxDipendente}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagGruppiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagGruppiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagGruppiUser(idxDipendente);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagGruppiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagGruppiUser | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati orario globali da setup
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagOrariModel>> AnagOrarioAll()
|
|
{
|
|
string source = "DB";
|
|
List<AnagOrariModel> dbResult = new List<AnagOrariModel>();
|
|
string currKey = $"{rKeyAnOrDip}:ALL";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagOrariModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagOrariModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagOrarioGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagOrariModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagOrarioAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati orario del dipendente indicato
|
|
/// </summary>
|
|
/// <param name="idxProj"></param>
|
|
/// <returns></returns>
|
|
public async Task<AnagOrariModel> AnagOrarioByDip(int idxDip)
|
|
{
|
|
string source = "DB";
|
|
AnagOrariModel? dbResult = new AnagOrariModel();
|
|
string currKey = $"{rKeyAnOrDip}:{idxDip}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<AnagOrariModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new AnagOrariModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagOrarioByDip(idxDip);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new AnagOrariModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagOrarioByDip | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco progetti (tutti)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagProgettiModel>> AnagProjAll()
|
|
{
|
|
string source = "DB";
|
|
List<AnagProgettiModel>? dbResult = new List<AnagProgettiModel>();
|
|
string currKey = $"{rKeyProjAll}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagProgettiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AnagProgettiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AnagProjAll(false);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AnagProgettiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AnagProjAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista chiusure aziendali dato periodo
|
|
/// </summary>
|
|
/// <param name="dtStart"></param>
|
|
/// <param name="dtEnd"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<CalFesteFerieModel>> CalFestFeriePeriodo(DateTime dtStart, DateTime dtEnd)
|
|
{
|
|
string source = "DB";
|
|
List<CalFesteFerieModel>? dbResult = new List<CalFesteFerieModel>();
|
|
string currKey = $"{rKeyCalendari}:ClaFestFer:{dtStart:yyyyMMdd}:{dtEnd:yyyyMMdd}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<CalFesteFerieModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<CalFesteFerieModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.CalFestFeriePeriodo(dtStart, dtEnd);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<CalFesteFerieModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"CalFestFeriePeriodo | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco dei controlli VC19 nel periodo indicato x dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<CheckVc19Model>> CheckVC19List(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
string source = "DB";
|
|
List<CheckVc19Model>? dbResult = new List<CheckVc19Model>();
|
|
string currKey = $"{rKeyVC19}:{dtInizio:yyyyMMdd}:{dtFine:yyyMMdd}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<CheckVc19Model>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<CheckVc19Model>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.CheckVC19List(idxDipendente, dtInizio, dtFine);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<CheckVc19Model>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"CheckVC19List | {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>
|
|
/// Recupera l'elenco dei dettagli giornalieri attività di un dipendente, dato periodo riferimento
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<DailyDataDTO>> DailyDetails(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
string source = "DB";
|
|
List<DailyDataDTO>? dbResult = new List<DailyDataDTO>();
|
|
string currKey = $"{rKeyDailyData}:{idxDipendente}:{dtInizio.ToString("yyyy-MM-dd")}:{dtFine.ToString("yyyy-MM-dd")}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<DailyDataDTO>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<DailyDataDTO>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.DailyDetails(idxDipendente, dtInizio, dtFine);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"DailyDetails | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public string DecriptData(string encData)
|
|
{
|
|
return SteamCrypto.DecryptString(encData, passPhrase);
|
|
}
|
|
|
|
public async Task<AnagDeviceModel?> DeviceBySecret(string devSecret)
|
|
{
|
|
string source = "DB";
|
|
AnagDeviceModel? dbResult = new AnagDeviceModel();
|
|
List<AnagDeviceModel>? cachedResult = new List<AnagDeviceModel>();
|
|
string currKey = $"{rKeyUserSecrets}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AnagDeviceModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
cachedResult = new List<AnagDeviceModel>();
|
|
}
|
|
else
|
|
{
|
|
cachedResult = tempResult;
|
|
}
|
|
}
|
|
|
|
// controllo se c'è in cache...
|
|
dbResult = cachedResult.FirstOrDefault(x => x.DeviceSecret == devSecret);
|
|
if (dbResult == null)
|
|
{
|
|
source = "REDIS + DB";
|
|
//altrimenti cerco nel DB ed aggiungo
|
|
dbResult = dbController.AnagDeviceByKey(devSecret);
|
|
if (dbResult != null)
|
|
{
|
|
cachedResult.Add(dbResult);
|
|
rawData = JsonConvert.SerializeObject(cachedResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"DeviceBySecret | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Crea un record device
|
|
/// </summary>
|
|
/// <param name="newRecord"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeviceInsert(AnagDeviceModel newRecord)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool fatto = dbController.AnagDeviceInsert(newRecord);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"DeviceInsert | {source} in: {ts.TotalMilliseconds} ms");
|
|
await Task.Delay(1);
|
|
return fatto;
|
|
}
|
|
|
|
public async Task<List<Dipendenti2RuoliModel>> Dip2RuoliGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<Dipendenti2RuoliModel>? dbResult = new List<Dipendenti2RuoliModel>();
|
|
string? rawData = await redisDb.StringGetAsync(rKeyDip2Ruoli);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<Dipendenti2RuoliModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<Dipendenti2RuoliModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.Dipendenti2RuoliGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(rKeyDip2Ruoli, rawData, UltraLongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<Dipendenti2RuoliModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"Dip2RuoliGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<List<DipendentiModel>> DipendentiGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<DipendentiModel>? dbResult = new List<DipendentiModel>();
|
|
string? rawData = await redisDb.StringGetAsync(rKeyDipendenti);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<DipendentiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<DipendentiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.DipendentiGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(rKeyDipendenti, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DipendentiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"DipendentiGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record dipendenti
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DipendentiUpdate(DipendentiModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.DipendentiUpdate(currItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
public string EncriptData(string rawData)
|
|
{
|
|
return SteamCrypto.EncryptString(rawData, passPhrase);
|
|
}
|
|
|
|
public async Task<bool> FlushRedisCache()
|
|
{
|
|
await Task.Delay(1);
|
|
RedisValue pattern = new RedisValue($"{redisBaseAddr}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
public async Task<bool> RegAttDelete(RegAttivitaModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
dbController.RegAttDelete(currItem);
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDailyData}:*");
|
|
await ExecFlushRedisPattern($"{rKeyParetoRegAtt}:*");
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera ultima attività dipendente
|
|
/// </summary>
|
|
/// <param name="IdxDipendente"></param>
|
|
/// <param name="onlyActive">cerca ultima solo tra attive (=true) o tutte (=false)</param>
|
|
/// <returns></returns>
|
|
public async Task<RegAttivitaModel> RegAttLastByDip(int IdxDipendente, bool onlyActive)
|
|
{
|
|
await Task.Delay(1);
|
|
RegAttivitaModel dbResult = dbController.RegAttLastByDip(IdxDipendente, onlyActive);
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<bool> RegAttUpdate(RegAttivitaModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
dbController.RegAttUpdate(currItem);
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDailyData}:*");
|
|
await ExecFlushRedisPattern($"{rKeyParetoRegAtt}:*");
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua eliminazione record registro malattie (SE non confermato)
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RegMalattieDelete(RegMalattieModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.RegMalattieDelete(currItem);
|
|
if (answ)
|
|
{
|
|
// recupero info dipendente
|
|
var listDip = await DipendentiGetAll();
|
|
var currDip = listDip.FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente);
|
|
// gestisco email notifica...
|
|
string emailDest = _configuration.GetValue<string>("MailDest:InfoMal") ?? "samuele@steamware.net";
|
|
if (currDip.idxResp > 0)
|
|
{
|
|
string mailResp = await emailResp(currDip.idxResp);
|
|
if (!string.IsNullOrEmpty(mailResp) && !emailDest.Contains(mailResp))
|
|
{
|
|
emailDest += $",{mailResp}";
|
|
}
|
|
}
|
|
string urlRedir = _configuration.GetValue<string>("AdmApp:LinkMal") ?? "samuele@steamware.net";
|
|
StringBuilder sbMain = new StringBuilder();
|
|
DateTime adesso = DateTime.Now;
|
|
sbMain.AppendLine($"Il giorno {adesso:yyyy.MM.dd} alle ore {adesso:HH:mm:ss} è stato <b>rimosso</b> un record di registrazione malattia");
|
|
sbMain.AppendLine("");
|
|
sbMain.Append("<div style=\"font-size: 1.3em; color: #CC0066;\">");
|
|
sbMain.Append($"<b>{currItem.DipNav.Cognome}</b> {currItem.DipNav.Nome} ({currItem.DipNav.Matricola})");
|
|
//sbMain.Append($"<b>{currDip.Cognome}</b> {currDip.Nome} ({currDip.Matricola})");
|
|
sbMain.Append("</div>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Data Inizio: <b>{currItem.DtInizio}</b>");
|
|
sbMain.AppendLine($"Numero Giorni: <b>{currItem.NumGG}</b>");
|
|
sbMain.AppendLine($"Certificato: <b>{currItem.CodCert}</b>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Cliccare sul <a href=\"{urlRedir}\">seguente link</a> per accedere alla pagina di gestione");
|
|
string msgBody = sbMain.ToString();
|
|
await sendEmail(emailDest, "Eliminazione record Malattia Dipendente", msgBody.Replace($"{Environment.NewLine}", "<br/>"));
|
|
}
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDipendenti}:*");
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce elenco malattie globali
|
|
/// </summary>
|
|
/// <param name="maxRecord">max record da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<RegMalattieModel>> RegMalattieGetAll(int maxRecord)
|
|
{
|
|
string source = "DB";
|
|
List<RegMalattieModel>? dbResult = new List<RegMalattieModel>();
|
|
string currKey = $"{rKeyDipendenti}:ALL:RegMal:{maxRecord}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<RegMalattieModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<RegMalattieModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.RegMalattieGetAll(maxRecord);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<RegMalattieModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"RegMalattieGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce elenco malattie dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="maxRecord">max record da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<RegMalattieModel>> RegMalattieGetByDip(int idxDipendente, int maxRecord)
|
|
{
|
|
string source = "DB";
|
|
List<RegMalattieModel>? dbResult = new List<RegMalattieModel>();
|
|
string currKey = $"{rKeyDipendenti}:{idxDipendente}:RegMal:{maxRecord}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<RegMalattieModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<RegMalattieModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.RegMalattieGetByDip(idxDipendente, maxRecord);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<RegMalattieModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"RegMalattieGetByDip | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registra o aggiorna record registro malattie
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RegMalattieUpsert(RegMalattieModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
// scrivo su DB
|
|
answ = dbController.RegMalattieUpsert(currItem);
|
|
if (answ)
|
|
{
|
|
// recupero info dipendente
|
|
var listDip = await DipendentiGetAll();
|
|
var currDip = listDip.FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente);
|
|
// gestisco email notifica...
|
|
string emailDest = _configuration.GetValue<string>("MailDest:InfoMal") ?? "";
|
|
if (currDip.idxResp > 0)
|
|
{
|
|
string mailResp = await emailResp(currDip.idxResp);
|
|
if (!string.IsNullOrEmpty(mailResp) && !emailDest.Contains(mailResp))
|
|
{
|
|
emailDest += $",{mailResp}";
|
|
}
|
|
}
|
|
string urlRedir = _configuration.GetValue<string>("AdmApp:LinkMal") ?? "linkmal.is.missing";
|
|
StringBuilder sbMain = new StringBuilder();
|
|
DateTime adesso = DateTime.Now;
|
|
sbMain.AppendLine($"Il giorno {adesso:yyyy.MM.dd} alle ore {adesso:HH:mm:ss} è stato inserito un nuovo record di registrazione malattia");
|
|
sbMain.AppendLine("");
|
|
sbMain.Append("<div style=\"font-size: 1.3em;\">");
|
|
sbMain.Append($"<b>{currDip.Cognome}</b> {currDip.Nome} ({currDip.Matricola})");
|
|
sbMain.Append("</div>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Data Inizio: <b>{currItem.DtInizio}</b>");
|
|
sbMain.AppendLine($"Numero Giorni: <b>{currItem.NumGG}</b>");
|
|
sbMain.AppendLine($"Certificato: <b>{currItem.CodCert}</b>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Cliccare sul <a href=\"{urlRedir}\">seguente link</a> per accedere alla pagina di gestione");
|
|
|
|
string msgBody = sbMain.ToString();
|
|
if (!string.IsNullOrEmpty(emailDest))
|
|
{
|
|
await sendEmail(emailDest, "Nuovo record Malattia Dipendente", msgBody.Replace($"{Environment.NewLine}", "<br/>"));
|
|
}
|
|
}
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDipendenti}:*");
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua eliminazione record registro richieste dipendente (SE non confermato)
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RegRichiesteDelete(RegRichiesteModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.RegRichiesteDelete(currItem);
|
|
if (answ)
|
|
{
|
|
// recupero info dipendente
|
|
var listDip = await DipendentiGetAll();
|
|
var currDip = listDip.FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente);
|
|
// gestisco email notifica...
|
|
string emailDest = _configuration.GetValue<string>("MailDest:InfoRich") ?? "maildest.is.missing";
|
|
if (currDip.idxResp > 0)
|
|
{
|
|
string mailResp = await emailResp(currDip.idxResp);
|
|
if (!string.IsNullOrEmpty(mailResp) && !emailDest.Contains(mailResp))
|
|
{
|
|
emailDest += $",{mailResp}";
|
|
}
|
|
}
|
|
string urlRedir = _configuration.GetValue<string>("AdmApp:LinkRich") ?? "linkrich.is.missing";
|
|
StringBuilder sbMain = new StringBuilder();
|
|
DateTime adesso = DateTime.Now;
|
|
sbMain.AppendLine($"Il giorno {adesso:yyyy.MM.dd} alle ore {adesso:HH:mm:ss} è stato <b>rimosso</b> un record di registrazione richiesta utente");
|
|
sbMain.AppendLine("");
|
|
sbMain.Append("<div style=\"font-size: 1.3em; color: #CC0066;\">");
|
|
sbMain.Append($"<b>{currItem.DipNav.Cognome}</b> {currItem.DipNav.Nome} ({currItem.DipNav.Matricola})");
|
|
//sbMain.Append($"<b>{currDip.Cognome}</b> {currDip.Nome} ({currDip.Matricola})");
|
|
sbMain.Append("</div>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Richiesta: <b>{currItem.CodGiust}</b>");
|
|
sbMain.AppendLine($"Periodo: <b>{currItem.DtStart}</b> --> <b>{currItem.DtEnd}</b>");
|
|
sbMain.AppendLine($"Note: <b>{currItem.Note}</b>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Cliccare sul <a href=\"{urlRedir}\">seguente link</a> per accedere alla pagina di gestione");
|
|
string msgBody = sbMain.ToString();
|
|
if (!string.IsNullOrEmpty(emailDest))
|
|
{
|
|
await sendEmail(emailDest, $"Eliminazione Richiesta Dipendente | {currItem.CodGiust}", msgBody.Replace($"{Environment.NewLine}", "<br/>"));
|
|
}
|
|
}
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDipendenti}:*");
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce elenco richieste dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtFrom">Inizio periodo richiesto</param>
|
|
/// <param name="dtTo">Fine periodo richiesto</param>
|
|
/// <returns></returns>
|
|
public async Task<List<RegRichiesteModel>> RegRichiesteGetByDip(int idxDipendente, DateTime dtFrom, DateTime dtTo)
|
|
{
|
|
string source = "DB";
|
|
List<RegRichiesteModel>? dbResult = new List<RegRichiesteModel>();
|
|
string currKey = $"{rKeyDipendenti}:{idxDipendente}:RegRichieste:{dtFrom:yyyyMMdd}:{dtTo:yyyyMMdd}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<RegRichiesteModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<RegRichiesteModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.RegRichiesteGetByDip(idxDipendente, dtFrom, dtTo);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<RegRichiesteModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"RegRichiesteGetByDip | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registra o aggiorna record registro richieste
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RegRichiesteUpsert(RegRichiesteModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
// scrivo su DB
|
|
answ = dbController.RegRichiesteUpsert(currItem);
|
|
if (answ)
|
|
{
|
|
// recupero info dipendente
|
|
var listDip = await DipendentiGetAll();
|
|
var currDip = listDip.FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente);
|
|
// gestisco email notifica...
|
|
string emailDest = _configuration.GetValue<string>("MailDest:InfoRich") ?? "inforich.is.missing";
|
|
if (currDip.idxResp > 0)
|
|
{
|
|
string mailResp = await emailResp(currDip.idxResp);
|
|
if (!string.IsNullOrEmpty(mailResp) && !emailDest.Contains(mailResp))
|
|
{
|
|
emailDest += $",{mailResp}";
|
|
}
|
|
}
|
|
string urlRedir = _configuration.GetValue<string>("AdmApp:LinkRich") ?? "linkrich.is.missing";
|
|
StringBuilder sbMain = new StringBuilder();
|
|
DateTime adesso = DateTime.Now;
|
|
sbMain.AppendLine($"Il giorno {adesso:yyyy.MM.dd} alle ore {adesso:HH:mm:ss} è stato inserito una nuova richiesta dipendente");
|
|
sbMain.AppendLine("");
|
|
sbMain.Append("<div style=\"font-size: 1.3em;\">");
|
|
sbMain.Append($"<b>{currDip.Cognome}</b> {currDip.Nome} ({currDip.Matricola})");
|
|
sbMain.Append("</div>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Richiesta: <b>{currItem.CodGiust}</b>");
|
|
sbMain.AppendLine($"Periodo: <b>{currItem.DtStart}</b> --> <b>{currItem.DtEnd}</b>");
|
|
sbMain.AppendLine($"Note: <b>{currItem.Note}</b>");
|
|
sbMain.AppendLine("<hr/>");
|
|
sbMain.AppendLine($"Cliccare sul <a href=\"{urlRedir}\">seguente link</a> per accedere alla pagina di gestione");
|
|
|
|
string msgBody = sbMain.ToString();
|
|
if (!string.IsNullOrEmpty(emailDest))
|
|
{
|
|
await sendEmail(emailDest, $"Nuovo record Richiesta {currItem.CodGiust} Dipendente", msgBody.Replace($"{Environment.NewLine}", "<br/>"));
|
|
}
|
|
}
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDipendenti}:*");
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco dei Rilievi temperatura nel periodo indicato x dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<RilievoTempModel>> RilTempList(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
string source = "DB";
|
|
List<RilievoTempModel>? dbResult = new List<RilievoTempModel>();
|
|
string currKey = $"{rKeyRilTemp}:{dtInizio:yyyyMMdd}:{dtFine:yyyMMdd}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<RilievoTempModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<RilievoTempModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.RilTempList(idxDipendente, dtInizio, dtFine);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<RilievoTempModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"RilTempList | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registra update valore temp rilevata
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RilTempUpdate(RilievoTempModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
dbController.RilTempUpdate(currItem);
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyRilTemp}:*");
|
|
await ExecFlushRedisPattern($"{rKeyDailyData}:*");
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
public void rollBackEdit(object item)
|
|
{
|
|
dbController.rollBackEntity(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invio email tramite libreria
|
|
/// </summary>
|
|
/// <param name="destList">Elenco destinatari separati da virgola</param>
|
|
/// <param name="subject">Oggetto email</param>
|
|
/// <param name="message">Corpo Email (HTML)</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> sendEmailAsync(string destList, string subject, string message)
|
|
{
|
|
bool fatto = false;
|
|
List<string> emailDestList = destList.Split(",").ToList();
|
|
foreach (var dest in emailDestList)
|
|
{
|
|
try
|
|
{
|
|
await _emailSender.SendEmailAsync(dest, subject, message);
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante invio email:{Environment.NewLine}dest: {dest} | subject {subject}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco timbrature di un giorno
|
|
/// </summary>
|
|
/// <param name="dateRif"></param>
|
|
/// <param name="IdxDip"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TimbratureModel>> TimbratureDay(DateTime dateRif, int IdxDip)
|
|
{
|
|
await Task.Delay(1);
|
|
List<TimbratureModel>? dbResult = new List<TimbratureModel>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.TimbratureDay(dateRif, IdxDip);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TimbratureDay: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<bool> TimbratureDelete(TimbratureModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
dbController.TimbratureDelete(currItem);
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserimento richiesta mancata timbratura
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> TimbratureInsRichiesta(TimbratureModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
// aggiorna i campi x mancata timbratura (hard coded)
|
|
currItem.CodTipoTimb = "NoTim";
|
|
currItem.Approv = false;
|
|
// upsert!
|
|
answ = dbController.TimbratureUpdate(currItem);
|
|
// invio in pipe timbratura
|
|
mPipeRich.sendMessage(JsonConvert.SerializeObject(currItem));
|
|
Log.Info($"Registrata richiesta Mancata Timbratura | idxDip {currItem.IdxDipendente} | data-ora: {currItem.DataOra} | isEntrata: {currItem.Entrata}");
|
|
// invalido la cache...
|
|
await FlushRedisCache();
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco di tutte le timbrature richieste (= NON approvate)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<TimbratureModel>> TimbratureRichieste()
|
|
{
|
|
await Task.Delay(1);
|
|
List<TimbratureModel>? dbResult = new List<TimbratureModel>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.TimbratureRichieste();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TimbratureRichieste: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<bool> TimbratureUpdate(TimbratureModel currItem)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = dbController.TimbratureUpdate(currItem);
|
|
// invio in pipe timbratura
|
|
mPipeTimb.sendMessage(JsonConvert.SerializeObject(currItem));
|
|
// invalido la cache...
|
|
await ExecFlushRedisPattern($"{rKeyDailyData}:*");
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected const string passPhrase = "EB6BD8BE-638F-481E-85E4-F89012DA95BB";
|
|
|
|
protected const string redisBaseAddr = "GPW:Smart";
|
|
|
|
protected const string rKeyAKV = $"{redisBaseAddr}:Cache:AKV";
|
|
|
|
protected const string rKeyAnOrDip = $"{redisBaseAddr}:Cache:AnOrariDip";
|
|
|
|
protected const string rKeyCalcOreFase = $"{redisBaseAddr}:Cache:CalcOreFase";
|
|
|
|
protected const string rKeyCalcOreProj = $"{redisBaseAddr}:Cache:CalcOreProj";
|
|
|
|
protected const string rKeyCalendari = $"{redisBaseAddr}:Cache:Calendari";
|
|
|
|
protected const string rKeyCliAll = $"{redisBaseAddr}:Cache:CliAll";
|
|
|
|
protected const string rKeyConfig = $"{redisBaseAddr}:Cache:Config";
|
|
|
|
protected const string rKeyDailyData = $"{redisBaseAddr}:Cache:DailyData";
|
|
|
|
protected const string rKeyDip2Ruoli = $"{redisBaseAddr}:Cache:Dip2Ruoli";
|
|
|
|
protected const string rKeyDipendenti = $"{redisBaseAddr}:Cache:Dipendenti";
|
|
|
|
protected const string rKeyFasiAll = $"{redisBaseAddr}:Cache:FasiAll";
|
|
|
|
protected const string rKeyFasiByProj = $"{redisBaseAddr}:Cache:FasiByProj";
|
|
|
|
protected const string rKeyFasiSearch = $"{redisBaseAddr}:Cache:FasiSearch";
|
|
|
|
protected const string rKeyGiust = $"{redisBaseAddr}:Cache:Giustif";
|
|
|
|
protected const string rKeyGrpAll = $"{redisBaseAddr}:Cache:GrpAll";
|
|
|
|
protected const string rKeyGrpUser = $"{redisBaseAddr}:Cache:GrpUser";
|
|
|
|
protected const string rKeyParetoRegAtt = $"{redisBaseAddr}:Cache:ParetoRegAtt";
|
|
|
|
protected const string rKeyProjAll = $"{redisBaseAddr}:Cache:ProjAll";
|
|
|
|
protected const string rKeyRilTemp = $"{redisBaseAddr}:Cache:RilTemp";
|
|
|
|
protected const string rKeyUserSecrets = $"{redisBaseAddr}:Cache:DevSecrets";
|
|
|
|
protected const string rKeyVC19 = $"{redisBaseAddr}:Cache:VC19";
|
|
|
|
protected const string rKeyVetoIns = $"{redisBaseAddr}:Cache:VetoInsert";
|
|
|
|
protected const string rKeyWeekStats = $"{redisBaseAddr}:Cache:WeekStats";
|
|
|
|
protected static string connStringBBM = "";
|
|
|
|
protected Random rnd = new Random();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Calcola email del responsabile dato idx
|
|
/// </summary>
|
|
/// <param name="idxResp"></param>
|
|
/// <returns></returns>
|
|
protected async Task<string> emailResp(int idxResp)
|
|
{
|
|
string answ = "";
|
|
var listDip = await DipendentiGetAll();
|
|
// recupero email resp...
|
|
if (idxResp > 0)
|
|
{
|
|
var recResp = listDip.FirstOrDefault(x => x.IdxDipendente == idxResp);
|
|
if (recResp != null)
|
|
{
|
|
answ = $"{recResp.Email}";
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invio email tramite libreria
|
|
/// </summary>
|
|
/// <param name="destList"></param>
|
|
/// <param name="subject"></param>
|
|
/// <param name="message"></param>
|
|
/// <returns></returns>
|
|
protected async Task sendEmail(string destList, string subject, string message)
|
|
{
|
|
List<string> emailDestList = destList.Split(",").ToList();
|
|
foreach (var dest in emailDestList)
|
|
{
|
|
try
|
|
{
|
|
await _emailSender.SendEmailAsync(dest, subject, message);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante invio email:{Environment.NewLine}dest: {dest} | subject {subject}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static ILogger<CoreSmartDataService> _logger = null!;
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
private List<string> cachedDataList = new List<string>();
|
|
|
|
/// <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
|
|
}
|
|
} |