2023-04-01 15:14:54 +04:00
|
|
|
|
using BankYouBankruptContracts.BindingModels;
|
|
|
|
|
using BankYouBankruptContracts.BusinessLogicsContracts;
|
|
|
|
|
using BankYouBankruptContracts.SearchModels;
|
|
|
|
|
using BankYouBankruptContracts.StoragesContracts;
|
|
|
|
|
using BankYouBankruptContracts.ViewModels;
|
2023-05-18 21:00:46 +04:00
|
|
|
|
using BankYouBankruptContracts.ViewModels.Client.Diagram;
|
|
|
|
|
using BankYouBankruptDataModels.Enums;
|
2023-04-01 15:14:54 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankYouBankruptBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class AccountLogic : IAccountLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IAccountStorage _accountStorage;
|
2023-05-18 21:00:46 +04:00
|
|
|
|
private readonly ICashWithdrawalLogic _cashWithdrawalLogic;
|
|
|
|
|
private readonly IMoneyTransferLogic _moneyTransferLogic;
|
2023-04-01 15:14:54 +04:00
|
|
|
|
|
2023-05-18 21:00:46 +04:00
|
|
|
|
public AccountLogic(ILogger<AccountLogic> logger, IAccountStorage accountStorage,
|
|
|
|
|
ICashWithdrawalLogic cashWithdrawalLogic, IMoneyTransferLogic moneyTransferLogic)
|
2023-04-01 15:14:54 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_accountStorage = accountStorage;
|
2023-05-18 21:00:46 +04:00
|
|
|
|
_cashWithdrawalLogic = cashWithdrawalLogic;
|
|
|
|
|
_moneyTransferLogic = moneyTransferLogic;
|
2023-04-01 15:14:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AccountViewModel? ReadElement(AccountSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. AccountNumber:{Name}. Id:{Id}", model.AccountNumber, model?.Id);
|
|
|
|
|
|
|
|
|
|
var element = _accountStorage.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<AccountViewModel>? ReadList(AccountSearchModel? model)
|
|
|
|
|
{
|
2023-05-17 00:06:05 +04:00
|
|
|
|
//_logger.LogInformation("ReadList. AccountNumber:{AccountNumber}. Id:{Id}", model.AccountNumber, model?.Id);
|
2023-04-01 15:14:54 +04:00
|
|
|
|
|
|
|
|
|
//list хранит весь список в случае, если model пришло со значением null на вход метода
|
|
|
|
|
var list = model == null ? _accountStorage.GetFullList() : _accountStorage.GetFilteredList(model);
|
|
|
|
|
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 21:23:18 +04:00
|
|
|
|
//метод, отвечающий за изменение баланса счёта
|
|
|
|
|
public bool ChangeBalance(AccountSearchModel? model, int sum)
|
|
|
|
|
{
|
2023-05-16 23:06:28 +04:00
|
|
|
|
//ищем счёт
|
|
|
|
|
var account = ReadElement(model);
|
|
|
|
|
|
|
|
|
|
if (account == null)
|
2023-05-14 21:23:18 +04:00
|
|
|
|
{
|
2023-05-16 23:06:28 +04:00
|
|
|
|
throw new ArgumentNullException("Счёт не найден", nameof(account));
|
2023-05-14 21:23:18 +04:00
|
|
|
|
}
|
2023-05-16 23:06:28 +04:00
|
|
|
|
|
|
|
|
|
//проверяем возможность операции снятия (sum может быть отрицательной)
|
|
|
|
|
if (sum + account.Balance < 0)
|
2023-05-14 21:23:18 +04:00
|
|
|
|
{
|
2023-05-17 20:00:17 +04:00
|
|
|
|
return false;
|
2023-05-14 21:23:18 +04:00
|
|
|
|
}
|
2023-05-16 23:06:28 +04:00
|
|
|
|
|
|
|
|
|
//обновляем балланс счёта
|
|
|
|
|
_accountStorage.Update(new AccountBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = account.Id,
|
|
|
|
|
Balance = account.Balance + sum
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-05-14 21:23:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 15:14:54 +04:00
|
|
|
|
public bool Create(AccountBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_accountStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(AccountBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_accountStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(AccountBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_accountStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 21:00:46 +04:00
|
|
|
|
public List<CashierDiagramElementsViewModel> GetMonthInfo(int AccountId)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Dictionary<(int, int), int> cashWithdrawals = _cashWithdrawalLogic.ReadList(new CashWithdrawalSearchModel()
|
|
|
|
|
{
|
|
|
|
|
AccountId = AccountId,
|
2023-05-20 00:25:24 +04:00
|
|
|
|
}).Where(x => x.DebitingStatus == StatusEnum.Закрыта ).GroupBy(x => new { x.DateOperation.Month, x.DateOperation.Year })
|
2023-05-18 21:00:46 +04:00
|
|
|
|
.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));
|
2023-05-19 01:53:16 +04:00
|
|
|
|
|
|
|
|
|
Dictionary<(int, int), int> moneyTransfers = _moneyTransferLogic.ReadList(new MoneyTransferSearchModel()
|
2023-05-18 21:00:46 +04:00
|
|
|
|
{
|
|
|
|
|
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));
|
2023-05-19 01:53:16 +04:00
|
|
|
|
|
|
|
|
|
Dictionary<(int, int), int> moneyTransfersDebiting = _moneyTransferLogic.ReadList(new MoneyTransferSearchModel()
|
2023-05-18 21:00:46 +04:00
|
|
|
|
{
|
|
|
|
|
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));
|
2023-05-19 01:53:16 +04:00
|
|
|
|
|
2023-05-18 21:00:46 +04:00
|
|
|
|
List<CashierDiagramElementsViewModel> result = new();
|
2023-05-19 01:53:16 +04:00
|
|
|
|
|
|
|
|
|
int sum = 0;
|
|
|
|
|
|
2023-05-20 08:00:06 +04:00
|
|
|
|
foreach (var key in cashWithdrawals.Keys.Union(moneyTransfers.Keys).Union(moneyTransfers.Keys).OrderBy(x => x.Item1 * 12 + x.Item2))
|
2023-05-18 21:00:46 +04:00
|
|
|
|
{
|
2023-05-19 01:53:16 +04:00
|
|
|
|
if (cashWithdrawals.ContainsKey(key)) sum += cashWithdrawals.GetValueOrDefault(key);
|
|
|
|
|
if (moneyTransfers.ContainsKey(key)) sum += moneyTransfers.GetValueOrDefault(key);
|
|
|
|
|
if (moneyTransfers.ContainsKey(key)) sum -= moneyTransfersDebiting.GetValueOrDefault(key);
|
2023-05-18 21:00:46 +04:00
|
|
|
|
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)
|
2023-04-01 15:14:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//так как при удалении передаём как параметр false
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие номера счёта
|
|
|
|
|
if (string.IsNullOrEmpty(model.AccountNumber))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Отсутствие номера у счёта", nameof(model.AccountNumber));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 06:42:50 +04:00
|
|
|
|
//проверка на наличие id владельца
|
|
|
|
|
if (model.CashierId < 0)
|
2023-04-01 15:14:54 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный Id владельца счёта", nameof(model.CashierId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие id кассира, создавшего счёт
|
|
|
|
|
if (model.CashierId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный Id кассира, открывшего счёт", nameof(model.CashierId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие пароля счёта
|
2023-05-20 06:42:50 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.PasswordAccount) )
|
2023-04-01 15:14:54 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный пароль счёта", nameof(model.PasswordAccount));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 06:42:50 +04:00
|
|
|
|
if (model.Balance < 0) {
|
|
|
|
|
throw new ArgumentNullException("Изначальный баланс аккаунта не может быть < 0", nameof(model.Balance));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 15:14:54 +04:00
|
|
|
|
//проверка на корректную дату открытия счёта
|
|
|
|
|
if (model.DateOpen > DateTime.Now)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Дата открытия счёта не может быть ранее текущей", nameof(model.DateOpen));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Account. AccountNumber:{AccountNumber}. PasswordAccount:{PasswordAccount}. ClientId:{ClientId}. " +
|
|
|
|
|
"CashierId:{CashierId}. DateOpen:{DateOpen}. Id:{Id}",
|
|
|
|
|
model.AccountNumber, model.PasswordAccount, model.ClientId, model.CashierId, model.DateOpen, model.Id);
|
|
|
|
|
|
|
|
|
|
//для проверка на наличие такого же счёта
|
|
|
|
|
var element = _accountStorage.GetElement(new AccountSearchModel
|
|
|
|
|
{
|
|
|
|
|
AccountNumber = model.AccountNumber,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//если элемент найден и его Id не совпадает с Id переданного объекта
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Счёт с таким номером уже существует");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|