From 01d73b8a2c7fdbb8635fa288685135d4b6e5fe01 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Thu, 7 May 2020 14:55:48 +0800 Subject: [PATCH] Added TenantId to Etos --- .../EasyAbp/EShop/Orders/Orders/OrderEto.cs | 2 + ...erProductInventoryReductionEventHandler.cs | 31 +++++---- ...ntoryReductionAfterOrderPlacedResultEto.cs | 2 + .../Products/OrderCreatedEventHandler.cs | 67 ++++++++++--------- 4 files changed, 60 insertions(+), 42 deletions(-) diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain.Shared/EasyAbp/EShop/Orders/Orders/OrderEto.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain.Shared/EasyAbp/EShop/Orders/Orders/OrderEto.cs index a0f56d71..67801bee 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain.Shared/EasyAbp/EShop/Orders/Orders/OrderEto.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain.Shared/EasyAbp/EShop/Orders/Orders/OrderEto.cs @@ -8,6 +8,8 @@ namespace EasyAbp.EShop.Orders.Orders { public Guid Id { get; set; } + public Guid? TenantId { get; set; } + public Guid StoreId { get; set; } public Guid CustomerUserId { get; set; } diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderProductInventoryReductionEventHandler.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderProductInventoryReductionEventHandler.cs index 703dad8e..349771af 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderProductInventoryReductionEventHandler.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderProductInventoryReductionEventHandler.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using EasyAbp.EShop.Products.Products; using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; using Volo.Abp.Timing; using Volo.Abp.Uow; @@ -9,35 +10,41 @@ namespace EasyAbp.EShop.Orders.Orders public class OrderProductInventoryReductionEventHandler : IOrderProductInventoryReductionEventHandler, ITransientDependency { private readonly IClock _clock; + private readonly ICurrentTenant _currentTenant; private readonly IOrderRepository _orderRepository; public OrderProductInventoryReductionEventHandler( IClock clock, + ICurrentTenant currentTenant, IOrderRepository orderRepository) { _clock = clock; + _currentTenant = currentTenant; _orderRepository = orderRepository; } [UnitOfWork(true)] public virtual async Task HandleEventAsync(ProductInventoryReductionAfterOrderPlacedResultEto eventData) { - var order = await _orderRepository.GetAsync(eventData.OrderId); - - if (order.OrderStatus != OrderStatus.Pending || order.ReducedInventoryAfterPlacingTime.HasValue) + using (_currentTenant.Change(eventData.TenantId)) { - return; - } + var order = await _orderRepository.GetAsync(eventData.OrderId); - if (!eventData.IsSuccess) - { - // Todo: Cancel order. - return; - } + if (order.OrderStatus != OrderStatus.Pending || order.ReducedInventoryAfterPlacingTime.HasValue) + { + 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 diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/ProductInventoryReductionAfterOrderPlacedResultEto.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/ProductInventoryReductionAfterOrderPlacedResultEto.cs index 998034ee..f057e3bd 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/ProductInventoryReductionAfterOrderPlacedResultEto.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/ProductInventoryReductionAfterOrderPlacedResultEto.cs @@ -5,6 +5,8 @@ namespace EasyAbp.EShop.Products.Products [Serializable] public class ProductInventoryReductionAfterOrderPlacedResultEto { + public Guid? TenantId { get; set; } + public Guid OrderId { get; set; } public bool IsSuccess { get; set; } diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs index 96a17ba1..4b49bf10 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs @@ -4,23 +4,27 @@ using EasyAbp.EShop.Orders.Orders; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; namespace EasyAbp.EShop.Products.Products { public class OrderCreatedEventHandler : IOrderCreatedEventHandler, ITransientDependency { + private readonly ICurrentTenant _currentTenant; private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IDistributedEventBus _distributedEventBus; private readonly IProductRepository _productRepository; private readonly IProductManager _productManager; public OrderCreatedEventHandler( + ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, IDistributedEventBus distributedEventBus, IProductRepository productRepository, IProductManager productManager) { + _currentTenant = currentTenant; _unitOfWorkManager = unitOfWorkManager; _distributedEventBus = distributedEventBus; _productRepository = productRepository; @@ -29,43 +33,46 @@ namespace EasyAbp.EShop.Products.Products public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { - using var uow = _unitOfWorkManager.Begin(true, true); - - foreach (var orderLine in eventData.Entity.OrderLines) + using (_currentTenant.Change(eventData.Entity.TenantId)) { - 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) - { - await uow.RollbackAsync(); - await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto - {OrderId = eventData.Entity.Id, IsSuccess = false}); - return; - } + if (productSku == null) + { + await uow.RollbackAsync(); + await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto + {OrderId = eventData.Entity.Id, IsSuccess = false}); + return; + } - if (product.InventoryStrategy != InventoryStrategy.ReduceAfterPlacing) - { - await uow.RollbackAsync(); - await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto - {OrderId = eventData.Entity.Id, IsSuccess = false}); - return; - } + if (product.InventoryStrategy != InventoryStrategy.ReduceAfterPlacing) + { + await uow.RollbackAsync(); + await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto + {OrderId = eventData.Entity.Id, IsSuccess = false}); + return; + } - if (!await _productManager.TryReduceInventoryAsync(product, productSku, eventData.Entity.StoreId, - orderLine.Quantity)) - { - await uow.RollbackAsync(); - await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto - {OrderId = eventData.Entity.Id, IsSuccess = false}); - return; + if (!await _productManager.TryReduceInventoryAsync(product, productSku, eventData.Entity.StoreId, + orderLine.Quantity)) + { + await uow.RollbackAsync(); + await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto + {OrderId = eventData.Entity.Id, IsSuccess = false}); + return; + } } - } - await uow.CompleteAsync(); - await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto - {OrderId = eventData.Entity.Id, IsSuccess = true}); + await uow.CompleteAsync(); + await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto + {OrderId = eventData.Entity.Id, IsSuccess = true}); + } } } } \ No newline at end of file