下单时对设备上锁

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_TIME_BILL("add_time_bill", "创建时长订单"),
PAY_BILL("pay_bill", "支付订单"),
PREPAY_DEPOSIT("prepay_deposit", "支付押金");
PREPAY_DEPOSIT("prepay_deposit", "支付押金"),
ADD_RECHARGE_ORDER("add_recharge_order", "创建充值订单");
private final String key;

View File

@ -228,22 +228,32 @@ public class TransactionBillServiceImpl implements TransactionBillService, After
@Override
public String addOrder(RechargeBO bo) {
// 拉取设备信息
deviceService.pullDeviceInfo(Collections.singletonList(bo.getDevice().getDeviceId()));
ServiceUtil.assertion(bo == null || bo.getDevice() == null || bo.getDevice().getDeviceId() == null, "设备不存在");
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 -> {
// 插入数据库
int insert = this.insertSmTransactionBill(order);
ServiceUtil.assertion(insert != 1, "下单失败");
return insert;
});
// 下单
TransactionBill order = parseToOrder(bo);
// 加入延时队列超时取消
billDelayedManager.add(order.getBillNo(), order.getExpireTime().getTime());
return order.getBillNo();
transactionTemplate.execute(status -> {
// 插入数据库
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);
}
}
// 处理充值服务费