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 definizione Regexp /// /// RegExp x Qty odette /// public static string re_QtyOdette = memLayer.ML.cdv("regExp_QtyOdette"); #endregion #region oggetti public // reports public DS_ReportTableAdapters.PrintJobQueueTableAdapter taPJQ; public DS_ReportTableAdapters.stp_prt_CartellinoFinitiOdetteTableAdapter taCFOdette; public DS_ReportTableAdapters.stp_prt_CartellinoPedaneTableAdapter taCPed; public DS_ReportTableAdapters.stp_prt_CartellinoSemilavoratiTableAdapter taCSemil; public DS_MagTableAdapters.AnagArtTableAdapter taAA; public DS_MagTableAdapters.ElencoALTableAdapter taEAL; public DS_MagTableAdapters.ElencoLottiTableAdapter taEL; public DS_MagTableAdapters.Elencopost2QueueTableAdapter taEP2Q; public DS_MagTableAdapters.ElencoUdcTableAdapter taEUdc; public DS_MagTableAdapters.ODLTableAdapter taODL; #endregion #region init classe public MagDataLayer() { // inizializzaizone classe initTA(); setupConnString(); } 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; taAA.Connection.ConnectionString = connString; taEAL.Connection.ConnectionString = connString; taEL.Connection.ConnectionString = connString; taEP2Q.Connection.ConnectionString = connString; taEUdc.Connection.ConnectionString = connString; taODL.Connection.ConnectionString = connString; } 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(); taAA = new DS_MagTableAdapters.AnagArtTableAdapter(); 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(); } /// /// oggetto static/singleton per fare chiamate sul datalayer /// public static MagDataLayer man = new MagDataLayer(); #endregion #region helpers e utils public static string redQueueConf = "MP-CTRACK:MAG:queueConf"; /// /// 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 printQueue"); } } } 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 = queueConfJson; } return answ; } set { string redKey = memLayer.ML.redHash(redQueueConf); string rawData = ""; // salvo inc ache per 1 h... memLayer.ML.setRSV(redKey, rawData, 3600); } } /// /// effettua la stampa di un documento /// /// Codice UNIVOCO del documento /// stampante specifica (da postazione o std da web.config, a cura dell'utente /// IP del chiamante /// public bool stampaDoc(string keyParam, string printQueue, string clientIp) { bool answ = false; // controllo se esista UDC if (checkDoc(printQueue, keyParam)) { answ = reportPrinter.obj.stampaReport(keyParam, printQueue); 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; } /// /// Verifica esistenza record da NOME CODA stampa + chiave... /// /// /// /// private bool checkDoc(string printQueue, string keyParam) { bool answ = false; reportRichiesto 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; default: break; } return answ; } /// /// Decodifica barcode /// /// /// public decodedData decodeBcode(string bcValue) { decodedData answ = new decodedData(); answ.rawData = bcValue; if (bcValue.StartsWith("M")) { answ.codeType = codeType.LottoInt; answ.code = bcValue.Replace("M", ""); answ.codeInt = 0; answ.description = $"Lotto: {answ.code}"; } else if (bcValue.StartsWith("P")) { // conrollo se data (6 numeri...) oppure part... if (bcValue.Length == 7) { answ.codeType = codeType.Date; answ.code = bcValue.Replace("P", ""); int codeInt = 0; 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 codeInt = 0; 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 codeInt = 0; 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", ""); int codeInt = 0; 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; int codeInt = 0; 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; int codeInt = 0; answ.codeInt = codeInt; answ.description = $"UDC: {answ.code}"; } else { answ.codeType = codeType.UNK; } } return answ; } #endregion } }