Sku扣减库存没有成功
七月老师好,最近我测试下单接口发现,测试小程序使用林白的测试数据下单一切正常,修改测试小程序下单(1)的下单商品之后,Sku扣减库存存在问题。查看order表有发现下单的记录,但是查看sku表发现对应id商品的库存却没有变化,查看日志显示扣减库存Update语句已经执行,扣减之后额外执行了一句Sku的Update语句,请问老师是这句Update语句导致Sku扣减库存失败的吗?为什么会出现这一句语句?
相关截图:
相关代码:
@Transactional
public Long placeOrder(Long uid, OrderDTO orderDTO, OrderChecker orderChecker) {
Calendar calendar = Calendar.getInstance();
Date placedTime = calendar.getTime();
calendar.add(Calendar.SECOND, payLimitTime);
Date expiredTime = calendar.getTime();
Order order = Order.builder()
.orderNo(OrderUtil.makeOrderId())
.userId(uid)
.totalPrice(orderChecker.getTotalPrice())
.totalCount(orderChecker.getTotalCount())
.snapImg(orderChecker.getLeaderImg())
.snapTitle(orderChecker.getLeaderTitle())
.finalTotalPrice(orderChecker.getFinalTotalPrice())
.status(OrderStatus.UNPAID.getCode())
.placedTime(placedTime)
.expiredTime(expiredTime)
.build();
order.setSnapItems(orderChecker.getOrderSkuList());
order.setSnapAddress(orderDTO.getAddress());
orderRepository.save(order);
log.info("准备扣减库存");
//扣减库存
this.reduceStock(orderChecker);
log.info("扣减库存成功");
//核销优惠券
if (orderDTO.getCouponId() != null) {
log.info("准备核销优惠券");
this.writeOffCoupon(orderDTO.getCouponId(), uid, order.getId());
log.info("核销优惠券成功");
}
log.info("下单成功,返回订单id");
return order.getId();
}
private void reduceStock(OrderChecker orderChecker) {
orderChecker.getOrderSkuList().forEach(orderSku -> {
int result = skuRepository.reduceStock(orderSku.getId(), orderSku.getCount().longValue());
if (result != 1) {
throw new ParameterException("商品库存不足");
}
});
}
private void writeOffCoupon(Long couponId, Long uid, Long oid) {
int result = userCouponRepository.writeOff(uid, couponId, oid);
if (result != 1) {
throw new ParameterException("优惠券核销失败");
}
}
72
收起
正在回答 回答被采纳积分+1
7回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星