using System; using System.Threading.Tasks; using AutoMapper; using gehGassi.Core.Interfaces; using gehGassi.Domain.Common; using gehGassi.Permissions; using gehGassi.Web.Auth.Attributes; using gehGassi.Web.Helper; using gehGassi.Web.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; namespace gehGassi.Web.Controllers { /// /// Controller für die Anzeige von Statistiken /// [Authorize] public class StatisticController : BaseController { private readonly IMapper _mapper; private readonly IStringLocalizer _localizer; private readonly IStatisticService _statisticService; private readonly IUserService _userService; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz eines IStringLocalizer /// Instanz eines IStatisticService /// public StatisticController(IMapper mapper, IStringLocalizer localizer, IStatisticService statisticService, IUserService userService) { _mapper = mapper; _localizer = localizer; _statisticService = statisticService; _userService = userService; } /// /// Anzeigen der Statistiken für Walks an einem Tag /// /// View [HasPermission(Permission.StatisticsManage)] public async Task WalksPerDay() { await Task.Delay(1); var dateRangeVm = new StatisticDateRangeVm() { Start = DateTimeOffset.Now, End = null, CountryIso = string.Empty }; return View(dateRangeVm); } /// /// Abrufen der Statistiken der Walks für einen Tag /// /// StatisticDateRangeVm /// JSON [HttpPost] [HasPermission(Permission.StatisticsManage)] public async Task GetWalksPerDay([FromBody] StatisticDateRangeVm model) { var result = new StatisticWalkVm(); var user = await _userService.GetByUsernameAsync(User.Identity.Name); var startDate = new DateTime(model.Start.Value.Year, model.Start.Value.Month, model.Start.Value.Day, 0, 0, 0, DateTimeKind.Local); var startDateUtc = startDate.ToUniversalTime(); var start = new DateTimeOffset(startDateUtc.Year, startDateUtc.Month, startDateUtc.Day, startDateUtc.Hour,startDateUtc.Minute,startDateUtc.Second, TimeSpan.Zero); var end = start.AddDays(1); result.Total = await _statisticService.CountWalksAsync(PaymentStatus.Pending, null, start, end, model.CountryIso); result.ToPay = await _statisticService.CountWalkByStatusAsync(WalkStatus.Accepted, PaymentStatus.Pending, PaymentStatus.Pending, start, end, model.CountryIso); result.Running = await _statisticService.CountWalkByStatusAsync(WalkStatus.Started ,PaymentStatus.Authorized, null, start, end, model.CountryIso); result.Completed = await _statisticService.CountWalkByStatusAsync(WalkStatus.Completed, PaymentStatus.Authorized, null, start, end, model.CountryIso); result.NotCompleted = await _statisticService.CountWalkByStatusAsync(WalkStatus.Accepted, PaymentStatus.Authorized, null, start, end, model.CountryIso); result.Confirmed = await _statisticService.CountWalkByStatusAsync(WalkStatus.Confirmed, PaymentStatus.Paid, PaymentStatus.Paid, start, end, model.CountryIso); result.Complained = await _statisticService.CountWalkByStatusAsync(WalkStatus.Complained, null, null, start, end, model.CountryIso); result.ComplainResolved = await _statisticService.CountWalkByStatusAsync(WalkStatus.ComplainResolved, null, null, start, end, model.CountryIso); return Json(result); } /// /// Anzeigen der Statistiken für Walks in einem Zeitraum /// /// View [HasPermission(Permission.StatisticsManage)] public async Task WalksPerTimeange() { await Task.Delay(1); var dateRangeVm = new StatisticDateRangeVm() { Start = DateTimeOffset.Now, End = DateTimeOffset.Now.AddDays(1), CountryIso = string.Empty }; return View(dateRangeVm); } /// /// Abrufen der Statistiken der Walks für einen Zeitraum /// /// StatisticDateRangeVm /// JSON [HttpPost] [HasPermission(Permission.StatisticsManage)] public async Task GetWalksPerTimeRange([FromBody] StatisticDateRangeVm model) { var result = new StatisticWalkVm(); var user = await _userService.GetByUsernameAsync(User.Identity.Name); var startDate = new DateTime(model.Start.Value.Year, model.Start.Value.Month, model.Start.Value.Day, 0, 0, 0, DateTimeKind.Local); var startDateUtc = startDate.ToUniversalTime(); var start = new DateTimeOffset(startDateUtc.Year, startDateUtc.Month, startDateUtc.Day, startDateUtc.Hour, startDateUtc.Minute, startDateUtc.Second, TimeSpan.Zero); var endDate = new DateTime(model.End.Value.Year, model.End.Value.Month, model.End.Value.Day, 0, 0, 0, DateTimeKind.Local); var endDateUtc = endDate.ToUniversalTime(); var end = new DateTimeOffset(endDateUtc.Year, endDateUtc.Month, endDateUtc.Day, endDateUtc.Hour, endDateUtc.Minute, endDateUtc.Second, TimeSpan.Zero); result.Total = await _statisticService.CountWalksAsync(PaymentStatus.Pending, null, start, end, model.CountryIso); result.ToPay = await _statisticService.CountWalkByStatusAsync(WalkStatus.Accepted, PaymentStatus.Pending, PaymentStatus.Pending, start, end, model.CountryIso); result.Running = await _statisticService.CountWalkByStatusAsync(WalkStatus.Started, PaymentStatus.Authorized, null, start, end, model.CountryIso); result.Completed = await _statisticService.CountWalkByStatusAsync(WalkStatus.Completed, PaymentStatus.Authorized, null, start, end, model.CountryIso); result.NotCompleted = await _statisticService.CountWalkByStatusAsync(WalkStatus.Accepted, PaymentStatus.Authorized, null, start, end, model.CountryIso); result.Confirmed = await _statisticService.CountWalkByStatusAsync(WalkStatus.Confirmed, PaymentStatus.Paid, PaymentStatus.Paid, start, end, model.CountryIso); result.Complained = await _statisticService.CountWalkByStatusAsync(WalkStatus.Complained, null, null, start, end, model.CountryIso); result.ComplainResolved = await _statisticService.CountWalkByStatusAsync(WalkStatus.ComplainResolved, null, null, start, end, model.CountryIso); return Json(result); } } }