using MagData;
using Newtonsoft.Json;
//using SteamWare.IO;
//using SteamWare.Reports;
//using SteamWare;
using SteamWare.Reports;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web.Http;
namespace MP_MAG.Controllers
{
public class PrintQueueController : ApiController
{
#region Protected Fields
///
/// oggetto static/singleton per fare chiamate sul datalayer
///
protected MagDataLayer MagDataLayerObj = new MagDataLayer();
#endregion Protected Fields
#region Protected Properties
///
/// Conteggio elementi in attesa stsampa da DB
///
protected int countWaitingDb
{
get
{
int answ = 0;
// resetto conteggio in redis...
DS_Report.PrintJobQueueDataTable nextJob = MagDataLayerObj.taPJQ.getWaiting();
if (nextJob != null)
{
answ = nextJob.Count;
}
return answ;
}
}
#endregion Protected Properties
#region Private Methods
///
/// Carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...) e restituisce Dictionary [nome RDS / tab RDS]
///
///
/// cod UDC
/// tabella dati
private Dictionary caricaDati(MagData.reportRichiesto tipoReport, string keyParam)
{
Dictionary answ = new Dictionary();
DataTable tab = new DataTable();
switch (tipoReport)
{
case MagData.reportRichiesto.CartellinoFinitiOdette:
tab = MagDataLayerObj.taCFOdette.GetData(keyParam);
answ.Add("stp_prt_CartellinoFinitiOdette", tab);
break;
case MagData.reportRichiesto.CartellinoPedane:
tab = MagDataLayerObj.taCPed.GetData(keyParam);
answ.Add("stp_prt_CartellinoPedane", tab);
break;
case MagData.reportRichiesto.CartellinoSemilavorati:
tab = MagDataLayerObj.taCSemil.GetData(keyParam);
answ.Add("stp_prt_CartellinoSemilavorati", tab);
break;
case MagData.reportRichiesto.ReportPackList:
tab = MagDataLayerObj.taRepPL.GetData(keyParam, false);
answ.Add("stp_prt_ReportPackList", tab);
break;
case MagData.reportRichiesto.ReportPackListFull:
tab = MagDataLayerObj.taRepPLFull.GetData(keyParam, false);
answ.Add("stp_prt_ReportPackListFull", tab);
break;
default:
break;
}
// CHECK FIXME 2021.03.03: verificare sia OK dispose
tab.Dispose();
return answ;
}
#endregion Private Methods
#region Protected Methods
///
/// Recupera report da CODA report
///
///
///
protected MagData.reportRichiesto reportByQueue(string queueName)
{
MagData.reportRichiesto report = MagData.reportRichiesto.ND;
var qConf = MagDataLayerObj.queueConfRedis.Find(x => x.name == queueName);
if (qConf != null)
{
// decodifico
try
{
report = (MagData.reportRichiesto)Enum.Parse(typeof(MagData.reportRichiesto), qConf.template.Replace(".rdlc", ""));
}
catch
{ }
}
return report;
}
///
/// Recupera report da conf TipoDoc
///
///
///
protected MagData.reportRichiesto reportByTipoDoc(string name)
{
MagData.reportRichiesto report = MagData.reportRichiesto.ND;
var qConf = MagDataLayerObj.reportConfRedis.Find(x => x.name == name);
if (qConf != null)
{
// decodifico
try
{
report = (MagData.reportRichiesto)Enum.Parse(typeof(MagData.reportRichiesto), qConf.template.Replace(".rdlc", ""));
}
catch
{ }
}
return report;
}
#endregion Protected Methods
#region Public Methods
///
/// Restituisce numero jobs aperti (stato = 0...), se 0 = NESSUNO
/// GET: api/PrintQueue
///
///
public int Get()
{
// restituisco...
int answ = 0;
string redVal = SteamWare.memLayer.ML.getRSV(MagDataLayer.redQueueCount);
// cerco in redis se ci sia chiave..
if (!string.IsNullOrEmpty(redVal))
{
// recupero
int.TryParse(redVal, out answ);
}
else
{
int ttlPrintCount = SteamWare.memLayer.ML.CRI("ttlPrintCount");
// recupero da dbe e salvo
answ = countWaitingDb;
SteamWare.memLayer.ML.setRSV(MagDataLayer.redQueueCount, $"{answ}", ttlPrintCount);
}
return answ;
}
///
/// Elenco dei jobs aperti per la coda indicata
/// GET: api/PrintQueue/queue_01
///
///
///
public List Get(string id)
{
List answ = new List();
// recupero da DB...
DS_Report.PrintJobQueueDataTable tabPJQ = MagDataLayerObj.taPJQ.getByQueue(id);
if (tabPJQ.Count > 0)
{
reportData currReport = new reportData();
Dictionary currRdsData;
// ciclo!
MagData.reportRichiesto tipoRep = MagData.reportRichiesto.ND;
foreach (var pjReq in tabPJQ)
{
// verifico SE HO un tipo report particolare...
tipoRep = reportByTipoDoc(pjReq.TipoReport);
if (tipoRep == MagData.reportRichiesto.ND)
{
tipoRep = reportByQueue(pjReq.prtName);
}
currRdsData = caricaDati(tipoRep, pjReq.KeyParam);
// in base alla coda --> recupero i dati
currReport = new reportData()
{
ticketNum = $"{pjReq.IdxPrintJob}",
template = $"{pjReq.TipoReport}",
rdsData = currRdsData
};
answ.Add(currReport);
}
}
// compongo risposta...
return answ;
}
///
/// Processa una chiamata POST per l'invio in blocco del risultato dell'elaborazione
/// POST: api/PrintQueue
///
///
[HttpPost]
public string Post()
{
string answ = "UNKN";
// questa classe è derivata da Controller.Response... x cui recupero lo stream in altro modo...
string content = "";
System.Web.HttpContext.Current.Request.InputStream.Position = 0;
using (var reader = new StreamReader(System.Web.HttpContext.Current.Request.InputStream, System.Text.Encoding.UTF8, true, 4096, true))
{
content = reader.ReadToEnd();
}
//Rest
System.Web.HttpContext.Current.Request.InputStream.Position = 0;
// procedo a deserializzare in blocco l'oggetto...
try
{
// deserializzo.
printTask printAnsw = JsonConvert.DeserializeObject(content);
// verifico se mi abbia dato esito 1 --> aggiorno DB!
if (printAnsw != null)
{
int idxPJQ = 0;
int.TryParse(printAnsw.ticketNum, out idxPJQ);
MagDataLayerObj.taPJQ.updateStato(idxPJQ, printAnsw.newStatus);
// resetto conteggio in redis...
SteamWare.memLayer.ML.setRSV(MagDataLayer.redQueueCount, $"{countWaitingDb}", 30);
answ = "OK";
}
}
catch
{ }
// restituisco esito!
return answ;
}
#endregion Public Methods
}
}