328 lines
15 KiB
C#
328 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Aspose.Cells;
|
|
using Hangfire;
|
|
using Kreta.Core.Configuratiaton.Interface;
|
|
using Kreta.Core.Logic;
|
|
using Kreta.DataAccessManual;
|
|
using Kreta.DataAccessManual.ParameterClasses;
|
|
using Kreta.Framework;
|
|
using Kreta.Job.Tasks.Core.Models;
|
|
using Kreta.Resources;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Kreta.Job.Tasks.Core
|
|
{
|
|
public abstract class BankszamlaIgenylesJob
|
|
{
|
|
private readonly string FINISHED_EXTENSION = "finished";
|
|
private readonly string CSV_EXTENSION = "csv";
|
|
private readonly string DATETIMEFORMAT = "MM/dd/yyyy HH:mm:ss";
|
|
|
|
public void BankszamlaIgenyles()
|
|
{
|
|
|
|
}
|
|
|
|
protected abstract string GetInPart();
|
|
protected abstract string GetErrorPart();
|
|
protected abstract string GetOkPart();
|
|
protected abstract string GetArchivePart();
|
|
protected abstract string DecryptEgyediAzonosito(string resceivedEgyediAzonosito);
|
|
protected abstract bool HasRowWithError(List<BankszamlaIgenylesPco> bankszamlaIgenylesPcos);
|
|
|
|
protected void RunBankszamlaIgenylesJob<T>() where T : IBankszamlaIgenylesConfiguration
|
|
{
|
|
foreach ((FileStream FileStream, string FileName) fileStreamAndName in GetSzamlaIgenylesFileList<T>())
|
|
{
|
|
var bankszamlaIgenylesPcoList = GetPcoListFromCsv(fileStreamAndName.FileStream);
|
|
|
|
var connectionStringList = GetBankiIntezmenyConnectionStringListFromFile();
|
|
foreach (string connectionString in connectionStringList)
|
|
{
|
|
var resultBankszamlaIgenylesPcoList = new List<BankszamlaIgenylesPco>();
|
|
foreach (BankszamlaIgenylesPco bankszamlaIgenylesPco in bankszamlaIgenylesPcoList)
|
|
{
|
|
if (!bankszamlaIgenylesPco.IsError && string.IsNullOrWhiteSpace(bankszamlaIgenylesPco.IntezmenyAzonosito))
|
|
{
|
|
Dal.ServiceSystemConnection.Run(connectionString, h => { resultBankszamlaIgenylesPcoList.Add(h.BankszamlaIgenylesDal().BankszamlaIgenyles(bankszamlaIgenylesPco)); });
|
|
}
|
|
else
|
|
{
|
|
resultBankszamlaIgenylesPcoList.Add(bankszamlaIgenylesPco);
|
|
}
|
|
}
|
|
bankszamlaIgenylesPcoList = resultBankszamlaIgenylesPcoList;
|
|
}
|
|
|
|
foreach (var bankszamlaIgenylesPco in bankszamlaIgenylesPcoList)
|
|
{
|
|
if (bankszamlaIgenylesPco.IsTanuloNemtalalhatoEgyediAzonositoAlapjan)
|
|
{
|
|
bankszamlaIgenylesPco.IsError = true;
|
|
bankszamlaIgenylesPco.ErrorText = string.Format(ErrorResource.BankiTanuloNemTalalhatoEgyediAzonositoAlapjan, bankszamlaIgenylesPco.EgyediAzonosito);
|
|
}
|
|
}
|
|
|
|
CreateFiles<T>(bankszamlaIgenylesPcoList, fileStreamAndName.FileName);
|
|
SendEmail<T>(bankszamlaIgenylesPcoList, fileStreamAndName.FileName);
|
|
}
|
|
}
|
|
|
|
protected List<BankszamlaIgenylesPco> GetPcoListFromCsv(FileStream fileStream)
|
|
{
|
|
var result = new List<BankszamlaIgenylesPco>();
|
|
|
|
var loadOptions = new TxtLoadOptions(LoadFormat.CSV)
|
|
{
|
|
Separator = '|',
|
|
Encoding = Encoding.GetEncoding("ISO-8859-1")
|
|
};
|
|
using (fileStream)
|
|
using (var workbook = new Workbook(fileStream, loadOptions))
|
|
{
|
|
Worksheet worksheet = workbook.Worksheets[0];
|
|
Cells cells = worksheet.Cells;
|
|
int lastRowIndex = cells.Rows.Count;
|
|
for (int rowIndex = 2; rowIndex < lastRowIndex; rowIndex++)
|
|
{
|
|
string egyediAzonosito = cells.GetCell(rowIndex, 0) == null ? string.Empty : cells.GetCell(rowIndex, 0).Value.ToString();
|
|
string szamlaszam = cells.GetCell(rowIndex, 1) == null ? string.Empty : cells.GetCell(rowIndex, 1).Value.ToString();
|
|
string nyitasNapja = cells.GetCell(rowIndex, 2) == null ? string.Empty : cells.GetCell(rowIndex, 2).Value.ToString();
|
|
var pco = new BankszamlaIgenylesPco
|
|
{
|
|
EgyediAzonosito = egyediAzonosito,
|
|
BankszamlaSzam = szamlaszam,
|
|
NyitasNapja = nyitasNapja,
|
|
Sorszam = rowIndex - 1
|
|
};
|
|
|
|
result.Add(BankszamlaIgenylesPcoValidator(pco));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<(FileStream FileStream, string FileName)> GetSzamlaIgenylesFileList<T>() where T : IBankszamlaIgenylesConfiguration
|
|
{
|
|
var configuration = (T)ConfigurationManager.GetSection(typeof(T).Name);
|
|
|
|
var fileStreamAndNameList = new List<(FileStream, string)>();
|
|
|
|
List<string> finishedFileList = Directory.GetFiles($"{configuration.ArrivedFilePathRoot}{GetInPart()}", $"*.{FINISHED_EXTENSION}")
|
|
.Select(Path.GetFileName)
|
|
.ToList();
|
|
|
|
List<string> csvFileList = Directory.GetFiles($"{configuration.ArrivedFilePathRoot}{GetInPart()}", $"*.{CSV_EXTENSION}")
|
|
.Select(Path.GetFileName)
|
|
.ToList();
|
|
|
|
foreach (var fileName in csvFileList)
|
|
{
|
|
int index = fileName.LastIndexOf(".");
|
|
if (index > 0)
|
|
{
|
|
string fileNameWithoutExtenion = fileName.Substring(0, index);
|
|
if (finishedFileList.Any(x => x.Substring(0, index).Equals(fileNameWithoutExtenion)))
|
|
{
|
|
var file = new FileStream($"{configuration.ArrivedFilePathRoot}{GetInPart()}{fileName}", FileMode.Open, FileAccess.Read);
|
|
fileStreamAndNameList.Add((file, fileName));
|
|
}
|
|
}
|
|
}
|
|
|
|
return fileStreamAndNameList;
|
|
}
|
|
|
|
protected void CreateFiles<T>(List<BankszamlaIgenylesPco> bankszamlaIgenylesPco, string fileName) where T : IBankszamlaIgenylesConfiguration
|
|
{
|
|
var configuration = (T)ConfigurationManager.GetSection(typeof(T).Name);
|
|
|
|
if (bankszamlaIgenylesPco.Any(x => !x.IsError))
|
|
{
|
|
//NOTE:Create OK file
|
|
SaveFileFromMemoryStream(CreateFileFromPco(bankszamlaIgenylesPco.Where(x => !x.IsError).ToList(), fileName), fileName, $"{configuration.ArrivedFilePathRoot}{GetOkPart()}");
|
|
}
|
|
|
|
if (bankszamlaIgenylesPco.Any(x => x.IsError))
|
|
{
|
|
//NOTE:Create Error file
|
|
SaveFileFromMemoryStream(CreateFileFromPco(bankszamlaIgenylesPco.Where(x => x.IsError).ToList(), fileName), fileName, $"{configuration.ArrivedFilePathRoot}{GetErrorPart()}");
|
|
}
|
|
|
|
//NOTE: Az eredeti fájlok (.csv, .finished) az archive mappába kerülnek
|
|
ArchivateOldFiles(configuration, fileName);
|
|
}
|
|
|
|
private void ArchivateOldFiles(IBankszamlaIgenylesConfiguration configuration, string fileName)
|
|
{
|
|
var fileNameWithoutExtenion = string.Empty;
|
|
int index = fileName.LastIndexOf(".");
|
|
if (index > 0)
|
|
{
|
|
fileNameWithoutExtenion = fileName.Substring(0, index);
|
|
}
|
|
|
|
File.Move($"{configuration.ArrivedFilePathRoot}{GetInPart()}{fileNameWithoutExtenion}.{CSV_EXTENSION}", $"{configuration.ArrivedFilePathRoot}{GetArchivePart()}{fileNameWithoutExtenion}.{CSV_EXTENSION}");
|
|
if (File.Exists($"{configuration.ArrivedFilePathRoot}{GetInPart()}{fileNameWithoutExtenion}.{FINISHED_EXTENSION}"))
|
|
{
|
|
File.Move($"{configuration.ArrivedFilePathRoot}{GetInPart()}{fileNameWithoutExtenion}.{FINISHED_EXTENSION}", $"{configuration.ArrivedFilePathRoot}{GetArchivePart()}{fileNameWithoutExtenion}.{FINISHED_EXTENSION}");
|
|
}
|
|
if (File.Exists($"{configuration.ArrivedFilePathRoot}{GetInPart()}{fileNameWithoutExtenion}.{CSV_EXTENSION}.{FINISHED_EXTENSION}"))
|
|
{
|
|
File.Move($"{configuration.ArrivedFilePathRoot}{GetInPart()}{fileNameWithoutExtenion}.{CSV_EXTENSION}.{FINISHED_EXTENSION}", $"{configuration.ArrivedFilePathRoot}{GetArchivePart()}{fileNameWithoutExtenion}.{CSV_EXTENSION}.{FINISHED_EXTENSION}");
|
|
}
|
|
}
|
|
|
|
private MemoryStream CreateFileFromPco(List<BankszamlaIgenylesPco> bankszamlaIgenylesPco, string fileName)
|
|
{
|
|
using (var workbook = new Workbook())
|
|
{
|
|
WorksheetCollection worksheets = workbook.Worksheets;
|
|
Worksheet worksheet = worksheets.Add(fileName.Length < 32 ? fileName : fileName.Substring(0, 30));
|
|
|
|
int rowNumber = 1;
|
|
foreach (BankszamlaIgenylesPco pco in bankszamlaIgenylesPco)
|
|
{
|
|
worksheet.Cells[rowNumber, 0].Value = $"'{pco.EgyediAzonosito}";
|
|
worksheet.Cells[rowNumber, 1].Value = $"'{pco.BankszamlaSzam}";
|
|
|
|
rowNumber++;
|
|
}
|
|
|
|
var saveOptions = new TxtSaveOptions(SaveFormat.CSV)
|
|
{
|
|
Separator = '|',
|
|
Encoding = Encoding.GetEncoding("ISO-8859-1")
|
|
};
|
|
|
|
var memorystream = new MemoryStream();
|
|
workbook.Worksheets.ActiveSheetIndex = 1;
|
|
workbook.Save(memorystream, saveOptions);
|
|
memorystream.Position = 0;
|
|
return memorystream;
|
|
}
|
|
}
|
|
|
|
private void SaveFileFromMemoryStream(MemoryStream memoryStream, string fileName, string path)
|
|
{
|
|
using (var file = new FileStream($"{path}{fileName}", FileMode.Create, FileAccess.Write))
|
|
{
|
|
var bytes = new byte[memoryStream.Length];
|
|
_ = memoryStream.Read(bytes, 0, (int)memoryStream.Length);
|
|
file.Write(bytes, 0, bytes.Length);
|
|
memoryStream.Close();
|
|
file.Close();
|
|
}
|
|
}
|
|
|
|
private BankszamlaIgenylesPco BankszamlaIgenylesPcoValidator(BankszamlaIgenylesPco bankszamlaIgenylesPco)
|
|
{
|
|
var errorList = new List<string>();
|
|
bankszamlaIgenylesPco.EgyediAzonosito = DecryptEgyediAzonosito(bankszamlaIgenylesPco.EgyediAzonosito);
|
|
BankszamlaIgenylesPco pco = bankszamlaIgenylesPco;
|
|
if (string.IsNullOrWhiteSpace(bankszamlaIgenylesPco.EgyediAzonosito))
|
|
{
|
|
errorList.Add(ErrorResource.BankiEgyediAzonositoKotelezo);
|
|
}
|
|
else
|
|
{
|
|
if (!Guid.TryParse(bankszamlaIgenylesPco.EgyediAzonosito, out _))
|
|
{
|
|
errorList.Add(ErrorResource.BankiEgyediAzonositoNemMegfeleloFormatum);
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(bankszamlaIgenylesPco.NyitasNapja))
|
|
{
|
|
errorList.Add(ErrorResource.BankiNyitasNapjaKotelezo);
|
|
}
|
|
else
|
|
{
|
|
if (DateTime.TryParseExact(bankszamlaIgenylesPco.NyitasNapja, DATETIMEFORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
|
|
{
|
|
errorList.Add(ErrorResource.BankiNyitasNapjaNemMegfeleloFormatum);
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(bankszamlaIgenylesPco.BankszamlaSzam))
|
|
{
|
|
errorList.Add(ErrorResource.BankiBankszamlaSzamKotelezo);
|
|
}
|
|
else
|
|
{
|
|
bankszamlaIgenylesPco.BankszamlaSzam = bankszamlaIgenylesPco.BankszamlaSzam.Replace(" ", string.Empty).Replace("-", string.Empty);
|
|
string errorText = BankszamlaLogic.CDVValidacio(string.Join("-", BankszamlaLogic.StringSplitByChunkSize(bankszamlaIgenylesPco.BankszamlaSzam, 8)));
|
|
if (!string.IsNullOrWhiteSpace(errorText))
|
|
{
|
|
errorList.Add(errorText);
|
|
}
|
|
}
|
|
|
|
bankszamlaIgenylesPco.IsError = errorList.Count > 0;
|
|
bankszamlaIgenylesPco.ErrorText = string.Join(Environment.NewLine, errorList);
|
|
return pco;
|
|
}
|
|
|
|
protected void SendEmail<T>(List<BankszamlaIgenylesPco> bankszamlaIgenylesPcoList, string fileName) where T : IBankszamlaIgenylesConfiguration
|
|
{
|
|
if (!HasRowWithError(bankszamlaIgenylesPcoList))
|
|
{
|
|
// NOTE: OTP esetén csak akkor kell küldeni email-t, ha van hibás sor a feldolgozott állományban. MKB esetén minden esetben.
|
|
return;
|
|
}
|
|
|
|
var configuration = (T)ConfigurationManager.GetSection(typeof(T).Name);
|
|
|
|
var message = string.Empty;
|
|
int leadingZeros = bankszamlaIgenylesPcoList.Select(x => x.Sorszam).Max().ToString().Length;
|
|
foreach (var pco in bankszamlaIgenylesPcoList.OrderBy(x => x.Sorszam))
|
|
{
|
|
message += $"{pco.Sorszam.ToString($"D{leadingZeros}")}: {(pco.IsError ? pco.ErrorText : CommonResource.Ok)}<br />";
|
|
}
|
|
|
|
var emailModel = new EmailModel
|
|
{
|
|
Message = message,
|
|
Subject = $"{fileName} {EmailResource.BankszamlaIgenylesFileFeldolgozasReport}",
|
|
TargetEmail = configuration.EmailAddress
|
|
};
|
|
|
|
BackgroundJob.Enqueue<IEmailJob>(email => email.SendMailAsync(emailModel));
|
|
}
|
|
|
|
protected List<string> GetBankiIntezmenyConnectionStringListFromFile()
|
|
{
|
|
var systemConnectionStrings = new List<string>();
|
|
|
|
if (!string.IsNullOrWhiteSpace(KretaServer.KretaServer.Instance.Configuration.BankszamlaIgenylesConnectionStringFile))
|
|
{
|
|
string bankszamlaIgenylesConnectionStringFile = KretaServer.KretaServer.Instance.Configuration.BankszamlaIgenylesConnectionStringFile;
|
|
|
|
try
|
|
{
|
|
if (File.Exists(bankszamlaIgenylesConnectionStringFile))
|
|
{
|
|
JsonTextReader reader = new JsonTextReader(new StreamReader(bankszamlaIgenylesConnectionStringFile));
|
|
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
|
|
builder.AddJsonFile(bankszamlaIgenylesConnectionStringFile);
|
|
IConfigurationRoot configuration = builder.Build();
|
|
systemConnectionStrings = configuration.GetSection("config:connectionStrings").GetChildren().Select(x => x.Value).ToList();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SDAServer.Instance.Logger.ExceptionThrown(ex);
|
|
}
|
|
}
|
|
|
|
return systemConnectionStrings;
|
|
}
|
|
}
|
|
}
|