Files
2022-11-29 11:46:54 +01:00

592 lines
23 KiB
C#

using MapoSDK;
using Newtonsoft.Json;
using SteamWare.IO;
using SteamWare.Reports;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
namespace MagData
{
public class MagDataLayer
{
#region Public Fields
/// <summary>
/// oggetto static/singleton per fare chiamate sul datalayer
/// </summary>
public static MagDataLayer man = new MagDataLayer();
/// <summary>
/// RegExp x Qty odette
/// </summary>
public static string re_QtyOdette = memLayer.ML.cdv("regExp_QtyOdette");
public static string redQueueConf = "MP-CTRACK:MAG:queueConf";
/// <summary>
/// chaive redis x cache conteggio coda PJQ
/// </summary>
public static string redQueueCount = "MP:MAG:PJQ";
/// <summary>
/// chaive redis x cache conteggio coda PJQ composta da tante code
/// </summary>
public static string redQueueCountSet = "MP:MAG:PJQCOUNT";
public static string redReportConf = "MP-CTRACK:MAG:reportConf";
public DS_MagTableAdapters.AnagArtTableAdapter taAA;
public DS_MagTableAdapters.AnagArticoli2ClientiTableAdapter taAA2C;
public DS_MagTableAdapters.AnagClientiTableAdapter taACF;
public DS_MagTableAdapters.AnagDestinClientiTableAdapter taADC;
public DS_ReportTableAdapters.stp_prt_CartellinoFinitiOdetteTableAdapter taCFOdette;
public DS_ReportTableAdapters.stp_prt_CartellinoPedaneTableAdapter taCPed;
public DS_ReportTableAdapters.stp_prt_CartellinoSemilavoratiTableAdapter taCSemil;
public DS_MagTableAdapters.ElencoAL2UDCTableAdapter taEA2U;
public DS_MagTableAdapters.ElencoALTableAdapter taEAL;
public DS_MagTableAdapters.ElencoLottiTableAdapter taEL;
public DS_PackListTableAdapters.ExtOrdersListTableAdapter taEOL;
public DS_MagTableAdapters.Elencopost2QueueTableAdapter taEP2Q;
public DS_MagTableAdapters.ElencoUdcTableAdapter taEUdc;
public DS_MagTableAdapters.ODLTableAdapter taODL;
public DS_PackListTableAdapters.OrdersListTableAdapter taOList;
public DS_ReportTableAdapters.PrintJobQueueTableAdapter taPJQ;
public DS_PackListTableAdapters.PackListTableAdapter taPList;
public DS_MagTableAdapters.RegGiacenzeTableAdapter taRegGia;
public DS_ReportTableAdapters.stp_prt_ReportPackListTableAdapter taRepPL;
public DS_ReportTableAdapters.stp_prt_ReportPackListFullTableAdapter taRepPLFull;
#endregion Public Fields
#region Public Constructors
public MagDataLayer()
{
// inizializzaizone classe
initTA();
setupConnString();
}
#endregion Public Constructors
#region Public Properties
/// <summary>
/// Dati Prod e Print correnti x sessione utente salvati come cookie
/// </summary>
public prodPrintData currProdPrintData
{
set
{
string rawData = JsonConvert.SerializeObject(value);
memLayer.ML.setSessionVal("currProdPrintData", rawData, true);
}
get
{
prodPrintData answ = new prodPrintData();
string rawData = memLayer.ML.StringSessionObj("currProdPrintData");
if (!string.IsNullOrEmpty(rawData))
{
answ = JsonConvert.DeserializeObject<prodPrintData>(rawData);
}
return answ;
}
}
/// <summary>
/// Restituisce un array JSon x le conf delle code di stampa leggendo dal file
/// </summary>
/// <returns>lista oggetto Json in formato SteamwareSDK.queueConf</returns>
public List<queueConf> queueConfJson
{
get
{
List<queueConf> answ = new List<queueConf>();
string dirPath = HttpContext.Current.Server.MapPath("~/Reports/");
string nomeFile = "queueConf.json";
bool fileExist = fileMover.obj.fileExist(dirPath, nomeFile);
if (fileExist)
{
string rawData = File.ReadAllText($"{dirPath}\\{nomeFile}");
if (!string.IsNullOrEmpty(rawData))
{
try
{
answ = JsonConvert.DeserializeObject<List<queueConf>>(rawData);
}
catch (Exception exc)
{
SteamWare.Logger.Logging.Instance.Error(exc, "Eccezione in decodifica file conf queueConfJson");
}
}
}
return answ;
}
}
/// <summary>
/// Cache redis della conf delle code
/// </summary>
/// <returns>lista oggetto Json in formato SteamwareSDK.queueConf</returns>
public List<queueConf> queueConfRedis
{
get
{
List<queueConf> answ = new List<queueConf>();
string redKey = memLayer.ML.redHash(redQueueConf);
string rawData = memLayer.ML.getRSV(redKey);
if (!string.IsNullOrEmpty(rawData))
{
try
{
answ = JsonConvert.DeserializeObject<List<queueConf>>(rawData);
}
catch (Exception exc)
{
SteamWare.Logger.Logging.Instance.Error(exc, "Eccezione in recupero dati queueConf da redis");
}
}
// controllo se vuoto rileggo da file
if (answ.Count == 0)
{
answ = queueConfJson;
// a quesot punto salvo in Redis
queueConfRedis = answ;
}
return answ;
}
set
{
string redKey = memLayer.ML.redHash(redQueueConf);
string rawData = JsonConvert.SerializeObject(value);
// salvo in cache per 1 h...
memLayer.ML.setRSV(redKey, rawData, 3600);
}
}
/// <summary>
/// Restituisce un array JSon x le conf dei report/template di stampa leggendo dal file
/// </summary>
/// <returns>lista oggetto Json in formato SteamwareSDK.templateConf</returns>
public List<reportConf> reportConfJson
{
get
{
List<reportConf> answ = new List<reportConf>();
string dirPath = HttpContext.Current.Server.MapPath("~/Reports/");
string nomeFile = "reportConf.json";
bool fileExist = fileMover.obj.fileExist(dirPath, nomeFile);
if (fileExist)
{
string rawData = File.ReadAllText($"{dirPath}\\{nomeFile}");
if (!string.IsNullOrEmpty(rawData))
{
try
{
answ = JsonConvert.DeserializeObject<List<reportConf>>(rawData);
}
catch (Exception exc)
{
SteamWare.Logger.Logging.Instance.Error(exc, "Eccezione in decodifica file conf reportConfJson");
}
}
}
return answ;
}
}
/// <summary>
/// Cache redis della conf dei tipo report
/// </summary>
/// <returns>lista oggetto Json in formato SteamwareSDK.queueConf</returns>
public List<reportConf> reportConfRedis
{
get
{
List<reportConf> answ = new List<reportConf>();
string redKey = memLayer.ML.redHash(redReportConf);
string rawData = memLayer.ML.getRSV(redKey);
if (!string.IsNullOrEmpty(rawData))
{
try
{
answ = JsonConvert.DeserializeObject<List<reportConf>>(rawData);
}
catch (Exception exc)
{
SteamWare.Logger.Logging.Instance.Error(exc, "Eccezione in recupero dati reportConf da redis");
}
}
// controllo se vuoto rileggo da file
if (answ.Count == 0)
{
answ = reportConfJson;
// a quesot punto salvo in Redis
reportConfRedis = answ;
}
return answ;
}
set
{
string redKey = memLayer.ML.redHash(redReportConf);
string rawData = JsonConvert.SerializeObject(value);
// salvo in cache per 1 h...
memLayer.ML.setRSV(redKey, rawData, 3600);
}
}
#endregion Public Properties
#region Public Methods
/// <summary>
/// Decodifica barcode
/// </summary>
/// <param name="bcValue"></param>
/// <returns></returns>
public decodedData decodeBcode(string bcValue)
{
decodedData answ = new decodedData();
int codeInt = 0;
answ.rawData = bcValue;
if (bcValue.StartsWith("CMD"))
{
answ.codeType = codeType.Command;
answ.code = bcValue.Replace("CMD", "");
answ.codeInt = codeInt;
answ.description = $"Comando: {answ.code}";
}
else if (bcValue.StartsWith("M"))
{
answ.codeType = codeType.LottoInt;
answ.code = bcValue.Replace("M", "");
answ.codeInt = codeInt;
answ.description = $"Lotto: {answ.code}";
}
else if (bcValue.StartsWith("PED"))
{
answ.codeType = codeType.Pedana;
answ.code = bcValue.Replace("PED", "");
int.TryParse(answ.code, NumberStyles.Integer, CultureInfo.InvariantCulture, out codeInt);
answ.codeInt = codeInt;
answ.description = $"Pedana: {bcValue}";
}
else if (bcValue.StartsWith("PL"))
{
answ.codeType = codeType.PackList;
answ.code = bcValue.Replace("PL", "");
int.TryParse(answ.code, NumberStyles.Integer, CultureInfo.InvariantCulture, out codeInt);
answ.codeInt = codeInt;
answ.description = $"PackList: {answ.code}";
}
else if (bcValue.StartsWith("P"))
{
// controllo se data (6 numeri...) oppure part...
if (bcValue.Length == 7)
{
answ.codeType = codeType.Date;
answ.code = bcValue.Replace("P", "");
int.TryParse(answ.code, NumberStyles.Integer, CultureInfo.InvariantCulture, out codeInt);
answ.codeInt = 0;
answ.description = $"Data YYMMDD: {answ.code}";
}
else
{
answ.codeType = codeType.PartNumber;
answ.code = bcValue.Replace("P", "");
int.TryParse(answ.code, NumberStyles.Integer, CultureInfo.InvariantCulture, out codeInt);
answ.codeInt = 0;
answ.description = $"PartNumber Odette: {answ.code}";
}
}
else if (bcValue.StartsWith("Q"))
{
// controllo regex
Match testQty = Regex.Match(bcValue, re_QtyOdette);
if (testQty.Success)
{
answ.codeType = codeType.Quantity;
answ.code = bcValue.Replace("Q", "");
int.TryParse(answ.code, NumberStyles.Integer, CultureInfo.InvariantCulture, out codeInt);
answ.codeInt = codeInt;
answ.description = $"Qty Odette: {answ.code}";
}
else
{
answ.codeType = codeType.UNK;
}
}
else if (bcValue.StartsWith("H"))
{
answ.codeType = codeType.Lotto;
answ.code = bcValue.Replace("H", "");
answ.codeInt = 0;
answ.description = $"Lotto Odette: {answ.code}";
}
else if (bcValue.StartsWith("S"))
{
answ.codeType = codeType.Udc;
answ.code = bcValue.Replace("S", "");
answ.codeInt = codeInt;
answ.description = $"UDC Odette: {answ.code}";
}
else if (bcValue.Length >= 6 && bcValue.Length <= 7)
{
// cerco x lotto SOLO Ext (M=INT, E=Ext)
DS_Mag.ElencoLottiDataTable tabLotti;
tabLotti = taEL.getByKey(bcValue, "*", "*");
if (tabLotti.Count > 0)
{
var riga = tabLotti[0];
answ.codeType = codeType.Lotto;
answ.code = bcValue;
answ.codeInt = codeInt;
answ.description = $"Lotto: {answ.code}";
}
else
{
answ.codeType = codeType.UNK;
}
}
else if (bcValue.Length >= 12)
{
// cerco x UDC
var tabEUdc = taEUdc.getByKey(bcValue);
if (tabEUdc.Count > 0)
{
answ.codeType = codeType.Udc;
answ.code = bcValue;
answ.codeInt = codeInt;
answ.description = $"UDC: {answ.code}";
}
else
{
answ.codeType = codeType.UNK;
}
}
return answ;
}
/// <summary>
/// Esegue svuotamento giacenze dato elenco ODL...
/// </summary>
/// <param name="listODL"></param>
/// <returns></returns>
public bool resetRegGiacByOdl(List<int> listODL)
{
bool fatto = false;
if (listODL.Count > 0)
{
foreach (var item in listODL)
{
taRegGia.DeleteByOdl(item);
fatto = true;
}
}
else
{
fatto = true;
}
return fatto;
}
/// <summary>
/// Esegue salvataggio records giacenze...
/// </summary>
/// <param name="listGiacenze"></param>
/// <returns></returns>
public bool salvaRegGiac(List<RegGiacenze> listGiacenze)
{
bool fatto = false;
if (listGiacenze.Count > 0)
{
foreach (var item in listGiacenze)
{
taRegGia.InsertQuery(item.IdxODL, item.IdentRG, item.Product, item.Variety, item.Supplier, item.ExtDoc, item.DateRif, item.QtyTot, item.NumPack, item.Notes);
fatto = true;
}
}
else
{
fatto = true;
}
return fatto;
}
/// <summary>
/// effettua la stampa di un documento
/// </summary>
/// <param name="keyParam">Codice UNIVOCO del documento</param>
/// <param name="printQueue">
/// stampante specifica (da postazione o std da web.config, a cura dell'utente
/// </param>
/// <param name="tipoDoc">Tipo documento richiesto</param>
/// <param name="clientIp">IP del chiamante</param>
/// <returns></returns>
public bool stampaDoc(string keyParam, string printQueue, tipoDocumento tipoDoc, string clientIp)
{
bool answ = false;
// controllo se esista UDC
if (checkDoc(printQueue, tipoDoc, keyParam))
{
answ = reportPrinter.obj.stampaReport(tipoDoc, keyParam, printQueue);
// svuoto code redis salvate..
memLayer.ML.redFlushKey(redQueueCount);
memLayer.ML.redFlushKey(redQueueCountSet);
if (answ)
{
SteamWare.Logger.Logging.Instance.Info($" | {clientIp} | salvata richiesta stampa su coda {printQueue} | key {keyParam}");
}
else
{
SteamWare.Logger.Logging.Instance.Error($"ERRORE | {clientIp} | richiesta stampa su coda {printQueue} | key {keyParam}");
}
}
else
{
SteamWare.Logger.Logging.Instance.Error($"Impossibile | {clientIp} | richiesta stampa su coda {printQueue} | key {keyParam}");
}
return answ;
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Verifica esistenza record da NOME CODA stampa + tipoDoc + chiave...
/// </summary>
/// <param name="printQueue"></param>
/// <param name="tipoDoc"></param>
/// <param name="keyParam"></param>
/// <returns></returns>
private bool checkDoc(string printQueue, tipoDocumento tipoDoc, string keyParam)
{
bool answ = false;
reportRichiesto tipoReport = reportRichiesto.ND;
// se ho un TIPO DOC --> cerco
if (tipoDoc != tipoDocumento.docND)
{
//cerco in setup queueConf
var repConf = reportConfRedis.Find(a => a.name == $"{tipoDoc}");
if (repConf != null)
{
try
{
tipoReport = (reportRichiesto)Enum.Parse(typeof(reportRichiesto), repConf.template.Replace(".rdlc", ""));
}
catch
{ }
}
}
// se non trovato esplicitamente
if (tipoReport == reportRichiesto.ND)
{
//cerco in setup queueConf
var coda = queueConfRedis.Find(a => a.name == printQueue);
if (coda != null)
{
try
{
tipoReport = (reportRichiesto)Enum.Parse(typeof(reportRichiesto), coda.template.Replace(".rdlc", ""));
}
catch
{ }
}
}
// ricerco!
switch (tipoReport)
{
case reportRichiesto.CartellinoFinitiOdette:
var tabFiniti = taCFOdette.GetData(keyParam);
answ = tabFiniti.Count > 0;
break;
case reportRichiesto.CartellinoPedane:
var tabPedane = taCPed.GetData(keyParam);
answ = tabPedane.Count > 0;
break;
case reportRichiesto.CartellinoSemilavorati:
var tabSemilav = taCSemil.GetData(keyParam);
answ = tabSemilav.Count > 0;
break;
case reportRichiesto.ReportPackList:
var tabPackList = taRepPL.GetData(keyParam, false);
answ = tabPackList.Count > 0;
break;
case reportRichiesto.ReportPackListFull:
var tabPackListFull = taRepPLFull.GetData(keyParam, false);
answ = tabPackListFull.Count > 0;
break;
default:
break;
}
return answ;
}
private void initTA()
{
// reports
taPJQ = new DS_ReportTableAdapters.PrintJobQueueTableAdapter();
taCFOdette = new DS_ReportTableAdapters.stp_prt_CartellinoFinitiOdetteTableAdapter();
taCPed = new DS_ReportTableAdapters.stp_prt_CartellinoPedaneTableAdapter();
taCSemil = new DS_ReportTableAdapters.stp_prt_CartellinoSemilavoratiTableAdapter();
taRepPL = new DS_ReportTableAdapters.stp_prt_ReportPackListTableAdapter();
taRepPLFull = new DS_ReportTableAdapters.stp_prt_ReportPackListFullTableAdapter();
taAA = new DS_MagTableAdapters.AnagArtTableAdapter();
taAA2C = new DS_MagTableAdapters.AnagArticoli2ClientiTableAdapter();
taACF = new DS_MagTableAdapters.AnagClientiTableAdapter();
taADC = new DS_MagTableAdapters.AnagDestinClientiTableAdapter();
taEA2U = new DS_MagTableAdapters.ElencoAL2UDCTableAdapter();
taEAL = new DS_MagTableAdapters.ElencoALTableAdapter();
taEL = new DS_MagTableAdapters.ElencoLottiTableAdapter();
taEP2Q = new DS_MagTableAdapters.Elencopost2QueueTableAdapter();
taEUdc = new DS_MagTableAdapters.ElencoUdcTableAdapter();
taODL = new DS_MagTableAdapters.ODLTableAdapter();
taRegGia = new DS_MagTableAdapters.RegGiacenzeTableAdapter();
taEOL = new DS_PackListTableAdapters.ExtOrdersListTableAdapter();
taOList = new DS_PackListTableAdapters.OrdersListTableAdapter();
taPList = new DS_PackListTableAdapters.PackListTableAdapter();
}
private void setupConnString()
{
string connString = memLayer.ML.CRS("MagDataConnectionString");
// reports
taPJQ.Connection.ConnectionString = connString;
taCFOdette.Connection.ConnectionString = connString;
taCPed.Connection.ConnectionString = connString;
taCSemil.Connection.ConnectionString = connString;
taRepPL.Connection.ConnectionString = connString;
taRepPLFull.Connection.ConnectionString = connString;
taAA.Connection.ConnectionString = connString;
taAA2C.Connection.ConnectionString = connString;
taACF.Connection.ConnectionString = connString;
taADC.Connection.ConnectionString = connString;
taEA2U.Connection.ConnectionString = connString;
taEAL.Connection.ConnectionString = connString;
taEL.Connection.ConnectionString = connString;
taEP2Q.Connection.ConnectionString = connString;
taEUdc.Connection.ConnectionString = connString;
taODL.Connection.ConnectionString = connString;
taRegGia.Connection.ConnectionString = connString;
taEOL.Connection.ConnectionString = connString;
taOList.Connection.ConnectionString = connString;
taPList.Connection.ConnectionString = connString;
}
#endregion Private Methods
}
}