261 lines
10 KiB
C#
261 lines
10 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.Advertisements;
|
|
using gehGassi.Domain.Common;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Pins realisiert
|
|
/// </summary>
|
|
public class PinService : ServiceBase<Pin>, IPinService
|
|
{
|
|
private readonly IRepository<PinWithNames> _namesRepository;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public PinService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
_namesRepository = unitOfWork.GetRepository<PinWithNames>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Pins zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Pins</returns>
|
|
public IQueryable<Pin> Filter(string filter)
|
|
{
|
|
return Repository.Query(c => c.NameLocalized.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Pins
|
|
/// </summary>
|
|
/// <returns>Pin</returns>
|
|
public Pin Create()
|
|
{
|
|
var item = new Pin()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
Created = DateTimeOffset.UtcNow
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Pins zurück.
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Pins</returns>
|
|
public async Task<List<Pin>> SearchAsync(string filter)
|
|
{
|
|
return (await Repository.FindAsync(c => c.NameLocalized.Contains(filter))).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Pins mit Namen zurück in einer gewählten Sprache
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="fallback">Fallback Sprache</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <param name="excludeDraft">Sollen Entitäten im Draft-Status (Cart) ausgeblendet werden?</param>
|
|
/// <returns>Liste betroffener Pins</returns>
|
|
public IQueryable<PinWithNames> FilterWithNames(string filter, string language, string fallback, bool includeDeleted, bool excludeDraft)
|
|
{
|
|
language = language.ToUpperInvariant();
|
|
fallback = fallback.ToUpperInvariant();
|
|
|
|
var baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_PinsWithNames({0},{1})", c => c.Name.Contains(filter) || c.CustomerName.Contains(filter), language, fallback);
|
|
|
|
if (!includeDeleted)
|
|
baseQuery = baseQuery.Where(c => c.Deleted == false);
|
|
|
|
if (excludeDraft)
|
|
baseQuery = baseQuery.Where(c => c.Status != PinStatus.Draft);
|
|
|
|
return baseQuery;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Pins mit Namen für Kunden zurück in einer gewählten Sprache
|
|
/// </summary>
|
|
/// <param name="customerId">Id des Kunden</param>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="fallback">Fallback Sprache</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <param name="excludeDraft">Sollen Entitäten im Draft-Status (Cart) ausgeblendet werden?</param>
|
|
/// <returns>Liste betroffener Pins</returns>
|
|
public IQueryable<PinWithNames> FilterWithNamesCustomer(long customerId, string filter, string language, string fallback, bool includeDeleted, bool excludeDraft)
|
|
{
|
|
language = language.ToUpperInvariant();
|
|
fallback = fallback.ToUpperInvariant();
|
|
|
|
var baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_PinsWithNames({0},{1})", c => c.CustomerId == customerId && (c.Name.Contains(filter) || c.CustomerName.Contains(filter)), language, fallback);
|
|
|
|
if (!includeDeleted)
|
|
baseQuery = baseQuery.Where(c => c.Deleted == false);
|
|
|
|
if (excludeDraft)
|
|
baseQuery = baseQuery.Where(c => c.Status != PinStatus.Draft);
|
|
|
|
return baseQuery;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zurücksetzen der Zuordnung zu einer Listung
|
|
/// </summary>
|
|
/// <param name="listingId">Id der Listung</param>
|
|
/// <returns>Task</returns>
|
|
public async Task<int> ResetListingAsync(string listingId)
|
|
{
|
|
var items = (await Repository.FindAsync(c => c.ListingId == listingId).ConfigureAwait(false)).ToList();
|
|
foreach (var item in items)
|
|
{
|
|
item.ListingId = string.Empty;
|
|
}
|
|
|
|
return items.Count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stornieren eines Pins basierend auf einer Listung
|
|
/// </summary>
|
|
/// <param name="listingId">Id der Listung</param>
|
|
/// <returns>Task</returns>
|
|
public async Task<int> CancelByListingAsync(string listingId)
|
|
{
|
|
var items = (await Repository.FindAsync(c => c.ListingId == listingId).ConfigureAwait(false)).ToList();
|
|
foreach (var item in items)
|
|
{
|
|
item.Status = PinStatus.Cancelled;
|
|
}
|
|
|
|
return items.Count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl von Pins zurück
|
|
/// </summary>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <param name="excludeDraft">Sollen Entitäten im Draft-Status (Cart) ausgeblendet werden?</param>
|
|
/// <returns>Anzahl Pins</returns>
|
|
public async Task<int> CountAsync(bool includeDeleted, bool excludeDraft)
|
|
{
|
|
var query = Repository.Query(c => c.Id != "");
|
|
if (!includeDeleted)
|
|
query = query.Where(c => c.Deleted == false);
|
|
if (excludeDraft)
|
|
query = query.Where(c => c.Status != PinStatus.Draft);
|
|
return await query.CountAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl von Pins in einem Status zurück
|
|
/// </summary>
|
|
/// <param name="status">Gesuchter Status</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>Anzahl Pins</returns>
|
|
public async Task<int> CountAsync(PinStatus status, bool includeDeleted)
|
|
{
|
|
var query = Repository.Query(c => c.Status == status);
|
|
if (!includeDeleted)
|
|
query = query.Where(c => c.Deleted == false);
|
|
return await query.CountAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Pin für einen Artikel im Warenkorb zurück
|
|
/// </summary>
|
|
/// <param name="cartItemId">Id des Artikels im Warenkorb</param>
|
|
/// <returns>Pin oder null, wenn nicht gefunden</returns>
|
|
public async Task<Pin> GetByCartItemAsync(long cartItemId)
|
|
{
|
|
return await Repository.FirstOrDefaultAsync(c => c.CartItemId == cartItemId && c.Status == PinStatus.Draft);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Pin für einen Artikel in einer Bestellung zurück
|
|
/// </summary>
|
|
/// <param name="orderItemId">Id des Artikels in der Bestellung</param>
|
|
/// <returns>Pin oder null, wenn nicht gefunden</returns>
|
|
public async Task<Pin> GetByOrderItemAsync(long orderItemId)
|
|
{
|
|
return await Repository.FirstOrDefaultAsync(c => c.OrderItemId == orderItemId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setzen des Status von Pins nach Bestellung
|
|
/// </summary>
|
|
/// <param name="orderId">Id der Bestellung</param>
|
|
/// <param name="status">Zu setzender Status</param>
|
|
/// <returns>Anzahl betroffener Pins</returns>
|
|
public async Task<int> SetStatusByOrderAsync(long orderId, PinStatus status)
|
|
{
|
|
var items = await Repository.FindAsync(c => c.OrderId == orderId);
|
|
foreach (var item in items)
|
|
{
|
|
item.Status = status;
|
|
}
|
|
|
|
return items.Count();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setzen des Bezahlstatus von Pins nach Bestellung
|
|
/// </summary>
|
|
/// <param name="orderId">Id der Bestellung</param>
|
|
/// <param name="status">Zu setzender Status</param>
|
|
/// <returns>Anzahl betroffener Pins</returns>
|
|
public async Task<int> SetPaymentStatusByOrderAsync(long orderId, PaymentStatus status)
|
|
{
|
|
var items = await Repository.FindAsync(c => c.OrderId == orderId);
|
|
foreach (var item in items)
|
|
{
|
|
item.PaymentStatus = status;
|
|
}
|
|
|
|
return items.Count();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starten von gebuchten Pins wenn diese das Start-Datum erreicht haben
|
|
/// </summary>
|
|
/// <returns>Anzahl gestartete Pins</returns>
|
|
public async Task<int> StartBookedAsnyc()
|
|
{
|
|
var items = await Repository.FindAsync(c => c.Status == PinStatus.Booked && c.Deleted == false && c.StartDate <= DateTimeOffset.UtcNow && c.EndDate.Value >= DateTimeOffset.UtcNow);
|
|
foreach (var item in items)
|
|
{
|
|
item.Status = PinStatus.Running;
|
|
}
|
|
|
|
return items.Count();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stoppen von abgelaufenen Pins wenn diese das Enddatum erreicht haben
|
|
/// </summary>
|
|
/// <returns>Anzahl gestoppte Pins</returns>
|
|
public async Task<int> StopRunningAsync()
|
|
{
|
|
var items = await Repository.FindAsync(c => c.Status == PinStatus.Running && c.Deleted == false && c.EndDate.Value <= DateTimeOffset.UtcNow);
|
|
foreach (var item in items)
|
|
{
|
|
item.Status = PinStatus.Ended;
|
|
}
|
|
|
|
return items.Count();
|
|
}
|
|
}
|
|
}
|