Commit a16c670b authored by gdlcf88's avatar gdlcf88

Improved OrderCreatedEventHandler

parent f38115e4
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
......
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
......
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
......
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
......
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders;
using Volo.Abp.DependencyInjection;
......@@ -34,39 +36,42 @@ namespace EasyAbp.EShop.Products.Products
[UnitOfWork(true)]
public virtual async Task HandleEventAsync(EntityCreatedEto<OrderEto> eventData)
{
var uow = _unitOfWorkManager.Current;
using (_currentTenant.Change(eventData.Entity.TenantId))
{
var models = new List<ReduceInventoryModel>();
foreach (var orderLine in eventData.Entity.OrderLines)
{
var product = await _productRepository.FindAsync(orderLine.ProductId);
var productSku = product?.ProductSkus.FirstOrDefault(sku => sku.Id == orderLine.ProductSkuId);
if (productSku == null)
if (productSku == null || product.InventoryStrategy != InventoryStrategy.ReduceAfterPlacing)
{
await uow.RollbackAsync();
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{OrderId = eventData.Entity.Id, IsSuccess = false});
return;
continue;
}
if (product.InventoryStrategy != InventoryStrategy.ReduceAfterPlacing)
if (!await _productManager.IsInventorySufficientAsync(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))
models.Add(new ReduceInventoryModel
{
await uow.RollbackAsync();
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
{OrderId = eventData.Entity.Id, IsSuccess = false});
return;
}
Product = product,
ProductSku = productSku,
StoreId = eventData.Entity.StoreId,
Quantity = orderLine.Quantity
});
}
foreach (var model in models)
{
await _productManager.TryReduceInventoryAsync(model.Product, model.ProductSku, model.StoreId,
model.Quantity);
}
await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPlacedResultEto
......@@ -74,4 +79,15 @@ namespace EasyAbp.EShop.Products.Products
}
}
}
internal class ReduceInventoryModel
{
public Product Product { get; set; }
public ProductSku ProductSku { get; set; }
public Guid StoreId { get; set; }
public int Quantity { get; set; }
}
}
\ No newline at end of file
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
......
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