Commit 0e4a2864 by 王世昌

优化对库存限制的判断

parent a3c00a62
......@@ -25,6 +25,7 @@ import com.freemud.application.sdk.api.productcenter.request.product.valid.Valid
import com.freemud.application.sdk.api.productcenter.response.ProductResponseDTO;
import com.freemud.application.sdk.api.productcenter.response.valid.ValiadShopProductResponse;
import com.freemud.sdk.api.assortment.shoppingcart.service.impl.ShoppingCartBaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -91,14 +92,14 @@ public class KgdProductServiceImpl implements ProductService {
GetProductStockRequestDto getProductStockRequestDto = productBO2DTOAdapter.convert2GetProductStockDto(getProductStock);
GetProductStockResponseDto availableStocks = stockClient.getAvailableStocks(getProductStockRequestDto);
List<GetProductStockResponseDto.DataBean> result = availableStocks.getResult();
if (availableStocks != null && !ResponseCodeConstant.RESPONSE_SUCCESS_STR.equals(availableStocks.getCode())) {
if (availableStocks == null || !ResponseCodeConstant.RESPONSE_SUCCESS_STR.equals(availableStocks.getCode())) {
throw new ServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE);
}
List<ProductStockBO> list = new ArrayList<>();
for (GetProductStockResponseDto.DataBean dataBean : result){
if (CollectionUtils.isEmpty(availableStocks.getResult())) {
return list;
}
for (GetProductStockResponseDto.DataBean dataBean : availableStocks.getResult()){
ProductStockBO productStockBO = productDTO2BOAdapter.convert2ProductStockBO(dataBean);
list.add(productStockBO);
}
......
......@@ -112,7 +112,7 @@ public abstract class AbstractAddGoodsService implements AddGoodsService {
// 业务校验,例如商品数量库存之类的信息
AddGoodsToShoppingCartBO addGoodsToShoppingCartBO = this.checkBusinessRules(addGoodsBO, checkBussinessRulesBO);
LogUtil.info("checkBusinessRules >>> ",JsonUtil.toJSONString(Arrays.asList(addGoodsBO,addGoodsBO)),JsonUtil.toJSONString(addGoodsToShoppingCartBO));
LogUtil.info("checkBusinessRules >>> ",JsonUtil.toJSONString(addGoodsBO),JsonUtil.toJSONString(addGoodsToShoppingCartBO));
// 添加商品到购物车
DiscountResultBO discountResultBO = this.addGoodsToShoppingCart(addGoodsBO, addGoodsToShoppingCartBO);
......@@ -235,9 +235,7 @@ public abstract class AbstractAddGoodsService implements AddGoodsService {
cartGoods.setOriginalPrice(spqBO.getProductPrice());
// 商品券Id
if (CollectionUtils.isNotEmpty(baseRequestBO.getAddGoods().getExtra())){
cartGoods.setExtra(baseRequestBO.getAddGoods().getExtra());
} else {
if (CollectionUtils.isEmpty(cartGoods.getExtra())){
cartGoods.setExtra(spqBO.getExtra());
}
cartGoods.setSpecProductId(spqBO.getSpecProductId());
......@@ -744,37 +742,24 @@ public abstract class AbstractAddGoodsService implements AddGoodsService {
private void checkStocks(BaseAddGoodsBO baseRequestDTO, CheckBussinessRulesBO checkBussinessRulesBO){
ProductBO product = checkBussinessRulesBO.getProduct();
ProductStockBO availableStocks = null;
GetProductStock getProductStock = new GetProductStock();
getProductStock.setChannel(BusinessTypeEnum.getByType(baseRequestDTO.getMenuType()).getCode());
getProductStock.setPartnerId(baseRequestDTO.getPartnerId());
getProductStock.setStoreId(baseRequestDTO.getShopId());
List<Long> productIds = new ArrayList<>();
productIds.add(Long.parseLong(checkBussinessRulesBO.getProduct().getGoodsId()));
getProductStock.setProductIds(productIds);
boolean isLimit =false;
if (product.getType() == ProductType.NOSPEC.getCode()
&& product.getStockLimit() == 1) {
List<ProductStockBO> productStock = productManager.getProductStock(getProductStock, baseRequestDTO.getManagerService().getProductService());
if (CollectionUtils.isNotEmpty(productStock)){
availableStocks = productStock.get(0);
}
isLimit = true;
} else {
for (ProductBO.SkuProductBean skuProductBean : product.getSkuList()) {
if (baseRequestDTO.getSkuId().equals(skuProductBean.getSkuId()) && skuProductBean.getStockLimit() == 1) {
List<ProductStockBO> productStock = productManager.getProductStock(getProductStock, baseRequestDTO.getManagerService().getProductService());
if (CollectionUtils.isNotEmpty(productStock)){
availableStocks = productStock.get(0);
}
isLimit = true;
break;
}
}
}
if (!isLimit){
return;
}
List<CartGoods> oldCartGoodsList = checkBussinessRulesBO.getCartGoods();
CartGoods cartGoods = baseRequestDTO.getAddGoods();
Integer qty = 0;
int qty;
if (CollectionUtils.isEmpty(oldCartGoodsList)) {
qty = cartGoods.getQty();
} else {
......@@ -785,15 +770,28 @@ public abstract class AbstractAddGoodsService implements AddGoodsService {
}
}
}
if (availableStocks != null &&
(availableStocks.getQty() == null
|| availableStocks.getQty() < qty)) {
Integer stock = 0;
if ((stock = availableStocks.getQty()) > 0) {
throw new BizServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE, "仅剩" + stock + "件库存了");
}
GetProductStock getProductStock = new GetProductStock();
getProductStock.setChannel(BusinessTypeEnum.getByType(baseRequestDTO.getMenuType()).getCode());
getProductStock.setPartnerId(baseRequestDTO.getPartnerId());
getProductStock.setStoreId(baseRequestDTO.getShopId());
List<Long> productIds = new ArrayList<>();
productIds.add(Long.parseLong(checkBussinessRulesBO.getProduct().getGoodsId()));
getProductStock.setProductIds(productIds);
// 查询库存
List<ProductStockBO> productStock = productManager.getProductStock(getProductStock, baseRequestDTO.getManagerService().getProductService());
ProductStockBO availableStocks = null;
if (CollectionUtils.isNotEmpty(productStock)){
availableStocks = productStock.get(0);
}
int stock = availableStocks == null || availableStocks.getQty() == null ? 0 : availableStocks.getQty();
if (stock <= 0) {
throw new ServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE);
} else if (stock < qty) {
throw new BizServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE, "仅剩" + stock + "件库存了");
}
}
......
......@@ -463,17 +463,13 @@ public abstract class AbstractUpdateGoodsQtyService implements UpdateGoodsQtySer
if (CollectionUtils.isNotEmpty(productStock)){
availableStocks = productStock.get(0);
}
if (availableStocks != null) {
if (availableStocks.getQty() == null
|| availableStocks.getQty() < baseRequestDTO.getQty()) {
Integer stock = 0;
if ((stock = availableStocks.getQty()) > 0) {
throw new BizServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE, "仅剩" + stock + "件库存了");
}
throw new ServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE);
}
}
int stock = availableStocks == null || availableStocks.getQty() == null ? 0 : availableStocks.getQty();
if (stock <= 0) {
throw new ServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE);
} else if (stock < baseRequestDTO.getQty()) {
throw new BizServiceException(ResponseResult.SHOPPING_CART_STOCK_NOT_HAVE, "仅剩" + stock + "件库存了");
}
}
}
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