293 lines
11 KiB
C#
293 lines
11 KiB
C#
using BankYouBankruptBusinessLogic.OfficePackage.HelperModels;
|
||
using BankYouBankruptBusinessLogic.OfficePackage;
|
||
using BankYouBankruptContracts.BindingModels;
|
||
using BankYouBankruptContracts.BusinessLogicsContracts;
|
||
using BankYouBankruptContracts.SearchModels;
|
||
using BankYouBankruptContracts.StoragesContracts;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using BankYouBankruptContracts.ViewModels.Client.Reports;
|
||
using BankYouBankruptContracts.ViewModels;
|
||
using BankYouBankruptDataModels.Enums;
|
||
using BankYouBankruptContracts.ViewModels.Client.Default;
|
||
using DocumentFormat.OpenXml.Wordprocessing;
|
||
using System.Net.Mail;
|
||
using System.Reflection.PortableExecutable;
|
||
using Spire.Pdf.Graphics;
|
||
using DocumentFormat.OpenXml.Packaging;
|
||
using BankYouBankruptBusinessLogic.MailWorker;
|
||
|
||
namespace BankYouBankruptBusinessLogic.BusinessLogics
|
||
{
|
||
public class ReportClientLogic : IReportClientLogic
|
||
{
|
||
private readonly ICreditingStorage _creditingStorage;
|
||
private readonly IDebitingStorage _debitingStorage;
|
||
private readonly ICardStorage _cardStorage;
|
||
private readonly IMoneyTransferStorage _moneyTransferStorage;
|
||
private readonly IClientStorage _clientStorage;
|
||
|
||
private readonly AbstractSaveToExcel _saveToExcel;
|
||
private readonly AbstractSaveToWord _saveToWord;
|
||
private readonly AbstractSaveToPdf _saveToPdf;
|
||
|
||
private readonly MailKitWorker _mailKitWorker;
|
||
|
||
public ReportClientLogic(ICreditingStorage creditingStorage, IDebitingStorage debitingStorage,
|
||
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf,
|
||
ICardStorage cardStorage, IMoneyTransferStorage moneyTransferStorage,
|
||
MailKitWorker mailKitWorker, IClientStorage clientStorage)
|
||
{
|
||
_creditingStorage = creditingStorage;
|
||
_debitingStorage = debitingStorage;
|
||
_cardStorage = cardStorage;
|
||
_moneyTransferStorage = moneyTransferStorage;
|
||
_clientStorage = clientStorage;
|
||
|
||
_saveToExcel = saveToExcel;
|
||
_saveToWord = saveToWord;
|
||
_saveToPdf = saveToPdf;
|
||
|
||
_mailKitWorker = mailKitWorker;
|
||
}
|
||
|
||
public List<ReportClientViewModel>? GetCrediting(ReportBindingModel model)
|
||
{
|
||
return _creditingStorage.GetFilteredList(new CreditingSearchModel
|
||
{
|
||
DateFrom = model.DateFrom,
|
||
DateTo = model.DateTo,
|
||
}).Select(x => new ReportClientViewModel
|
||
{
|
||
OperationId = x.Id,
|
||
CardNumber = x.CardNumber,
|
||
SumOperation = x.Sum,
|
||
DateComplite = x.DateOpen
|
||
}).ToList();
|
||
}
|
||
|
||
public List<ReportClientViewModel>? GetDebiting(ReportBindingModel model)
|
||
{
|
||
return _debitingStorage.GetFilteredList(new DebitingSearchModel
|
||
{
|
||
DateTo = model.DateFrom,
|
||
DateFrom = model.DateTo,
|
||
}).Select(x => new ReportClientViewModel
|
||
{
|
||
OperationId = x.Id,
|
||
CardNumber = x.CardNumber,
|
||
SumOperation = x.Sum,
|
||
DateComplite = x.DateClose
|
||
}).ToList();
|
||
}
|
||
|
||
//для excel отчёта по переводам между счетов
|
||
public List<MoneyTransferViewModel>? GetMoneyTransfer(ReportBindingModel model)
|
||
{
|
||
//список счетов по выбранным картам
|
||
List<int> accountId = new();
|
||
|
||
foreach(var index in model.CardList)
|
||
{
|
||
accountId.Add(_cardStorage.GetElement(new CardSearchModel { Id = index}).AccountId);
|
||
}
|
||
|
||
var list = accountId.ToHashSet().ToList();
|
||
|
||
List<MoneyTransferViewModel> totalList = new();
|
||
|
||
foreach (var index in list)
|
||
{
|
||
var result = _moneyTransferStorage.GetFilteredList(new MoneyTransferSearchModel
|
||
{
|
||
AccountSenderId = index,
|
||
AccountPayeeId = index
|
||
}).OrderBy(x => x.AccountSenderId).ToList();
|
||
|
||
totalList.AddRange(result);
|
||
}
|
||
|
||
return totalList;
|
||
}
|
||
|
||
//для excel отчёта по пополнениям карты
|
||
public List<CreditingViewModel> GetExcelCrediting(ReportBindingModel model)
|
||
{
|
||
List<CreditingViewModel> totalList = new();
|
||
|
||
foreach (var index in model.CardList)
|
||
{
|
||
var result = _creditingStorage.GetFilteredList(new CreditingSearchModel
|
||
{
|
||
CardId = index
|
||
});
|
||
|
||
totalList.AddRange(result);
|
||
}
|
||
|
||
return totalList;
|
||
}
|
||
|
||
//для excel отчёта по снятиям с карты
|
||
public List<DebitingViewModel> GetExcelDebiting(ReportBindingModel model)
|
||
{
|
||
List<DebitingViewModel> totalList = new();
|
||
|
||
foreach (var index in model.CardList)
|
||
{
|
||
var result = _debitingStorage.GetFilteredList(new DebitingSearchModel
|
||
{
|
||
CardId = index
|
||
});
|
||
|
||
totalList.AddRange(result);
|
||
}
|
||
|
||
return totalList;
|
||
}
|
||
|
||
public void SaveToExcelFile(ReportBindingModel model, OfficeOperationEnum operationEnum)
|
||
{
|
||
byte[] excel = Array.Empty<byte>();
|
||
|
||
if (operationEnum == OfficeOperationEnum.Между_cчетами)
|
||
{
|
||
_saveToExcel.CreateReport(new ExcelInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по переводам",
|
||
MoneyTransfer = GetMoneyTransfer(model)
|
||
}, operationEnum);
|
||
|
||
excel = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт по переводам.xlsx");
|
||
}
|
||
|
||
if (operationEnum == OfficeOperationEnum.Пополнение_карт)
|
||
{
|
||
_saveToExcel.CreateReport(new ExcelInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по пополнениям (переводам из налички на карту)",
|
||
Crediting = GetExcelCrediting(model)
|
||
}, operationEnum);
|
||
|
||
excel = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт по пополнениям.xlsx");
|
||
}
|
||
|
||
if (operationEnum == OfficeOperationEnum.Cнятие_с_карты)
|
||
{
|
||
_saveToExcel.CreateReport(new ExcelInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по снятиям денежных средств",
|
||
Debiting = GetExcelDebiting(model)
|
||
}, operationEnum);
|
||
|
||
excel = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт по снятиям.xlsx");
|
||
}
|
||
|
||
_mailKitWorker.SendMailAsync(new()
|
||
{
|
||
MailAddress = model.Email,
|
||
Subject = "Отчёт по картам",
|
||
Text = $"Отчёт по состоянию на {DateTime.Now.ToShortTimeString}",
|
||
File = excel,
|
||
Role = model.Role,
|
||
TypeDoc = TypeDocEnum.EXCEL
|
||
});
|
||
}
|
||
|
||
public void SaveToWordFile(ReportBindingModel model, OfficeOperationEnum operationEnum)
|
||
{
|
||
byte[] word = Array.Empty<byte>();
|
||
|
||
if (operationEnum == OfficeOperationEnum.Между_cчетами)
|
||
{
|
||
_saveToWord.CreateDoc(new WordInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по переводам",
|
||
MoneyTransfer = GetMoneyTransfer(model)
|
||
}, operationEnum);
|
||
|
||
word = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт по переводам.docx");
|
||
}
|
||
|
||
if (operationEnum == OfficeOperationEnum.Пополнение_карт)
|
||
{
|
||
_saveToWord.CreateDoc(new WordInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по пополнениям (переводам из налички на карту)",
|
||
Crediting = GetExcelCrediting(model)
|
||
}, operationEnum);
|
||
|
||
word = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт по пополнениям.docx");
|
||
}
|
||
|
||
if (operationEnum == OfficeOperationEnum.Cнятие_с_карты)
|
||
{
|
||
_saveToWord.CreateDoc(new WordInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по снятиям денежных средств",
|
||
Debiting = GetExcelDebiting(model)
|
||
}, operationEnum);
|
||
|
||
word = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт по снятиям.docx");
|
||
}
|
||
|
||
_mailKitWorker.SendMailAsync(new()
|
||
{
|
||
MailAddress = model.Email,
|
||
Subject = "Отчёт по картам",
|
||
Text = $"Отчёт по состоянию на {DateTime.Now.ToShortTimeString}",
|
||
File = word,
|
||
Role = model.Role,
|
||
TypeDoc = TypeDocEnum.WORD
|
||
});
|
||
}
|
||
|
||
//отчёт в формате PDF для клиента
|
||
public ReportClientViewModelForHTML SaveClientReportToPdfFile(ReportBindingModel model)
|
||
{
|
||
var listCreditings = GetCrediting(model);
|
||
var listDebitings = GetDebiting(model);
|
||
|
||
_saveToPdf.CreateDoc(new PdfInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Отчёт по операциям с картами",
|
||
DateFrom = model.DateFrom!.Value,
|
||
DateTo = model.DateTo!.Value,
|
||
ReportCrediting = listCreditings,
|
||
ReportDebiting = listDebitings
|
||
});
|
||
|
||
byte[] pdf = System.IO.File.ReadAllBytes("C:\\Users\\Programmist73\\Desktop\\Практика\\2-й курс\\4-й семестр\\CourseWork_BankYouBankrupt\\BankYouBankrupt\\BankYouBankruptRestAPI\\Отчёт_по_картам.pdf");
|
||
|
||
_mailKitWorker.SendMailAsync(new()
|
||
{
|
||
MailAddress = model.Email,
|
||
Subject = "Отчёт по картам",
|
||
Text = $"За период с {model.DateFrom} " +
|
||
$"по {model.DateTo}.",
|
||
File = pdf,
|
||
Role = model.Role,
|
||
TypeDoc = TypeDocEnum.PDF
|
||
});
|
||
|
||
//возврат полученных списков для отображения на вебе
|
||
return new ReportClientViewModelForHTML
|
||
{
|
||
ReportCrediting = listCreditings,
|
||
|
||
ReportDebiting = listDebitings
|
||
};
|
||
}
|
||
}
|
||
}
|