/**
* @Description: 订单状态 枚举
*/
public enum OrderStatusEnum {
WAIT_PAY(10, "待付款"),
WAIT_DELIVER(20, "已付款,待发货"),
WAIT_RECEIVE(30, "已发货,待收货"),
SUCCESS(40, "交易成功"),
CLOSE(50, "交易关闭");
public final Integer type;
public final String value;
OrderStatusEnum(Integer type, String value){
this.type = type;
this.value = value;
}
}
/**
* 减少库存
* @param specId
* @param buyCounts
*/
public void decreaseItemSpecStock(String specId, int buyCounts);
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void decreaseItemSpecStock(String specId, int buyCounts) {
// synchronized 不推荐使用,集群下无用,性能低下
// 锁数据库: 不推荐,导致数据库性能低下
// 分布式锁 zookeeper redis
// lockUtil.getLock(); -- 加锁
// 1. 查询库存
// int stock = 10;
// 2. 判断库存,是否能够减少到0以下
// if (stock - buyCounts < 0) {
// 提示用户库存不够
// 10 - 3 -3 - 5 = -1
// }
// lockUtil.unLock(); -- 解锁
int result = itemsMapperCustom.decreaseItemSpecStock(specId, buyCounts);
if (result != 1) {
throw new RuntimeException("订单创建失败,原因:库存不足!");
}
}
<update id="decreaseItemSpecStock">
update
items_spec
set
stock = stock - #{pendingCounts}
where
id = #{specId}
and
stock >= #{pendingCounts}
</update>
public int decreaseItemSpecStock(@Param("specId") String specId,
@Param("pendingCounts") int pendingCounts);
// 2.4 在用户提交订单以后,规格表中需要扣除库存
itemService.decreaseItemSpecStock(itemSpecId, buyCounts);