Primo ciclo raccolta dati con elenco dipendenti
This commit is contained in:
@@ -43,7 +43,7 @@ namespace GPW.CORE.Data.Controllers
|
||||
}
|
||||
|
||||
|
||||
public List<DipendentiModel> MaterialsGetAll()
|
||||
public List<DipendentiModel> DipendentiGetAll()
|
||||
{
|
||||
List<DipendentiModel> dbResult = new List<DipendentiModel>();
|
||||
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace GPW.CORE.Data
|
||||
{
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
string connString = _configuration.GetConnectionString("NKC.DB");
|
||||
string connString = _configuration.GetConnectionString("GPW.DB");
|
||||
if (!string.IsNullOrEmpty(connString))
|
||||
{
|
||||
optionsBuilder.UseSqlServer(connString);
|
||||
|
||||
@@ -0,0 +1,332 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Newtonsoft.Json;
|
||||
using GPW.CORE.Data.DbModels;
|
||||
using NLog;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace GPW.CORE.UI.Data
|
||||
{
|
||||
public class GpwDataService : IDisposable
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private static IConfiguration _configuration = null!;
|
||||
private static ILogger<GpwDataService> _logger = null!;
|
||||
private static JsonSerializerSettings? JSSettings;
|
||||
|
||||
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
//private readonly IEmailSender _emailSender;
|
||||
//private readonly UserManager<IdentityUser> _userManager;
|
||||
private readonly IDistributedCache distributedCache;
|
||||
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private List<string> cachedDataList = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Durata assoluta massima della cache IN SECONDI
|
||||
/// </summary>
|
||||
private int chAbsExp = 60 * 5;
|
||||
|
||||
/// <summary>
|
||||
/// Durata della cache IN SECONDI in modalità inattiva (non acceduta) prima di venire rimossa
|
||||
/// NON estende oltre il tempo massimo di validità della cache (chAbsExp)
|
||||
/// </summary>
|
||||
private int chSliExp = 60 * 1;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Protected Fields
|
||||
|
||||
protected const string rKeyDipendenti = "Cache:Dipendenti";
|
||||
protected const string rKeyQrRemnants = "Cache:QrRemnants";
|
||||
protected const string rKeyRemnants = "Cache:Remnants";
|
||||
protected static string connStringBBM = "";
|
||||
|
||||
#endregion Protected Fields
|
||||
|
||||
#region Public Fields
|
||||
|
||||
public static CORE.Data.Controllers.GPWController dbController = null!;
|
||||
|
||||
#endregion Public Fields
|
||||
|
||||
#region Public Constructors
|
||||
|
||||
public GpwDataService(IConfiguration configuration, ILogger<GpwDataService> logger, IMemoryCache memoryCache, IDistributedCache distributedCache)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
// conf cache
|
||||
this.memoryCache = memoryCache;
|
||||
this.distributedCache = distributedCache;
|
||||
|
||||
// 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
|
||||
};
|
||||
// conf DB
|
||||
string connStr = _configuration.GetConnectionString("GPW.DB");
|
||||
if (string.IsNullOrEmpty(connStr))
|
||||
{
|
||||
_logger.LogError("ConnString empty!");
|
||||
}
|
||||
else
|
||||
{
|
||||
dbController = new CORE.Data.Controllers.GPWController(configuration);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private DistributedCacheEntryOptions cacheOpt(bool fastCache)
|
||||
{
|
||||
var numSecAbsExp = fastCache ? chAbsExp : chAbsExp * 10;
|
||||
var numSecSliExp = fastCache ? chSliExp : chSliExp * 10;
|
||||
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Clear database controller
|
||||
dbController.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// invalida tutta la cache in caso di update
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task InvalidateAllCache()
|
||||
{
|
||||
await distributedCache.RemoveAsync(rKeyDipendenti);
|
||||
await distributedCache.RemoveAsync(rKeyRemnants);
|
||||
foreach (var item in cachedDataList)
|
||||
{
|
||||
await distributedCache.RemoveAsync(item);
|
||||
}
|
||||
cachedDataList = new List<string>();
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<DipendentiModel>> DipendentiGetAll()
|
||||
{
|
||||
List<DipendentiModel>? dbResult = new List<DipendentiModel>();
|
||||
string rawData;
|
||||
var redisDataList = await distributedCache.GetAsync(rKeyDipendenti);
|
||||
if (redisDataList != null)
|
||||
{
|
||||
rawData = Encoding.UTF8.GetString(redisDataList);
|
||||
dbResult = JsonConvert.DeserializeObject<List<DipendentiModel>>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
dbResult = dbController.DipendentiGetAll();
|
||||
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
||||
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
||||
await distributedCache.SetAsync(rKeyDipendenti, redisDataList, cacheOpt(false));
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuata lettura da DB + caching per DipendentiGetAll: {ts.TotalMilliseconds} ms");
|
||||
}
|
||||
if (dbResult == null)
|
||||
{
|
||||
dbResult = new List<DipendentiModel>();
|
||||
}
|
||||
return await Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
#if false
|
||||
public async Task<List<MaterialDTO>> MaterialsGetAll()
|
||||
{
|
||||
List<MaterialDTO>? dbResult = new List<MaterialDTO>();
|
||||
string rawData;
|
||||
var redisDataList = await distributedCache.GetAsync(rKeyDipendenti);
|
||||
if (redisDataList != null)
|
||||
{
|
||||
rawData = Encoding.UTF8.GetString(redisDataList);
|
||||
dbResult = JsonConvert.DeserializeObject<List<MaterialDTO>>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
dbResult = dbController.MaterialsGetAll();
|
||||
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
||||
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
||||
await distributedCache.SetAsync(rKeyDipendenti, redisDataList, cacheOpt(false));
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuata lettura da DB + caching per GetMaterials: {ts.TotalMilliseconds} ms");
|
||||
}
|
||||
if (dbResult == null)
|
||||
{
|
||||
dbResult = new List<MaterialDTO>();
|
||||
}
|
||||
return await Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
public async Task<List<MovMagModel>> MovMagGetFilt(int RemnId, int numShow)
|
||||
{
|
||||
List<MovMagModel> dbResult = new List<MovMagModel>();
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
dbResult = dbController.MovMagGetFilt(RemnId, numShow);
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuata lettura da DB + caching per MovMagGetFilt: {ts.TotalMilliseconds} ms");
|
||||
return await Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
public async Task<bool> AddPrintJob(int RemnId)
|
||||
{
|
||||
bool answ = dbController.AddPrintJob("docRemnant", $"{RemnId}", "queueRemnants");
|
||||
return await Task.FromResult(answ);
|
||||
}
|
||||
|
||||
public async Task<List<RemnantsModel>> RemnantsGetFilt(int matId, int minQty)
|
||||
{
|
||||
List<RemnantsModel>? dbResult = new List<RemnantsModel>();
|
||||
string rawData;
|
||||
string cacheKey = $"{rKeyRemnants}:{matId}:{minQty}";
|
||||
if (!cachedDataList.Contains(cacheKey))
|
||||
{
|
||||
cachedDataList.Add(cacheKey);
|
||||
}
|
||||
|
||||
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
||||
if (redisDataList != null)
|
||||
{
|
||||
rawData = Encoding.UTF8.GetString(redisDataList);
|
||||
dbResult = JsonConvert.DeserializeObject<List<RemnantsModel>>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
var rawList = dbController.RemnantsGetFilt(matId, minQty);
|
||||
dbResult = rawList.OrderBy(o => o.Area).ToList();
|
||||
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
||||
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
||||
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(false));
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuata lettura da DB + caching per RemnantsGetAll: {ts.TotalMilliseconds} ms");
|
||||
}
|
||||
if (dbResult == null)
|
||||
{
|
||||
dbResult = new List<RemnantsModel>();
|
||||
}
|
||||
return await Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
public async Task<bool> RemnantsIsDupl(RemnantsModel currItem)
|
||||
{
|
||||
bool answ = false;
|
||||
var rawList = dbController.RemnantsGetFilt(currItem.MatID, 0);
|
||||
var duplicati = rawList
|
||||
.Where(x => x.RemnID != currItem.RemnID && x.LMm == currItem.LMm && x.WMm == currItem.WMm && x.TMm == currItem.TMm)
|
||||
.ToList();
|
||||
answ = duplicati.Count > 0;
|
||||
return await Task.FromResult(answ);
|
||||
}
|
||||
|
||||
public async Task<bool> RemnantsMovMag(RemnantsModel currItem, string userId, int deltaQty)
|
||||
{
|
||||
bool done = false;
|
||||
try
|
||||
{
|
||||
// recupero item da DB
|
||||
var currRecord = dbController.RemnantGetByid(currItem.RemnID);
|
||||
if (currRecord != null && currRecord.RemnID == currItem.RemnID)
|
||||
{
|
||||
// modifico qty entro limiti >=0..
|
||||
if (currRecord.QtyAvail + deltaQty >= 0)
|
||||
{
|
||||
currRecord.QtyAvail = currRecord.QtyAvail + deltaQty;
|
||||
done = dbController.RemnantsUpsert(currRecord, userId);
|
||||
await InvalidateAllCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in RemnantsMovMag:{Environment.NewLine}{exc}");
|
||||
}
|
||||
return await Task.FromResult(done);
|
||||
}
|
||||
|
||||
public async Task<bool> RemnantsUpsert(RemnantsModel currItem, string userId)
|
||||
{
|
||||
bool done = false;
|
||||
try
|
||||
{
|
||||
done = dbController.RemnantsUpsert(currItem, userId);
|
||||
await InvalidateAllCache();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in RemnantsUpsert:{Environment.NewLine}{exc}");
|
||||
}
|
||||
return await Task.FromResult(done);
|
||||
}
|
||||
#endif
|
||||
|
||||
public void rollBackEdit(object item)
|
||||
{
|
||||
dbController.rollBackEntity(item);
|
||||
}
|
||||
|
||||
#if false
|
||||
public async Task<RemnantsModel> SearchQrRemnant(string QrCode)
|
||||
{
|
||||
RemnantsModel? answ = new RemnantsModel();
|
||||
string rawData = "";
|
||||
string cacheKey = $"{rKeyQrRemnants}:{QrCode}";
|
||||
// cerco in redis
|
||||
var redisData = await distributedCache.GetAsync(cacheKey);
|
||||
if (redisData != null)
|
||||
{
|
||||
rawData = Encoding.UTF8.GetString(redisData);
|
||||
answ = JsonConvert.DeserializeObject<RemnantsModel>(rawData);
|
||||
}
|
||||
// se non trovo cerco su DB
|
||||
else
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
var foundItem = dbController.RemnantGetByQr(QrCode);
|
||||
if (foundItem != null && foundItem.RemDtmx == QrCode)
|
||||
{
|
||||
rawData = JsonConvert.SerializeObject(foundItem, JSSettings);
|
||||
redisData = Encoding.UTF8.GetBytes(rawData);
|
||||
await distributedCache.SetAsync(cacheKey, redisData, cacheOpt(false));
|
||||
answ = foundItem;
|
||||
}
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuata lettura da DB + caching per SearchQrRemnant: {ts.TotalMilliseconds} ms");
|
||||
}
|
||||
if (answ == null)
|
||||
{
|
||||
answ = new RemnantsModel();
|
||||
}
|
||||
return await Task.FromResult(answ);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
namespace GPW.CORE.UI.Data
|
||||
{
|
||||
public class MessageService
|
||||
{
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private string _pageIcon = "";
|
||||
|
||||
private string _pageName = "";
|
||||
|
||||
private string _searchVal = "";
|
||||
|
||||
private int _matId;
|
||||
|
||||
private bool showSearch;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Public Events
|
||||
|
||||
public event Action EA_HideSearch = null!;
|
||||
|
||||
public event Action EA_PageUpdated = null!;
|
||||
|
||||
public event Action EA_SearchUpdated = null!;
|
||||
|
||||
public event Action EA_ShowSearch = null!;
|
||||
|
||||
#endregion Public Events
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public string PageIcon
|
||||
{
|
||||
get => _pageIcon;
|
||||
set
|
||||
{
|
||||
if (_pageIcon != value)
|
||||
{
|
||||
_pageIcon = value;
|
||||
ReportPageUpd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string PageName
|
||||
{
|
||||
get => _pageName;
|
||||
set
|
||||
{
|
||||
if (_pageName != value)
|
||||
{
|
||||
_pageName = value;
|
||||
ReportPageUpd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string SearchVal
|
||||
{
|
||||
get => _searchVal;
|
||||
set
|
||||
{
|
||||
if (_searchVal != value)
|
||||
{
|
||||
_searchVal = value;
|
||||
|
||||
if (EA_SearchUpdated != null)
|
||||
{
|
||||
EA_SearchUpdated?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int MatIdSel
|
||||
{
|
||||
get => _matId;
|
||||
set
|
||||
{
|
||||
if (_matId != value)
|
||||
{
|
||||
_matId = value;
|
||||
|
||||
if (EA_SearchUpdated != null)
|
||||
{
|
||||
EA_SearchUpdated?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool ShowSearch
|
||||
{
|
||||
get => showSearch;
|
||||
set
|
||||
{
|
||||
if (showSearch != value)
|
||||
{
|
||||
showSearch = value;
|
||||
if (showSearch)
|
||||
{
|
||||
if (EA_ShowSearch != null)
|
||||
{
|
||||
EA_ShowSearch?.Invoke();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (EA_HideSearch != null)
|
||||
{
|
||||
EA_HideSearch?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Public Properties
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void ReportPageUpd()
|
||||
{
|
||||
if (EA_PageUpdated != null)
|
||||
{
|
||||
EA_PageUpdated?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReportSearch()
|
||||
{
|
||||
if (EA_SearchUpdated != null)
|
||||
{
|
||||
EA_SearchUpdated?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="NLog" Version="4.7.13" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GPW.CORE.Data\GPW.CORE.Data.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user