Mango.Nop.Plugins/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs

90 lines
4.2 KiB
C#

using AyCode.Core.Extensions;
using AyCode.Utils.Extensions;
using Mango.Nop.Services;
using Microsoft.AspNetCore.SignalR;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Data;
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Services.Customers;
using Nop.Services.Logging;
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public class AuctionBackgroundService : MgBackgroundServiceBase
{
private readonly ILogger _logger;
private readonly IHubContext<AuctionHub> _auctionHubContext;
private readonly ILockService _lockService;
private readonly AuctionService _auctionService;
private readonly Customer _auctionSystemCustomer;
public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext<AuctionHub> auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository<Customer> customerService) : base(logger, service)
{
_logger = logger;
_auctionHubContext = auctionHubContext;
_lockService = lockService;
_auctionService = auctionService;
_auctionSystemCustomer = customerService.Table.FirstOrDefault(x => x.Email == "builtin@background_task_auction.com");
if (_auctionSystemCustomer == null)
_logger.Error($"AuctionBackgroundService.AuctionBackgroundService(); _auctionSystemCustomer == null;", null, null);
}
protected override async Task OnExecuteAsync()
{
await Task.Delay(15000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J.
if (_auctionSystemCustomer == null) return;
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Before lock; ", null, _auctionSystemCustomer);
using (await _lockService.SemaphoreSlim.UseWaitAsync())
{
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Enter lock;", null, _auctionSystemCustomer);
var auctions = await _auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync();
if (auctions.Count > 0)
{
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); auctions.Count > 0; count: {auctions.Count}; names: {string.Join("; ", auctions.Select(x => x.AuctionName))}", null, _auctionSystemCustomer);
var statusChangedMessageWrapper = new MessageWrapper
{
MessageType = nameof(ProductToAuctionStatusNotification),
SenderId = _auctionSystemCustomer.Id,
ResponseType = ResponseType.ToAllClients
};
foreach (var auction in auctions)
{
auction.Closed = false;
await _auctionService.UpdateAuctionAsync(auction);
var auctionDto = new AuctionDto(auction);
var productToAuctions = (await _auctionService.GetProductToAuctionsByAuctionIdAsync(auction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList();
foreach (var productToAuction in productToAuctions)
{
productToAuction.AuctionStatus = AuctionStatus.Active;
auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(productToAuction));
}
await _auctionService.UpdateProductToAuctionMappingAsync(productToAuctions);
statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {auction.AuctionName}").ToJson();
await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
}
}
await _auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G")); //TODO: ez csak a teszt időszakig van itt!!! - J.
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Exit lock;", null, _auctionSystemCustomer);
}
}
}