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
{
///
/// Service der Statistiken zur Verfügung stellt
///
public class StatistcService : IStatisticService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IRepository _walksRepository;
///
/// Erstellt eine Instanz
///
/// Instanz einer IUnitOfWork
public StatistcService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_walksRepository = _unitOfWork.GetRepository();
}
///
/// Gibt zurück wie viele gültige Walks insgesamt vorhanden sind.
/// Das sind alle Walks die nicht folgenden Status haben:
/// - Requested
/// - Declined
/// - Cancelled
///
/// Minimaler Zahlungsstatus, null wenn alle
/// Maximaler Zahlungsstatus, null wenn alle
/// Land oder leer wenn alle
/// Anzahl Walks
public async Task 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;
}
///
/// 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
///
/// Minimaler Zahlungsstatus, null wenn alle
/// Maximaler Zahlungsstatus, null wenn alle
/// Startdatum
/// Enddatum
/// Land oder leer wenn alle
/// Anzahl Walks
public async Task 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;
}
///
/// Gibt die Anzahl von Walks in einem bestimmten Status in einem Zeitraum zurück
///
/// Status der gesucht wird, null wenn alle
/// Minimaler Zahlungsstatus, null wenn alle
/// Maximaler Zahlungsstatus, null wenn alle
/// Startdatum
/// Enddatum
/// Land oder leer wenn alle
/// Anzahl Walks
public async Task 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;
}
}
}