gehgassi_backend/gehGassi.Web/Controllers/StatisticController.cs

145 lines
7.2 KiB
C#

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
{
/// <summary>
/// Controller für die Anzeige von Statistiken
/// </summary>
[Authorize]
public class StatisticController : BaseController
{
private readonly IMapper _mapper;
private readonly IStringLocalizer<StatisticController> _localizer;
private readonly IStatisticService _statisticService;
private readonly IUserService _userService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizer">Instanz eines IStringLocalizer</param>
/// <param name="statisticService">Instanz eines IStatisticService</param>
/// <param name="userService"></param>
public StatisticController(IMapper mapper, IStringLocalizer<StatisticController> localizer, IStatisticService statisticService, IUserService userService)
{
_mapper = mapper;
_localizer = localizer;
_statisticService = statisticService;
_userService = userService;
}
/// <summary>
/// Anzeigen der Statistiken für Walks an einem Tag
/// </summary>
/// <returns>View</returns>
[HasPermission(Permission.StatisticsManage)]
public async Task<IActionResult> WalksPerDay()
{
await Task.Delay(1);
var dateRangeVm = new StatisticDateRangeVm()
{
Start = DateTimeOffset.Now,
End = null,
CountryIso = string.Empty
};
return View(dateRangeVm);
}
/// <summary>
/// Abrufen der Statistiken der Walks für einen Tag
/// </summary>
/// <param name="model">StatisticDateRangeVm</param>
/// <returns>JSON</returns>
[HttpPost]
[HasPermission(Permission.StatisticsManage)]
public async Task<IActionResult> 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);
}
/// <summary>
/// Anzeigen der Statistiken für Walks in einem Zeitraum
/// </summary>
/// <returns>View</returns>
[HasPermission(Permission.StatisticsManage)]
public async Task<IActionResult> WalksPerTimeange()
{
await Task.Delay(1);
var dateRangeVm = new StatisticDateRangeVm()
{
Start = DateTimeOffset.Now,
End = DateTimeOffset.Now.AddDays(1),
CountryIso = string.Empty
};
return View(dateRangeVm);
}
/// <summary>
/// Abrufen der Statistiken der Walks für einen Zeitraum
/// </summary>
/// <param name="model">StatisticDateRangeVm</param>
/// <returns>JSON</returns>
[HttpPost]
[HasPermission(Permission.StatisticsManage)]
public async Task<IActionResult> 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);
}
}
}