Commit 01d73b8a authored by gdlcf88's avatar gdlcf88

Added TenantId to Etos

parent c6a81c9a
...@@ -8,6 +8,8 @@ namespace EasyAbp.EShop.Orders.Orders ...@@ -8,6 +8,8 @@ namespace EasyAbp.EShop.Orders.Orders
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public Guid? TenantId { get; set; }
public Guid StoreId { get; set; } public Guid StoreId { get; set; }
public Guid CustomerUserId { get; set; } public Guid CustomerUserId { get; set; }
......
using System.Threading.Tasks; using System.Threading.Tasks;
using EasyAbp.EShop.Products.Products; using EasyAbp.EShop.Products.Products;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing; using Volo.Abp.Timing;
using Volo.Abp.Uow; using Volo.Abp.Uow;
...@@ -9,35 +10,41 @@ namespace EasyAbp.EShop.Orders.Orders ...@@ -9,35 +10,41 @@ namespace EasyAbp.EShop.Orders.Orders
public class OrderProductInventoryReductionEventHandler : IOrderProductInventoryReductionEventHandler, ITransientDependency public class OrderProductInventoryReductionEventHandler : IOrderProductInventoryReductionEventHandler, ITransientDependency
{ {
private readonly IClock _clock; private readonly IClock _clock;
private readonly ICurrentTenant _currentTenant;
private readonly IOrderRepository _orderRepository; private readonly IOrderRepository _orderRepository;
public OrderProductInventoryReductionEventHandler( public OrderProductInventoryReductionEventHandler(
IClock clock, IClock clock,
ICurrentTenant currentTenant,
IOrderRepository orderRepository) IOrderRepository orderRepository)
{ {
_clock = clock; _clock = clock;
_currentTenant = currentTenant;
_orderRepository = orderRepository; _orderRepository = orderRepository;
} }
[UnitOfWork(true)] [UnitOfWork(true)]
public virtual async Task HandleEventAsync(ProductInventoryReductionAfterOrderPlacedResultEto eventData) public virtual async Task HandleEventAsync(ProductInventoryReductionAfterOrderPlacedResultEto eventData)
{ {
var order = await _orderRepository.GetAsync(eventData.OrderId); using (_currentTenant.Change(eventData.TenantId))
if (order.OrderStatus != OrderStatus.Pending || order.ReducedInventoryAfterPlacingTime.HasValue)
{ {
return; var order = await _orderRepository.GetAsync(eventData.OrderId);
}
if (!eventData.IsSuccess) if (order.OrderStatus != OrderStatus.Pending || order.ReducedInventoryAfterPlacingTime.HasValue)
{ {
// Todo: Cancel order. return;
return; }
}
if (!eventData.IsSuccess)
{
// Todo: Cancel order.
return;
}
order.SetReducedInventoryAfterPaymentTime(_clock.Now); order.SetReducedInventoryAfterPaymentTime(_clock.Now);
await _orderRepository.UpdateAsync(order, true); await _orderRepository.UpdateAsync(order, true);
}
} }
} }
} }
\ No newline at end of file
...@@ -5,6 +5,8 @@ namespace EasyAbp.EShop.Products.Products ...@@ -5,6 +5,8 @@ namespace EasyAbp.EShop.Products.Products
[Serializable] [Serializable]
public class ProductInventoryReductionAfterOrderPlacedResultEto public class ProductInventoryReductionAfterOrderPlacedResultEto
{ {
public Guid? TenantId { get; set; }
public Guid OrderId { get; set; } public Guid OrderId { get; set; }
public bool IsSuccess { get; set; } public bool IsSuccess { get; set; }
......
...@@ -4,23 +4,27 @@ using EasyAbp.EShop.Orders.Orders; ...@@ -4,23 +4,27 @@ using EasyAbp.EShop.Orders.Orders;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace EasyAbp.EShop.Products.Products namespace EasyAbp.EShop.Products.Products
{ {
public class OrderCreatedEventHandler : IOrderCreatedEventHandler, ITransientDependency public class OrderCreatedEventHandler : IOrderCreatedEventHandler, ITransientDependency
{ {
private readonly ICurrentTenant _currentTenant;
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IDistributedEventBus _distributedEventBus; private readonly IDistributedEventBus _distributedEventBus;
private readonly IProductRepository _productRepository; private readonly IProductRepository _productRepository;
private readonly IProductManager _productManager; private readonly IProductManager _productManager;
public OrderCreatedEventHandler( public OrderCreatedEventHandler(
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkManager unitOfWorkManager,
IDistributedEventBus distributedEventBus, IDistributedEventBus distributedEventBus,
IProductRepository productRepository, IProductRepository productRepository,
IProductManager productManager) IProductManager productManager)
{ {
_currentTenant = currentTenant;
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
_distributedEventBus = distributedEventBus; _distributedEventBus = distributedEventBus;
_productRepository = productRepository; _productRepository = productRepository;
...@@ -29,43 +33,46 @@ namespace EasyAbp.EShop.Products.Products ...@@ -29,43 +33,46 @@ namespace EasyAbp.EShop.Products.Products
public virtual async Task HandleEventAsync(EntityCreatedEto<OrderEto> eventData) public virtual async Task HandleEventAsync(EntityCreatedEto<OrderEto> eventData)
{ {
using var uow = _unitOfWorkManager.Begin(true, true); using (_currentTenant.Change(eventData.Entity.TenantId))
foreach (var orderLine in eventData.Entity.OrderLines)
{ {
var product = await _productRepository.FindAsync(orderLine.ProductId); using var uow = _unitOfWorkManager.Begin(true, true);
foreach (var orderLine in eventData.Entity.OrderLines)
{
var product = await _productRepository.FindAsync(orderLine.ProductId);
var productSku = product?.ProductSkus.FirstOrDefault(sku => sku.Id == orderLine.ProductSkuId); var productSku = product?.ProductSkus.FirstOrDefault(sku => sku.Id == orderLine.ProductSkuId);
if (productSku == null) if (productSku == null)
{ {
await uow.RollbackAsync(); await uow.RollbackAsync();
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{OrderId = eventData.Entity.Id, IsSuccess = false}); {OrderId = eventData.Entity.Id, IsSuccess = false});
return; return;
} }
if (product.InventoryStrategy != InventoryStrategy.ReduceAfterPlacing) if (product.InventoryStrategy != InventoryStrategy.ReduceAfterPlacing)
{ {
await uow.RollbackAsync(); await uow.RollbackAsync();
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{OrderId = eventData.Entity.Id, IsSuccess = false}); {OrderId = eventData.Entity.Id, IsSuccess = false});
return; return;
} }
if (!await _productManager.TryReduceInventoryAsync(product, productSku, eventData.Entity.StoreId, if (!await _productManager.TryReduceInventoryAsync(product, productSku, eventData.Entity.StoreId,
orderLine.Quantity)) orderLine.Quantity))
{ {
await uow.RollbackAsync(); await uow.RollbackAsync();
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{OrderId = eventData.Entity.Id, IsSuccess = false}); {OrderId = eventData.Entity.Id, IsSuccess = false});
return; return;
}
} }
}
await uow.CompleteAsync(); await uow.CompleteAsync();
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{OrderId = eventData.Entity.Id, IsSuccess = true}); {OrderId = eventData.Entity.Id, IsSuccess = true});
}
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment