| | |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取所有有效比赛(用于下拉选择) |
| | | * 获取所有有效比赛和阶段(用于下拉选择) |
| | | */ |
| | | public List<ActivityResponse> findAllCompetitions() { |
| | | // 临时修改:不使用state字段进行查询 |
| | | List<Activity> activities = activityRepository.findByPidOrderByNameAsc(0L); |
| | | return activities.stream() |
| | | .map(ActivityResponse::new) |
| | | public List<ActivityResponse> findAllActivitiesForSelection() { |
| | | // 获取所有活动(包括比赛和阶段) |
| | | List<Activity> activities = activityRepository.findByStateOrderByPidAscNameAsc(1); |
| | | |
| | | // 创建比赛ID到比赛对象的映射,用于快速查找父比赛 |
| | | Map<Long, Activity> competitionMap = activities.stream() |
| | | .filter(activity -> activity.getPid() == 0) |
| | | .collect(Collectors.toMap(Activity::getId, activity -> activity)); |
| | | |
| | | // 转换为ActivityResponse并填充parent信息 |
| | | List<ActivityResponse> result = activities.stream() |
| | | .map(activity -> { |
| | | ActivityResponse response = new ActivityResponse(activity); |
| | | // 如果是阶段(pid > 0),填充parent信息 |
| | | if (activity.getPid() > 0) { |
| | | Activity parentActivity = competitionMap.get(activity.getPid()); |
| | | if (parentActivity != null) { |
| | | response.setParent(new ActivityResponse(parentActivity)); |
| | | } |
| | | } |
| | | return response; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 自定义排序:比赛和其阶段放在一起 |
| | | result.sort((a, b) -> { |
| | | // 如果都是比赛(pid=0),按名称排序 |
| | | if (a.getPid() == 0 && b.getPid() == 0) { |
| | | return a.getName().compareTo(b.getName()); |
| | | } |
| | | // 如果都是阶段,先按父比赛名称排序,再按阶段名称排序 |
| | | if (a.getPid() > 0 && b.getPid() > 0) { |
| | | String aParentName = a.getParent() != null ? a.getParent().getName() : ""; |
| | | String bParentName = b.getParent() != null ? b.getParent().getName() : ""; |
| | | int parentCompare = aParentName.compareTo(bParentName); |
| | | if (parentCompare != 0) { |
| | | return parentCompare; |
| | | } |
| | | return a.getName().compareTo(b.getName()); |
| | | } |
| | | // 如果一个是比赛,一个是阶段 |
| | | if (a.getPid() == 0 && b.getPid() > 0) { |
| | | String bParentName = b.getParent() != null ? b.getParent().getName() : ""; |
| | | int compare = a.getName().compareTo(bParentName); |
| | | return compare <= 0 ? -1 : 1; // 比赛排在其阶段前面 |
| | | } |
| | | if (a.getPid() > 0 && b.getPid() == 0) { |
| | | String aParentName = a.getParent() != null ? a.getParent().getName() : ""; |
| | | int compare = aParentName.compareTo(b.getName()); |
| | | return compare < 0 ? -1 : 1; // 阶段排在其比赛后面 |
| | | } |
| | | return 0; |
| | | }); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |