142 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Common;
using gehGassi.Domain.Walks;
using Microsoft.EntityFrameworkCore;
namespace gehGassi.Core.Services
{
/// <summary>
/// Service der Statistiken zur Verfügung stellt
/// </summary>
public class StatistcService : IStatisticService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IRepository<Walk> _walksRepository;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="unitOfWork">Instanz einer IUnitOfWork</param>
public StatistcService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_walksRepository = _unitOfWork.GetRepository<Walk>();
}
/// <summary>
/// Gibt zurück wie viele gültige Walks insgesamt vorhanden sind.
/// Das sind alle Walks die nicht folgenden Status haben:
/// - Requested
/// - Declined
/// - Cancelled
/// </summary>
/// <param name="minPaymentStatus">Minimaler Zahlungsstatus, null wenn alle</param>
/// <param name="maxPaymentStatus">Maximaler Zahlungsstatus, null wenn alle</param>
/// <param name="countryIso">Land oder leer wenn alle</param>
/// <returns>Anzahl Walks</returns>
public async Task<int> CountWalksTotalAsync(PaymentStatus? minPaymentStatus, PaymentStatus? maxPaymentStatus, string countryIso)
{
var baseQuery = _walksRepository.Query(c => c.Status != WalkStatus.Requested && c.Status != WalkStatus.Declined && c.Status != WalkStatus.Cancelled && !c.Deleted);
if (!string.IsNullOrEmpty(countryIso))
{
baseQuery = baseQuery.Where(c => c.PickupAddress.CountryCode == countryIso);
}
if (minPaymentStatus.HasValue)
{
baseQuery = baseQuery.Where(c => c.PaymentStatus >= minPaymentStatus.Value);
}
if (maxPaymentStatus.HasValue)
{
baseQuery = baseQuery.Where(c => c.PaymentStatus <= maxPaymentStatus.Value);
}
var count = await baseQuery.CountAsync();
return count;
}
/// <summary>
/// Gibt zurück wie viele gültige Walks in einem Zeitraum vorhanden sind.
/// Das sind alle Walks die nicht folgenden Status haben:
/// - Requested
/// - Declined
/// - Cancelled
/// </summary>
/// <param name="minPaymentStatus">Minimaler Zahlungsstatus, null wenn alle</param>
/// <param name="maxPaymentStatus">Maximaler Zahlungsstatus, null wenn alle</param>
/// <param name="start">Startdatum</param>
/// <param name="end">Enddatum</param>
/// <param name="countryIso">Land oder leer wenn alle</param>
/// <returns>Anzahl Walks</returns>
public async Task<int> CountWalksAsync(PaymentStatus? minPaymentStatus, PaymentStatus? maxPaymentStatus, DateTimeOffset start, DateTimeOffset end, string countryIso)
{
var baseQuery = _walksRepository.Query(c => c.Status != WalkStatus.Requested && c.Status != WalkStatus.Declined && c.Status != WalkStatus.Cancelled && !c.Deleted);
if (!string.IsNullOrEmpty(countryIso))
{
baseQuery = baseQuery.Where(c => c.PickupAddress.CountryCode == countryIso);
}
if (minPaymentStatus.HasValue)
{
baseQuery = baseQuery.Where(c => c.PaymentStatus >= minPaymentStatus.Value);
}
if (maxPaymentStatus.HasValue)
{
baseQuery = baseQuery.Where(c => c.PaymentStatus <= maxPaymentStatus.Value);
}
//Start- oder Enddatum des Walks liegen im Bereich zwischen start und end
baseQuery = baseQuery.Where(c => (c.Start >= start && c.Start <= end) || (c.End >= start && c.End <= end));
var count = await baseQuery.CountAsync();
return count;
}
/// <summary>
/// Gibt die Anzahl von Walks in einem bestimmten Status in einem Zeitraum zurück
/// </summary>
/// <param name="status">Status der gesucht wird, null wenn alle</param>
/// <param name="minPaymentStatus">Minimaler Zahlungsstatus, null wenn alle</param>
/// <param name="maxPaymentStatus">Maximaler Zahlungsstatus, null wenn alle</param>
/// <param name="start">Startdatum</param>
/// <param name="end">Enddatum</param>
/// <param name="countryIso">Land oder leer wenn alle</param>
/// <returns>Anzahl Walks</returns>
public async Task<int> CountWalkByStatusAsync(WalkStatus? status, PaymentStatus? minPaymentStatus, PaymentStatus? maxPaymentStatus, DateTimeOffset start, DateTimeOffset end, string countryIso)
{
var baseQuery = _walksRepository.Query(c => !c.Deleted);
if (status.HasValue)
{
baseQuery = baseQuery.Where(c => c.Status == status.Value);
}
if (!string.IsNullOrEmpty(countryIso))
{
baseQuery = baseQuery.Where(c => c.PickupAddress.CountryCode == countryIso);
}
if (minPaymentStatus.HasValue)
{
baseQuery = baseQuery.Where(c => c.PaymentStatus >= minPaymentStatus.Value);
}
if (maxPaymentStatus.HasValue)
{
baseQuery = baseQuery.Where(c => c.PaymentStatus <= maxPaymentStatus.Value);
}
//Start- oder Enddatum des Walks liegen im Bereich zwischen start und end
baseQuery = baseQuery.Where(c => (c.Start >= start && c.Start <= end) || (c.End >= start && c.End <= end));
var count = await baseQuery.CountAsync();
return count;
}
}
}