创建订单 - 扣除商品库存与订单状态保存

/**
 * @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);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值