瀏覽代碼

fix 下单分配车位

zoujiajian 1 年之前
父節點
當前提交
f4f9bb0d4b

+ 24 - 5
netflix-service/src/main/java/com/cyksj/service/exchange/impl/ExchangeCodeServiceImpl.java

@@ -3,6 +3,7 @@ package com.cyksj.service.exchange.impl;
 import cn.hutool.core.date.DateField;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -445,10 +446,8 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 
 			groupRelationFrontService.adjustGroupsParkingTime(relation.getId(), sku);
 		} catch (Exception e) {
-			Object value = redisService.get(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
-			if (value != null && userId.equals(Long.parseLong(value.toString()))) {
-				redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relation.getId());
-			}
+			//删除座位缓存
+			delRelationKey(relation.getId(), userId);
 			errorsRelationIds.add(relation.getId());
 			if (retryNum == 15) {
 				log.error("用户:{}获取车票:{}异常", userId, relation.getId());
@@ -480,8 +479,28 @@ public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, Exc
 		if (count == 0) {
 			log.error("车位异常 groupId:{}", groupsId);
 			groupsMapper.updateAvailableNum(groupsId);
-			redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relationId);
+			//删除座位缓存
+			delRelationKey(relationId, userId);
 			throw new BusinessRuntimeException("车位异常");
 		}
 	}
+
+	/**
+	 * 清除座位缓存key
+	 */
+	public void delRelationKey(Long relationId, Long userId) {
+		String relation_num_key = RedisKey.GROUPS_RELATION_NUM_KEY + relationId;
+		Object object = redisService.get(relation_num_key);
+		if (object != null) {
+			try {
+				Long keyUserId = Long.parseLong(object.toString());
+				//清除车票key
+				if (ObjectUtil.equal(userId, keyUserId)) {
+					redisService.del(relation_num_key);
+				}
+			} catch (Exception e) {
+				log.error("删除缓存key错误:{}", StringUtil.getErrorText(e));
+			}
+		}
+	}
 }

+ 9 - 0
netflix-service/src/main/java/com/cyksj/service/mange/impl/CmsOrderDonServiceImpl.java

@@ -797,6 +797,15 @@ public class CmsOrderDonServiceImpl extends ServiceImpl<OrderDonMapper,OrderDon>
             newRelation = getRelationThisTrips(orderDon.getUserId(), groupId, 1);
         }
 
+        //校验座位
+        int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
+                .set(GroupsRelation::getUserId, userId)
+                .eq(GroupsRelation::getUserId, 0)
+                .eq(GroupsRelation::getId, newRelation.getId()));
+        if (update == 0) {
+            throw BusinessRuntimeException.getInstance("系统繁忙,请重新操作");
+        }
+
         newRelation.setUserId(userId);
         newRelation.setStartTime(relation.getStartTime());
         newRelation.setExpiryTime(relation.getExpiryTime());

+ 29 - 1
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -1525,7 +1525,8 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		if (count == 0) {
 			log.error("车位异常 groupId:{}", groupsId);
 			groupsMapper.updateAvailableNum(groupsId);
-			redisService.del(RedisKey.GROUPS_RELATION_NUM_KEY + relationId);
+			//清除座位缓存key
+			delRelationKey(relationId, userId);
 			throw new BusinessRuntimeException("车位异常");
 		}
 	}
@@ -2517,6 +2518,14 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 
 		//锁定座位
 		setRelation(relation);
+		//校验座位
+		int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
+				.set(GroupsRelation::getUserId, userId)
+				.eq(GroupsRelation::getUserId, 0)
+				.eq(GroupsRelation::getId, relation.getId()));
+		if (update == 0) {
+			throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
+		}
 		relation.setUserId(userId);
 		//重新置为锁定
 		relation.setStatus(GroupsRelation.Status.locking);
@@ -5141,4 +5150,23 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			}
 		}
 	}
+
+	/**
+	 * 清除座位缓存key
+	 */
+	public void delRelationKey(Long relationId, Long userId) {
+		String relation_num_key = RedisKey.GROUPS_RELATION_NUM_KEY + relationId;
+		Object object = redisService.get(relation_num_key);
+		if (object != null) {
+			try {
+				Long keyUserId = Long.parseLong(object.toString());
+				//清除车票key
+				if (ObjectUtil.equal(userId, keyUserId)) {
+					redisService.del(relation_num_key);
+				}
+			} catch (Exception e) {
+				log.error("删除缓存key错误:{}", StringUtil.getErrorText(e));
+			}
+		}
+	}
 }