| | |
| | | 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; |
| | |
| | | @Transactional |
| | | public class ActivityService { |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(ActivityService.class); |
| | | |
| | | @Autowired |
| | | private ActivityRepository activityRepository; |
| | | |
| | |
| | | /** |
| | | * 分页查询比赛列表 |
| | | */ |
| | | public PageResponse<ActivityResponse> findActivities(PageRequest pageRequest, String name) { |
| | | public PageResponse<ActivityResponse> findActivities(PageRequest pageRequest, String name, Integer state) { |
| | | Pageable pageable = pageRequest.toPageable(); |
| | | Page<Activity> page; |
| | | |
| | | if (StringUtils.hasText(name)) { |
| | | boolean hasName = StringUtils.hasText(name); |
| | | if (state != null) { |
| | | if (hasName) { |
| | | page = activityRepository.findByPidAndStateAndNameContainingOrderByCreateTimeDesc(0L, state, name, pageable); |
| | | } else { |
| | | page = activityRepository.findByPidAndStateOrderByCreateTimeDesc(0L, state, pageable); |
| | | } |
| | | } else if (hasName) { |
| | | page = activityRepository.findByPidAndNameContainingOrderByCreateTimeDesc(0L, name, pageable); |
| | | } else { |
| | | // 查询所有主活动(pid = 0) |
| | |
| | | return response; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 调试:打印返回给前端的响应对象中的报名截止 |
| | | try { |
| | | content.stream().limit(10).forEach(r -> |
| | | log.info("Response(id={}, name={}) signupDeadline={}, matchTime={}", r.getId(), r.getName(), r.getSignupDeadline(), r.getMatchTime()) |
| | | ); |
| | | } catch (Exception e) { |
| | | log.warn("打印ActivityResponse日志失败: {}", e.getMessage()); |
| | | } |
| | | |
| | | return new PageResponse<>(content, page.getTotalElements(), page.getNumber(), page.getSize()); |
| | | } |
| | |
| | | // 保存比赛 |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public boolean updateActivityState(Long id, Integer state) { |
| | | Optional<Activity> activityOpt = activityRepository.findById(id); |
| | | if (activityOpt.isPresent()) { |
| | | Activity activity = activityOpt.get(); |
| | | activity.setState(state); |
| | | activityRepository.save(activity); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取所有有效主比赛(用于下拉选择) |