Commit 892480d0 by 吕明尚

订单增加30秒锁定

parent 09801629
...@@ -26,6 +26,8 @@ public class Constants ...@@ -26,6 +26,8 @@ public class Constants
public static final String ORDER_AUTO_CANCEL_KEY = "order_auto_cancel_key"; public static final String ORDER_AUTO_CANCEL_KEY = "order_auto_cancel_key";
// 订单锁定Key // 订单锁定Key
public static final String ORDER_LOCK_KEY = "order_lock_key"; public static final String ORDER_LOCK_KEY = "order_lock_key";
//订单锁定
public static final String ORDER_LOCK_KEY_PREFIX = "order_lock_key_prefix:";
//测试环境,经测试需要,手机验证码测试环境可以不需要就能登录,value为1,则不验证验证码 //测试环境,经测试需要,手机验证码测试环境可以不需要就能登录,value为1,则不验证验证码
public static final String PHONE_TEST_KEY = "phone_test_key"; public static final String PHONE_TEST_KEY = "phone_test_key";
//通过uuid缓存的key 查找对应的 token数据 WX_TOKEN_USER:TOKEN //通过uuid缓存的key 查找对应的 token数据 WX_TOKEN_USER:TOKEN
......
...@@ -801,6 +801,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -801,6 +801,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
public OrderPayResultResponse createOrder(CreateOrderRequest request) { public OrderPayResultResponse createOrder(CreateOrderRequest request) {
String lockResult = ""; String lockResult = "";
try { try {
lockOrder(request.getRoomId());
lockResult = lockOrder(request); lockResult = lockOrder(request);
SConsumer user = FrontTokenComponent.getWxSConsumerEntry(); SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
if (ObjectUtil.isNull(user)) { if (ObjectUtil.isNull(user)) {
...@@ -926,6 +927,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -926,6 +927,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
map.put("expirationTime", sOrder.getEndDate().toString()); map.put("expirationTime", sOrder.getEndDate().toString());
JSONObject jsonObject = new JSONObject(map); JSONObject jsonObject = new JSONObject(map);
redisUtil.set(ReceiptRdeisEnum.ORDER_NO_KEY.getValue() + sOrder.getOrderNo(), jsonObject.toString()); redisUtil.set(ReceiptRdeisEnum.ORDER_NO_KEY.getValue() + sOrder.getOrderNo(), jsonObject.toString());
unLockOrder(request.getRoomId());
} }
if (response.getStatus().equals(YesNoEnum.yes.getFlag())) { if (response.getStatus().equals(YesNoEnum.yes.getFlag())) {
if (Objects.nonNull(byId)) { if (Objects.nonNull(byId)) {
...@@ -943,6 +945,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -943,6 +945,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
JSONObject jsonObject = new JSONObject(map); JSONObject jsonObject = new JSONObject(map);
if (sOrder.getOrderType().equals(OrderTypeEnum.RESERVER.getCode())) { if (sOrder.getOrderType().equals(OrderTypeEnum.RESERVER.getCode())) {
redisUtil.set(ReceiptRdeisEnum.ORDER_NO.getValue() + sOrder.getOrderNo(), jsonObject.toString()); redisUtil.set(ReceiptRdeisEnum.ORDER_NO.getValue() + sOrder.getOrderNo(), jsonObject.toString());
unLockOrder(request.getRoomId());
} }
// 订单预定成功, 发送短信通知运维客服 // 订单预定成功, 发送短信通知运维客服
List<String> phones = new ArrayList<>(); List<String> phones = new ArrayList<>();
...@@ -966,6 +969,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -966,6 +969,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
throw e; throw e;
}finally { }finally {
unLockOrder(request,lockResult); unLockOrder(request,lockResult);
unLockOrder(request.getRoomId());
} }
} }
...@@ -987,12 +991,22 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -987,12 +991,22 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
} }
} }
private void lockOrder(Long roomId) {
if (redisUtil.exists(Constants.ORDER_LOCK_KEY_PREFIX + roomId)) {
throw new BaseException("当前房间下单太火爆了,请稍后再试!");
}
redisUtil.set(Constants.ORDER_LOCK_KEY_PREFIX + roomId, roomId, Long.valueOf(Constants.ROOM_LOCK_DELAY_MINUTE));
}
private void unLockOrder(CreateOrderRequest request,String keyValue) { private void unLockOrder(CreateOrderRequest request,String keyValue) {
if(StringUtils.isNotBlank(keyValue)) { if(StringUtils.isNotBlank(keyValue)) {
redisUtil.unLock(Constants.ORDER_LOCK_KEY + request.getRoomId(), keyValue, 2); redisUtil.unLock(Constants.ORDER_LOCK_KEY + request.getRoomId(), keyValue, 2);
} }
} }
private void unLockOrder(Long roomId) {
redisUtil.delete(Constants.ORDER_LOCK_KEY_PREFIX + roomId);
}
private void checkOrderPack(CreateOrderRequest request, SConsumer user) { private void checkOrderPack(CreateOrderRequest request, SConsumer user) {
if (Objects.nonNull(request.getPackId()) && request.getPackId() > 0) { if (Objects.nonNull(request.getPackId()) && request.getPackId() > 0) {
//判断是否首次订单套餐 //判断是否首次订单套餐
...@@ -1332,6 +1346,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -1332,6 +1346,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
map.put("expirationTime", date.toString()); map.put("expirationTime", date.toString());
JSONObject jsonObject = new JSONObject(map); JSONObject jsonObject = new JSONObject(map);
redisUtil.set(ReceiptRdeisEnum.ORDER_CANCEL_PAY.getValue() + orderNo, jsonObject.toString()); redisUtil.set(ReceiptRdeisEnum.ORDER_CANCEL_PAY.getValue() + orderNo, jsonObject.toString());
unLockOrder(selectSOrderByOrderNo(orderNo).getRoomId());
return true; return true;
} }
...@@ -1390,6 +1405,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme ...@@ -1390,6 +1405,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
@Override @Override
public void paymentSuccessful(SOrder sOrder) { public void paymentSuccessful(SOrder sOrder) {
unLockOrder(sOrder.getRoomId());
if (sOrder.getOrderType().equals(OrderTypeEnum.RENEW.getCode())) { if (sOrder.getOrderType().equals(OrderTypeEnum.RENEW.getCode())) {
SRoom room = roomService.getById(sOrder.getRoomId()); SRoom room = roomService.getById(sOrder.getRoomId());
if (room.getStatus().equals(RoomStatusEnum.FREE.getValue())) { if (room.getStatus().equals(RoomStatusEnum.FREE.getValue())) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment