From 714f0be5476bdd3d9a971a567709cd1392d77a7f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 23 Jul 2024 12:39:04 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91MALL=EF=BC=9A=E6=9B=B4=E6=96=B0=E5=BA=93=E5=AD=98?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=90=8C=E6=97=B6=E6=9B=B4=E6=96=B0=E9=94=80?= =?UTF-8?q?=E9=87=8F=EF=BC=8C=E5=92=8C=20taobao=20=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/dal/mysql/sku/ProductSkuMapper.java | 13 ++++++++----- .../product/dal/mysql/spu/ProductSpuMapper.java | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) 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 82bfc87fad..5e79582f0f 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 cf8ae64837..a5926d18ce 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); }