diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/sku/ProductSkuMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/sku/ProductSkuMapper.java index 82bfc87fa..5e79582f0 100755 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/sku/ProductSkuMapper.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/sku/ProductSkuMapper.java @@ -26,7 +26,7 @@ public interface ProductSkuMapper extends BaseMapperX { } /** - * 更新 SKU 库存(增加) + * 更新 SKU 库存(增加)、销量(减少) * * @param id 编号 * @param incrCount 增加库存(正数) @@ -34,13 +34,14 @@ public interface ProductSkuMapper extends BaseMapperX { default void updateStockIncr(Long id, Integer incrCount) { Assert.isTrue(incrCount > 0); LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper() - .setSql(" stock = stock + " + incrCount) + .setSql(" stock = stock + " + incrCount + + ", sales_count = sales_count - " + incrCount) .eq(ProductSkuDO::getId, id); update(null, lambdaUpdateWrapper); } /** - * 更新 SKU 库存(减少) + * 更新 SKU 库存(减少)、销量(增加) * * @param id 编号 * @param incrCount 减少库存(负数) @@ -48,10 +49,12 @@ public interface ProductSkuMapper extends BaseMapperX { */ default int updateStockDecr(Long id, Integer incrCount) { Assert.isTrue(incrCount < 0); + incrCount = - incrCount; // 取正 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper() - .setSql(" stock = stock + " + incrCount) // 负数,所以使用 + 号 + .setSql(" stock = stock - " + incrCount + + ", sales_count = sales_count + " + incrCount) .eq(ProductSkuDO::getId, id) - .ge(ProductSkuDO::getStock, -incrCount); // cas 逻辑 + .ge(ProductSkuDO::getStock, incrCount); return update(null, updateWrapper); } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/spu/ProductSpuMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/spu/ProductSpuMapper.java index cf8ae6483..a5926d18c 100755 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/spu/ProductSpuMapper.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/spu/ProductSpuMapper.java @@ -85,9 +85,19 @@ public interface ProductSpuMapper extends BaseMapperX { * @param incrCount 增加的库存数量 */ default void updateStock(Long id, Integer incrCount) { + // 拼接 SQL + if (incrCount == 0) { + return; + } + String sql; + if (incrCount > 0) { + sql = " stock = stock + " + incrCount + ", sales_count = sales_count - " + incrCount; + } else { + sql = " stock = stock - " + Math.abs(incrCount) + ", sales_count = sales_count + " + Math.abs(incrCount); + } + // 执行更新 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper() - // 负数,所以使用 + 号 - .setSql(" stock = stock +" + incrCount) + .setSql(sql) .eq(ProductSpuDO::getId, id); update(null, updateWrapper); }