소스 검색

Merge branch 'codex/multi-quantity-orders' into pre

zoujiajian 2 주 전
부모
커밋
156683fe67
1개의 변경된 파일44개의 추가작업 그리고 20개의 파일을 삭제
  1. 44 20
      netflix-service/src/main/java/com/cyksj/service/relation/impl/GroupRelationFrontServiceImpl.java

+ 44 - 20
netflix-service/src/main/java/com/cyksj/service/relation/impl/GroupRelationFrontServiceImpl.java

@@ -738,26 +738,7 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
 //					if (sku.getGoodsId() == Constant.NETFLIX_GID) {
 //						filterAccountTime = DateUtil.parse("2025-06-23");
 //					}
-					groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.validity, maxNum, ip, filterAccountTime);
-
-					if (groups == null) {
-						//待发车
-						groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.waiting, maxNum, ip, null);
-					}
-					//新增空车队
-					if (groups == null) {
-						relation = groupsFuncService.createNewGroupsTrips(sku);
-					}
-					//获取车位
-					if (relation == null) {
-						//寻找快满编车队进行占座
-						relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
-						if (relation == null) {
-							Long groupsId = groups.getId();
-							TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
-							throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
-						}
-					}
+					relation = selectAvailableRelation(sku, maxNum, ip, filterAccountTime);
 				}
 			}
 			if (groups == null) {
@@ -770,6 +751,49 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
 		return relation;
 	}
 
+	/**
+	 * Repairs stale seat availability and tries another group.
+	 * Creates a new group when no real free seat can be found.
+	 */
+	private GroupsRelation selectAvailableRelation(GoodsDonSku sku, Integer maxNum, String ip,
+	                                               DateTime filterAccountTime) {
+		Set<Long> exhaustedGroupIds = new HashSet<>();
+		for (int attempt = 0; attempt < 3; attempt++) {
+			GroupsTrips candidate = selectTicketGroupsExcludingIds(
+					sku, GroupsTrips.Status.validity, maxNum, ip, filterAccountTime, exhaustedGroupIds);
+			if (candidate == null) {
+				candidate = selectTicketGroupsExcludingIds(
+						sku, GroupsTrips.Status.waiting, maxNum, ip, null, exhaustedGroupIds);
+			}
+			if (candidate == null) {
+				break;
+			}
+			GroupsRelation relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(candidate.getId());
+			if (relation != null) {
+				return relation;
+			}
+			exhaustedGroupIds.add(candidate.getId());
+			groupsMapper.updateAvailableNum(candidate.getId());
+			log.warn("Repair stale available seat count and try another group, groupsId:{}, skuId:{}",
+					candidate.getId(), sku.getId());
+		}
+		return groupsFuncService.createNewGroupsTrips(sku);
+	}
+
+	private GroupsTrips selectTicketGroupsExcludingIds(GoodsDonSku sku, GroupsTrips.Status status,
+	                                                   Integer maxNum, String ip, DateTime filterAccountTime,
+	                                                   Set<Long> exhaustedGroupIds) {
+		for (int repair = 0; repair <= exhaustedGroupIds.size(); repair++) {
+			GroupsTrips candidate = groupsMapper.selectTicketGroups(
+					sku.getId(), status, maxNum, ip, filterAccountTime);
+			if (candidate == null || !exhaustedGroupIds.contains(candidate.getId())) {
+				return candidate;
+			}
+			groupsMapper.updateAvailableNum(candidate.getId());
+		}
+		return null;
+	}
+
 	@Override
 	public String getAiVerifyCode(TicketLoginReq loginReq) throws Exception {
 		Integer type = loginReq.getType();