2025-06-03 15:11:16 +02:00

176 lines
6.5 KiB
C#

using System;
using System.Configuration;
using DataAccessLayer;
using System.Net;
//using NUnit.Framework.SyntaxHelpers;
using System.Linq;
using NLog;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using System.Reflection;
using GestorePDF.Logic;
namespace GestorePDF
{
public class ThreadManager
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
logger.WriteToConsole("Start GestorePdf");
//=0= START OF GESTOREPDF
CACHE.Start(Assembly.GetExecutingAssembly());
//
string qc = MyInCommand(args, "querycontratti");
if (qc != string.Empty)
CACHE.QueryContratti = qc;
string qctest = MyInCommand(args, "recuperopdf");
GestorePDF.Logic.ThreadManager m = new GestorePDF.Logic.ThreadManager();
if (qctest == string.Empty)
m.Main();
else
{
//string f = @"C:\REPO\LISTAGG.txt";
//if (File.Exists(f)) File.Delete(f);
//Recupero differenziale da dei pdf di lettere alle righe nella tabella C6ReportFisico e Metadati
RecuperoLettere(m, qctest);
}
// =0= USCITA AL TERMINE DELL'ELABORAZIONE di GESTOREPDF
SQLServer.CLOSECONNECTIONS();
logger.WriteToConsole("End GestorePdf");
}
private static string MyInCommand(string[] args, string command)
{
string ret = string.Empty;
if (args != null && args.Length > 0)
{
foreach (var arg in args)
{
if (arg.Contains("="))
{
var valori = arg.Split('=');
if (valori.Length == 2)
{
if (valori[0].ToLower() == command.ToLower())
{
ret = valori[1];
break;
}
}
}
}
}
return ret;
}
private static void RecuperoLettere(GestorePDF.Logic.ThreadManager m, string PdfPath)
{
if (PdfPath == null || PdfPath.Trim().Length == 0) return;
try
{
var apdfs = Directory.GetFiles(PdfPath, "DIAGNOSI_*_*.pdf", SearchOption.TopDirectoryOnly);
if (apdfs == null || apdfs.Length == 0)
{
logger.WriteToConsole("La cartella indicata non contiene files");
}
else
{
logger.WriteToConsole($"La cartella indicata contiene {apdfs.Length} files");
DataTable differenziale = null;
using (SQLServer db = new SQLServer())
{
differenziale = db.GetDataTableFromProcedure(CACHE.QueryLettereRecupero);
}
if (differenziale == null || differenziale.Rows.Count == 0)
{
logger.WriteToConsole($"La sp {CACHE.QueryLettereRecupero} indicata non contiene records");
return;
}
else
{
logger.WriteToConsole($"La sp {CACHE.QueryLettereRecupero} indicata contiene {differenziale.Rows.Count} records");
}
var arIE = apdfs.ToList();
List<RecuperoPDF> lista = new List<RecuperoPDF>();
int c = 0;
System.Threading.Tasks.Parallel.ForEach(differenziale.Rows.Cast<DataRow>(), new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, dr =>
{
string cf = dr["CodiceFiscale"].TOString();
string rt = dr["Rete"].TOString();
string CAMPIONE = $"DIAGNOSI_{cf}_{rt}.PDF";
var element = arIE.FirstOrDefault(x => Path.GetFileName(x).ToUpper() == CAMPIONE);
if (element != null)
{
RecuperoPDF r = new RecuperoPDF
{
CodiceContratto = dr["CodiceContratto"].TOString(),
Rete = rt,
CodiceFiscale = cf,
NomeCliente = dr["NomeCliente"].TOString(),
CognomeCliente = dr["CognomeCliente"].TOString(),
CodicePB = dr["CodicePB"].TOString(),
NomePB = dr["NomePB"].TOString(),
CognomePB = dr["CognomePB"].TOString(),
numeroPagine = dr["numeroPagine"].ToInt32(),
Bytes = File.ReadAllBytes(element)
};
lista.Add(r);
string msg = $"Aggiunto il record da inserire corrispondente al file {CAMPIONE}";
logger.Debugs(msg);
logger.WriteToConsole(msg);
}
else
{
logger.Debugs($"Non ho trovato il file {CAMPIONE} corrispondente al dato in tabella");
}
c++;
});
if (lista.Count > 0)
{
logger.Debugs($"Inizio inserimento dei {lista.Count} records...");
var record_inseriti = m.Recupero(lista);
logger.Debugs($"Fine inserimento dei {lista.Count} records.");
if (record_inseriti > 0)
logger.Debugs($"Inseriti {record_inseriti} nella tabella richiesta");
else
logger.Debugs("Non e' stato inserito alcun record nella tabella richiesta");
}
else
{
logger.WriteToConsole($"Non ci sono pdf nella cartella {PdfPath} che matchano l'esecuzione della SP:{CACHE.QueryLettereRecupero}");
}
}
}
catch (Exception ex)
{
}
}
}
}