AuctionBackgroundService; MinimumPrice, StoreId; refactoring, improvements, fixes, etc...
This commit is contained in:
parent
23c1a8fb15
commit
54d70baddf
|
|
@ -45,6 +45,7 @@ public class AuctionDbContext : MgDbContextBase, IAuctionDbSet<AuctionDbTable>,
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public Task<List<AuctionBid>> GetAllLastBidByAuctionIdAsync(int auctionId) => AuctionBids.GetAllLastBidByAuctionId(auctionId).ToListAsync();
|
public Task<List<AuctionBid>> GetAllLastBidByAuctionIdAsync(int auctionId) => AuctionBids.GetAllLastBidByAuctionId(auctionId).ToListAsync();
|
||||||
|
|
||||||
public Task<Dictionary<int, AuctionBid>> GetAllLastBidDictionaryByAuctionIdAsync(int auctionId)
|
public Task<Dictionary<int, AuctionBid>> GetAllLastBidDictionaryByAuctionIdAsync(int auctionId)
|
||||||
=> AuctionBids.GetAllLastBidByAuctionId(auctionId).ToDictionaryAsync(x => x.ProductAuctionMappingId);
|
=> AuctionBids.GetAllLastBidByAuctionId(auctionId).ToDictionaryAsync(x => x.ProductAuctionMappingId);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,12 @@ public class AuctionDbTable: MgDbTableBase<Auction>
|
||||||
/// x.StartDateUtc <= utcNow && x.EndDateUtc >= utcNow
|
/// x.StartDateUtc <= utcNow && x.EndDateUtc >= utcNow
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IQueryable<Auction> GetAllCurrentAutoOpenAndClosedAuctions()
|
public IOrderedQueryable<Auction> GetAllCurrentAutoOpenAndClosedAuctions()
|
||||||
{
|
{
|
||||||
var utcNow = DateTime.UtcNow;
|
var utcNow = DateTime.UtcNow;
|
||||||
return Table.Where(x => x.AuctionType == AuctionType.AutomaticAll && x.Closed && x.StartDateUtc <= utcNow && x.EndDateUtc >= utcNow).OrderByDescending(x=>x.StartDateUtc);
|
|
||||||
|
return GetAllAuctions()
|
||||||
|
.Where(x => x.AuctionType == AuctionType.AutomaticAll && x.StartDateUtc <= utcNow && (!x.Closed || (x.Closed && x.EndDateUtc >= utcNow)))
|
||||||
|
.OrderByDescending(x => x.StartDateUtc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -24,7 +24,7 @@ public class ProductToAuctionDbTable : MgDbTableBase<ProductToAuctionMapping>
|
||||||
private static bool HasActiveAuctionStatus(AuctionStatus auctionStatus)
|
private static bool HasActiveAuctionStatus(AuctionStatus auctionStatus)
|
||||||
{
|
{
|
||||||
//TODO: erre a problémára kitalálni valamit! - J.
|
//TODO: erre a problémára kitalálni valamit! - J.
|
||||||
return auctionStatus == AuctionStatus.Active || auctionStatus == AuctionStatus.FirstWarning || auctionStatus == AuctionStatus.SecondWarning;
|
return auctionStatus == AuctionStatus.Active || auctionStatus == AuctionStatus.FirstWarning || auctionStatus == AuctionStatus.SecondWarning || auctionStatus == AuctionStatus.Pause;
|
||||||
//return auctionStatus.HasFlag(AuctionStatus.Active) || auctionStatus.HasFlag(AuctionStatus.FirstWarning) || auctionStatus.HasFlag(AuctionStatus.SecondWarning);
|
//return auctionStatus.HasFlag(AuctionStatus.Active) || auctionStatus.HasFlag(AuctionStatus.FirstWarning) || auctionStatus.HasFlag(AuctionStatus.SecondWarning);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,13 +36,13 @@ public class ProductToAuctionDbTable : MgDbTableBase<ProductToAuctionMapping>
|
||||||
public IQueryable<ProductToAuctionMapping> GetByProductId(int productId, bool activeProductOnly = false)
|
public IQueryable<ProductToAuctionMapping> GetByProductId(int productId, bool activeProductOnly = false)
|
||||||
{
|
{
|
||||||
return Table.Where(x => x.ProductId == productId &&
|
return Table.Where(x => x.ProductId == productId &&
|
||||||
(!activeProductOnly || x.AuctionStatus == AuctionStatus.Active || x.AuctionStatus == AuctionStatus.FirstWarning || x.AuctionStatus == AuctionStatus.SecondWarning /*HasActiveAuctionStatus(x.AuctionStatus)*/));
|
(!activeProductOnly || x.AuctionStatus == AuctionStatus.Active || x.AuctionStatus == AuctionStatus.FirstWarning || x.AuctionStatus == AuctionStatus.SecondWarning || x.AuctionStatus == AuctionStatus.Pause /*HasActiveAuctionStatus(x.AuctionStatus)*/));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQueryable<ProductToAuctionMapping> GetProductToAuctionsByAuctionId(int auctionId, bool activeProductOnly = false)
|
public IQueryable<ProductToAuctionMapping> GetProductToAuctionsByAuctionId(int auctionId, bool activeProductOnly = false)
|
||||||
{
|
{
|
||||||
return Table.Where(x => x.AuctionId == auctionId &&
|
return Table.Where(x => x.AuctionId == auctionId &&
|
||||||
(!activeProductOnly || x.AuctionStatus == AuctionStatus.Active || x.AuctionStatus == AuctionStatus.FirstWarning || x.AuctionStatus == AuctionStatus.SecondWarning /*HasActiveAuctionStatus(x.AuctionStatus)*/));
|
(!activeProductOnly || x.AuctionStatus == AuctionStatus.Active || x.AuctionStatus == AuctionStatus.FirstWarning || x.AuctionStatus == AuctionStatus.SecondWarning || x.AuctionStatus == AuctionStatus.Pause /*HasActiveAuctionStatus(x.AuctionStatus)*/));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQueryable<ProductToAuctionMapping> GetNotClosedItemsByAuctionId(int auctionId)
|
public IQueryable<ProductToAuctionMapping> GetNotClosedItemsByAuctionId(int auctionId)
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,9 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
||||||
|
|
||||||
public partial class Auction: MgEntityBase, IAuction
|
public class Auction: MgEntityBase, IAuction
|
||||||
{
|
{
|
||||||
|
public int StoreId { get; set; }
|
||||||
public string AuctionName { get; set; }
|
public string AuctionName { get; set; }
|
||||||
|
|
||||||
//[NotMapped]
|
//[NotMapped]
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
|
||||||
|
|
||||||
public interface IAuction : IAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt
|
public interface IAuction : IAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt
|
||||||
{
|
{
|
||||||
|
public int StoreId { get; set; }
|
||||||
public int? CategoryId { get; set; }
|
public int? CategoryId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
|
||||||
|
|
||||||
public interface IProductToAuctionMapping : IProductToAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt
|
public interface IProductToAuctionMapping : IProductToAuctionDtoBase, ITimeStampInfo //, ISoftRemoveEntityInt
|
||||||
{
|
{
|
||||||
|
public decimal MinimumPrice{ get; set; }
|
||||||
|
|
||||||
public int OrderId { get; set; }
|
public int OrderId { get; set; }
|
||||||
public int OrderItemId { get; set; }
|
public int OrderItemId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -30,6 +30,7 @@ public partial class ProductToAuctionMapping : MgEntityBase, IProductToAuctionMa
|
||||||
|
|
||||||
public decimal StartingPrice { get; set; }
|
public decimal StartingPrice { get; set; }
|
||||||
public decimal CurrentPrice { get; set; }
|
public decimal CurrentPrice { get; set; }
|
||||||
|
public decimal MinimumPrice { get; set; }
|
||||||
|
|
||||||
public int ProductAmount { get; set; } = 1;
|
public int ProductAmount { get; set; } = 1;
|
||||||
public int SortIndex { get; set; }
|
public int SortIndex { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -159,11 +159,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
var productToAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(revertAuctionBidRequest.ProductToAuctionId));
|
var productToAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(revertAuctionBidRequest.ProductToAuctionId));
|
||||||
if (productToAuction == null)
|
if (productToAuction == null)
|
||||||
{
|
{
|
||||||
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction == null);", null, customer);
|
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction == null); ptaId: {revertAuctionBidRequest.ProductToAuctionId}", null, customer);
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
await logger.InformationAsync($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
|
await logger.InformationAsync($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}; ptaId: {revertAuctionBidRequest.ProductToAuctionId}", null, customer);
|
||||||
|
|
||||||
var product = await auctionService.GetProductById(productToAuction.ProductId);
|
var product = await auctionService.GetProductById(productToAuction.ProductId);
|
||||||
if (product == null)
|
if (product == null)
|
||||||
|
|
@ -174,7 +174,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
|
|
||||||
if (productToAuction.AuctionStatus != AuctionStatus.Pause)
|
if (productToAuction.AuctionStatus != AuctionStatus.Pause)
|
||||||
{
|
{
|
||||||
logger.Warning($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction.AuctionStatus != AuctionStatus.Pause; AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
|
logger.Warning($"AuctionHub.HandleRevertAuctionBidRequest(); (productToAuction.AuctionStatus != AuctionStatus.Pause); AuctionStatus: {productToAuction.AuctionStatus}; ptaId: {revertAuctionBidRequest.ProductToAuctionId}", null, customer);
|
||||||
|
|
||||||
await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id, ResponseType.ToCaller);
|
await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id, ResponseType.ToCaller);
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
|
|
@ -194,7 +194,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer);
|
logger.Error($"AuctionHub.HandleRevertAuctionBidRequest(); ptaId: {revertAuctionBidRequest.ProductToAuctionId}; Exception: {ex.Message}", ex, customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
|
|
@ -211,7 +211,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await logger.InformationAsync($"AuctionHub.HandleBidRequestAsync(); Bid received; Auction: {bidRequestMessage.AuctionId}; ProductToAuction: {bidRequestMessage.ProductAuctionMappingId}; Product: {bidRequestMessage.ProductId}; Bid: {bidRequestMessage.BidPrice}; Customer: {bidRequestMessage.CustomerId}", null, customer);
|
await logger.InformationAsync($"AuctionHub.HandleBidRequestAsync(); Bid received; Auction: {bidRequestMessage.AuctionId}; ptaId: {bidRequestMessage.ProductAuctionMappingId}; Product: {bidRequestMessage.ProductId}; Bid: {bidRequestMessage.BidPrice}; Customer: {bidRequestMessage.CustomerId}", null, customer);
|
||||||
|
|
||||||
var auction = await auctionService.GetAuctionDtoByIdAsync(bidRequestMessage.AuctionId, false, false);
|
var auction = await auctionService.GetAuctionDtoByIdAsync(bidRequestMessage.AuctionId, false, false);
|
||||||
if (auction == null || auction.Closed)
|
if (auction == null || auction.Closed)
|
||||||
|
|
@ -231,7 +231,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
var activeProductAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(bidRequestMessage.ProductAuctionMappingId));
|
var activeProductAuction = (await auctionService.GetProductToAuctionMappingByIdAsync(bidRequestMessage.ProductAuctionMappingId));
|
||||||
if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
|
if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
|
||||||
{
|
{
|
||||||
logger.Warning($"AuctionHub.HandleBidRequestAsync(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
|
logger.Warning($"AuctionHub.HandleBidRequestAsync(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); ptaId: {bidRequestMessage.ProductAuctionMappingId}; AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
|
||||||
|
|
||||||
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
|
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
|
|
@ -250,6 +250,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
}
|
}
|
||||||
|
|
||||||
var auctionBid = bidRequestMessage.CreateMainEntity();
|
var auctionBid = bidRequestMessage.CreateMainEntity();
|
||||||
|
auctionBid.AuctionId = auction.Id;
|
||||||
auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
|
auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
|
||||||
|
|
||||||
await auctionService.InsertBidAsync(auctionBid);
|
await auctionService.InsertBidAsync(auctionBid);
|
||||||
|
|
@ -268,7 +269,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.Error($"AuctionHub.HandleBidRequestAsync(); Exception: {ex.Message}", ex, customer);
|
logger.Error($"AuctionHub.HandleBidRequestAsync(); ptaId: {bidRequestMessage.ProductAuctionMappingId}; Exception: {ex.Message}", ex, customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
|
|
@ -286,7 +287,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//var responseType = ResponseType.ToAllClients;
|
//var responseType = ResponseType.ToAllClients;
|
||||||
await logger.InformationAsync($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
|
await logger.InformationAsync($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); ptaId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
|
||||||
|
|
||||||
var (auction, productToAuction, responseType) = await auctionService.GetAndUpdateProductToAuctionStatusIfValidAsync(auctionProductStatusRequest.ProductToAuctionId, auctionProductStatusRequest.AuctionStatus, customer);
|
var (auction, productToAuction, responseType) = await auctionService.GetAndUpdateProductToAuctionStatusIfValidAsync(auctionProductStatusRequest.ProductToAuctionId, auctionProductStatusRequest.AuctionStatus, customer);
|
||||||
if (auction == null || productToAuction == null || responseType == ResponseType.None) return ResponseType.None;
|
if (auction == null || productToAuction == null || responseType == ResponseType.None) return ResponseType.None;
|
||||||
|
|
@ -301,7 +302,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); Exception: {ex.Message}", ex, customer);
|
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); ptaId: {auctionProductStatusRequest.ProductToAuctionId}; Exception: {ex.Message}", ex, customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseType.Error;
|
return ResponseType.Error;
|
||||||
|
|
@ -423,7 +424,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
//activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
|
//activeProductAuction.StartingPrice = product.OldPrice; //TODO: ez biztosan kezelve van mikor összerendeljük? - J.
|
||||||
productToAuction.CurrentPrice = bidPrice;
|
productToAuction.CurrentPrice = bidPrice;
|
||||||
productToAuction.WinnerCustomerId = lastBidCustomerId;
|
productToAuction.WinnerCustomerId = lastBidCustomerId;
|
||||||
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
|
await auctionService.UpdateProductToAuctionAsync(productToAuction);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,20 @@ using Nop.Core.Domain.Customers;
|
||||||
using Nop.Data;
|
using Nop.Data;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
|
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
|
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
|
||||||
using Nop.Services.Customers;
|
using Nop.Services.Customers;
|
||||||
using Nop.Services.Logging;
|
using Nop.Services.Logging;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
|
||||||
|
|
||||||
public class AuctionBackgroundService : MgBackgroundServiceBase
|
public class AuctionBackgroundService : MgBackgroundServiceBase
|
||||||
{
|
{
|
||||||
|
private const int WARNING_STATUS_INTERVAL_SECOND = 15;
|
||||||
|
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IHubContext<AuctionHub> _auctionHubContext;
|
private readonly IHubContext<AuctionHub> _auctionHubContext;
|
||||||
private readonly ILockService _lockService;
|
private readonly ILockService _lockService;
|
||||||
|
|
@ -40,7 +44,7 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
|
||||||
|
|
||||||
protected override async Task OnExecuteAsync()
|
protected override async Task OnExecuteAsync()
|
||||||
{
|
{
|
||||||
await Task.Delay(15000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J.
|
await Task.Delay(WARNING_STATUS_INTERVAL_SECOND * 1000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J.
|
||||||
|
|
||||||
if (_auctionSystemCustomer == null) return;
|
if (_auctionSystemCustomer == null) return;
|
||||||
|
|
||||||
|
|
@ -50,49 +54,115 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
|
||||||
{
|
{
|
||||||
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Enter lock;", null, _auctionSystemCustomer);
|
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Enter lock;", null, _auctionSystemCustomer);
|
||||||
|
|
||||||
var auctions = await _auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync();
|
var currentAutomaticAuctions = await _auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync();
|
||||||
if (auctions.Count > 0)
|
if (currentAutomaticAuctions.Count > 0)
|
||||||
{
|
{
|
||||||
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); auctions.Count > 0; count: {auctions.Count}; names: {string.Join("; ", auctions.Select(x => x.AuctionName))}", null, _auctionSystemCustomer);
|
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); currentAutomaticAuctions.Count > 0; count: {currentAutomaticAuctions.Count}; names: {string.Join("; ", currentAutomaticAuctions.Select(x => x.AuctionName))}", null, _auctionSystemCustomer);
|
||||||
|
|
||||||
var statusChangedMessageWrapper = new MessageWrapper
|
var statusChangedMessageWrapper = new MessageWrapper
|
||||||
{
|
{
|
||||||
MessageType = nameof(ProductToAuctionStatusNotification),
|
MessageType = nameof(ProductToAuctionStatusNotification),
|
||||||
SenderId = _auctionSystemCustomer.Id,
|
SenderId = _auctionSystemCustomer.Id, //TODO: -1; - J.
|
||||||
ResponseType = ResponseType.ToAllClients
|
ResponseType = ResponseType.ToAllClients
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (var auction in auctions)
|
foreach (var automaticAuction in currentAutomaticAuctions)
|
||||||
{
|
{
|
||||||
auction.Closed = false;
|
await CheckAndUpdateProductToAuctionsStatusAsync(automaticAuction, statusChangedMessageWrapper);
|
||||||
await _auctionService.UpdateAuctionAsync(auction);
|
await OpenOrCloseAutomaticAuctionsAsync(automaticAuction, statusChangedMessageWrapper);
|
||||||
|
|
||||||
var auctionDto = new AuctionDto(auction);
|
|
||||||
var productToAuctions = (await _auctionService.GetProductToAuctionsByAuctionIdAsync(auction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList();
|
|
||||||
|
|
||||||
if (productToAuctions.Count == 0) continue;
|
|
||||||
|
|
||||||
var allLastBidbyPtaId = await _auctionService.GetAllLastBidDictionaryByAuctionIdAsync(auction.Id);
|
|
||||||
|
|
||||||
foreach (var productToAuction in productToAuctions)
|
|
||||||
{
|
|
||||||
allLastBidbyPtaId.TryGetValue(productToAuction.Id, out var lastBid);
|
|
||||||
|
|
||||||
var responseType = await _auctionService.UpdateProductToAuctionStatusIfValidAsync(AuctionStatus.Active, auction, productToAuction, _auctionSystemCustomer, false);
|
|
||||||
|
|
||||||
if (responseType != ResponseType.None)
|
|
||||||
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 _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);
|
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Exit lock;", null, _auctionSystemCustomer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task CheckAndUpdateProductToAuctionsStatusAsync(Auction automaticAuction, MessageWrapper statusChangedMessageWrapper)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (automaticAuction.Closed || automaticAuction.EndDateUtc >= DateTime.UtcNow.AddSeconds(2 * WARNING_STATUS_INTERVAL_SECOND)) return;
|
||||||
|
|
||||||
|
var activeProductToAuctions = await _auctionService.GetProductToAuctionsByAuctionIdAsync(automaticAuction.Id, true);
|
||||||
|
if (activeProductToAuctions.Count == 0) return;
|
||||||
|
|
||||||
|
var auctionDto = new AuctionDto(automaticAuction);
|
||||||
|
var changedProductToAuctions = new List<ProductToAuctionMapping>(activeProductToAuctions.Count);
|
||||||
|
|
||||||
|
foreach (var activeProductToAuction in activeProductToAuctions)
|
||||||
|
{
|
||||||
|
var newPtaStatus = activeProductToAuction.AuctionStatus switch
|
||||||
|
{
|
||||||
|
AuctionStatus.Active => AuctionStatus.FirstWarning,
|
||||||
|
AuctionStatus.FirstWarning => AuctionStatus.SecondWarning,
|
||||||
|
AuctionStatus.SecondWarning => AuctionStatus.Sold,
|
||||||
|
_ => AuctionStatus.None
|
||||||
|
};
|
||||||
|
|
||||||
|
if (newPtaStatus == AuctionStatus.None) continue;
|
||||||
|
|
||||||
|
var responseType = await _auctionService.UpdateProductToAuctionStatusIfValidAsync(newPtaStatus, automaticAuction, activeProductToAuction, _auctionSystemCustomer, false);
|
||||||
|
if (responseType == ResponseType.None) continue;
|
||||||
|
|
||||||
|
changedProductToAuctions.Add(activeProductToAuction);
|
||||||
|
auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(activeProductToAuction));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changedProductToAuctions.Count == 0) return;
|
||||||
|
|
||||||
|
await _auctionService.UpdateProductToAuctionAsync(changedProductToAuctions);
|
||||||
|
|
||||||
|
statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, "EMPTY").ToJson();
|
||||||
|
await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await _logger.ErrorAsync($"AuctionBackgroundService.CheckAndUpdateProductToAuctionsStatusAsync(); auctionId: {automaticAuction.Id}; auctionName: {automaticAuction.AuctionName}", ex, _auctionSystemCustomer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task OpenOrCloseAutomaticAuctionsAsync(Auction automaticAuction, MessageWrapper statusChangedMessageWrapper)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!automaticAuction.Closed)
|
||||||
|
{
|
||||||
|
var allItemsFinished = (await _auctionService.GetProductToAuctionsByAuctionIdAsync(automaticAuction.Id, false)).All(x => x.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold);
|
||||||
|
|
||||||
|
if (!allItemsFinished) return;
|
||||||
|
|
||||||
|
automaticAuction.Closed = true;
|
||||||
|
await _auctionService.UpdateAuctionAsync(automaticAuction);
|
||||||
|
|
||||||
|
//TODO: send message... - J.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var productToAuctions = (await _auctionService.GetProductToAuctionsByAuctionIdAsync(automaticAuction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList();
|
||||||
|
if (productToAuctions.Count == 0) return;
|
||||||
|
|
||||||
|
automaticAuction.Closed = false;
|
||||||
|
var auctionDto = new AuctionDto(automaticAuction);
|
||||||
|
|
||||||
|
foreach (var productToAuction in productToAuctions)
|
||||||
|
{
|
||||||
|
var responseType = await _auctionService.UpdateProductToAuctionStatusIfValidAsync(AuctionStatus.Active, automaticAuction, productToAuction, _auctionSystemCustomer, false);
|
||||||
|
if (responseType == ResponseType.None) continue;
|
||||||
|
|
||||||
|
auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(productToAuction));
|
||||||
|
}
|
||||||
|
|
||||||
|
await _auctionService.UpdateAuctionAsync(automaticAuction);
|
||||||
|
await _auctionService.UpdateProductToAuctionAsync(productToAuctions);
|
||||||
|
|
||||||
|
statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {automaticAuction.AuctionName}").ToJson();
|
||||||
|
statusChangedMessageWrapper.HideToaster = true;
|
||||||
|
await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await _logger.ErrorAsync($"AuctionBackgroundService.OpenOrCloseAutomaticAuctionsAsync(); auctionId: {automaticAuction.Id}; name: {automaticAuction.AuctionName}", ex, _auctionSystemCustomer);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,8 +70,9 @@ public class AuctionService(
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AuctionStatus.Sold:
|
case AuctionStatus.Sold:
|
||||||
|
case AuctionStatus.NotSold:
|
||||||
var lastAuctionBid = await GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
var lastAuctionBid = await GetLastAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
|
||||||
if (lastAuctionBid == null) productToAuction.AuctionStatus = AuctionStatus.NotSold;
|
if (lastAuctionBid == null || lastAuctionBid.BidPrice < productToAuction.MinimumPrice) productToAuction.AuctionStatus = AuctionStatus.NotSold;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
productToAuction.AuctionStatus = AuctionStatus.Sold;
|
productToAuction.AuctionStatus = AuctionStatus.Sold;
|
||||||
|
|
@ -95,12 +96,11 @@ public class AuctionService(
|
||||||
|
|
||||||
default:
|
default:
|
||||||
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); AuctionStatus not found; (newStatus == {newProductToAuctionStatus})", null, customer);
|
logger.Error($"AuctionHub.UpdateProductToAuctionStatusIfValidAsync(); AuctionStatus not found; (newStatus == {newProductToAuctionStatus})", null, customer);
|
||||||
|
|
||||||
return ResponseType.ToCaller;
|
return ResponseType.ToCaller;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updateInDatabase)
|
if (updateInDatabase)
|
||||||
await UpdateProductToAuctionMappingAsync(productToAuction);
|
await UpdateProductToAuctionAsync(productToAuction);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseType.ToAllClients;
|
return ResponseType.ToAllClients;
|
||||||
|
|
@ -673,12 +673,12 @@ public class AuctionService(
|
||||||
return await ctx.ProductToAuctions.GetByIdAsync(productToAuctionMappingId);
|
return await ctx.ProductToAuctions.GetByIdAsync(productToAuctionMappingId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping)
|
public async Task UpdateProductToAuctionAsync(ProductToAuctionMapping productToAuctionMapping)
|
||||||
{
|
{
|
||||||
await ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping);
|
await ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateProductToAuctionMappingAsync(IList<ProductToAuctionMapping> productToAuctionMappings)
|
public async Task UpdateProductToAuctionAsync(IList<ProductToAuctionMapping> productToAuctionMappings)
|
||||||
{
|
{
|
||||||
await ctx.ProductToAuctions.UpdateAsync(productToAuctionMappings);
|
await ctx.ProductToAuctions.UpdateAsync(productToAuctionMappings);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,8 +73,8 @@ public interface IAuctionService
|
||||||
|
|
||||||
Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId, bool activeProductOnly);
|
Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId, bool activeProductOnly);
|
||||||
Task<ProductToAuctionMapping> GetProductToAuctionMappingByIdAsync(int productToAuctionMappingId);
|
Task<ProductToAuctionMapping> GetProductToAuctionMappingByIdAsync(int productToAuctionMappingId);
|
||||||
Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping);
|
Task UpdateProductToAuctionAsync(ProductToAuctionMapping productToAuctionMapping);
|
||||||
Task UpdateProductToAuctionMappingAsync(IList<ProductToAuctionMapping> productToAuctionMappings);
|
Task UpdateProductToAuctionAsync(IList<ProductToAuctionMapping> productToAuctionMappings);
|
||||||
|
|
||||||
Task<Order> CreateOrderForWinnerAsync(ProductToAuctionMapping productToAuctionMapping);
|
Task<Order> CreateOrderForWinnerAsync(ProductToAuctionMapping productToAuctionMapping);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue