diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/DefaultProductInventoryProvider.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/DefaultProductInventoryProvider.cs index cf2cb961..e22a1881 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/DefaultProductInventoryProvider.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/DefaultProductInventoryProvider.cs @@ -5,6 +5,8 @@ using System.Threading.Tasks; using EasyAbp.EShop.Products.ProductInventories; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Guids; +using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; namespace EasyAbp.EShop.Products.Products @@ -12,15 +14,21 @@ namespace EasyAbp.EShop.Products.Products public class DefaultProductInventoryProvider : IProductInventoryProvider, ITransientDependency { // Todo: should use IProductInventoryStore. + private readonly IGuidGenerator _guidGenerator; + private readonly ICurrentTenant _currentTenant; private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IDistributedEventBus _distributedEventBus; private readonly IProductInventoryRepository _productInventoryRepository; public DefaultProductInventoryProvider( + IGuidGenerator guidGenerator, + ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, IDistributedEventBus distributedEventBus, IProductInventoryRepository productInventoryRepository) { + _guidGenerator = guidGenerator; + _currentTenant = currentTenant; _unitOfWorkManager = unitOfWorkManager; _distributedEventBus = distributedEventBus; _productInventoryRepository = productInventoryRepository; @@ -49,7 +57,7 @@ namespace EasyAbp.EShop.Products.Products [UnitOfWork(true)] public virtual async Task TryIncreaseInventoryAsync(Product product, ProductSku productSku, int quantity, bool decreaseSold) { - var productInventory = await _productInventoryRepository.GetAsync(x => x.ProductSkuId == productSku.Id); + var productInventory = await GetOrCreateProductInventoryAsync(product.Id, productSku.Id); return await TryIncreaseInventoryAsync(product, productInventory, quantity, decreaseSold); } @@ -57,11 +65,29 @@ namespace EasyAbp.EShop.Products.Products [UnitOfWork(true)] public virtual async Task TryReduceInventoryAsync(Product product, ProductSku productSku, int quantity, bool increaseSold) { - var productInventory = await _productInventoryRepository.GetAsync(x => x.ProductSkuId == productSku.Id); + var productInventory = await GetOrCreateProductInventoryAsync(product.Id, productSku.Id); return await TryReduceInventoryAsync(product, productInventory, quantity, increaseSold); } + [UnitOfWork] + protected virtual async Task GetOrCreateProductInventoryAsync(Guid productId, Guid productSkuId) + { + var productInventory = + await _productInventoryRepository.FindAsync(x => + x.ProductId == productId && x.ProductSkuId == productSkuId); + + if (productInventory is null) + { + productInventory = new ProductInventory(_guidGenerator.Create(), _currentTenant.Id, productId, + productSkuId, 0, 0); + + await _productInventoryRepository.InsertAsync(productInventory, true); + } + + return productInventory; + } + [UnitOfWork(true)] public virtual async Task TryIncreaseInventoryAsync(Product product, ProductInventory productInventory, int quantity, bool decreaseSold) {