Files
myoffice/MyOffice.Services/Dashboard/DashboardService.cs
T
2023-08-03 08:58:53 +03:00

101 lines
2.9 KiB
C#

namespace MyOffice.Services.Dashboard;
using MyOffice.Services.Dashboard.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Core.Extensions;
using Data.Repositories.Account;
using Data.Repositories.Currency;
using MyOffice.Data.Models.Accounts;
public class DashboardService
{
private readonly IAccountRepository _accountRepository;
private readonly ICurrencyRateRepository _currencyRateRepository;
public DashboardService(
IAccountRepository accountRepository,
ICurrencyRateRepository currencyRateRepository
)
{
_accountRepository = accountRepository;
_currencyRateRepository = currencyRateRepository;
}
public DashboardData GetDashboardRestData(Guid userId)
{
var rests = _accountRepository.GetRestAtDate(userId, DateTime.UtcNow);
var result = new DashboardData
{
Balance = rests
.Where(x => (x.Type == AccountAccessTypeEnum.balance || x.Type == AccountAccessTypeEnum.credit) && x.CurrencyRate.HasValue)
.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
BalanceDebit = rests
.Where(x => x.Type == AccountAccessTypeEnum.balance && x.CurrencyRate.HasValue)
.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
BalanceCredit = rests
.Where(x => x.Type == AccountAccessTypeEnum.credit && x.CurrencyRate.HasValue)
.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
BalanceRests = rests
.Where(x => x.Type == AccountAccessTypeEnum.balance)
.Where(x => x.CurrencyRate.HasValue && x.Balance.HasValue && x.Balance != 0)
.Select(x => new DashboardRestData
{
Id = x.Id,
Name = x.Name,
Balance = x.Balance,
CurrencyName = x.CurrencyName,
CurrencyShortName = x.CurrencyShortName,
CurrencyRate = x.CurrencyRate,
CurrencyQuantity = x.CurrencyQuantity,
})
.OrderByDescending(x => x.BalanceAtRate)
.ToList(),
};
return result;
}
public DashboardIncomeData GetDashboardIncomeData(Guid userId, DateTime from, DateTime to, Guid? category)
{
var data = category.HasValue
? _accountRepository.GetIncomeByCategory(userId, category.Value, from.ToUtc(), to.ToUtc())
: _accountRepository.GetIncomeByCategories(userId, from.ToUtc(), to.ToUtc());
return new DashboardIncomeData
{
Data = data.Select(x => new DashboardIncomeDataItem
{
Id = x.Id?.ToShort(),
Name = x.Name,
Value = x.Amount,
}).ToList()
};
}
public DashboardIncomeData GetDashboardOutcomeData(Guid userId, DateTime from, DateTime to, Guid? category)
{
var data = category.HasValue
? _accountRepository.GetOutcomeByCategory(userId, category.Value, from.ToUtc(), to.ToUtc())
: _accountRepository.GetOutcomeByCategories(userId, from.ToUtc(), to.ToUtc());
return new DashboardIncomeData
{
Data = data.Select(x => new DashboardIncomeDataItem
{
Id = x.Id?.ToShort(),
Name = x.Name,
Value = x.Amount,
}).ToList()
};
}
}