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
///
/// oggetto static/singleton per fare chiamate sul datalayer
///
public static MagDataLayer man = new MagDataLayer();
///
/// RegExp x Qty odette
///
public static string re_QtyOdette = memLayer.ML.cdv("regExp_QtyOdette");
public static string redQueueConf = "MP-CTRACK:MAG:queueConf";
///
/// chaive redis x cache conteggio coda PJQ
///
public static string redQueueCount = "MP:MAG:PJQ";
///
/// chaive redis x cache conteggio coda PJQ composta da tante code
///
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
///
/// Dati Prod e Print correnti x sessione utente salvati come cookie
///
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(rawData);
}
return answ;
}
}
///
/// Restituisce un array JSon x le conf delle code di stampa leggendo dal file
///
/// lista oggetto Json in formato SteamwareSDK.queueConf
public List queueConfJson
{
get
{
List answ = new List();
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>(rawData);
}
catch (Exception exc)
{
SteamWare.Logger.Logging.Instance.Error(exc, "Eccezione in decodifica file conf queueConfJson");
}
}
}
return answ;
}
}
///
/// Cache redis della conf delle code
///
/// lista oggetto Json in formato SteamwareSDK.queueConf
public List queueConfRedis
{
get
{
List answ = new List();
string redKey = memLayer.ML.redHash(redQueueConf);
string rawData = memLayer.ML.getRSV(redKey);
if (!string.IsNullOrEmpty(rawData))
{
try
{
answ = JsonConvert.DeserializeObject>(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);
}
}
///
/// Restituisce un array JSon x le conf dei report/template di stampa leggendo dal file
///
/// lista oggetto Json in formato SteamwareSDK.templateConf
public List reportConfJson
{
get
{
List answ = new List();
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>(rawData);
}
catch (Exception exc)
{
SteamWare.Logger.Logging.Instance.Error(exc, "Eccezione in decodifica file conf reportConfJson");
}
}
}
return answ;
}
}
///
/// Cache redis della conf dei tipo report
///
/// lista oggetto Json in formato SteamwareSDK.queueConf
public List reportConfRedis
{
get
{
List answ = new List();
string redKey = memLayer.ML.redHash(redReportConf);
string rawData = memLayer.ML.getRSV(redKey);
if (!string.IsNullOrEmpty(rawData))
{
try
{
answ = JsonConvert.DeserializeObject>(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
///
/// Decodifica barcode
///
///
///
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;
}
///
/// Esegue svuotamento giacenze dato elenco ODL...
///
///
///
public bool resetRegGiacByOdl(List listODL)
{
bool fatto = false;
if (listODL.Count > 0)
{
foreach (var item in listODL)
{
taRegGia.DeleteByOdl(item);
fatto = true;
}
}
else
{
fatto = true;
}
return fatto;
}
///
/// Esegue salvataggio records giacenze...
///
///
///
public bool salvaRegGiac(List 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;
}
///
/// effettua la stampa di un documento
///
/// Codice UNIVOCO del documento
///
/// stampante specifica (da postazione o std da web.config, a cura dell'utente
///
/// Tipo documento richiesto
/// IP del chiamante
///
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
///
/// Verifica esistenza record da NOME CODA stampa + tipoDoc + chiave...
///
///
///
///
///
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
}
}