|
|
|
@ -98,7 +98,7 @@ public class GoodsSpecsServiceImpl implements GoodsSpecsService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void decreaseStockNum(Long specsId, Integer num) { |
|
|
|
|
public void editStockNum(Long specsId, Integer num , Integer type) { |
|
|
|
|
String key = "GOODS_STOCK_NUM"; |
|
|
|
|
|
|
|
|
|
GoodsSpecs goodsSpecs = queryDetailByGoodsId(specsId); |
|
|
|
@ -108,18 +108,24 @@ public class GoodsSpecsServiceImpl implements GoodsSpecsService { |
|
|
|
|
Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, specsId); |
|
|
|
|
|
|
|
|
|
if(Boolean.TRUE.equals(lock)) { |
|
|
|
|
if (goodsSpecs.getStock() < num) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsSpecs.getName() + "库存不足"); |
|
|
|
|
|
|
|
|
|
if (type == 1) { |
|
|
|
|
// 加锁成功,处理业务
|
|
|
|
|
goodsSpecs.setStock(goodsSpecs.getStock() + num); |
|
|
|
|
} else { |
|
|
|
|
if (goodsSpecs.getStock() < num) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsSpecs.getName() + "库存不足"); |
|
|
|
|
} |
|
|
|
|
// 加锁成功,处理业务
|
|
|
|
|
goodsSpecs.setStock(goodsSpecs.getStock() - num); |
|
|
|
|
} |
|
|
|
|
// 加锁成功,处理业务
|
|
|
|
|
goodsSpecs.setStock(goodsSpecs.getStock() - num); |
|
|
|
|
|
|
|
|
|
update(goodsSpecs); |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
// 加锁失败,重试
|
|
|
|
|
Thread.sleep(100); |
|
|
|
|
decreaseStockNum(specsId , num); |
|
|
|
|
editStockNum(specsId , num , type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|