kreta/Kreta.Job.Tasks/EESZTInterfaceJob.cs
2024-03-13 00:33:46 +01:00

152 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Kreta.Core;
using Kreta.Core.Configuratiaton;
using Kreta.DataAccessManual;
using Kreta.EESZTInterface;
using Kreta.EESZTInterface.Processors;
using Kreta.Job.Tasks.Core;
using Kreta.Resources;
namespace Kreta.Job.Tasks
{
public class EESZTInterfaceJob : IEESZTInterfaceJob
{
public EESZTConfiguration EESZTConfiguration { get; }
public KretaJobConfig KretaJobConfig { get; set; }
public EESZTInterfaceJob(EESZTConfiguration eESZTConfiguration, KretaJobConfig kretaJobConfig)
{
EESZTConfiguration = eESZTConfiguration;
KretaJobConfig = kretaJobConfig;
}
public void GetEESZTAllomany()
{
var adat = new List<byte>();
string allomanyNev = "";
try
{
var tajProcessor = new TAJProcessor();
var stsHelper = new STSHelper();
var samlAssertion = stsHelper.GetSamlAssertion(EESZTConfiguration.InterfaceUrl);
var eFtHelper = new EFTHelper(EESZTConfiguration.InterfaceUrl);
var clientUserId = EESZTConfiguration.ClientUserId;
var organizationId = EESZTConfiguration.OrganizationId;
var getIntezmenyiAllomanyListaResponse = eFtHelper.CallIntezmenyAllomanyLista(samlAssertion, clientUserId, organizationId);
var allomany = getIntezmenyiAllomanyListaResponse.IntezmenyiAllomanyListaResponse.IntezmenyiAllomanyListaResponseBusinessContent.Allomanyok.OrderByDescending(x => x.Datum).FirstOrDefault();
if (allomany == default)
{
throw new ApplicationException(EESZTInterfaceResource.NemVoltLetolthetoAllomany);
}
allomanyNev = $"{allomany.Nev}{allomany.Kiterjesztes}";
var publikusId = allomany.PublikusId;
var sorszam = allomany.Darabszam;
for (int j = 0; j < sorszam; j++)
{
var (getAllomanyReszResponse, attachment) = eFtHelper.CallAllomanyResz(samlAssertion, clientUserId, organizationId, publikusId, j + 1);
var computedHashArray = EFTHelper.ComputeSha256Hash(attachment);
var computedHash = Convert.ToBase64String(computedHashArray);
if (computedHash != getAllomanyReszResponse.AllomanyReszResponse.AllomanyReszResponseBusinessContent.AllomanyResz.Hash)
{
throw new ApplicationException(string.Format(EESZTInterfaceResource.AllomanyReszHashKulonbozoseg, publikusId, j));
}
adat.AddRange(attachment);
}
var hashArray = EFTHelper.ComputeSha256Hash(adat.ToArray());
var hash = Convert.ToBase64String(hashArray);
if (hash != allomany.Hash)
{
throw new ApplicationException(string.Format(EESZTInterfaceResource.AllomanyHashKulonbozoseg, publikusId));
}
//File.WriteAllBytes($@"c:\temp\eFT\{allomany.Nev}_{allomany.Datum:yyMMdd_hhmmss}_{DateTime.Now:yyMMdd_hhmmss}{allomany.Kiterjesztes}", adat.ToArray());// EFTHelper.Decompress(adat.ToArray()));
var sikeresAllomanyLetoltesResponse = eFtHelper.CallSikeresAllomanyLetoltes(samlAssertion, clientUserId, organizationId, publikusId);
if (sikeresAllomanyLetoltesResponse.SikeresAllomanyLetoltesResponse.SikeresAllomanyLetoltesResponseBusinessContent != "")
{
throw new ApplicationException(string.Format(EESZTInterfaceResource.SikeresLetoltesVisszejelzesHiba, publikusId));
}
tajProcessor.AddFileContent(adat.ToArray());
var fertozottOssz = 0;
var marNemFertozottOssz = 0;
foreach (var azonosito in KretaServer.KretaServer.Instance.GetOsszesIntezmeny())
{
var connectionString = KretaServer.KretaServer.Instance.GetIntezmenyConnectionString(azonosito);
Dal.ServiceSystemConnection.Run(connectionString, h =>
{
var intezmenyId = h.IntezmenyDal().GetIntezmenyIdByAzonosito(azonosito);
if (intezmenyId.HasValue)
{
var intezmeny = h.IntezmenyDal().Get(intezmenyId.Value);
var tanev = intezmeny.Tanev.SingleOrDefault(x => x.Aktiv.HasValue && x.Aktiv.Value && !x.Torolt);
if (tanev != default)
{
var (fertozott, marNemFertozott) = tajProcessor.UpdateDB(h, tanev.ID);
fertozottOssz += fertozott;
marNemFertozottOssz += marNemFertozott;
}
}
});
}
if (!string.IsNullOrWhiteSpace(EESZTConfiguration.MailToAddresses))
{
SendEESZTInterfaceEmail(true, new MemoryStream(adat.ToArray()), allomanyNev, string.Format(EESZTInterfaceResource.FertozottMarNemFertozottText, fertozottOssz, marNemFertozottOssz));
}
}
catch (Exception ex)
{
if (!string.IsNullOrWhiteSpace(EESZTConfiguration.MailToAddresses))
{
SendEESZTInterfaceEmail(false, new MemoryStream(adat.ToArray()), allomanyNev, ex.Message);
}
throw;
}
}
private void SendEESZTInterfaceEmail(bool sikeres, MemoryStream ms, string allomanynev, string bodyExtension = null)
{
var mail = new MailMessage(KretaJobConfig.DefaultEmail, KretaJobConfig.DefaultEmail)
{
Subject = sikeres ? EESZTInterfaceResource.EmailSubjectSikeres : EESZTInterfaceResource.EmailSubjectSikertelen,
SubjectEncoding = Encoding.UTF8,
Body = string.Format((sikeres ? EESZTInterfaceResource.EmailTorzsSikeres : EESZTInterfaceResource.EmailTorzsSikertelen), KretaServer.KretaServer.Instance.Configuration.SystemType),
IsBodyHtml = false,
BodyEncoding = Encoding.UTF8,
DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
};
if (!string.IsNullOrWhiteSpace(bodyExtension))
{
mail.Body += (Environment.NewLine + bodyExtension);
}
Attachment attachment = null;
if (ms.Length > 0)
{
attachment = new Attachment(ms, allomanynev);
mail.Attachments.Add(attachment);
}
foreach (var address in EESZTConfiguration.MailToAddresses.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
mail.To.Add(address);
}
mail.To.RemoveAt(0);
using (var client = new SmtpClient())
{
client.Send(mail);
}
if (attachment != null)
{
attachment.Dispose();
}
ms.Close();
}
}
}