CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/ReportClientLogic.cs

307 lines
10 KiB
C#
Raw Permalink Normal View History

2023-04-04 19:21:57 +04:00
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;
2023-05-18 17:56:47 +04:00
using BankYouBankruptContracts.ViewModels.Client.Reports;
2023-05-18 23:52:40 +04:00
using BankYouBankruptContracts.ViewModels;
2023-05-19 20:11:24 +04:00
using BankYouBankruptDataModels.Enums;
using BankYouBankruptContracts.ViewModels.Client.Default;
2023-05-20 01:43:20 +04:00
using DocumentFormat.OpenXml.Wordprocessing;
using System.Net.Mail;
using System.Reflection.PortableExecutable;
using Spire.Pdf.Graphics;
using DocumentFormat.OpenXml.Packaging;
using BankYouBankruptBusinessLogic.MailWorker;
2023-04-04 19:21:57 +04:00
namespace BankYouBankruptBusinessLogic.BusinessLogics
{
public class ReportClientLogic : IReportClientLogic
{
private readonly ICreditingStorage _creditingStorage;
private readonly IDebitingStorage _debitingStorage;
2023-05-19 13:04:22 +04:00
private readonly ICardStorage _cardStorage;
private readonly IMoneyTransferStorage _moneyTransferStorage;
2023-05-20 01:43:20 +04:00
private readonly IClientStorage _clientStorage;
2023-04-04 19:21:57 +04:00
2023-05-19 13:04:22 +04:00
private readonly AbstractSaveToExcel _saveToExcel;
2023-05-19 23:32:09 +04:00
private readonly AbstractSaveToWord _saveToWord;
2023-05-19 13:04:22 +04:00
private readonly AbstractSaveToPdf _saveToPdf;
2023-04-04 19:21:57 +04:00
2023-05-20 01:43:20 +04:00
private readonly MailKitWorker _mailKitWorker;
2023-04-04 19:27:13 +04:00
public ReportClientLogic(ICreditingStorage creditingStorage, IDebitingStorage debitingStorage,
2023-05-19 23:32:09 +04:00
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf,
2023-05-20 01:43:20 +04:00
ICardStorage cardStorage, IMoneyTransferStorage moneyTransferStorage,
MailKitWorker mailKitWorker, IClientStorage clientStorage)
2023-04-04 19:21:57 +04:00
{
_creditingStorage = creditingStorage;
_debitingStorage = debitingStorage;
2023-05-19 13:04:22 +04:00
_cardStorage = cardStorage;
_moneyTransferStorage = moneyTransferStorage;
2023-05-20 01:43:20 +04:00
_clientStorage = clientStorage;
2023-04-04 19:21:57 +04:00
_saveToExcel = saveToExcel;
_saveToWord = saveToWord;
_saveToPdf = saveToPdf;
2023-05-20 01:43:20 +04:00
_mailKitWorker = mailKitWorker;
2023-04-04 19:21:57 +04:00
}
public List<ReportClientViewModel>? GetCrediting(ReportBindingModel model)
{
return _creditingStorage.GetFilteredList(new CreditingSearchModel
{
2023-05-16 16:45:35 +04:00
DateFrom = model.DateFrom,
DateTo = model.DateTo,
2023-04-04 19:21:57 +04:00
}).Select(x => new ReportClientViewModel
{
2023-05-18 11:31:42 +04:00
OperationId = x.Id,
CardNumber = x.CardNumber,
SumOperation = x.Sum,
2023-05-16 16:45:35 +04:00
DateComplite = x.DateOpen
2023-04-04 19:21:57 +04:00
}).ToList();
}
public List<ReportClientViewModel>? GetDebiting(ReportBindingModel model)
{
2023-04-04 19:27:13 +04:00
return _debitingStorage.GetFilteredList(new DebitingSearchModel
{
2023-05-16 16:45:35 +04:00
DateTo = model.DateFrom,
DateFrom = model.DateTo,
2023-04-04 19:27:13 +04:00
}).Select(x => new ReportClientViewModel
{
2023-05-18 11:31:42 +04:00
OperationId = x.Id,
CardNumber = x.CardNumber,
2023-04-04 19:27:13 +04:00
SumOperation = x.Sum,
DateComplite = x.DateClose
2023-04-04 19:27:13 +04:00
}).ToList();
2023-04-04 19:21:57 +04:00
}
2023-05-19 20:11:24 +04:00
//для excel отчёта по переводам между счетов
2023-05-19 13:04:22 +04:00
public List<MoneyTransferViewModel>? GetMoneyTransfer(ReportBindingModel model)
2023-04-04 19:21:57 +04:00
{
//список счетов по выбранным картам
List<int> accountId = new();
foreach(var index in model.CardList)
2023-05-19 13:04:22 +04:00
{
accountId.Add(_cardStorage.GetElement(new CardSearchModel { Id = index}).AccountId);
}
var list = accountId.ToHashSet().ToList();
List<MoneyTransferViewModel> totalList = new();
2023-04-04 19:21:57 +04:00
foreach (var index in list)
2023-05-19 13:04:22 +04:00
{
var result = _moneyTransferStorage.GetFilteredList(new MoneyTransferSearchModel
{
AccountSenderId = index,
AccountPayeeId = index
}).OrderBy(x => x.AccountSenderId).ToList();
2023-05-19 20:11:24 +04:00
totalList.AddRange(result);
}
return totalList;
2023-04-04 19:21:57 +04:00
}
2023-05-19 20:11:24 +04:00
//для excel отчёта по пополнениям карты
public List<CreditingViewModel> GetExcelCrediting(ReportBindingModel model)
2023-04-04 19:21:57 +04:00
{
2023-05-19 20:11:24 +04:00
List<CreditingViewModel> totalList = new();
foreach (var index in model.CardList)
2023-05-19 13:04:22 +04:00
{
2023-05-19 20:11:24 +04:00
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;
}
2023-05-20 01:43:20 +04:00
public void SaveToExcelFile(ReportBindingModel model, OfficeOperationEnum operationEnum)
2023-05-19 20:11:24 +04:00
{
2023-05-20 04:48:52 +04:00
byte[] excel = Array.Empty<byte>();
if (operationEnum == OfficeOperationEnum.Между_cчетами)
2023-05-19 20:11:24 +04:00
{
_saveToExcel.CreateReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Отчёт по переводам",
MoneyTransfer = GetMoneyTransfer(model)
}, operationEnum);
2023-05-20 04:48:52 +04:00
2023-05-20 05:10:33 +04:00
excel = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт по переводам.xlsx");
File.Delete("../BankYouBankruptRestAPI/Отчёт по переводам.xlsx");
2023-05-19 20:11:24 +04:00
}
2023-05-19 23:32:09 +04:00
if (operationEnum == OfficeOperationEnum.Пополнениеарт)
2023-05-19 20:11:24 +04:00
{
_saveToExcel.CreateReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Отчёт по пополнениям (переводам из налички на карту)",
Crediting = GetExcelCrediting(model)
}, operationEnum);
2023-05-20 04:48:52 +04:00
2023-05-20 05:10:33 +04:00
excel = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт по пополнениям.xlsx");
File.Delete("../BankYouBankruptRestAPI/Отчёт по пополнениям.xlsx");
2023-05-19 20:11:24 +04:00
}
2023-05-19 23:32:09 +04:00
if (operationEnum == OfficeOperationEnum.Cнятие_сарты)
2023-05-19 20:11:24 +04:00
{
_saveToExcel.CreateReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Отчёт по снятиям денежных средств",
2023-05-19 20:32:27 +04:00
Debiting = GetExcelDebiting(model)
2023-05-19 20:11:24 +04:00
}, operationEnum);
2023-05-20 04:48:52 +04:00
2023-05-20 05:10:33 +04:00
excel = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт по снятиям.xlsx");
File.Delete("../BankYouBankruptRestAPI/Отчёт по снятиям.xlsx");
2023-05-19 20:11:24 +04:00
}
2023-05-20 04:48:52 +04:00
_mailKitWorker.SendMailAsync(new()
{
MailAddress = model.Email,
Subject = "Отчёт по картам",
2023-05-20 05:10:33 +04:00
Text = $"Отчёт по состоянию на {DateTime.Now.ToString()}",
2023-05-20 04:48:52 +04:00
File = excel,
Role = model.Role,
TypeDoc = TypeDocEnum.EXCEL
});
2023-05-19 13:04:22 +04:00
}
2023-04-04 19:21:57 +04:00
2023-05-19 23:32:09 +04:00
public void SaveToWordFile(ReportBindingModel model, OfficeOperationEnum operationEnum)
2023-04-04 19:21:57 +04:00
{
2023-05-20 04:48:52 +04:00
byte[] word = Array.Empty<byte>();
2023-05-19 23:32:09 +04:00
if (operationEnum == OfficeOperationEnum.Между_cчетами)
{
_saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Отчёт по переводам",
MoneyTransfer = GetMoneyTransfer(model)
}, operationEnum);
2023-05-20 04:48:52 +04:00
2023-05-20 05:10:33 +04:00
word = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт по переводам.docx");
File.Delete("../BankYouBankruptRestAPI/Отчёт по переводам.docx");
2023-05-19 23:32:09 +04:00
}
if (operationEnum == OfficeOperationEnum.Пополнениеарт)
{
_saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Отчёт по пополнениям (переводам из налички на карту)",
Crediting = GetExcelCrediting(model)
}, operationEnum);
2023-05-20 04:48:52 +04:00
2023-05-20 05:10:33 +04:00
word = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт по пополнениям.docx");
File.Delete("../BankYouBankruptRestAPI/Отчёт по пополнениям.docx");
2023-05-19 23:32:09 +04:00
}
if (operationEnum == OfficeOperationEnum.Cнятие_сарты)
{
_saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Отчёт по снятиям денежных средств",
Debiting = GetExcelDebiting(model)
}, operationEnum);
2023-05-20 04:48:52 +04:00
2023-05-20 05:10:33 +04:00
word = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт по снятиям.docx");
File.Delete("../BankYouBankruptRestAPI/Отчёт по снятиям.docx");
2023-05-19 23:32:09 +04:00
}
2023-05-20 04:48:52 +04:00
_mailKitWorker.SendMailAsync(new()
{
MailAddress = model.Email,
Subject = "Отчёт по картам",
2023-05-20 05:10:33 +04:00
Text = $"Отчёт по состоянию на {DateTime.Now.ToString()}",
2023-05-20 04:48:52 +04:00
File = word,
Role = model.Role,
TypeDoc = TypeDocEnum.WORD
});
2023-05-19 23:32:09 +04:00
}
//отчёт в формате PDF для клиента
2023-05-18 23:52:40 +04:00
public ReportClientViewModelForHTML SaveClientReportToPdfFile(ReportBindingModel model)
{
2023-05-18 23:52:40 +04:00
var listCreditings = GetCrediting(model);
var listDebitings = GetDebiting(model);
2023-05-18 00:47:56 +04:00
_saveToPdf.CreateDoc(new PdfInfo
{
FileName = model.FileName,
2023-05-18 11:31:42 +04:00
Title = "Отчёт по операциям с картами",
DateFrom = model.DateFrom!.Value,
DateTo = model.DateTo!.Value,
2023-05-18 23:52:40 +04:00
ReportCrediting = listCreditings,
ReportDebiting = listDebitings
2023-05-20 01:43:20 +04:00
});
2023-05-20 05:10:33 +04:00
byte[] pdf = System.IO.File.ReadAllBytes("../BankYouBankruptRestAPI/Отчёт_поартам.pdf");
2023-05-20 01:43:20 +04:00
_mailKitWorker.SendMailAsync(new()
{
2023-05-20 02:03:01 +04:00
MailAddress = model.Email,
2023-05-20 01:43:20 +04:00
Subject = "Отчёт по картам",
Text = $"За период с {model.DateFrom} " +
$"по {model.DateTo}.",
File = pdf,
2023-05-20 04:48:52 +04:00
Role = model.Role,
TypeDoc = TypeDocEnum.PDF
});
2023-05-18 23:52:40 +04:00
2023-05-20 05:10:33 +04:00
File.Delete("../BankYouBankruptRestAPI/Отчёт_поартам.pdf");
//возврат полученных списков для отображения на вебе
return new ReportClientViewModelForHTML
2023-05-18 23:52:40 +04:00
{
ReportCrediting = listCreditings,
ReportDebiting = listDebitings
};
}
}
2023-04-04 19:21:57 +04:00
}