b83731097b
+ fix modifica posizioni
1027 lines
37 KiB
C#
1027 lines
37 KiB
C#
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using StockMan.Data.Controllers;
|
|
using StockMan.Data.DbModels;
|
|
using System.Diagnostics;
|
|
|
|
namespace StockMan.CORE.Data
|
|
{
|
|
public class StockDataService : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public StockDataService(IConfiguration configuration, ILogger<StockDataService> 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["CodApp"];
|
|
// Conf DB
|
|
string connStr = _configuration.GetConnectionString("StockMan.DB");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new StoManController(configuration);
|
|
}
|
|
_logger.LogInformation("Avviata classe StockDataService");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<bool> ConfermaMov(int itemID)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool result = false;
|
|
string source = "DB";
|
|
result = await dbController.ConfermaMov(itemID);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ConfermaMov Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// eliminazione record flux
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteFlux(ItemFluxModel currRec)
|
|
{
|
|
var dbResult = await dbController.DeleteFlux(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItemFlux}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
GC.Collect();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public async Task<bool> EliminaDup(int itemID)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool result = false;
|
|
string source = "DB";
|
|
result = await dbController.EliminaDup(itemID);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"EliminaDup Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Svuotamento comleto cache REDIS
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushRedisCache()
|
|
{
|
|
await Task.Delay(1);
|
|
RedisValue pattern = new RedisValue($"{redisBaseAddr}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// inserimento nuovo record item flux
|
|
/// </summary>
|
|
/// <param name="newRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> FluxAdd(ItemFluxModel newRec)
|
|
{
|
|
var dbResult = await dbController.FluxAdd(newRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItemFlux}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento record selezionato flusso
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> FluxMod(ItemFluxModel currRec)
|
|
{
|
|
var dbResult = await dbController.FluxMod(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItemFlux}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// inserimento nuovo record item
|
|
/// </summary>
|
|
/// <param name="newRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemAdd(ItemModel newRec)
|
|
{
|
|
var dbResult = await dbController.ItemAdd(newRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItems}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// inserimento nuovo record famiglia item
|
|
/// </summary>
|
|
/// <param name="newRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemFamilyAdd(ItemFamilyModel newRec)
|
|
{
|
|
var dbResult = await dbController.ItemFamilyAdd(newRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItemFamily}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// eliminazione record item family
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemFamilyDeleteByID(ItemFamilyModel currRec)
|
|
{
|
|
var dbResult = await dbController.ItemFamilyDeleteByID(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItemFamily}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Famiglie Articoli
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemFamilyModel>> ItemFamilyGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<ItemFamilyModel>? dbResult = new List<ItemFamilyModel>();
|
|
string currKey = $"{rKeyItemFamily}:All";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ItemFamilyModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ItemFamilyModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemFamilyGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemFamilyModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemFamilyGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Famiglie Articoli da ricerca
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemFamilyModel>> ItemFamilyGetSearch(string searchVal)
|
|
{
|
|
string source = "DB";
|
|
List<ItemFamilyModel>? dbResult = new List<ItemFamilyModel>();
|
|
string currKey = $"{rKeyItemFamily}:Search:{searchVal}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ItemFamilyModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ItemFamilyModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemFamilyGetSearch(searchVal);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemFamilyModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemFamilyGetSearch | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento record selezionato famiglia item
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemFamilyMod(ItemFamilyModel currRec)
|
|
{
|
|
var dbResult = await dbController.ItemFamilyMod(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItemFamily}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista flussi x item da cercare
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemFluxModel>> ItemFluxGetById(int id)
|
|
{
|
|
string source = "DB";
|
|
List<ItemFluxModel>? dbResult = new List<ItemFluxModel>();
|
|
string currKey = $"{rKeyItemFlux}:Id:{id}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ItemFluxModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ItemFluxModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemFluxGetById(id);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemFluxModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemFluxGetById | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento record selezionato item
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemMod(ItemModel currRec)
|
|
{
|
|
var dbResult = await dbController.ItemMod(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyItems}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Articoli da ricerca
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemModel>> ItemsGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<ItemModel>? dbResult = new List<ItemModel>();
|
|
string currKey = $"{rKeyItems}:*";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ItemModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ItemModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemsGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemsGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Articoli da ricerca per id
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemModel>> ItemsGetByID(int id)
|
|
{
|
|
string source = "DB";
|
|
List<ItemModel>? dbResult = new List<ItemModel>();
|
|
string currKey = "";
|
|
currKey = $"{rKeyItems}:Id:{id}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
dbResult = dbController.ItemsGetByID(id);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemsGetByID | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Articoli da ricerca per location
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemStockModel>> ItemsGetByLocation(string location)
|
|
{
|
|
string source = "DB";
|
|
List<ItemStockModel>? dbResult = new List<ItemStockModel>();
|
|
string currKey = "";
|
|
currKey = $"{rKeyItems}:Location:{location}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
dbResult = dbController.ItemsGetByLocation(location);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemStockModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemsGetByLocation | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
///// <summary>
|
|
///// Lista Articoli da ricerca
|
|
///// </summary>
|
|
///// <returns></returns>
|
|
//public async Task<List<ItemModel>> ItemsGetSearch(string searchVal, string famID)
|
|
//{
|
|
// string source = "DB";
|
|
// List<ItemModel>? dbResult = new List<ItemModel>();
|
|
// string currKey = $"{rKeyItems}:Search:{searchVal}:FamilyID:{famID}";
|
|
// Stopwatch stopWatch = new Stopwatch();
|
|
// stopWatch.Start();
|
|
// string? rawData = await redisDb.StringGetAsync(currKey);
|
|
// if (!string.IsNullOrEmpty(rawData))
|
|
// {
|
|
// source = "REDIS";
|
|
// var tempResult = JsonConvert.DeserializeObject<List<ItemModel>>(rawData);
|
|
// if (tempResult == null)
|
|
// {
|
|
// dbResult = new List<ItemModel>();
|
|
// }
|
|
// else
|
|
// {
|
|
// dbResult = tempResult;
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// dbResult = dbController.ItemsGetSearch(searchVal, famID);
|
|
// rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
// await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
// }
|
|
// if (dbResult == null)
|
|
// {
|
|
// dbResult = new List<ItemModel>();
|
|
// }
|
|
// stopWatch.Stop();
|
|
// TimeSpan ts = stopWatch.Elapsed;
|
|
// Log.Debug($"ItemsGetSearch | {source} in: {ts.TotalMilliseconds} ms");
|
|
// return dbResult;
|
|
//}
|
|
/// <summary>
|
|
/// Lista Articoli da ricerca
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemModel>> ItemsGetSearch(string searcVal, string itemFamId)
|
|
{
|
|
string source = "DB";
|
|
List<ItemModel>? dbResult = new List<ItemModel>();
|
|
string currKey = "";
|
|
if (itemFamId != "*" && string.IsNullOrEmpty(searcVal))
|
|
{
|
|
currKey = $"{rKeyItems}:Search:{searcVal}";
|
|
}
|
|
else if (!string.IsNullOrEmpty(searcVal) && itemFamId == "*")
|
|
{
|
|
currKey = $"{rKeyItems}:Family:{itemFamId}";
|
|
}
|
|
else
|
|
{
|
|
currKey = $"{rKeyItems}:Family:{itemFamId}:Search:{searcVal}";
|
|
}
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
dbResult = dbController.ItemsGetSearch(searcVal, itemFamId);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemsGetSearch | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Stock da cercare
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemStockModel>> ItemsStockGetById(int ItemID)
|
|
{
|
|
string source = "DB";
|
|
List<ItemStockModel>? dbResult = new List<ItemStockModel>();
|
|
string currKey = "";
|
|
currKey = $"{rKeyItemStock}:Id:{ItemID}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
dbResult = dbController.ItemsStockGetById(ItemID);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ItemStockModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemsStockGetById | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunta locazione
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> LocationAddNew(LocationModel currRec)
|
|
{
|
|
var dbResult = await dbController.LocationAddNew(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyLocation}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Location
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<LocationModel>> LocationGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<LocationModel>? dbResult = new List<LocationModel>();
|
|
string currKey = $"{rKeyLocation}:*";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<LocationModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<LocationModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.LocationGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<LocationModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"LocationGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
/// <summary>
|
|
/// Lista Location
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<LocTypeModel>> LocTypeGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<LocTypeModel>? dbResult = new List<LocTypeModel>();
|
|
string currKey = $"{rKeyLocType}:*";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<LocTypeModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<LocTypeModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.LocTypeGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<LocTypeModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"LocTypeGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Location
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<LocationModel>> LocationGetLocType()
|
|
{
|
|
string source = "DB";
|
|
List<LocationModel>? dbResult = new List<LocationModel>();
|
|
string currKey = $"{rKeyLocType}: *";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<LocationModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<LocationModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.LocationGetLocType();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<LocationModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"LocationGetLocType | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento record selezionato locazione
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> LocationMod(LocationModel currRec)
|
|
{
|
|
var dbResult = await dbController.LocationMod(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyLocation}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
public async Task<bool> MoveItems(int stockId, string locationIdFrom, string locationIdTo, string oprId)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool result = false;
|
|
string source = "DB";
|
|
result = await dbController.MoveItems(stockId, locationIdFrom, locationIdTo, oprId);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MoveItems Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> MoveItems_New(int stockId, string locationIdFrom, string locationIdTo, string oprId, int qtyMov)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool result = false;
|
|
string source = "DB";
|
|
result = await dbController.MoveItems_New(stockId, locationIdFrom, locationIdTo, oprId, qtyMov);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MoveItems_New Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunta operatore
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> OperatorAddNew(OperatorModel currRec)
|
|
{
|
|
var dbResult = await dbController.OperatorAddNew(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyOperator}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento record selezionato operatore
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> OperatorMod(OperatorModel currRec)
|
|
{
|
|
var dbResult = await dbController.OperatorMod(currRec);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{rKeyOperator}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
await Task.Delay(1);
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Location
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<OperatorModel>> OperatorsGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<OperatorModel>? dbResult = new List<OperatorModel>();
|
|
string currKey = $"{rKeyOperator}:*";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<OperatorModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<OperatorModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.OperatorsGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<OperatorModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"OperatorsGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista flussi x item da cercare
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<OperatorModel> OperatorsGetByCodExt(string id)
|
|
{
|
|
string source = "DB";
|
|
OperatorModel? dbResult = new OperatorModel();
|
|
string currKey = $"{rKeyOperator}:Id:{id}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<OperatorModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new OperatorModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.OperatorsGetByCodExt(id);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new OperatorModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"OperatorsGetByCodExt | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Permessi
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<PermessiModel>> PermessiGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<PermessiModel>? dbResult = new List<PermessiModel>();
|
|
string currKey = $"{rKeyPermessi}:All";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<PermessiModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<PermessiModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.PermessiGetAll();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<PermessiModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"PermessiGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Location
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<VLocationValModel>> StockStatus()
|
|
{
|
|
string source = "DB";
|
|
List<VLocationValModel>? dbResult = new List<VLocationValModel>();
|
|
string currKey = $"{rKeyLocationVal}:*";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<VLocationValModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<VLocationValModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.StockStatus();
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<VLocationValModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"StockStatus | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected Random rnd = new Random();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
protected string CodApp { get; set; } = "";
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Private Fields
|
|
|
|
private const string passPhrase = "53DBA37E-257D-4276-BAC2-C0BF7E04BBD6";
|
|
private const string redisBaseAddr = "STOCKMAN:STEAM";
|
|
private const string rKeyCounters = $"{redisBaseAddr}:Cache:Counters";
|
|
private const string rKeyItemFamily = $"{redisBaseAddr}:Cache:ItemFamily";
|
|
private const string rKeyItemFlux = $"{redisBaseAddr}:Cache:ItemFlux";
|
|
private const string rKeyItems = $"{redisBaseAddr}:Cache:Items";
|
|
private const string rKeyItemStock = $"{redisBaseAddr}:Cache:Item:Stock";
|
|
private const string rKeyLocation = $"{redisBaseAddr}:Cache:Location";
|
|
private const string rKeyLocationVal = $"{redisBaseAddr}:Cache:LocationVal";
|
|
private const string rKeyLocType = $"{redisBaseAddr}:Cache:LocType";
|
|
private const string rKeyMovType = $"{redisBaseAddr}:Cache:MovType";
|
|
private const string rKeyOperator = $"{redisBaseAddr}:Cache:Operator";
|
|
private const string rKeyPermessi = $"{redisBaseAddr}:Cache:Permessi";
|
|
private static IConfiguration _configuration = null!;
|
|
private static ILogger<StockDataService> _logger = null!;
|
|
private static StoManController dbController = 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
|
|
}
|
|
} |