From 23e1580c931626b6dca2f87bcd13b2203cce6066 Mon Sep 17 00:00:00 2001 From: shadowik Date: Thu, 18 May 2023 21:00:46 +0400 Subject: [PATCH] CashierDiagram --- .../BusinessLogics/AccountLogic.cs | 43 ++++++++- .../BusinessLogics/CardLogic.cs | 8 +- .../Controllers/HomeController.cs | 37 +++++++- .../Views/Home/Diagram.cshtml | 89 +++++++++++++++++++ .../Views/Shared/_Layout.cshtml | 3 + .../BusinessLogicsContracts/IAccountLogic.cs | 4 +- .../SearchModels/MoneyTransferSearchModel.cs | 4 +- .../CashierDiagramElementsViewModel.cs | 15 ++++ .../ViewModels/CashierDiagramViewModel.cs | 16 ++++ .../Implements/CashWithdrawalStorage.cs | 47 +++------- .../Implements/MoneyTransferStorage.cs | 42 ++++----- .../Controllers/AccountController.cs | 17 +++- 12 files changed, 253 insertions(+), 72 deletions(-) create mode 100644 BankYouBankrupt/BankYouBankruptCashierApp/Views/Home/Diagram.cshtml create mode 100644 BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramElementsViewModel.cs create mode 100644 BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramViewModel.cs diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs index 762acf3..c6eb154 100644 --- a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/AccountLogic.cs @@ -3,6 +3,8 @@ using BankYouBankruptContracts.BusinessLogicsContracts; using BankYouBankruptContracts.SearchModels; using BankYouBankruptContracts.StoragesContracts; using BankYouBankruptContracts.ViewModels; +using BankYouBankruptContracts.ViewModels.Client.Diagram; +using BankYouBankruptDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -17,11 +19,16 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IAccountStorage _accountStorage; + private readonly ICashWithdrawalLogic _cashWithdrawalLogic; + private readonly IMoneyTransferLogic _moneyTransferLogic; - public AccountLogic(ILogger logger, IAccountStorage accountStorage) + public AccountLogic(ILogger logger, IAccountStorage accountStorage, + ICashWithdrawalLogic cashWithdrawalLogic, IMoneyTransferLogic moneyTransferLogic) { _logger = logger; _accountStorage = accountStorage; + _cashWithdrawalLogic = cashWithdrawalLogic; + _moneyTransferLogic = moneyTransferLogic; } public AccountViewModel? ReadElement(AccountSearchModel model) @@ -136,8 +143,38 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics return true; } - //проверка входного аргумента для методов Insert, Update и Delete - private void CheckModel(AccountBindingModel model, bool withParams = true) + public List GetMonthInfo(int AccountId) + { + + Dictionary<(int, int), int> cashWithdrawals = _cashWithdrawalLogic.ReadList(new CashWithdrawalSearchModel() + { + AccountId = AccountId, + }).GroupBy(x => new { x.DateOperation.Month, x.DateOperation.Year }) + .Select(x => new { x.Key.Month, x.Key.Year, Sum = x.Select(y => y.Sum).Sum() }).ToDictionary(x => (x.Month, x.Year), x => (x.Sum)); + Dictionary<(int, int), int> moneyTransfersDebiting = _moneyTransferLogic.ReadList(new MoneyTransferSearchModel() + { + AccountPayeeId = AccountId, + }).GroupBy(x => new { x.DateOperation.Month, x.DateOperation.Year }) + .Select(x => new { x.Key.Month, x.Key.Year, Sum = x.Select(y => y.Sum).Sum() }).ToDictionary(x => (x.Month, x.Year), x => (x.Sum)); + Dictionary<(int, int), int> moneyTransfers = _moneyTransferLogic.ReadList(new MoneyTransferSearchModel() + { + AccountSenderId = AccountId, + }).GroupBy(x => new { x.DateOperation.Month, x.DateOperation.Year }) + .Select(x => new { x.Key.Month, x.Key.Year, Sum = x.Select(y => y.Sum).Sum() }).ToDictionary(x => (x.Month, x.Year), x => (x.Sum)); + List result = new(); + foreach (var key in cashWithdrawals.Keys.Union(moneyTransfersDebiting.Keys).Union(moneyTransfers.Keys)) + { + int sum = 0; + if (cashWithdrawals.ContainsKey(key)) sum -= cashWithdrawals.GetValueOrDefault(key); + if (moneyTransfersDebiting.ContainsKey(key)) sum += moneyTransfersDebiting.GetValueOrDefault(key); + if (moneyTransfersDebiting.ContainsKey(key)) sum -= moneyTransfersDebiting.GetValueOrDefault(key); + result.Add(new CashierDiagramElementsViewModel() { Name = Enum.GetName(typeof(Months), key.Item1) + " " + key.Item2.ToString(), Value = sum }); + } + return result; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(AccountBindingModel model, bool withParams = true) { if (model == null) { diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/CardLogic.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/CardLogic.cs index b407830..cf26784 100644 --- a/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/CardLogic.cs +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/CardLogic.cs @@ -19,15 +19,15 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly ICardStorage _cardStorage; - private readonly IAccountStorage _accountStorage; + private readonly IAccountLogic _accountLogic; private readonly IDebitingLogic _debitingLogic; private readonly ICreditingLogic _creditingLogic; - public CardLogic(ILogger logger, ICardStorage cardStorage, IAccountStorage accountStorage, + public CardLogic(ILogger logger, ICardStorage cardStorage, IAccountLogic accountLogic, IDebitingLogic debitingLogic, ICreditingLogic creditingLogic) { _logger = logger; _cardStorage = cardStorage; - _accountStorage = accountStorage; + _accountLogic = accountLogic; _debitingLogic = debitingLogic; _creditingLogic = creditingLogic; } @@ -153,7 +153,7 @@ namespace BankYouBankruptBusinessLogic.BusinessLogics throw new InvalidOperationException("Карта с таким ноиером уже есть"); } - var accountElement = _accountStorage.GetElement(new AccountSearchModel + var accountElement = _accountLogic.ReadElement(new AccountSearchModel { Id = model.AccountId, ClientId = model.ClientID diff --git a/BankYouBankrupt/BankYouBankruptCashierApp/Controllers/HomeController.cs b/BankYouBankrupt/BankYouBankruptCashierApp/Controllers/HomeController.cs index 7106767..ae67221 100644 --- a/BankYouBankrupt/BankYouBankruptCashierApp/Controllers/HomeController.cs +++ b/BankYouBankrupt/BankYouBankruptCashierApp/Controllers/HomeController.cs @@ -2,6 +2,7 @@ using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.ViewModels; using BankYouBankruptContracts.ViewModels.Client.Default; +using BankYouBankruptContracts.ViewModels.Client.Diagram; using BankYouBankruptDataModels.Enums; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; @@ -496,6 +497,38 @@ namespace BankYouBankruptCashierApp.Controllers Response.Redirect("Index"); } - #endregion - } + #endregion + + [HttpGet] + public IActionResult Diagram() + { + if (APICashier.Cashier == null) + { + return Redirect("~/Home/Enter"); + } + + ViewBag.Accounts = APICashier.GetRequest>($"api/Account/GetAllAccounts"); + + + return View(); + } + + [HttpPost] + public IActionResult Diagram(int accountId) + { + if (APICashier.Cashier == null) + { + return Redirect("~/Home/Enter"); + } + + ViewBag.Accounts = APICashier.GetRequest>($"api/Account/GetAllAccounts"); + + + return View(new CashierDiagramViewModel() + { + DiagramName = "Hello World", + Elements = APICashier.GetRequest>($"api/Account/getAccountMonthResult?cardId={accountId}") + }); + } + } } \ No newline at end of file diff --git a/BankYouBankrupt/BankYouBankruptCashierApp/Views/Home/Diagram.cshtml b/BankYouBankrupt/BankYouBankruptCashierApp/Views/Home/Diagram.cshtml new file mode 100644 index 0000000..da14fad --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptCashierApp/Views/Home/Diagram.cshtml @@ -0,0 +1,89 @@ +@using BankYouBankruptContracts.ViewModels + +@model CashierDiagramViewModel + +@{ + ViewData["Title"] = "Диаграмма"; +} + +
+

