Codex Assistant
2 天以前 9d9519354fb304855258a5c97e26dd12c3caf5e1
backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java
@@ -2,6 +2,7 @@
import com.rongyichuang.judge.dto.request.JudgeInput;
import com.rongyichuang.judge.dto.response.JudgeResponse;
import com.rongyichuang.judge.dto.response.JudgeStatsResponse;
import com.rongyichuang.judge.entity.Judge;
import com.rongyichuang.tag.entity.Tag;
import com.rongyichuang.judge.repository.JudgeRepository;
@@ -12,12 +13,14 @@
import com.rongyichuang.common.service.MediaService;
import com.rongyichuang.common.exception.BusinessException;
import com.rongyichuang.common.enums.MediaTargetType;
import com.rongyichuang.common.util.UserContextUtil;
import com.rongyichuang.tag.repository.TagRepository;
import com.rongyichuang.user.service.UserService;
import com.rongyichuang.user.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
@@ -39,18 +42,23 @@
    private final MediaRepository mediaRepository;
    private final MediaService mediaService;
    private final UserService userService;
    private final JdbcTemplate jdbcTemplate;
    private final UserContextUtil userContextUtil;
    
    @Value("${app.media-url}")
    private String mediaBaseUrl;
    public JudgeService(JudgeRepository judgeRepository, TagRepository tagRepository, 
                       MediaRepository mediaRepository, MediaService mediaService,
                       UserService userService) {
                       UserService userService, JdbcTemplate jdbcTemplate,
                       UserContextUtil userContextUtil) {
        this.judgeRepository = judgeRepository;
        this.tagRepository = tagRepository;
        this.mediaRepository = mediaRepository;
        this.mediaService = mediaService;
        this.userService = userService;
        this.jdbcTemplate = jdbcTemplate;
        this.userContextUtil = userContextUtil;
    }
    public List<JudgeResponse> findAll() {
@@ -79,6 +87,63 @@
        return judge.orElse(null);
    }
    /**
     * 获取当前评委的统计数据
     */
    public JudgeStatsResponse getJudgeStats() {
        log.info("获取评委统计数据");
        Long currentJudgeId = userContextUtil.getCurrentJudgeId();
        if (currentJudgeId == null) {
            throw new RuntimeException("当前用户不是评委,无法查询评审统计");
        }
        // 查询我未评审的数量
        String unReviewedSql = "SELECT COUNT(*) FROM t_activity_player ap " +
                              "WHERE EXISTS (" +
                              "  SELECT 1 FROM t_activity_judge aj " +
                              "  WHERE ap.activity_id = aj.activity_id " +
                              "  AND ap.stage_id = aj.stage_id " +
                              "  AND aj.judge_id = ? " +
                              ") " +
                              "AND NOT EXISTS (" +
                              "  SELECT 1 FROM t_activity_player_rating apr " +
                              "  WHERE ap.id = apr.activity_player_id " +
                              "  AND apr.judge_id = ? " +
                              "  AND apr.state = 1 " +
                              ") " +
                              "AND ap.state = 1";
        Integer unReviewedCount = jdbcTemplate.queryForObject(unReviewedSql, Integer.class, currentJudgeId, currentJudgeId);
        // 查询我已评审的数量
        String reviewedSql = "SELECT COUNT(*) FROM t_activity_player ap " +
                            "WHERE EXISTS (" +
                            "  SELECT 1 FROM t_activity_judge aj " +
                            "  WHERE ap.activity_id = aj.activity_id " +
                            "  AND ap.stage_id = aj.stage_id " +
                            "  AND aj.judge_id = ? " +
                            ") " +
                            "AND EXISTS (" +
                            "  SELECT 1 FROM t_activity_player_rating apr " +
                            "  WHERE ap.id = apr.activity_player_id " +
                            "  AND apr.judge_id = ? " +
                            "  AND apr.state = 1 " +
                            ") " +
                            "AND ap.state = 1";
        Integer reviewedCount = jdbcTemplate.queryForObject(reviewedSql, Integer.class, currentJudgeId, currentJudgeId);
        // 计算总数
        Integer totalCount = (unReviewedCount != null ? unReviewedCount : 0) + (reviewedCount != null ? reviewedCount : 0);
        log.info("评委统计数据 - 待评审: {}, 已评审: {}, 总数: {}", unReviewedCount, reviewedCount, totalCount);
        return new JudgeStatsResponse(
            unReviewedCount != null ? unReviewedCount : 0,
            reviewedCount != null ? reviewedCount : 0,
            totalCount
        );
    }
    @Transactional
    public JudgeResponse save(JudgeInput input) {
        Judge judge;
@@ -94,18 +159,28 @@
        }
        if (input.getId() != null) {
            // 更新现有评委
            judge = judgeRepository.findById(input.getId())
                    .orElseThrow(() -> new BusinessException("评委不存在"));
            // 检查用户ID是否被其他评委使用
            Optional<Judge> existingJudgeByUserId = judgeRepository.findByUserId(user.getId());
            if (existingJudgeByUserId.isPresent() && !existingJudgeByUserId.get().getId().equals(input.getId())) {
                throw new BusinessException("该手机号已被其他评委使用");
            }
        } else {
            // 新增评委
            judge = new Judge();
            // 新增评委时检查手机号是否已存在
            if (judgeRepository.existsByPhone(input.getPhone())) {
                throw new BusinessException("PHONE_EXISTS", "手机号码已存在,请使用其他手机号码");
            // 检查该用户是否已是评委
            Optional<Judge> existingJudge = judgeRepository.findByUserId(user.getId());
            if (existingJudge.isPresent()) {
                throw new BusinessException("该手机号已被其他评委使用");
            }
        }
        judge.setName(input.getName());
        judge.setPhone(input.getPhone());
        judge.setPhone(null); // 废弃judge.phone字段,设置为null
        judge.setGender(input.getGender());
        judge.setDescription(input.getDescription());
        judge.setTitle(input.getTitle());
@@ -139,7 +214,19 @@
        JudgeResponse response = new JudgeResponse();
        response.setId(judge.getId());
        response.setName(judge.getName());
        response.setPhone(judge.getPhone());
        // 通过关联的user获取phone
        if (judge.getUserId() != null) {
            Optional<User> userOpt = userService.findById(judge.getUserId());
            if (userOpt.isPresent()) {
                response.setPhone(userOpt.get().getPhone());
            } else {
                response.setPhone(null);
            }
        } else {
            response.setPhone(null);
        }
        response.setGender(judge.getGender());
        response.setDescription(judge.getDescription());
        response.setTitle(judge.getTitle());