| | |
| | | import com.rongyichuang.common.dto.PageResponse; |
| | | import com.rongyichuang.rating.entity.RatingScheme; |
| | | import com.rongyichuang.rating.repository.RatingSchemeRepository; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.domain.Example; |
| | | import org.springframework.data.domain.ExampleMatcher; |
| | |
| | | @Service |
| | | @Transactional |
| | | public class ActivityService { |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(ActivityService.class); |
| | | |
| | | @Autowired |
| | | private ActivityRepository activityRepository; |
| | |
| | | List<ActivityResponse> content = page.getContent().stream() |
| | | .map(activity -> { |
| | | ActivityResponse response = new ActivityResponse(activity); |
| | | // 设置参赛人数(审核通过的报名数量) |
| | | Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId()); |
| | | int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0; |
| | | // 设置参赛人数(只统计第一阶段的审核通过学员人数) |
| | | int playerCount = 0; |
| | | Activity firstStage = activityRepository.findFirstStageByActivityId(activity.getId()); |
| | | if (firstStage != null) { |
| | | // 如果有第一阶段,统计第一阶段的审核通过人数 |
| | | Long playerCountLong = activityPlayerRepository.countByStageIdAndState(firstStage.getId(), 1); |
| | | playerCount = playerCountLong != null ? playerCountLong.intValue() : 0; |
| | | } else { |
| | | // 如果没有阶段,统计活动本身的审核通过人数 |
| | | Long playerCountLong = activityPlayerRepository.countByActivityIdAndState(activity.getId(), 1); |
| | | playerCount = playerCountLong != null ? playerCountLong.intValue() : 0; |
| | | } |
| | | response.setPlayerCount(playerCount); |
| | | return response; |
| | | }) |
| | |
| | | // 保存比赛 |
| | | activity = activityRepository.save(activity); |
| | | |
| | | // 记录日志以便调试 |
| | | log.info("保存比赛成功,比赛ID: {}, 比赛名称: {}", activity.getId(), activity.getName()); |
| | | |
| | | // 如果是比赛且有阶段信息,保存阶段 |
| | | if (input.isMainActivity() && input.getStages() != null && !input.getStages().isEmpty()) { |
| | | saveActivityStages(activity.getId(), input.getStages()); |
| | |
| | | stage.setRatingSchemeId(activity.getRatingSchemeId()); |
| | | } |
| | | |
| | | activityRepository.save(stage); |
| | | // 保存阶段并获取自增ID |
| | | stage = activityRepository.save(stage); |
| | | |
| | | // 记录日志以便调试 |
| | | log.info("保存阶段成功,阶段ID: {}, 阶段名称: {}", stage.getId(), stage.getName()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private void saveActivityJudges(Long activityId, List<ActivityJudgeInput> judgeInputs) { |
| | | if (judgeInputs == null || judgeInputs.isEmpty()) { |
| | | log.info("没有评委需要保存,比赛ID: {}", activityId); |
| | | return; |
| | | } |
| | | |
| | | log.info("开始保存评委,比赛ID: {}, 评委数量: {}", activityId, judgeInputs.size()); |
| | | |
| | | // 获取比赛的所有阶段(如果有的话) |
| | | List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(activityId, 1); |
| | | |
| | | // 保存评委关联 |
| | | for (ActivityJudgeInput judgeInput : judgeInputs) { |
| | | log.info("处理评委: ID={}, 姓名={}, 阶段IDs={}", |
| | | judgeInput.getJudgeId(), judgeInput.getJudgeName(), judgeInput.getStageIds()); |
| | | |
| | | // 先删除该评委的现有关联 |
| | | activityJudgeRepository.deleteByActivityIdAndJudgeId(activityId, judgeInput.getJudgeId()); |
| | | |
| | |
| | | if (stages.isEmpty()) { |
| | | // 比赛没有阶段,直接关联到比赛(stage_id为null表示所有阶段) |
| | | ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), null); |
| | | activityJudgeRepository.save(activityJudge); |
| | | activityJudge = activityJudgeRepository.save(activityJudge); |
| | | log.info("保存评委关联成功: 比赛ID={}, 评委ID={}, 阶段ID=null", activityId, judgeInput.getJudgeId()); |
| | | } else { |
| | | // 为每个阶段创建关联 |
| | | for (Activity stage : stages) { |
| | | ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), stage.getId()); |
| | | activityJudgeRepository.save(activityJudge); |
| | | activityJudge = activityJudgeRepository.save(activityJudge); |
| | | log.info("保存评委关联成功: 比赛ID={}, 评委ID={}, 阶段ID={}", activityId, judgeInput.getJudgeId(), stage.getId()); |
| | | } |
| | | } |
| | | } else { |
| | |
| | | // 如果stageId等于当前比赛ID,表示评委负责整个比赛,stage_id设为null |
| | | Long actualStageId = stageId.equals(activityId) ? null : stageId; |
| | | ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), actualStageId); |
| | | activityJudgeRepository.save(activityJudge); |
| | | activityJudge = activityJudgeRepository.save(activityJudge); |
| | | log.info("保存评委关联成功: 比赛ID={}, 评委ID={}, 阶段ID={}", activityId, judgeInput.getJudgeId(), actualStageId); |
| | | } |
| | | } |
| | | } |
| | |
| | | // 获取所有状态为1的活动 |
| | | List<Activity> allActivities = activityRepository.findByStateOrderByPidAscNameAsc(1); |
| | | |
| | | // 过滤:只保留比赛阶段(pid>0) |
| | | // 过滤:只保留主比赛(pid=0) |
| | | List<Activity> filteredActivities = allActivities.stream() |
| | | .filter(activity -> activity.getPid() > 0) |
| | | .filter(activity -> activity.getPid() == 0) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 转换为ActivityResponse,包含父比赛信息 |
| | | // 转换为ActivityResponse |
| | | List<ActivityResponse> result = filteredActivities.stream() |
| | | .map(activity -> { |
| | | ActivityResponse response = new ActivityResponse(activity); |
| | | // 设置参赛人数(审核通过的报名数量) |
| | | // 设置参赛人数(所有阶段的报名数量总和) |
| | | Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId()); |
| | | int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0; |
| | | response.setPlayerCount(playerCount); |
| | | |
| | | // 设置父比赛信息 |
| | | Optional<Activity> parentOpt = activityRepository.findById(activity.getPid()); |
| | | if (parentOpt.isPresent()) { |
| | | Activity parent = parentOpt.get(); |
| | | response.setParent(new ActivityResponse(parent)); |
| | | } |
| | | |
| | | return response; |
| | | }) |
| | | .sorted((a, b) -> { |
| | | // 先按父比赛名称排序,再按阶段名称排序 |
| | | if (a.getParent() != null && b.getParent() != null) { |
| | | int parentCompare = a.getParent().getName().compareTo(b.getParent().getName()); |
| | | if (parentCompare != 0) { |
| | | return parentCompare; |
| | | } |
| | | } |
| | | return a.getName().compareTo(b.getName()); |
| | | }) |
| | | .sorted((a, b) -> a.getName().compareTo(b.getName())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | return result; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取所有比赛阶段(用于评审页面下拉选择) |
| | | * 返回所有状态为1且pid>0的比赛阶段 |
| | | */ |
| | | public List<ActivityResponse> findAllStagesForSelection() { |
| | | // 获取所有状态为1的活动 |
| | | List<Activity> allActivities = activityRepository.findByStateOrderByPidAscNameAsc(1); |
| | | |
| | | // 分离主比赛和阶段 |
| | | Map<Long, Activity> parentActivitiesMap = allActivities.stream() |
| | | .filter(activity -> activity.getPid() == 0) |
| | | .collect(Collectors.toMap(Activity::getId, activity -> activity)); |
| | | |
| | | // 过滤:只保留比赛阶段(pid>0) |
| | | List<Activity> filteredStages = allActivities.stream() |
| | | .filter(activity -> activity.getPid() > 0) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 转换为ActivityResponse |
| | | List<ActivityResponse> result = filteredStages.stream() |
| | | .map(activity -> { |
| | | ActivityResponse response = new ActivityResponse(activity); |
| | | // 设置参赛人数(审核通过的报名数量) |
| | | Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId()); |
| | | int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0; |
| | | response.setPlayerCount(playerCount); |
| | | |
| | | // 手动设置parent信息 |
| | | Activity parentActivity = parentActivitiesMap.get(activity.getPid()); |
| | | if (parentActivity != null) { |
| | | ActivityResponse parentResponse = new ActivityResponse(parentActivity); |
| | | response.setParent(parentResponse); |
| | | } |
| | | |
| | | return response; |
| | | }) |
| | | .sorted((a, b) -> { |
| | | // 先按父比赛ID排序,再按阶段名称排序 |
| | | int pidCompare = Long.compare(a.getPid(), b.getPid()); |
| | | if (pidCompare != 0) return pidCompare; |
| | | return a.getName().compareTo(b.getName()); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 统计比赛数量 |
| | | */ |
| | | public long countActiveActivities() { |