|
|
@@ -14,7 +14,6 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import shop.alien.entity.result.BusinessException;
|
|
|
import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.*;
|
|
|
import shop.alien.entity.store.dto.LifeUserOrderDto;
|
|
|
@@ -777,20 +776,31 @@ public class LifeUserOrderService extends ServiceImpl<LifeUserOrderMapper, LifeU
|
|
|
orderCouponMiddleService.save(orderCouponMiddle);
|
|
|
}
|
|
|
//4. 代金券/团购库存扣除 coupon_type 1 代金券 2团购
|
|
|
- int successful = 0;
|
|
|
- if(lifeUserOrderDto.getCouponType() == 2){
|
|
|
- // 团购库存扣除
|
|
|
- successful = lifeGroupBuyMainMapper.update(null, new UpdateWrapper<LifeGroupBuyMain>()
|
|
|
- .eq("id", lifeUserOrderDto.getCouponId()).ge("inventory_num",lifeUserOrderDto.getCount()).setSql("inventory_num = inventory_num - " + lifeUserOrderDto.getCount()));
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();
|
|
|
+ int soldOutStatus = CouponStatusEnum.SOLD_OUT.getCode();
|
|
|
+ int updateRows = 0;
|
|
|
+
|
|
|
+ if (lifeUserOrderDto.getCouponType() == 2) {
|
|
|
+ // 团购库存:原子扣减
|
|
|
+ updateRows = lifeGroupBuyMainMapper.deductInventoryAtomically(
|
|
|
+ lifeUserOrderDto.getCouponId(),
|
|
|
+ buyCount,
|
|
|
+ soldOutStatus
|
|
|
+ );
|
|
|
} else {
|
|
|
- successful = lifeCouponMapper.update(null, new UpdateWrapper<LifeCoupon>()
|
|
|
- .eq("id", lifeUserOrderDto.getCouponId()).ge("single_qty",lifeUserOrderDto.getCount()).setSql("single_qty = single_qty - " + lifeUserOrderDto.getCount()));
|
|
|
+ // 代金券库存:原子扣减
|
|
|
+ updateRows = lifeCouponMapper.deductInventoryAtomically(
|
|
|
+ lifeUserOrderDto.getCouponId(),
|
|
|
+ buyCount,
|
|
|
+ soldOutStatus
|
|
|
+ );
|
|
|
}
|
|
|
- if(successful == 0){
|
|
|
- log.error("库存不足");
|
|
|
- throw new BusinessException("库存不足");
|
|
|
+// 判断库存扣减结果
|
|
|
+ if (updateRows == 0) {
|
|
|
+ log.error("couponid:"+lifeUserOrderDto.getCouponId()+" 库存不足,当前购买数量:"+ buyCount);
|
|
|
+ // 手动抛出异常,触发事务回滚(回滚之前创建的订单和优惠券状态变更)
|
|
|
+ throw new RuntimeException("库存不足,下单失败");
|
|
|
}
|
|
|
- Map<String, Object> returnMap = new HashMap<>();
|
|
|
returnMap.put("success", "下单成功");
|
|
|
returnMap.put("orderNo", lifeUserOrderDto.getOrderNo());
|
|
|
returnMap.put("lifeUserOrder", lifeUserOrder);
|