支付、订单查询模块

This commit is contained in:
2023-10-13 16:41:19 +08:00
parent 1d6ed7f564
commit b98a4954ad
34 changed files with 1112 additions and 29 deletions

View File

@@ -39,4 +39,12 @@ public interface SkuInfoMapper extends BaseMapper<SkuInfo> {
* @param skuNum
*/
void unlockStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum);
/**
* 减库存
*
* @param skuId
* @param skuNum
*/
void minusStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum);
}

View File

@@ -0,0 +1,43 @@
package com.atguigu.ssyx.product.receiver;
import com.atguigu.ssyx.mq.constant.MqConst;
import com.atguigu.ssyx.product.service.SkuInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
/**
* ClassName: StockReceiver
* Package: com.atguigu.ssyx.product.receiver
*
* @author yovinchen
* @Create 2023/10/13 15:05
*/
@Component
public class StockReceiver {
@Autowired
private SkuInfoService skuInfoService;
/**
* 扣减库存成功,更新订单状态
*
* @param orderNo
* @throws IOException
*/
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_MINUS_STOCK, durable = "true"), exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT), key = {MqConst.ROUTING_MINUS_STOCK}))
public void minusStock(String orderNo, Message message, Channel channel) throws IOException {
if (!StringUtils.isEmpty(orderNo)) {
skuInfoService.minusStock(orderNo);
}
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
}

View File

@@ -115,4 +115,11 @@ public interface SkuInfoService extends IService<SkuInfo> {
* @return
*/
Boolean checkAndLock(List<SkuStockLockVo> skuStockLockVoList, String orderNo);
/**
* 扣减库存成功,更新订单状态
*
* @param orderNo
*/
void minusStock(String orderNo);
}

View File

@@ -312,9 +312,7 @@ public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> impl
//获取第一页数据,每页显示三条记录
Page<SkuInfo> pageParam = new Page<>(1, 3);
//调用方法查询
IPage<SkuInfo> skuInfoPage = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<SkuInfo>().eq(SkuInfo::getIsNewPerson, 1)
.eq(SkuInfo::getPublishStatus, 1)
.orderByDesc(SkuInfo::getStock));
IPage<SkuInfo> skuInfoPage = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<SkuInfo>().eq(SkuInfo::getIsNewPerson, 1).eq(SkuInfo::getPublishStatus, 1).orderByDesc(SkuInfo::getStock));
return skuInfoPage.getRecords();
}
@@ -333,31 +331,47 @@ public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> impl
}
//2 遍历skuStockLockVoList得到每个商品验证库存并锁定库存具备原子性
skuStockLockVoList.stream()
.forEach(skuStockLockVo -> {
this.checkLock(skuStockLockVo);
});
skuStockLockVoList.stream().forEach(skuStockLockVo -> {
this.checkLock(skuStockLockVo);
});
//3 只要有一个商品锁定失败,所有锁定成功的商品都解锁
boolean flag = skuStockLockVoList.stream()
.anyMatch(skuStockLockVo -> !skuStockLockVo.getIsLock());
boolean flag = skuStockLockVoList.stream().anyMatch(skuStockLockVo -> !skuStockLockVo.getIsLock());
if (flag) {
//所有锁定成功的商品都解锁
skuStockLockVoList.stream()
.filter(SkuStockLockVo::getIsLock)
.forEach(skuStockLockVo -> {
baseMapper.unlockStock(skuStockLockVo.getSkuId(), skuStockLockVo.getSkuNum());
});
skuStockLockVoList.stream().filter(SkuStockLockVo::getIsLock).forEach(skuStockLockVo -> {
baseMapper.unlockStock(skuStockLockVo.getSkuId(), skuStockLockVo.getSkuNum());
});
//返回失败的状态
return false;
}
//4 如果所有商品都锁定成功了redis缓存相关数据为了方便后面解锁和减库存
redisTemplate.opsForValue()
.set(RedisConst.SROCK_INFO + orderNo, skuStockLockVoList);
redisTemplate.opsForValue().set(RedisConst.SROCK_INFO + orderNo, skuStockLockVoList);
return true;
}
/**
* 扣减库存成功,更新订单状态
*
* @param orderNo
*/
@Override
public void minusStock(String orderNo) {
//从redis获取锁定库存信息
List<SkuStockLockVo> skuStockLockVoList = (List<SkuStockLockVo>) redisTemplate.opsForValue().get(RedisConst.SROCK_INFO + orderNo);
if (CollectionUtils.isEmpty(skuStockLockVoList)) {
return;
}
//遍历集合,得到每个对象,减库存
skuStockLockVoList.forEach(skuStockLockVo -> {
baseMapper.minusStock(skuStockLockVo.getSkuId(), skuStockLockVo.getSkuNum());
});
//删除redis数据
redisTemplate.delete(RedisConst.SROCK_INFO + orderNo);
}
//2 遍历skuStockLockVoList得到每个商品验证库存并锁定库存具备原子性
private void checkLock(SkuStockLockVo skuStockLockVo) {
//获取锁