using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using gehGassi.Common.Extensions; using gehGassi.Core.Interfaces; using gehGassi.Domain.Common; using gehGassi.Domain.Payment; using Microsoft.EntityFrameworkCore; namespace gehGassi.Core.Services { /// /// Service der die Verwaltung von Auszahlungen ermöglicht /// public class PayoutService : ServiceBase, IPayoutService { /// /// Erstellt eine Instanz /// /// Instanz eine IUnitOfWork public PayoutService(IUnitOfWork unitOfWork) : base(unitOfWork) { } /// /// Erstellen einer Auszahlung /// /// Payout public Payout Create() { var item = new Payout() { Id = Guid.NewGuid().ToString("N"), Created = DateTimeOffset.UtcNow }; return item; } /// /// Erstellen einer Auszahlung /// /// Id des App-Users /// Id des Users bei MangoPay /// Id der Auszahlung bei MangoPay /// Id des Wallets /// Id des Wallets bei MangoPay /// Id der Bankverbindung bei mangoPay /// Iban letzte 4 Stellen /// Bic letzte 4 Stellen oder leer /// Summe die ausgezahlt werden soll in der kleinsten Einheit /// Währung ISO 4217 Code /// Payout public Payout Create(string appUserId, string mangoPayUserId, string mangoPayId, string walletId, string mangoPayWalletId, string mangoPayBankId, string iban, string bic, long ammount, string currency) { var item = Create(); item.AppUserId = appUserId; item.MangoPayUserId = mangoPayUserId; item.MangoPayId = mangoPayId; item.WalletId = walletId; item.MangoPayWalletId = mangoPayWalletId; item.MangoPayBankId = mangoPayBankId; item.Iban = iban; item.Bic = bic; item.Ammount = ammount; item.Currency = currency; return item; } /// /// Gibt eine Liste der Auszahlungen eines Benutzers zurück. /// /// Id des AppUsers /// Letztes Update /// Liste der öffentlichen Anfragen public async Task> GetForSyncAppAsync(string appUserId, DateTimeOffset? lastUpdate) { var query = Repository.Query(c => c.AppUserId == appUserId); if (lastUpdate.HasValue) query = query.Where(c => c.UpdatedAt > lastUpdate); return await query.ToListAsync().ConfigureAwait(false); } /// /// Gibt eine Auszahlung anhand der MangoPayId zurück /// /// Id der Auszahlung bei MangoPay /// Payout oder null, wenn nicht gefunden public async Task GetByMangoPayIdAsync(string mangoPayId) { var item = await Repository.FirstOrDefaultAsync(c => c.MangoPayId == mangoPayId).ConfigureAwait(false); return item; } /// /// Gibt eine Liste von Auszahlungen zurück /// /// Id des App-Users /// Optional: Liste der sortierung /// Datensätze auslassen /// Datensätze nehmen /// Liste gefudene Walks public async Task<(int total, List list)> GetAsync(string appUserId, List sortList, int skip, int take) { var baseQuery = Repository.Query(c => c.AppUserId == appUserId); var total = await baseQuery.CountAsync().ConfigureAwait(false); if (sortList.Any()) { var isFirst = true; var orderedBaseQuery = baseQuery.SpecialOrderBy(sortList.First().Property, sortList.First().SortDirection); ; foreach (var sortOrder in sortList) { if (isFirst) { isFirst = false; continue; } else { orderedBaseQuery = orderedBaseQuery.SpecialThenBy(sortOrder.Property, sortOrder.SortDirection); } } var list = await orderedBaseQuery.ToListAsync().ConfigureAwait(false); return (total, list); } else { var list = await baseQuery.OrderByDescending(c => c.Created).Skip(skip).Take(take).ToListAsync().ConfigureAwait(false); return (total, list); } } /// /// Gibt die letzte Auszahlung eines Benutzers zurück /// /// Id des Benutzers /// Auszahlung oder null, wenn nicht gefunden public async Task GetLastPayoutAsync(string appUserId) { var lastPayout = await Repository.Query(c => c.AppUserId == appUserId).OrderByDescending(c => c.Created).FirstOrDefaultAsync(); return lastPayout; } /// /// Gibt die Anzahl der Auszahlungen eines Benutzers in einem Zeitrahmen zurück /// /// Id des App-Users /// Startdatum /// Enddatum /// Anzahl Auszahlungen public async Task CountPayoutsInTimeRangeAsync(string appUserId, DateTimeOffset startDate, DateTimeOffset endDate) { return await Repository.Query(c => c.AppUserId == appUserId && c.Created >= startDate && c.Created <= endDate).CountAsync().ConfigureAwait(false); } } }