完成下单操作
This commit is contained in:
parent
4e291d6329
commit
6a2a8372ed
@ -33,7 +33,7 @@ public class ShopOrderInfoController {
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
// 是否已经没有库存
|
||||
private ConcurrentHashMap<Integer, Boolean> stockOutMap = new ConcurrentHashMap<>();
|
||||
private ConcurrentHashMap<Long, Boolean> stockOutMap = new ConcurrentHashMap<>();
|
||||
private static final String CACHE_STOCK_KEY = "goods:stock:";
|
||||
|
||||
private String cacheKey(long gid) {
|
||||
@ -45,6 +45,10 @@ public class ShopOrderInfoController {
|
||||
//TODO 应该定时更新缓存数据
|
||||
goodsService.queryAllGoodsIdAndStock().forEach(g -> {
|
||||
log.info("缓存 id:{} stock:{} ", g.getId(), g.getStock());
|
||||
if(g.getStock() == 0){
|
||||
// 原本就没有库存
|
||||
stockOutMap.put(g.getId(),true);
|
||||
}
|
||||
// 缓存库存
|
||||
stringRedisTemplate.opsForValue().set(cacheKey(g.getId()), g.getStock().toString());
|
||||
});
|
||||
@ -55,22 +59,32 @@ public class ShopOrderInfoController {
|
||||
if (data.getGoodsId() <= 0 || data.getBuyCount() <= 0) {
|
||||
throw BizException.create("订单参数不正确");
|
||||
}
|
||||
// 如果限制用户只能购买的数量,可以添加一个map记录用户的请求数
|
||||
//1.内存判断
|
||||
if (stockOutMap.get(data.getGoodsId())) {
|
||||
if (stockOutMap.size() > 0 && stockOutMap.get(data.getGoodsId())) {
|
||||
throw BizException.create("库存不足");
|
||||
}
|
||||
//2.缓存(redis)判断
|
||||
long count = stringRedisTemplate.opsForValue().decrement(cacheKey(data.getGoodsId()));
|
||||
long count = stringRedisTemplate.opsForValue().decrement(cacheKey(data.getGoodsId()), data.getBuyCount());
|
||||
if (count < 0) {
|
||||
// 此时库存没有了 , 保存到已买完的对象
|
||||
stockOutMap.put(data.getGoodsId(),true);
|
||||
log.info("stock count ===>" + count);
|
||||
// 对缓存进行库存 + 1
|
||||
stringRedisTemplate.opsForValue().increment(cacheKey(data.getGoodsId())); //
|
||||
// 对缓存进行库存 还原
|
||||
stringRedisTemplate.opsForValue().increment(cacheKey(data.getGoodsId()),data.getBuyCount()); //
|
||||
throw BizException.create("库存不足");
|
||||
}
|
||||
if(count == 0){
|
||||
// 此时库存没有了 , 保存到已买完的对象
|
||||
stockOutMap.put((long)data.getGoodsId(),true);
|
||||
}
|
||||
//3.数据库
|
||||
return orderInfoService.create(StpUtil.getLoginIdAsInt(), data);
|
||||
try{
|
||||
return orderInfoService.create(StpUtil.getLoginIdAsInt(), data);
|
||||
}catch (BizException e){
|
||||
//下单失败 对缓存进行库存 还原
|
||||
stringRedisTemplate.opsForValue().increment(cacheKey(data.getGoodsId()),data.getBuyCount()); //
|
||||
stockOutMap.put((long)data.getGoodsId(),false);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,9 +49,10 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
||||
if (goods.getOnlineTime().getTime() > now || goods.getOfflineTime().getTime() < now) {
|
||||
throw BizException.create("商品未上架或已下架");
|
||||
}
|
||||
// TODO 需要判断积分
|
||||
if (goods.getStock() < data.getBuyCount()) throw BizException.create("商品存库不足");
|
||||
// 判断购买数量的限制
|
||||
if(buyHistoryCount(uid,data.getGoodsId()) + data.getBuyCount() > goods.getLimitCount()){
|
||||
if(goods.getLimitCount() > 0 && buyHistoryCount(uid,data.getGoodsId()) + data.getBuyCount() > goods.getLimitCount()){
|
||||
throw BizException.create("最多兑换" + goods.getLimitCount() + "件");
|
||||
}
|
||||
// 3.减库存
|
||||
|
Loading…
x
Reference in New Issue
Block a user