Диаграмма по месяцам

+
+ +
+
+
Номер счета:
+ +
+
+
+
+ +
+
+
+ +@if (Model == null) return; + +
+
+ +
+ @foreach (var info in Model.Elements) { + + } +
+
+
+ + + diff --git a/BankYouBankrupt/BankYouBankruptCashierApp/Views/Shared/_Layout.cshtml b/BankYouBankrupt/BankYouBankruptCashierApp/Views/Shared/_Layout.cshtml index bf4b73a..03ffbae 100644 --- a/BankYouBankrupt/BankYouBankruptCashierApp/Views/Shared/_Layout.cshtml +++ b/BankYouBankrupt/BankYouBankruptCashierApp/Views/Shared/_Layout.cshtml @@ -49,6 +49,9 @@ + diff --git a/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IAccountLogic.cs b/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IAccountLogic.cs index 9eaff1e..5e577f7 100644 --- a/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IAccountLogic.cs +++ b/BankYouBankrupt/BankYouBankruptContracts/BusinessLogicsContracts/IAccountLogic.cs @@ -22,5 +22,7 @@ namespace BankYouBankruptContracts.BusinessLogicsContracts bool Update(AccountBindingModel model); bool Delete(AccountBindingModel model); - } + public List GetMonthInfo(int AccountId); + + } } diff --git a/BankYouBankrupt/BankYouBankruptContracts/SearchModels/MoneyTransferSearchModel.cs b/BankYouBankrupt/BankYouBankruptContracts/SearchModels/MoneyTransferSearchModel.cs index c51e1c3..e878dec 100644 --- a/BankYouBankrupt/BankYouBankruptContracts/SearchModels/MoneyTransferSearchModel.cs +++ b/BankYouBankrupt/BankYouBankruptContracts/SearchModels/MoneyTransferSearchModel.cs @@ -14,7 +14,9 @@ namespace BankYouBankruptContracts.SearchModels public int? ClientId { get; set; } - public int? AccountSenderId { get; set; } + public int? CashierId { get; set; } + + public int? AccountSenderId { get; set; } public int? AccountPayeeId { get; set; } diff --git a/BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramElementsViewModel.cs b/BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramElementsViewModel.cs new file mode 100644 index 0000000..cd1b3ba --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramElementsViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptContracts.ViewModels +{ + public class CashierDiagramElementsViewModel + { + public string Name { get; set; } = "Column"; + public int Value { get; set; } = 0; + + } +} diff --git a/BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramViewModel.cs b/BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramViewModel.cs new file mode 100644 index 0000000..f453fe4 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptContracts/ViewModels/CashierDiagramViewModel.cs @@ -0,0 +1,16 @@ +using BankYouBankruptContracts.ViewModels.Client.Diagram; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptContracts.ViewModels +{ + public class CashierDiagramViewModel + { + public string DiagramName { get; set; } = "Diagram Name"; + + public List Elements { get; set; } = new(); + } +} diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs index f8553d4..9eed5e2 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs @@ -28,52 +28,27 @@ namespace BankYouBankruptDatabaseImplement.Implements public List GetFilteredList(CashWithdrawalSearchModel model) { - if (model.AccountId < 0) - { - return new(); - } - using var context = new BankYouBancruptDatabase(); - - if (model.CashierId.HasValue) - { - return context.CashWithdrawals - .Include(x => x.Cashier) + var result = context.CashWithdrawals.Include(x => x.Cashier) .Include(x => x.Debiting) .Include(x => x.Account) - .Where(x => x.CashierId == model.CashierId) - .Select(x => x.GetViewModel) .ToList(); - } - //выборка для заполнения отчёта - if(model.DateFrom.HasValue && model.DateTo.HasValue) - { - return context.CashWithdrawals - .Include(x => x.Cashier) - .Include(x => x.Debiting) - .Include(x => x.Account) - .Where(x => x.DateOperation >= model.DateFrom && x.DateOperation <= model.DateTo && x.Account.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); - } + if (model.AccountId.HasValue) result = result.Where(x => x.AccountId == model.AccountId).ToList(); - return context.CashWithdrawals - .Include(x => x.Cashier) - .Include(x => x.Debiting) - .Include(x => x.Account) - .Where(x => x.AccountId == model.AccountId) - .Select(x => x.GetViewModel) - .ToList(); + if (model.CashierId.HasValue) result = result.Where(x => x.CashierId == model.CashierId).ToList(); + + if (model.DateFrom.HasValue) result = result.Where(x => x.DateOperation >= model.DateFrom).ToList(); + + if (model.DateTo.HasValue) result = result.Where(x => x.DateOperation <= model.DateTo).ToList(); + + if (model.ClientId.HasValue) result = result.Where(x => x.Account.ClientId == model.ClientId).ToList(); + + return result.Select(x => x.GetViewModel).ToList(); } public CashWithdrawalViewModel? GetElement(CashWithdrawalSearchModel model) { - if (model.AccountId < 0 && !model.Id.HasValue) - { - return null; - } - using var context = new BankYouBancruptDatabase(); return context.CashWithdrawals diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/MoneyTransferStorage.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/MoneyTransferStorage.cs index 9bc49fe..db6eccd 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/MoneyTransferStorage.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/MoneyTransferStorage.cs @@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; +using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; @@ -28,33 +29,26 @@ namespace BankYouBankruptDatabaseImplement.Implements public List GetFilteredList(MoneyTransferSearchModel model) { - if (model.AccountSenderId < 0) - { - return new(); - } + using var context = new BankYouBancruptDatabase(); + var result = context.MoneyTransfers.Include(x => x.Cashier) + .Include(x => x.AccountPayeer) + .Include(x => x.AccountSender) + .ToList(); - using var context = new BankYouBancruptDatabase(); + if (model.AccountPayeeId.HasValue) result = result.Where(x => x.AccountPayeeId == model.AccountPayeeId).ToList(); - if(model.DateFrom.HasValue && model.DateTo.HasValue) - { - return context.MoneyTransfers - .Include(x => x.AccountPayeer) - .Include(x => x.AccountSender) - .Include(x => x.Cashier) - .Where(x => x.DateOperation >= model.DateFrom && x.DateOperation <= model.DateTo && x.AccountSenderId != null - && (x.AccountPayeer.ClientId == model.ClientId || x.AccountSender.Id == model.ClientId)) - .Select(x => x.GetViewModel) - .ToList(); - } + if (model.AccountSenderId.HasValue) result = result.Where(x => x.AccountSenderId == model.AccountSenderId).ToList(); - return context.MoneyTransfers - .Include(x => x.AccountPayeer) - .Include(x => x.AccountSender) - .Include(x => x.Cashier) - .Where(x => x.AccountSenderId == model.AccountSenderId) - .Select(x => x.GetViewModel) - .ToList(); - } + if (model.CashierId.HasValue) result = result.Where(x => x.CashierId == model.CashierId).ToList(); + + if (model.DateFrom.HasValue) result = result.Where(x => x.DateOperation >= model.DateFrom).ToList(); + + if (model.DateTo.HasValue) result = result.Where(x => x.DateOperation <= model.DateTo).ToList(); + + if (model.ClientId.HasValue) result = result.Where(x => (x.AccountPayeer.ClientId == model.ClientId || x.AccountSender.ClientId == model.ClientId)).ToList(); + + return result.Select(x => x.GetViewModel).ToList(); + } public MoneyTransferViewModel? GetElement(MoneyTransferSearchModel model) { diff --git a/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs b/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs index 86c4d6d..e814fa4 100644 --- a/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs +++ b/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/AccountController.cs @@ -4,6 +4,7 @@ using BankYouBankruptContracts.BusinessLogicsContracts; using BankYouBankruptContracts.SearchModels; using BankYouBankruptContracts.ViewModels; using BankYouBankruptContracts.ViewModels.Client.Default; +using BankYouBankruptContracts.ViewModels.Client.Diagram; using BankYouBankruptDatabaseImplement.Models; using BankYouBankruptDataModels.Enums; using Microsoft.AspNetCore.Mvc; @@ -315,5 +316,19 @@ namespace BankYouBankruptRestApi.Controllers throw; } } - } + + [HttpGet] + public List getAccountMonthResult(int cardId) + { + try + { + return _accountLogic.GetMonthInfo(cardId); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения пользователей"); + throw; + } + } + } }