Commit cc89701d authored by gdlcf88's avatar gdlcf88

Close #26: Use IProductInventoryProvider to get real inventories

parent 1ddc25c9
...@@ -25,6 +25,7 @@ namespace EasyAbp.EShop.Products.Products ...@@ -25,6 +25,7 @@ namespace EasyAbp.EShop.Products.Products
protected override string GetListPolicyName { get; set; } = null; protected override string GetListPolicyName { get; set; } = null;
private readonly IProductManager _productManager; private readonly IProductManager _productManager;
private readonly IProductInventoryProvider _productInventoryProvider;
private readonly IAttributeOptionIdsSerializer _attributeOptionIdsSerializer; private readonly IAttributeOptionIdsSerializer _attributeOptionIdsSerializer;
private readonly IProductStoreRepository _productStoreRepository; private readonly IProductStoreRepository _productStoreRepository;
private readonly IProductCategoryRepository _productCategoryRepository; private readonly IProductCategoryRepository _productCategoryRepository;
...@@ -32,12 +33,14 @@ namespace EasyAbp.EShop.Products.Products ...@@ -32,12 +33,14 @@ namespace EasyAbp.EShop.Products.Products
public ProductAppService( public ProductAppService(
IProductManager productManager, IProductManager productManager,
IProductInventoryProvider productInventoryProvider,
IAttributeOptionIdsSerializer attributeOptionIdsSerializer, IAttributeOptionIdsSerializer attributeOptionIdsSerializer,
IProductStoreRepository productStoreRepository, IProductStoreRepository productStoreRepository,
IProductCategoryRepository productCategoryRepository, IProductCategoryRepository productCategoryRepository,
IProductRepository repository) : base(repository) IProductRepository repository) : base(repository)
{ {
_productManager = productManager; _productManager = productManager;
_productInventoryProvider = productInventoryProvider;
_attributeOptionIdsSerializer = attributeOptionIdsSerializer; _attributeOptionIdsSerializer = attributeOptionIdsSerializer;
_productStoreRepository = productStoreRepository; _productStoreRepository = productStoreRepository;
_productCategoryRepository = productCategoryRepository; _productCategoryRepository = productCategoryRepository;
...@@ -211,21 +214,37 @@ namespace EasyAbp.EShop.Products.Products ...@@ -211,21 +214,37 @@ namespace EasyAbp.EShop.Products.Products
public virtual async Task<ProductDto> GetAsync(Guid id, Guid storeId) public virtual async Task<ProductDto> GetAsync(Guid id, Guid storeId)
{ {
var dto = await base.GetAsync(id); await CheckGetPolicyAsync();
var product = await GetEntityByIdAsync(id);
var dto = MapToGetOutputDto(product);
if (!dto.IsPublished) if (!dto.IsPublished)
{ {
await CheckStoreIsProductOwnerAsync(id, storeId); await CheckStoreIsProductOwnerAsync(id, storeId);
} }
// Todo: get real inventory. await LoadRealInventoriesAsync(product, dto, storeId);
dto.CategoryIds = (await _productCategoryRepository.GetListByProductIdAsync(dto.Id)) dto.CategoryIds = (await _productCategoryRepository.GetListByProductIdAsync(dto.Id))
.Select(x => x.CategoryId).ToList(); .Select(x => x.CategoryId).ToList();
return dto; return dto;
} }
protected virtual async Task<ProductDto> LoadRealInventoriesAsync(Product product, ProductDto productDto, Guid storeId)
{
var inventoryDict = await _productInventoryProvider.GetInventoryDictionaryAsync(product, storeId);
foreach (var productSkuDto in productDto.ProductSkus)
{
productSkuDto.Inventory = inventoryDict[productSkuDto.Id];
}
return productDto;
}
public override async Task<PagedResultDto<ProductDto>> GetListAsync(GetProductListDto input) public override async Task<PagedResultDto<ProductDto>> GetListAsync(GetProductListDto input)
{ {
await CheckGetListPolicyAsync(); await CheckGetListPolicyAsync();
...@@ -250,14 +269,16 @@ namespace EasyAbp.EShop.Products.Products ...@@ -250,14 +269,16 @@ namespace EasyAbp.EShop.Products.Products
query = ApplySorting(query, input); query = ApplySorting(query, input);
query = ApplyPaging(query, input); query = ApplyPaging(query, input);
var entities = await AsyncQueryableExecuter.ToListAsync(query); var products = await AsyncQueryableExecuter.ToListAsync(query);
// Todo: get real inventory. var items = new List<ProductDto>();
foreach (var product in products)
{
items.Add(await LoadRealInventoriesAsync(product, MapToGetListOutputDto(product), input.StoreId));
}
return new PagedResultDto<ProductDto>( return new PagedResultDto<ProductDto>(totalCount, items);
totalCount,
entities.Select(MapToGetListOutputDto).ToList()
);
} }
public async Task DeleteAsync(Guid id, Guid storeId) public async Task DeleteAsync(Guid id, Guid storeId)
......
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
...@@ -7,24 +8,29 @@ namespace EasyAbp.EShop.Products.Products ...@@ -7,24 +8,29 @@ namespace EasyAbp.EShop.Products.Products
[Dependency(TryRegister = true)] [Dependency(TryRegister = true)]
public class DefaultProductInventoryProvider : IProductInventoryProvider, ITransientDependency public class DefaultProductInventoryProvider : IProductInventoryProvider, ITransientDependency
{ {
public virtual async Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity) public virtual Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId)
{ {
var inventory = await GetInventoryAsync(product, productSku, storeId); return Task.FromResult(productSku.Inventory);
return product.InventoryStrategy == InventoryStrategy.NoNeed || inventory - quantity >= 0;
} }
public virtual Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId) public virtual Task<Dictionary<Guid, int>> GetInventoryDictionaryAsync(Product product, Guid storeId)
{ {
return Task.FromResult(productSku.Inventory); var dict = new Dictionary<Guid, int>();
foreach (var productSku in product.ProductSkus)
{
dict[productSku.Id] = productSku.Inventory;
}
return Task.FromResult(dict);
} }
public Task<bool> TryIncreaseInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity) public virtual Task<bool> TryIncreaseInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity)
{ {
return Task.FromResult(productSku.TryIncreaseInventory(quantity)); return Task.FromResult(productSku.TryIncreaseInventory(quantity));
} }
public Task<bool> TryReduceInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity) public virtual Task<bool> TryReduceInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity)
{ {
return Task.FromResult(productSku.TryReduceInventory(quantity)); return Task.FromResult(productSku.TryReduceInventory(quantity));
} }
......
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace EasyAbp.EShop.Products.Products namespace EasyAbp.EShop.Products.Products
{ {
public interface IProductInventoryProvider public interface IProductInventoryProvider
{ {
Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity);
Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId); Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId);
Task<Dictionary<Guid, int>> GetInventoryDictionaryAsync(Product product, Guid storeId);
Task<bool> TryIncreaseInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity); Task<bool> TryIncreaseInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity);
......
...@@ -16,22 +16,24 @@ namespace EasyAbp.EShop.Products.Products ...@@ -16,22 +16,24 @@ namespace EasyAbp.EShop.Products.Products
_productInventoryProvider = productInventoryProvider; _productInventoryProvider = productInventoryProvider;
} }
public async Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity) public virtual async Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity)
{ {
return await _productInventoryProvider.IsInventorySufficientAsync(product, productSku, storeId, quantity); var inventory = await _productInventoryProvider.GetInventoryAsync(product, productSku, storeId);
return product.InventoryStrategy == InventoryStrategy.NoNeed || inventory - quantity >= 0;
} }
public async Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId) public virtual async Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId)
{ {
return await _productInventoryProvider.GetInventoryAsync(product, productSku, storeId); return await _productInventoryProvider.GetInventoryAsync(product, productSku, storeId);
} }
public async Task<bool> TryIncreaseInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity) public virtual async Task<bool> TryIncreaseInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity)
{ {
return await _productInventoryProvider.TryIncreaseInventoryAsync(product, productSku, storeId, quantity); return await _productInventoryProvider.TryIncreaseInventoryAsync(product, productSku, storeId, quantity);
} }
public async Task<bool> TryReduceInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity) public virtual async Task<bool> TryReduceInventoryAsync(Product product, ProductSku productSku, Guid storeId, int quantity)
{ {
return await _productInventoryProvider.TryReduceInventoryAsync(product, productSku, storeId, quantity); return await _productInventoryProvider.TryReduceInventoryAsync(product, productSku, storeId, quantity);
} }
......
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