CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptBusinessLogic/BusinessLogics/CardLogic.cs

172 lines
6.6 KiB
C#
Raw Normal View History

2023-04-01 10:24:54 +04:00
using Microsoft.Extensions.Logging;
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.Default;
using BankYouBankruptContracts.ViewModels.Client.Diagram;
using BankYouBankruptDataModels.Enums;
using DocumentFormat.OpenXml.Drawing;
2023-04-01 10:24:54 +04:00
namespace BankYouBankruptBusinessLogic.BusinessLogics
{
public class CardLogic : ICardLogic
{
private readonly ILogger _logger;
private readonly ICardStorage _cardStorage;
2023-05-14 14:00:44 +04:00
private readonly IAccountStorage _accountStorage;
2023-05-18 17:56:47 +04:00
private readonly IDebitingLogic _debitingLogic;
private readonly ICreditingLogic _creditingLogic;
2023-04-01 10:24:54 +04:00
2023-05-18 17:56:47 +04:00
public CardLogic(ILogger<CardLogic> logger, ICardStorage cardStorage, IAccountStorage accountStorage,
IDebitingLogic debitingLogic, ICreditingLogic creditingLogic) {
2023-04-01 10:24:54 +04:00
_logger = logger;
_cardStorage = cardStorage;
2023-05-14 14:00:44 +04:00
_accountStorage = accountStorage;
2023-05-18 17:56:47 +04:00
_debitingLogic = debitingLogic;
_creditingLogic = creditingLogic;
2023-04-01 10:24:54 +04:00
}
public bool Create(CardBindingModel model)
2023-04-01 10:24:54 +04:00
{
CheckModel(model);
if (_cardStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CardBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_cardStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CardViewModel? ReadElement(CardSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. CardNumber:{Number}.Id:{ Id}", model.Number, model.Id);
var element = _cardStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<CardViewModel>? ReadList(CardSearchModel? model)
{
_logger.LogInformation("ReadList. CardId:{Id}", model?.Id);
var list = model == null ? _cardStorage.GetFullList() : _cardStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(CardBindingModel model)
{
CheckModel(model);
if (_cardStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2023-05-18 17:56:47 +04:00
public List<ClientDiagramElementsViewModel> GetMonthInfo(int CardId) {
Dictionary<(int?, int?), int> debitings = _debitingLogic.ReadList(new DebitingSearchModel()
{
CardId = CardId,
Status = StatusEnum.Закрыта
}).GroupBy(x => new { x.DateClose?.Month, x.DateClose?.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> creditings = _creditingLogic.ReadList(new CreditingSearchModel()
{
CardId = CardId,
Status = StatusEnum.Закрыта
}).GroupBy(x => new { x.DateClose?.Month, x.DateClose?.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<ClientDiagramElementsViewModel> result = new();
foreach (var key in debitings.Keys.Union(creditings.Keys)) {
int sum = 0;
if (debitings.ContainsKey(key)) sum -= debitings.GetValueOrDefault(key);
if (creditings.ContainsKey(key)) sum += creditings.GetValueOrDefault(key);
result.Add(new ClientDiagramElementsViewModel() { Name = Enum.GetName(typeof(Months), key.Item1) + " " + key.Item2.ToString(), Value = sum});
}
return result;
}
2023-04-01 10:24:54 +04:00
private void CheckModel(CardBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Number))
{
throw new ArgumentNullException("Нет номера карты", nameof(model.Number));
}
if (string.IsNullOrEmpty(model.CVC))
{
throw new ArgumentNullException("Нет СVC карты", nameof(model.CVC));
}
if (model.Period < DateTime.Now)
{
throw new ArgumentNullException("Нет периода действия", nameof(model.Period));
}
2023-05-14 14:00:44 +04:00
var cardElement = _cardStorage.GetElement(new CardSearchModel
2023-04-06 22:32:43 +04:00
{
Number = model.Number,
});
2023-05-14 14:00:44 +04:00
if (cardElement != null && cardElement.Id != model.Id)
2023-04-06 22:32:43 +04:00
{
throw new InvalidOperationException("Карта с таким ноиером уже есть");
}
2023-05-14 14:00:44 +04:00
var accountElement = _accountStorage.GetElement(new AccountSearchModel
{
Id = model.AccountId,
ClientId = model.ClientID
});
if (accountElement != null && accountElement.ClientId != model.ClientID)
{
throw new InvalidOperationException("Это не счёт данного клиента");
}
2023-04-01 10:24:54 +04:00
_logger.LogInformation("Card. Number:{Number}.CVC:{CVC}.ClientId:{ClientID}.Patronymic:{Period}.Id:{Id}",
model.Number, model.CVC, model.Period.ToString(), model.ClientID, model.Id);
}
}
}