lrj
昨天 9f8395fab13ca4b230a0f7d62636e209745c91d4
backend/src/main/java/com/rongyichuang/activity/service/ActivityService.java
@@ -2,12 +2,15 @@
import com.rongyichuang.activity.dto.ActivityInput;
import com.rongyichuang.activity.dto.ActivityJudgeInput;
import com.rongyichuang.activity.dto.ActivityJudgeResponse;
import com.rongyichuang.activity.dto.ActivityResponse;
import com.rongyichuang.activity.dto.ActivityStageInput;
import com.rongyichuang.activity.entity.Activity;
import com.rongyichuang.activity.entity.ActivityJudge;
import com.rongyichuang.activity.repository.ActivityJudgeRepository;
import com.rongyichuang.activity.repository.ActivityRepository;
import com.rongyichuang.judge.entity.Judge;
import com.rongyichuang.judge.repository.JudgeRepository;
import com.rongyichuang.common.dto.PageRequest;
import com.rongyichuang.common.dto.PageResponse;
import com.rongyichuang.rating.entity.RatingScheme;
@@ -19,6 +22,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -35,12 +39,15 @@
    private ActivityJudgeRepository activityJudgeRepository;
    
    @Autowired
    private JudgeRepository judgeRepository;
    @Autowired
    private RatingSchemeRepository ratingSchemeRepository;
    
    /**
     * 分页查询比赛列表
     */
    public PageResponse<ActivityResponse> findCompetitions(PageRequest pageRequest, String name) {
    public PageResponse<ActivityResponse> findActivities(PageRequest pageRequest, String name) {
        Pageable pageable = pageRequest.toPageable();
        Page<Activity> page;
        
@@ -67,13 +74,17 @@
            ActivityResponse response = new ActivityResponse(activity);
            
            // 如果是比赛,加载其阶段
            if (activity.isCompetition()) {
            if (activity.isMainActivity()) {
                List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(id, 1);
                List<ActivityResponse> stageResponses = stages.stream()
                    .map(ActivityResponse::new)
                    .collect(Collectors.toList());
                response.setStages(stageResponses);
            }
            // 加载评委数据
            List<ActivityJudgeResponse> judges = loadActivityJudges(id);
            response.setJudges(judges);
            
            return response;
        }
@@ -122,12 +133,12 @@
        activity = activityRepository.save(activity);
        
        // 如果是比赛且有阶段信息,保存阶段
        if (input.isCompetition() && input.getStages() != null && !input.getStages().isEmpty()) {
        if (input.isMainActivity() && input.getStages() != null && !input.getStages().isEmpty()) {
            saveActivityStages(activity.getId(), input.getStages());
        }
        
        // 如果是比赛且有评委信息,保存评委
        if (input.isCompetition() && input.getJudges() != null && !input.getJudges().isEmpty()) {
        if (input.isMainActivity() && input.getJudges() != null && !input.getJudges().isEmpty()) {
            saveActivityJudges(activity.getId(), input.getJudges());
        }
        
@@ -138,9 +149,9 @@
    /**
     * 保存比赛阶段
     */
    private void saveActivityStages(Long competitionId, List<ActivityStageInput> stageInputs) {
    private void saveActivityStages(Long activityId, List<ActivityStageInput> stageInputs) {
        // 获取现有的所有阶段
        List<Activity> existingStages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(competitionId, 1);
        List<Activity> existingStages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(activityId, 1);
        
        // 收集传入的阶段ID
        List<Long> inputStageIds = stageInputs.stream()
@@ -157,11 +168,11 @@
        }
        
        // 获取比赛信息,用于继承报名截止时间和评分模板
        Optional<Activity> competitionOpt = activityRepository.findById(competitionId);
        if (!competitionOpt.isPresent()) {
        Optional<Activity> activityOpt = activityRepository.findById(activityId);
        if (!activityOpt.isPresent()) {
            throw new RuntimeException("比赛不存在");
        }
        Activity competition = competitionOpt.get();
        Activity activity = activityOpt.get();
        
        // 处理传入的阶段(新增或更新)
        for (ActivityStageInput stageInput : stageInputs) {
@@ -170,8 +181,8 @@
            if (stageInput.isNew()) {
                // 新增阶段
                stage = new Activity();
                stage.setPid(competitionId);
                stage.setPath(generatePath(competitionId));
                stage.setPid(activityId);
                stage.setPath(generatePath(activityId));
            } else {
                // 编辑阶段
                Optional<Activity> existingOpt = activityRepository.findById(stageInput.getId());
@@ -190,13 +201,13 @@
            stage.setState(stageInput.getState());
            
            // 阶段继承比赛的报名截止时间
            stage.setSignupDeadline(competition.getSignupDeadline());
            stage.setSignupDeadline(activity.getSignupDeadline());
            
            // 阶段的评分模板:如果指定了则使用指定的,否则继承比赛的评分模板
            if (stageInput.getRatingSchemeId() != null) {
                stage.setRatingSchemeId(stageInput.getRatingSchemeId());
            } else {
                stage.setRatingSchemeId(competition.getRatingSchemeId());
                stage.setRatingSchemeId(activity.getRatingSchemeId());
            }
            
            activityRepository.save(stage);
@@ -206,36 +217,36 @@
    /**
     * 保存比赛评委
     */
    private void saveActivityJudges(Long competitionId, List<ActivityJudgeInput> judgeInputs) {
    private void saveActivityJudges(Long activityId, List<ActivityJudgeInput> judgeInputs) {
        if (judgeInputs == null || judgeInputs.isEmpty()) {
            return;
        }
        
        // 获取比赛的所有阶段(如果有的话)
        List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(competitionId, 1);
        List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(activityId, 1);
        
        // 保存评委关联
        for (ActivityJudgeInput judgeInput : judgeInputs) {
            // 先删除该评委的现有关联
            activityJudgeRepository.deleteByActivityIdAndJudgeId(competitionId, judgeInput.getJudgeId());
            activityJudgeRepository.deleteByActivityIdAndJudgeId(activityId, judgeInput.getJudgeId());
            
            if (judgeInput.getStageIds() == null || judgeInput.getStageIds().isEmpty()) {
                // 如果没有指定阶段,创建关联到所有阶段(如果有阶段)或直接关联到比赛(如果没有阶段)
                if (stages.isEmpty()) {
                    // 比赛没有阶段,直接关联到比赛(stage_id为null表示所有阶段)
                    ActivityJudge activityJudge = new ActivityJudge(competitionId, judgeInput.getJudgeId(), null);
                    ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), null);
                    activityJudgeRepository.save(activityJudge);
                } else {
                    // 为每个阶段创建关联
                    for (Activity stage : stages) {
                        ActivityJudge activityJudge = new ActivityJudge(competitionId, judgeInput.getJudgeId(), stage.getId());
                        ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), stage.getId());
                        activityJudgeRepository.save(activityJudge);
                    }
                }
            } else {
                // 为每个指定的阶段创建关联
                for (Long stageId : judgeInput.getStageIds()) {
                    ActivityJudge activityJudge = new ActivityJudge(competitionId, judgeInput.getJudgeId(), stageId);
                    ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), stageId);
                    activityJudgeRepository.save(activityJudge);
                }
            }
@@ -251,7 +262,7 @@
            Activity activity = activityOpt.get();
            
            // 如果是比赛,先删除其所有阶段
            if (activity.isCompetition()) {
            if (activity.isMainActivity()) {
                List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(id, 1);
                for (Activity stage : stages) {
                    stage.setState(0); // 软删除
@@ -275,7 +286,7 @@
        List<Activity> activities = activityRepository.findByStateOrderByPidAscNameAsc(1);
        
        // 创建比赛ID到比赛对象的映射,用于快速查找父比赛
        Map<Long, Activity> competitionMap = activities.stream()
        Map<Long, Activity> mainActivityMap = activities.stream()
            .filter(activity -> activity.getPid() == 0)
            .collect(Collectors.toMap(Activity::getId, activity -> activity));
        
@@ -285,7 +296,7 @@
                ActivityResponse response = new ActivityResponse(activity);
                // 如果是阶段(pid > 0),填充parent信息
                if (activity.getPid() > 0) {
                    Activity parentActivity = competitionMap.get(activity.getPid());
                    Activity parentActivity = mainActivityMap.get(activity.getPid());
                    if (parentActivity != null) {
                        response.setParent(new ActivityResponse(parentActivity));
                    }
@@ -330,8 +341,8 @@
    /**
     * 获取比赛的所有阶段
     */
    public List<ActivityResponse> findStagesByCompetitionId(Long competitionId) {
        List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(competitionId, 1);
    public List<ActivityResponse> findStagesByActivityId(Long activityId) {
        List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(activityId, 1);
        return stages.stream()
            .map(ActivityResponse::new)
            .collect(Collectors.toList());
@@ -340,15 +351,15 @@
    /**
     * 统计比赛数量
     */
    public long countActiveCompetitions() {
        return activityRepository.countActiveCompetitions();
    public long countActiveActivities() {
        return activityRepository.countActiveActivities();
    }
    
    /**
     * 获取进行中的比赛
     */
    public List<ActivityResponse> findOngoingCompetitions() {
        List<Activity> activities = activityRepository.findOngoingCompetitions();
    public List<ActivityResponse> findOngoingActivities() {
        List<Activity> activities = activityRepository.findOngoingActivities();
        return activities.stream()
            .map(ActivityResponse::new)
            .collect(Collectors.toList());
@@ -368,4 +379,41 @@
            return "/" + pid + "/";
        }
    }
    /**
     * 加载活动的评委数据
     */
    private List<ActivityJudgeResponse> loadActivityJudges(Long activityId) {
        // 查询活动的评委关联
        List<ActivityJudge> activityJudges = activityJudgeRepository.findByActivityId(activityId);
        // 按评委ID分组,收集每个评委负责的阶段
        Map<Long, List<Long>> judgeStageMap = activityJudges.stream()
            .collect(Collectors.groupingBy(
                ActivityJudge::getJudgeId,
                Collectors.mapping(ActivityJudge::getStageId, Collectors.toList())
            ));
        // 查询评委详细信息并构建响应
        List<ActivityJudgeResponse> result = new ArrayList<>();
        for (Map.Entry<Long, List<Long>> entry : judgeStageMap.entrySet()) {
            Long judgeId = entry.getKey();
            List<Long> stageIds = entry.getValue();
            Optional<Judge> judgeOpt = judgeRepository.findById(judgeId);
            if (judgeOpt.isPresent()) {
                Judge judge = judgeOpt.get();
                ActivityJudgeResponse judgeResponse = new ActivityJudgeResponse(
                    judge.getId(),
                    judge.getName(),
                    judge.getPhone(),
                    judge.getDescription(),
                    stageIds
                );
                result.add(judgeResponse);
            }
        }
        return result;
    }
}