下单时对设备上锁

This commit is contained in:
墨大叔 2024-08-24 10:45:06 +08:00
parent 7377d39b19
commit 9ed15965f0
2 changed files with 25 additions and 14 deletions

View File

@ -21,7 +21,8 @@ public enum RedisLockKey {
ADD_DEVICE("add_device", "创建设备"), ADD_DEVICE("add_device", "创建设备"),
ADD_TIME_BILL("add_time_bill", "创建时长订单"), ADD_TIME_BILL("add_time_bill", "创建时长订单"),
PAY_BILL("pay_bill", "支付订单"), PAY_BILL("pay_bill", "支付订单"),
PREPAY_DEPOSIT("prepay_deposit", "支付押金"); PREPAY_DEPOSIT("prepay_deposit", "支付押金"),
ADD_RECHARGE_ORDER("add_recharge_order", "创建充值订单");
private final String key; private final String key;

View File

@ -228,22 +228,32 @@ public class TransactionBillServiceImpl implements TransactionBillService, After
@Override @Override
public String addOrder(RechargeBO bo) { public String addOrder(RechargeBO bo) {
// 拉取设备信息 ServiceUtil.assertion(bo == null || bo.getDevice() == null || bo.getDevice().getDeviceId() == null, "设备不存在");
deviceService.pullDeviceInfo(Collections.singletonList(bo.getDevice().getDeviceId())); Long deviceId = bo.getDevice().getDeviceId();
// 下单 // 加锁
TransactionBill order = parseToOrder(bo); ServiceUtil.assertion(!redisLock.lock(RedisLockKey.ADD_RECHARGE_ORDER, deviceId), "当前使用该设备人数较多,请稍后再试");
try {
// 拉取设备信息
deviceService.pullDeviceInfo(Collections.singletonList(bo.getDevice().getDeviceId()));
transactionTemplate.execute(status -> { // 下单
// 插入数据库 TransactionBill order = parseToOrder(bo);
int insert = this.insertSmTransactionBill(order);
ServiceUtil.assertion(insert != 1, "下单失败");
return insert;
});
// 加入延时队列超时取消 transactionTemplate.execute(status -> {
billDelayedManager.add(order.getBillNo(), order.getExpireTime().getTime()); // 插入数据库
return order.getBillNo(); int insert = this.insertSmTransactionBill(order);
ServiceUtil.assertion(insert != 1, "下单失败");
return insert;
});
// 加入延时队列超时取消
billDelayedManager.add(order.getBillNo(), order.getExpireTime().getTime());
return order.getBillNo();
} finally {
// 解锁
redisLock.unlock(RedisLockKey.ADD_RECHARGE_ORDER, deviceId);
}
} }
// 处理充值服务费 // 处理充值服务费