From b89974dd9fcdca4c57aabcfc83d75d73d9db8835 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Fri, 7 Apr 2023 21:35:31 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D1=87=D1=91=D1=82=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=80=D0=BE=D0=BB=D0=B8=20=D0=97=D0=B0=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D1=87=D0=B8=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ReportLogic.cs | 130 ++++++++++++++++++ .../BindingModels/ReportBindingModel.cs | 14 ++ .../BusinessLogicsContracts/IReportLogic.cs | 19 +++ .../SearchModels/TransferSearchModel.cs | 2 + .../ReportPaymentCurrencyPurchaseViewModel.cs | 15 ++ ...ReportTransferCurrencyPurchaseViewModel.cs | 15 ++ .../Implements/TransferStorage.cs | 10 +- 7 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 Bank/BankBusinessLogic/BusinessLogics/ReportLogic.cs create mode 100644 Bank/BankContracts/BindingModels/ReportBindingModel.cs create mode 100644 Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs create mode 100644 Bank/BankContracts/ViewModels/ReportPaymentCurrencyPurchaseViewModel.cs create mode 100644 Bank/BankContracts/ViewModels/ReportTransferCurrencyPurchaseViewModel.cs diff --git a/Bank/BankBusinessLogic/BusinessLogics/ReportLogic.cs b/Bank/BankBusinessLogic/BusinessLogics/ReportLogic.cs new file mode 100644 index 0000000..becbd05 --- /dev/null +++ b/Bank/BankBusinessLogic/BusinessLogics/ReportLogic.cs @@ -0,0 +1,130 @@ +using BankContracts.BindingModels; +using BankContracts.BusinessLogicsContracts; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankBusinessLogic.BusinessLogics +{ + public class ReportLogic : IReportLogic + { + private readonly ITransferStorage _transferStorage; + private readonly IPaymentStorage _paymentStorage; + private readonly ICurrencyPurchaseStorage _currencyPurchaseStorage; + private readonly ICurrencyStorage _currencyStorage; + + public ReportLogic(ITransferStorage transferStorage, IPaymentStorage paymentStorage, ICurrencyPurchaseStorage currencyPurchaseStorage) + { + _transferStorage = transferStorage; + _paymentStorage = paymentStorage; + _currencyPurchaseStorage = currencyPurchaseStorage; + } + + public List GetPaymentPurchase(List payments) + { + var list = new List(); + var currencies = _currencyStorage.GetFullList(); + var currencyPurchases = _currencyPurchaseStorage.GetFullList(); + + foreach(var payment in payments) + { + var record = new ReportPaymentCurrencyPurchaseViewModel + { + PaymentId = payment.Id, + PaymentDate = payment.PaymentDate, + Purchases = new List<(int, float)>() + }; + var foundCurrenciesId = new List(); + //находим валюты, привязанные к платежу + foreach (var currency in currencies) + { + if (payment.CurrencyPayments.ContainsKey(currency.Id)) foundCurrenciesId.Add(currency.Id); + } + //выбираем из всех закупок те, которые относятся к найденным валютам + foreach (var currencyPurchase in currencyPurchases) + { + foreach(var currencyId in foundCurrenciesId) + { + if (currencyPurchase.CurrencyId == currencyId) + { + record.Purchases.Add(new(currencyPurchase.Id, currencyPurchase.Amount)); + break; + } + } + } + list.Add(record); + } + + return list; + } + + public List GetTransferPurchase(ReportBindingModel model) + { + var transfers = _transferStorage.GetFilteredList(new TransferSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo }); + var currencies = _currencyStorage.GetFullList(); + var currencyPurchases = _currencyPurchaseStorage.GetFullList(); + var list = new List(); + + foreach (var transfer in transfers) + { + var record = new ReportTransferCurrencyPurchaseViewModel + { + TransferId = transfer.Id, + TransferDate = transfer.TransferDateTime, + Purchases = new List<(int, float)>() + }; + var payment = _paymentStorage.GetElement(new PaymentSearchModel { Id = transfer.Id}); + if (payment == null) + { + throw new InvalidOperationException("Платеж не был найден!"); + } + var foundCurrenciesId = new List(); + //находим валюты, привязанные к платежу + foreach (var currency in currencies) + { + if (payment.CurrencyPayments.ContainsKey(currency.Id)) foundCurrenciesId.Add(currency.Id); + } + //выбираем из всех закупок те, которые относятся к найденным валютам + foreach (var currencyPurchase in currencyPurchases) + { + foreach (var currencyId in foundCurrenciesId) + { + if (currencyPurchase.CurrencyId == currencyId) + { + record.Purchases.Add(new(currencyPurchase.Id, currencyPurchase.Amount)); + break; + } + } + } + } + + return list; + } + + public void SavePaymentPurchaseToExcel(List payments) + { + var result = GetPaymentPurchase(payments); + //создание файла будет реализовано в будующих версиях + throw new NotImplementedException(); + } + + public void SavePaymentPurchaseToWord(List payments) + { + var result = GetPaymentPurchase(payments); + //создание файла будет реализовано в будующих версиях + throw new NotImplementedException(); + } + + public void SaveTransferPurchaseToPDF(ReportBindingModel model) + { + var result = GetTransferPurchase(model); + //создание файла будет реализовано в будующих версиях + throw new NotImplementedException(); + } + } +} diff --git a/Bank/BankContracts/BindingModels/ReportBindingModel.cs b/Bank/BankContracts/BindingModels/ReportBindingModel.cs new file mode 100644 index 0000000..8178bf5 --- /dev/null +++ b/Bank/BankContracts/BindingModels/ReportBindingModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankContracts.BindingModels +{ + public class ReportBindingModel + { + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + } +} diff --git a/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs b/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs new file mode 100644 index 0000000..de36b50 --- /dev/null +++ b/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs @@ -0,0 +1,19 @@ +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankContracts.BusinessLogicsContracts +{ + public interface IReportLogic + { + List GetPaymentPurchase(List payments); + List GetTransferPurchase(ReportBindingModel model); + void SavePaymentPurchaseToWord(List payments); + void SavePaymentPurchaseToExcel(List payments); + void SaveTransferPurchaseToPDF(ReportBindingModel model); + } +} diff --git a/Bank/BankContracts/SearchModels/TransferSearchModel.cs b/Bank/BankContracts/SearchModels/TransferSearchModel.cs index da6a87e..8b7916e 100644 --- a/Bank/BankContracts/SearchModels/TransferSearchModel.cs +++ b/Bank/BankContracts/SearchModels/TransferSearchModel.cs @@ -10,5 +10,7 @@ namespace BankContracts.SearchModels { public int? Id { get; set; } public int? OperatorId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } } } diff --git a/Bank/BankContracts/ViewModels/ReportPaymentCurrencyPurchaseViewModel.cs b/Bank/BankContracts/ViewModels/ReportPaymentCurrencyPurchaseViewModel.cs new file mode 100644 index 0000000..7f069e2 --- /dev/null +++ b/Bank/BankContracts/ViewModels/ReportPaymentCurrencyPurchaseViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankContracts.ViewModels +{ + public class ReportPaymentCurrencyPurchaseViewModel + { + public int PaymentId { get; set; } + public DateTime PaymentDate { get; set; } + public List<(int PurchaseId, float Amount)> Purchases { get; set; } = new(); + } +} diff --git a/Bank/BankContracts/ViewModels/ReportTransferCurrencyPurchaseViewModel.cs b/Bank/BankContracts/ViewModels/ReportTransferCurrencyPurchaseViewModel.cs new file mode 100644 index 0000000..e65bab4 --- /dev/null +++ b/Bank/BankContracts/ViewModels/ReportTransferCurrencyPurchaseViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankContracts.ViewModels +{ + public class ReportTransferCurrencyPurchaseViewModel + { + public int TransferId { get; set; } + public DateTime TransferDate { get; set; } + public List<(int PurchaseId, float Amount)> Purchases { get; set; } = new(); + } +} diff --git a/Bank/BankDatabaseImplement/Implements/TransferStorage.cs b/Bank/BankDatabaseImplement/Implements/TransferStorage.cs index 987010c..67fbdcf 100644 --- a/Bank/BankDatabaseImplement/Implements/TransferStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/TransferStorage.cs @@ -23,10 +23,18 @@ namespace BankDatabaseImplement.Implements } public List GetFilteredList(TransferSearchModel model) { - if (!model.Id.HasValue && !model.OperatorId.HasValue) + if (!model.Id.HasValue && !model.OperatorId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue) { return new(); } + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + using var context = new BankDatabase(); + return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId) + .Where(x => x.TransferDateTime >= model.DateFrom && x.TransferDateTime <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } if (model.OperatorId.HasValue) { using var context = new BankDatabase();