1662 lines
63 KiB
C#
1662 lines
63 KiB
C#
using MagMan.Core;
|
|
using MagMan.Core.DTO;
|
|
using MagMan.Core.Services;
|
|
using MagMan.Data.Tenant.Controllers;
|
|
using MagMan.Data.Tenant.DbModels;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
using static MagMan.Core.Enums;
|
|
|
|
namespace MagMan.Data.Tenant.Services
|
|
{
|
|
public class TenantService : BaseServ, IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public TenantService(IConfiguration configuration, IConnectionMultiplexer redisConnMult)
|
|
{
|
|
Log.Info("TenantService starting...");
|
|
_configuration = configuration;
|
|
|
|
// Conf cache
|
|
redisConn = redisConnMult;
|
|
redisDb = this.redisConn.GetDatabase();
|
|
|
|
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
|
|
JSSettings = new JsonSerializerSettings()
|
|
{
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
};
|
|
|
|
// cod app
|
|
CodApp = _configuration.GetValue<string>("OptConf:CodApp");
|
|
|
|
// gestione parallelismo...
|
|
numPar = _configuration.GetValue<int>("OptConf:NumPar");
|
|
|
|
// DB
|
|
DbServerAddr = _configuration["DbConfig:Server"];
|
|
dbController = new TenantController();
|
|
|
|
// chiudo log
|
|
Log.Info("TenantService started!");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public static bool hasDecimal(decimal num)
|
|
{
|
|
return (num - Math.Round(num) != 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina Alias da magazzino + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="rec2del">Alias da eliminare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AliasDelete(int nKey, AliasModel rec2del)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.AliasDelete(cString, rec2del);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during AliasDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in AliasModel
|
|
/// </summary>
|
|
/// <param name="origItem"></param>
|
|
/// <returns></returns>
|
|
public AliasModel AliasFromDto(AliasDTO origItem, string family)
|
|
{
|
|
AliasModel answ = new AliasModel()
|
|
{
|
|
Family = family,
|
|
ValueOriginal = origItem.ValOrig,
|
|
ValueAlias = origItem.ValAlias,
|
|
IsActive = origItem.IsActive
|
|
};
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Alias gestiti a magazzino
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="family">Famiglia richiesta, "" = tutti</param>
|
|
/// <returns></returns>
|
|
public async Task<List<AliasModel>> AliasGetFilt(int nKey, string family)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<AliasModel>? dbResult = new List<AliasModel>();
|
|
try
|
|
{
|
|
string dType = string.IsNullOrEmpty(family) ? "***" : family;
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:Alias:{dType}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<AliasModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<AliasModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.AliasGetFilt(cString, family);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
// per evitare loopback uso deserialize...
|
|
var tempResult = JsonConvert.DeserializeObject<List<AliasModel>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<AliasModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"AliasGetFilt | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during AliasGetFilt:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record Alias + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="currItem">Item da aggiornare/inserire</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AliasUpsert(int nKey, AliasModel currItem)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.AliasUpsert(cString, currItem);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during AliasUpsert:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update lista Alias + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="listItem">Elenco Item da aggiornare/inserire</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AliasUpsert(int nKey, List<AliasModel> listItem)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.AliasUpsert(cString, listItem);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during AliasUpsert:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
dbController.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina Item da magazzino + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="rec2del">Item da eliminare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemDelete(int nKey, RawItemModel rec2del)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ItemDelete(cString, rec2del);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in ItemModel, colmando eventuale mancante nelle note dell'item
|
|
/// </summary>
|
|
/// <param name="origItem">DTO di partenza</param>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <returns></returns>
|
|
public RawItemModel ItemFromDto(ItemDTO origItem, int nKey)
|
|
{
|
|
RawItemModel answ = ItemFromDto(origItem);
|
|
if (string.IsNullOrEmpty(answ.Note))
|
|
{
|
|
string cString = ConnString(nKey);
|
|
var matRec = dbController.MaterialGetFilt(cString, origItem.MatCloudId, false).FirstOrDefault();
|
|
if (matRec != null)
|
|
{
|
|
answ.Note = matRec.MatDesc;
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in ItemModel
|
|
/// </summary>
|
|
/// <param name="origItem">DTO di partenza</param>
|
|
/// <param name="isActive">Parametro active da impostare</param>
|
|
/// <returns></returns>
|
|
public RawItemModel ItemFromDto(ItemDTO origItem)
|
|
{
|
|
RawItemModel answ = new RawItemModel()
|
|
{
|
|
MatId = origItem.MatCloudId,
|
|
Note = origItem.Note,
|
|
LMm = origItem.LMm,
|
|
WMm = origItem.WMm,
|
|
HMm = origItem.HMm,
|
|
IsDeleted = origItem.IsDeleted,
|
|
IsRemn = origItem.IsRemn,
|
|
Location = origItem.Location,
|
|
QtyAvail = origItem.QtyAvail
|
|
};
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Items gestiti a magazzino
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <returns></returns>
|
|
public async Task<List<RawItemModel>> ItemGetAll(int nKey)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<RawItemModel>? dbResult = new List<RawItemModel>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ItemListAll";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<RawItemModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<RawItemModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemGetAll(cString);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<RawItemModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Items gestiti a magazzino x materiale
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="matID">ID del materiale x cui filtrare, 0 = tutti</param>
|
|
/// <param name="onlyActive">Solo attivi (default) o anche cancellati</param>
|
|
/// <returns></returns>
|
|
public async Task<List<RawItemModel>> ItemGetByMat(int nKey, int matID, bool onlyActive)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<RawItemModel>? dbResult = new List<RawItemModel>();
|
|
try
|
|
{
|
|
string keyAct = onlyActive ? "ACT" : "ALL";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ItemList:{matID}:{keyAct}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<RawItemModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<RawItemModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemGetByMat(cString, matID, onlyActive);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<RawItemModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemGetByMat | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemGetByMat:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricerca item da QrCode
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="QrCode">QrCode/Dtmx cercato</param>
|
|
/// <returns></returns>
|
|
public async Task<RawItemModel> ItemGetByQr(int nKey, string QrCode)
|
|
{
|
|
RawItemModel? dbResult = new RawItemModel();
|
|
string cacheKey = $"{Const.rKeyConfig}:{nKey}:{QrCode}";
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ItemByQr:{QrCode}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<RawItemModel>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new RawItemModel();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ItemGetByQr(cString, QrCode);
|
|
if (dbResult != null && dbResult.ItemDtmx.ToUpper() == QrCode.ToUpper())
|
|
{
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new RawItemModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ItemGetByQr | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemGetByQr:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record Item per quantità + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="currItem">Item interesato</param>
|
|
/// <param name="userId">User corrente (SE applicabile)</param>
|
|
/// <param name="msgAdd">
|
|
/// Messaggio registrato x variazione positiva (def: M01+: Rettifica Inventariale)
|
|
/// </param>
|
|
/// <param name="msgRem">
|
|
/// Messaggio registrato x variazione negativa (def: M01-: Rettifica Inventariale)
|
|
/// </param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemModQty(int nKey, RawItemModel currItem, int deltaQty, string userId, string msgAdd = "M01+: Rettifica Inventariale", string msgRem = "M01-: Rettifica Inventariale")
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ItemModQty(cString, currItem, deltaQty, userId, msgAdd, msgRem);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemModQty:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Riattiva Item da magazzino + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="rec2react">Item da riattivare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemReactiv(int nKey, RawItemModel rec2react)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ItemReactiv(cString, rec2react);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemReactiv:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record Item + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="currItem">Item interesato</param>
|
|
/// <param name="userId">User corrente (SE applicabile)</param>
|
|
/// <param name="forceQty">Se true aggiorna giacenze quantita correnti</param>
|
|
/// <param name="ignoreRemn">Se true ignora aggiornamento remnant in cloud</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemUpdate(int nKey, RawItemModel currItem, string userId, bool forceQty, bool ignoreRemn)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ItemUpdate(cString, currItem, userId, forceQty, ignoreRemn);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ItemUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in ItemModel
|
|
/// </summary>
|
|
/// <param name="origItem">DTO di partenza</param>
|
|
/// <returns></returns>
|
|
public LogMachineModel LogMacFromDto(LogMachineDTO origItem, int machineCloudId, int keyNum)
|
|
{
|
|
LogMachineModel answ = new LogMachineModel()
|
|
{
|
|
ProjDbId = origItem.ProjCloudId,
|
|
DtEvent = origItem.DtEvent,
|
|
EvType = origItem.EvType,
|
|
MachineID = machineCloudId,
|
|
KeyNum = keyNum,
|
|
SupervId = origItem.SupervId,
|
|
VarValue = origItem.VarValue
|
|
};
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Projects gestiti a magazzino
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="machineId">idMacchina di cui si vuole log</param>
|
|
/// <param name="numRec">num rec max da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<LogMachineModel>> LogMacGetLast(int nKey, int machineId, int numRec)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<LogMachineModel>? dbResult = new List<LogMachineModel>();
|
|
DateTime adesso = DateTime.Now;
|
|
try
|
|
{
|
|
// cache al minuto...
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:LogMacLast:{machineId}:{adesso:yyMMdd}::{adesso:HHmm}:{numRec}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<LogMachineModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<LogMachineModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.LogMacGetLast(cString, machineId, numRec);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<LogMachineModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"LogMacGetLast | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during LogMacGetLast:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge/Modifica un record Resource
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="KeyNum">Num Chiave record</param>
|
|
/// <param name="machineId">Id Macchina</param>
|
|
/// <param name="dtStart">Data inizio set da eliminare</param>
|
|
/// <param name="dtEnd">Data fine set da eliminare</param>
|
|
/// <returns></returns>
|
|
public async Task<int> LogMacRemoveRange(int nKey, int machineId, DateTime dtStart, DateTime dtEnd)
|
|
{
|
|
int nUpdated = 0;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
nUpdated = dbController.LogMacRemoveRange(cString, nKey, machineId, dtStart, dtEnd);
|
|
if (nUpdated > 0)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during LogMacRemoveRange:{Environment.NewLine}{exc}");
|
|
}
|
|
return nUpdated;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge/Modifica un record Resource
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="recList">Elenco record da aggiungere/aggiornare</param>
|
|
/// <returns></returns>
|
|
public async Task<int> LogMacUpdate(int nKey, List<LogMachineModel> recList)
|
|
{
|
|
int nUpdated = 0;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
nUpdated = dbController.LogMacUpdate(cString, recList);
|
|
if (nUpdated > 0)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during LogMacUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return nUpdated;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina Materiale da magazzino + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="rec2del">Item da eliminare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> MaterialDelete(int nKey, MaterialModel rec2del)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.MaterialDelete(cString, rec2del);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MaterialDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Materiali gestiti a magazzino in formato DTO
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="withChild">Se true allora include record child (Items)</param>
|
|
/// <param name="withRemn">Se true allora include record remnants (Items) nei conteggi qty</param>
|
|
/// <returns></returns>
|
|
public async Task<List<MaterialDTO>> MaterialDtoGetAll(int nKey, bool withChild, bool withRemn)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<MaterialDTO>? dbResult = new List<MaterialDTO>();
|
|
try
|
|
{
|
|
string dType = withChild ? "MatDtoFull" : "MatDto";
|
|
string remn = withRemn ? "All" : "Buy";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:{remn}:{dType}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialDTO>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<MaterialDTO>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.MaterialDtoGetAll(cString, withChild, withRemn);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
// per evitare loopback uso deserialize...
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialDTO>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MaterialDTO>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MaterialDtoGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MaterialDtoGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Materiali gestiti a magazzino in formato DTO
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="matID">Materiale richiesto, 0=tutti</param>
|
|
/// <param name="withChild">Se true allora include record child (Items)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<MaterialDTO>> MaterialDtoGetFilt(int nKey, int matID, bool withChild)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<MaterialDTO>? dbResult = new List<MaterialDTO>();
|
|
try
|
|
{
|
|
string dType = withChild ? "MaterialsDtoFiltFull" : "MaterialsDtoFilt";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:{dType}:{matID}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialDTO>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<MaterialDTO>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.MaterialDtoGetFilt(cString, matID, withChild);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
// per evitare loopback uso deserialize...
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialDTO>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MaterialDTO>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MaterialDtoGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MaterialDtoGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in MaterialModel
|
|
/// </summary>
|
|
/// <param name="origItem"></param>
|
|
/// <returns></returns>
|
|
public MaterialModel MaterialFromDto(MaterialDTO origItem)
|
|
{
|
|
MaterialModel answ = new MaterialModel();
|
|
// calcolo descrizione se fosse vuota
|
|
string matDescr = origItem.MatDesc;
|
|
if (string.IsNullOrEmpty(origItem.MatDesc))
|
|
{
|
|
matDescr = origItem.MatCode;
|
|
if (origItem.WMm > 0)
|
|
{
|
|
if (hasDecimal(origItem.WMm))
|
|
{
|
|
matDescr += $" {origItem.WMm:N2}";
|
|
}
|
|
else
|
|
{
|
|
matDescr += $" {origItem.WMm:N0}";
|
|
}
|
|
}
|
|
if (hasDecimal(origItem.HMm))
|
|
{
|
|
matDescr += $"x{origItem.HMm:N2}";
|
|
}
|
|
else
|
|
{
|
|
matDescr += $"x{origItem.HMm:N0}";
|
|
}
|
|
}
|
|
if (origItem != null)
|
|
{
|
|
answ = new MaterialModel()
|
|
{
|
|
MatId = origItem.MatCloudId,
|
|
MatCode = origItem.MatCode,
|
|
MatDesc = matDescr,
|
|
LMm = origItem.LMm,
|
|
WMm = origItem.WMm,
|
|
HMm = origItem.HMm
|
|
};
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Materiali gestiti a magazzino
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="withChild">Se true allora include record child (Items)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<MaterialModel>> MaterialGetAll(int nKey, bool withChild)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<MaterialModel>? dbResult = new List<MaterialModel>();
|
|
try
|
|
{
|
|
string dType = withChild ? "MaterModAllFull" : "MaterModAll";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:{dType}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<MaterialModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.MaterialGetAll(cString, withChild);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
// per evitare loopback uso deserialize...
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialModel>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MaterialModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MaterialGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MaterialGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Materiali gestiti a magazzino
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="matID">Materiale richiesto, 0=tutti</param>
|
|
/// <param name="withChild">Se true allora include record child (Items)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<MaterialModel>> MaterialGetFilt(int nKey, int matID, bool withChild)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<MaterialModel>? dbResult = new List<MaterialModel>();
|
|
try
|
|
{
|
|
string dType = withChild ? "MaterModFiltAll" : "MaterModFilt";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:{dType}:{matID}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData) && rawData.Length > 2)
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<MaterialModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.MaterialGetFilt(cString, matID, withChild);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
// per evitare loopback uso deserialize...
|
|
var tempResult = JsonConvert.DeserializeObject<List<MaterialModel>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MaterialModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MaterialGetFilt | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MaterialGetFilt:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il MaterialModel in DTO
|
|
/// </summary>
|
|
/// <param name="origItem"></param>
|
|
/// <returns></returns>
|
|
public MaterialDTO MaterialToDto(MaterialModel origItem)
|
|
{
|
|
MaterialDTO answ = new MaterialDTO();
|
|
if (origItem != null)
|
|
{
|
|
answ = new MaterialDTO()
|
|
{
|
|
MatCloudId = origItem.MatId,
|
|
MatCode = origItem.MatCode,
|
|
MatDesc = origItem.MatDesc,
|
|
LMm = origItem.LMm,
|
|
WMm = origItem.WMm,
|
|
HMm = origItem.HMm,
|
|
MatDtmx = origItem.MatDtmx,
|
|
IsBeam = origItem.IsBeam,
|
|
IsWall = origItem.IsWall,
|
|
};
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record Materiale + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="currItem">Item da aggiornare/inserire</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> MaterialUpdate(int nKey, MaterialModel currItem)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.MaterialUpdate(cString, currItem);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MaterialUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco MovMag dato Item
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="rawItemID">ID dell'item x cui filtrare, 0 = tutti</param>
|
|
/// <param name="maxRec">numMax record da leggere, default 1000</param>
|
|
/// <returns></returns>
|
|
public async Task<List<MovMagModel>> MovMagGetFilt(int nKey, int rawItemID, int maxRec = 1000)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<MovMagModel>? dbResult = new List<MovMagModel>();
|
|
try
|
|
{
|
|
// in cache tengo dati estratti ogni minuto...
|
|
string dtKey = DateTime.Now.ToString("yyMMdd:HHmm");
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:MovMag:{rawItemID}:{dtKey}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<MovMagModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<MovMagModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.MovMagGetFilt(cString, rawItemID, maxRec);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MovMagModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"MovMagGetFilt | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during MovMagGetFilt:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina record Project + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="rec2del">Item da eliminare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ProjectDelete(int nKey, ProjModel rec2del)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ProjectDelete(cString, rec2del);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in ItemModel
|
|
/// </summary>
|
|
/// <param name="origItem">DTO di partenza</param>
|
|
/// <returns></returns>
|
|
public ProjModel ProjectFromDto(ProjectDTO origItem)
|
|
{
|
|
ProjModel answ = new ProjModel()
|
|
{
|
|
ProjDbId = origItem.ProjCloudId,
|
|
ProjExtDbId = origItem.ProjLocalId,
|
|
ProjExtId = origItem.ProjExtId,
|
|
MachineID = origItem.MachineCloudId,
|
|
KeyNum = origItem.KeyNum,
|
|
BTLFileName = origItem.BTLFileName,
|
|
PType = origItem.PType,
|
|
Machine = origItem.Machine,
|
|
ProjDescription = origItem.ProjDescription,
|
|
DtCreated = origItem.DtCreated,
|
|
DtLastAction = origItem.DtLastAction,
|
|
DtSchedule = origItem.DtSchedule,
|
|
DtStartProd = origItem.DtStartProd,
|
|
ListName = origItem.ListName,
|
|
ProcTimeEst = origItem.ProcTimeEst,
|
|
ProcTimeReal = origItem.ProcTimeReal,
|
|
IsActive = origItem.IsActive,
|
|
IsArchived = origItem.IsArchived
|
|
};
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista Projects gestiti a magazzino
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ProjModel>> ProjectGetAll(int nKey)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<ProjModel>? dbResult = new List<ProjModel>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ProjListAll";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ProjModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ProjModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ProjectGetAll(cString);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ProjModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ProjectGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectGetAll:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Record progetto dato cliente e Key (ID)
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="ProjCloudId">Key del record cercato (>0)</param>
|
|
/// <returns></returns>
|
|
public async Task<ProjModel> ProjectGetById(int nKey, int ProjCloudId)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
ProjModel dbResult = new ProjModel();
|
|
ProjModel? tempResult = null;
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ProjRec:{ProjCloudId}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
tempResult = JsonConvert.DeserializeObject<ProjModel>(rawData);
|
|
}
|
|
else
|
|
{
|
|
tempResult = dbController.ProjectGetById(cString, ProjCloudId);
|
|
rawData = JsonConvert.SerializeObject(tempResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
dbResult = tempResult == null ? new ProjModel() : tempResult;
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ProjectGetById | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectGetById:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista progetti x macchina
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="machineId">ID macchina, 0 = tutti</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ProjModel>> ProjectGetByMachine(int nKey, int machineId)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<ProjModel>? dbResult = new List<ProjModel>();
|
|
try
|
|
{
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ProjList:{machineId}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ProjModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ProjModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ProjectGetByMachine(cString, machineId);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ProjModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ProjectGetByMachine | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectGetByMachine:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco record progetti dato Key + data ultima modifica registrata
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="DtRif">Data rispetto cui cercare modifiche successive</param>
|
|
/// <returns></returns>
|
|
public List<ProjModel> ProjectGetModAfter(int nKey, DateTime DtRif)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<ProjModel> dbResult = new List<ProjModel>();
|
|
try
|
|
{
|
|
// NON Usa la cache poiché voglio sync DB-DB in realtime
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.ProjectGetModAfter(cString, DtRif);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ProjectGetModAfter | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectGetModAfter:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upsert record Project + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="projDbId">ID Record progetto da aggiornare</param>
|
|
/// <param name="procTime">Durata progetto (cumulata)</param>
|
|
/// <param name="valAct">Valore Progresso attuale (tipicamente num barre/pezzi)</param>
|
|
/// <param name="valMax">Valore Progresso max atteso (tipicamente num barre/pezzi)</param>
|
|
/// <returns>Bool aggiornamento</returns>
|
|
public async Task<bool> ProjectSetProgr(int nKey, int projDbId, double procTime, double valAct, double valMax)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ProjectSetProgr(cString, projDbId, procTime, valAct, valMax);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectSetProgr:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in ItemModel
|
|
/// </summary>
|
|
/// <param name="origItem">DTO di partenza</param>
|
|
/// <returns></returns>
|
|
public ProjectDTO ProjectToDto(ProjModel origItem)
|
|
{
|
|
ProjectDTO answ = new ProjectDTO()
|
|
{
|
|
ProjCloudId = origItem.ProjDbId,
|
|
ProjLocalId = origItem.ProjExtDbId,
|
|
ProjExtId = origItem.ProjExtId,
|
|
MachineCloudId = origItem.MachineID,
|
|
KeyNum = origItem.KeyNum,
|
|
BTLFileName = origItem.BTLFileName,
|
|
PType = origItem.PType,
|
|
Machine = origItem.Machine,
|
|
ProjDescription = origItem.ProjDescription,
|
|
DtCreated = origItem.DtCreated,
|
|
DtLastAction = origItem.DtLastAction,
|
|
DtSchedule = origItem.DtSchedule,
|
|
DtStartProd = origItem.DtStartProd,
|
|
ListName = origItem.ListName,
|
|
ProcTimeEst = origItem.ProcTimeEst,
|
|
ProcTimeReal = origItem.ProcTimeReal,
|
|
IsActive = origItem.IsActive,
|
|
IsArchived = origItem.IsArchived
|
|
};
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record Project + refresh cache x info archived
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="newData">IDizionario ProdId / IsArchived da impostare</param>
|
|
/// <returns>Bool aggiornamento</returns>
|
|
public async Task<bool> ProjectUpdArchived(int nKey, Dictionary<int, bool> newData)
|
|
{
|
|
bool fatto = false;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
fatto = dbController.ProjectUpdArchived(cString, newData);
|
|
if (fatto)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectUpdArchived:{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upsert record Project + refresh cache
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="currItem">Item interesato</param>
|
|
/// <returns>ID del progetto creato/aggiornato da usare come CloudId</returns>
|
|
public async Task<int> ProjectUpsert(int nKey, ProjModel currItem)
|
|
{
|
|
int prjCloudId = 0;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
prjCloudId = dbController.ProjectUpsert(cString, currItem);
|
|
if (prjCloudId > 0)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ProjectUpsert:{Environment.NewLine}{exc}");
|
|
}
|
|
return prjCloudId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera ultimo record attivo ReqPlan x tipo richiesto
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="ProjCloudId">ID del progetto da cercare</param>
|
|
/// <param name="ResState">Stato richiesta da cercare</param>
|
|
/// <returns>Record cercato o default se non trovato</returns>
|
|
public RequestPlanModel ReqPlanGetLast(int nKey, int ProjCloudId, ProjResState ResState)
|
|
{
|
|
RequestPlanModel lastRec = new RequestPlanModel();
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
// cerco record (se fosse con valori "ini" p non trovata
|
|
lastRec = dbController.ReqPlanGetLast(cString, ProjCloudId, ResState);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ReqPlanGetLast:{Environment.NewLine}{exc}");
|
|
}
|
|
return lastRec;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge/Modifica un record ReqPlan
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="currItem">Record da aggiungere/aggiornare</param>
|
|
/// <returns></returns>
|
|
public async Task<int> ReqPlanUpdate(int nKey, RequestPlanModel currItem)
|
|
{
|
|
int newId = 0;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
newId = dbController.ReqPlanUpdate(cString, currItem);
|
|
if (newId > 0)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ReqPlanUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return newId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converte il DTO in ResourceModel
|
|
/// </summary>
|
|
/// <param name="origItem">DTO di partenza</param>
|
|
/// <returns></returns>
|
|
public ResourceModel ResourceFromDto(ResourceDTO origItem, int reqId)
|
|
{
|
|
ResourceModel answ = new ResourceModel()
|
|
{
|
|
Qty = origItem.Qty,
|
|
RawItemId = origItem.RawItemCloudId,
|
|
RequestId = reqId,
|
|
ResourceId = 0
|
|
};
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco risorse dato progetto e stato
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="projDbId">ID progetto</param>
|
|
/// <param name="isEstim">true = ultima stima attiva / false = consumi effettivi</param>
|
|
/// <param name="showAll">true= mostra ANCHE archiviate, false = solo attive</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ResourceExpDTO>> ResourcesExpGetByProject(int nKey, int projDbId, bool isEstim, bool showAll)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<ResourceExpDTO>? dbResult = new List<ResourceExpDTO>();
|
|
try
|
|
{
|
|
string tagEst = isEstim ? "EST" : "CON";
|
|
string tagAct = showAll ? "ALL" : "ACT";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ResExpList:{projDbId}:{tagEst}:{tagAct}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ResourceExpDTO>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ResourceExpDTO>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ResourcesExpGetByProject(cString, projDbId, isEstim, showAll);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ResourceExpDTO>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ResourcesExpGetByProject | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ResourcesExpGetByProject:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco risorse dato progetto e stato
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="projDbId">ID progetto</param>
|
|
/// <param name="isEstim">true = ultima stima attiva / false = consumi effettivi</param>
|
|
/// <param name="showAll">true= mostra ANCHE archiviate, false = solo attive</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ResourceModel>> ResourcesGetByProject(int nKey, int projDbId, bool isEstim, bool showAll)
|
|
{
|
|
string source = "DB";
|
|
string cString = ConnString(nKey);
|
|
List<ResourceModel>? dbResult = new List<ResourceModel>();
|
|
try
|
|
{
|
|
string tagEst = isEstim ? "EST" : "CON";
|
|
string tagAct = showAll ? "ALL" : "ACT";
|
|
string currKey = $"{Const.rKeyConfig}:{nKey}:ResList:{projDbId}:{tagEst}:{tagAct}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ResourceModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ResourceModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.ResourcesGetByProject(cString, projDbId, isEstim, showAll);
|
|
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<ResourceModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ResourcesGetByProject | {source} in: {ts.TotalMilliseconds} ms");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ResourcesGetByProject:{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge/Modifica un record Resource
|
|
/// </summary>
|
|
/// <param name="nKey">Key di riferimento</param>
|
|
/// <param name="requestPlanId">Key della richiesta di riferimento</param>
|
|
/// <param name="recList">Elenco record da aggiungere/aggiornare</param>
|
|
/// <param name="resState">Tipo di aggiornamento da registratre</param>
|
|
/// <param name="noteUid">Note / UserId (SE applicabile)</param>
|
|
/// <returns></returns>
|
|
public async Task<int> ResourceUpdate(int nKey, int requestPlanId, List<ResourceDTO> recList, Enums.ProjResState resState, string noteUid)
|
|
{
|
|
int newId = 0;
|
|
string cString = ConnString(nKey);
|
|
try
|
|
{
|
|
newId = dbController.ResourceUpdate(cString, requestPlanId, recList, resState, noteUid);
|
|
if (newId > 0)
|
|
{
|
|
await FlushRedisCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error during ResourceUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return newId;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected static TenantController dbController = null!;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Private Fields
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private string CodApp { get; set; } = "";
|
|
|
|
private Dictionary<int, string> ConnStringLUT { get; set; } = new Dictionary<int, string>();
|
|
|
|
private string DbServerAddr { get; set; } = "";
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Restituisce la stringa di connessione al DB data la Key di riferimento, effettuando
|
|
/// verifica ed eventuale migration
|
|
/// </summary>
|
|
/// <param name="nKey"></param>
|
|
/// <returns></returns>
|
|
private string ConnString(int nKey)
|
|
{
|
|
string answ = "";
|
|
if (ConnStringLUT.ContainsKey(nKey))
|
|
{
|
|
answ = ConnStringLUT[nKey];
|
|
}
|
|
else
|
|
{
|
|
// calcolo e aggiungo
|
|
answ = DbConfig.CustomerConnString(DbServerAddr, nKey);
|
|
// verifico eventuale creazione/migrazione...
|
|
DbConfig.InitDb(DbServerAddr, nKey);
|
|
// verifico se serve applicazione migrazioni
|
|
DbConfig.ExecMigrationMain(answ);
|
|
// aggiungo a LUT
|
|
ConnStringLUT.Add(nKey, answ);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |