1. Controller handles exceptions
/** * Lock the inventory, after placing an order to lock the inventory *@param vo
* @return* /
@PostMapping("/lock/order")
public R orderLockStock(@RequestBody WareSkuLockVo vo){
try {
wareSkuService.orderLockStock(vo);
return R.ok();
} catch (NoStockException e) {
log.warn("\n" + e.getMessage());
}
return R.error(BizCodeEnume.NO_STOCK_EXCEPTION.getCode(), BizCodeEnume.NO_STOCK_EXCEPTION.getMsg());
}
Copy the code
2. Define exception classes
/ * * *@DescriptionAbnormal inventory. * * /
public class NoStockException extends RuntimeException {
@Getter @Setter
private Long skuId;
public NoStockException(Long skuId) {
super("Commodity ID:"+ skuId + "Out of stock!");
}
public NoStockException(String msg) {
super(msg); }}Copy the code
3. Service throws an exception
@Transactional(rollbackFor = NoStockException.class) // Default: any run-time exception will be rolled back, so if not (rollbackFor = nostockException.class) it will work.
@Override
public Boolean orderLockStock(WareSkuLockVo vo) {
// Save the order before placing the stock for later retraction
WareOrderTaskEntity taskEntity = new WareOrderTaskEntity();
taskEntity.setOrderSn(vo.getOrderSn());
orderTaskService.save(taskEntity);
// [Theory]1. Find a nearby warehouse according to the harvest address of the order and lock the inventory
// [actually]1. Find out which warehouse has every item in stock
List<OrderItemVo> locks = vo.getLocks();
List<SkuWareHasStock> collect = locks.stream().map(item -> {
SkuWareHasStock hasStock = new SkuWareHasStock();
Long skuId = item.getSkuId();
hasStock.setSkuId(skuId);
// Where is this item in stock
List<Long> wareIds = wareSkuDao.listWareIdHasSkuStock(skuId);
hasStock.setWareId(wareIds);
hasStock.setNum(item.getCount());
return hasStock;
}).collect(Collectors.toList());
// Order more stock
for (SkuWareHasStock hasStock : collect) {
Boolean skuStocked = true;
Long skuId = hasStock.getSkuId();
List<Long> wareIds = hasStock.getWareId();
if(wareIds == null || wareIds.size() == 0) {// No warehouse has this inventory
throw new NoStockException(skuId.toString());
}
// If each item is successfully locked, a record of how many items are currently locked is sent to MQ
// If the lock fails, the work order information saved before is rolled back - it does not matter, because the database cannot find the ID
for (Long wareId : wareIds) {
Return 1 on success, 0 on failure
Long count = wareSkuDao.lockSkuStock(skuId, wareId, hasStock.getNum());
if(count == 1) {// TODO tells MQ that an order has been locked successfully
//WareOrderTaskDetailEntity detailEntity = new WareOrderTaskDetailEntity(null,skuId,"",hasStock.getNum() ,taskEntity.getId(),wareId,1);
WareOrderTaskDetailEntity detailEntity = new WareOrderTaskDetailEntity(null,skuId,"",hasStock.getNum(),taskEntity.getId(),wareId,1);
orderTaskDetailService.save(detailEntity); / / warehousing
StockLockedTo stockLockedTo = new StockLockedTo();
stockLockedTo.setId(taskEntity.getId());
StockDetailTo detailTo = new StockDetailTo();
BeanUtils.copyProperties(detailEntity, detailTo);
// In case the data cannot be found after rollback
stockLockedTo.setDetailTo(detailTo);
rabbitTemplate.convertAndSend(eventExchange, routingKey ,stockLockedTo);
skuStocked = false;
break;
}
// The current warehouse lock failed. Try the next warehouse
}
if(skuStocked){
// There are currently no lock posts in all warehouses
throw newNoStockException(skuId.toString()); }}// 3. All locks are successful
return true;
}
Copy the code
Throw new NoStockException(skuuid.toString ()); There is no stock left.
No inventory information is displayed on the return page.