Commit 714f0be5476bdd3d9a971a567709cd1392d77a7f

Authored by YunaiV
1 parent a55ca877

【代码修复】MALL:更新库存时,同时更新销量,和 taobao 保持一致的逻辑

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<ProductSkuDO> { @@ -26,7 +26,7 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> {
26 } 26 }
27 27
28 /** 28 /**
29 - * 更新 SKU 库存(增加) 29 + * 更新 SKU 库存(增加)、销量(减少)
30 * 30 *
31 * @param id 编号 31 * @param id 编号
32 * @param incrCount 增加库存(正数) 32 * @param incrCount 增加库存(正数)
@@ -34,13 +34,14 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> { @@ -34,13 +34,14 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> {
34 default void updateStockIncr(Long id, Integer incrCount) { 34 default void updateStockIncr(Long id, Integer incrCount) {
35 Assert.isTrue(incrCount > 0); 35 Assert.isTrue(incrCount > 0);
36 LambdaUpdateWrapper<ProductSkuDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<ProductSkuDO>() 36 LambdaUpdateWrapper<ProductSkuDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<ProductSkuDO>()
37 - .setSql(" stock = stock + " + incrCount) 37 + .setSql(" stock = stock + " + incrCount
  38 + + ", sales_count = sales_count - " + incrCount)
38 .eq(ProductSkuDO::getId, id); 39 .eq(ProductSkuDO::getId, id);
39 update(null, lambdaUpdateWrapper); 40 update(null, lambdaUpdateWrapper);
40 } 41 }
41 42
42 /** 43 /**
43 - * 更新 SKU 库存(减少) 44 + * 更新 SKU 库存(减少)、销量(增加)
44 * 45 *
45 * @param id 编号 46 * @param id 编号
46 * @param incrCount 减少库存(负数) 47 * @param incrCount 减少库存(负数)
@@ -48,10 +49,12 @@ public interface ProductSkuMapper extends BaseMapperX&lt;ProductSkuDO&gt; { @@ -48,10 +49,12 @@ public interface ProductSkuMapper extends BaseMapperX&lt;ProductSkuDO&gt; {
48 */ 49 */
49 default int updateStockDecr(Long id, Integer incrCount) { 50 default int updateStockDecr(Long id, Integer incrCount) {
50 Assert.isTrue(incrCount < 0); 51 Assert.isTrue(incrCount < 0);
  52 + incrCount = - incrCount; // 取正
51 LambdaUpdateWrapper<ProductSkuDO> updateWrapper = new LambdaUpdateWrapper<ProductSkuDO>() 53 LambdaUpdateWrapper<ProductSkuDO> updateWrapper = new LambdaUpdateWrapper<ProductSkuDO>()
52 - .setSql(" stock = stock + " + incrCount) // 负数,所以使用 + 号 54 + .setSql(" stock = stock - " + incrCount
  55 + + ", sales_count = sales_count + " + incrCount)
53 .eq(ProductSkuDO::getId, id) 56 .eq(ProductSkuDO::getId, id)
54 - .ge(ProductSkuDO::getStock, -incrCount); // cas 逻辑 57 + .ge(ProductSkuDO::getStock, incrCount);
55 return update(null, updateWrapper); 58 return update(null, updateWrapper);
56 } 59 }
57 60
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&lt;ProductSpuDO&gt; { @@ -85,9 +85,19 @@ public interface ProductSpuMapper extends BaseMapperX&lt;ProductSpuDO&gt; {
85 * @param incrCount 增加的库存数量 85 * @param incrCount 增加的库存数量
86 */ 86 */
87 default void updateStock(Long id, Integer incrCount) { 87 default void updateStock(Long id, Integer incrCount) {
  88 + // 拼接 SQL
  89 + if (incrCount == 0) {
  90 + return;
  91 + }
  92 + String sql;
  93 + if (incrCount > 0) {
  94 + sql = " stock = stock + " + incrCount + ", sales_count = sales_count - " + incrCount;
  95 + } else {
  96 + sql = " stock = stock - " + Math.abs(incrCount) + ", sales_count = sales_count + " + Math.abs(incrCount);
  97 + }
  98 + // 执行更新
88 LambdaUpdateWrapper<ProductSpuDO> updateWrapper = new LambdaUpdateWrapper<ProductSpuDO>() 99 LambdaUpdateWrapper<ProductSpuDO> updateWrapper = new LambdaUpdateWrapper<ProductSpuDO>()
89 - // 负数,所以使用 + 号  
90 - .setSql(" stock = stock +" + incrCount) 100 + .setSql(sql)
91 .eq(ProductSpuDO::getId, id); 101 .eq(ProductSpuDO::getId, id);
92 update(null, updateWrapper); 102 update(null, updateWrapper);
93 } 103 }