From 0a48616045ddce1562584543a0e89e5144051fde Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期日, 05 十月 2025 14:52:44 +0800
Subject: [PATCH] 报名审核

---
 backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerRatingService.java |  257 +++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 205 insertions(+), 52 deletions(-)

diff --git a/backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerRatingService.java b/backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerRatingService.java
index 24ca43f..94615e1 100644
--- a/backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerRatingService.java
+++ b/backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerRatingService.java
@@ -7,6 +7,7 @@
 import com.rongyichuang.common.util.UserContextUtil;
 import com.rongyichuang.judge.entity.Judge;
 import com.rongyichuang.judge.repository.JudgeRepository;
+import com.rongyichuang.activity.repository.ActivityJudgeRepository;
 import com.rongyichuang.player.dto.input.ActivityPlayerRatingInput;
 import com.rongyichuang.player.dto.input.ActivityPlayerRatingItemInput;
 import com.rongyichuang.player.dto.response.JudgeRatingStatusResponse;
@@ -50,6 +51,9 @@
     @Autowired
     private UserContextUtil userContextUtil;
 
+    @Autowired
+    private ActivityJudgeRepository activityJudgeRepository;
+
     @Transactional
     public boolean saveRating(ActivityPlayerRatingInput input) {
         try {
@@ -62,22 +66,37 @@
             }
             
             Long activityPlayerId = input.getActivityPlayerId();
+            Long stageId = input.getStageId();
+            
+            // 楠岃瘉鍓嶇浼犻�掔殑stageId
+            if (stageId == null) {
+                throw new RuntimeException("stageId涓嶈兘涓虹┖");
+            }
             
             // 鏌ヨ activity_id, player_id
             String queryPlayerSql = "SELECT activity_id, player_id FROM t_activity_player WHERE id = ?";
             Map<String, Object> playerData = jdbcTemplate.queryForMap(queryPlayerSql, activityPlayerId);
-            Long activityId = (Long) playerData.get("activity_id");
-            Long playerId = (Long) playerData.get("player_id");
-            log.info("鏌ヨ鍒扮殑鏁版嵁 - activityId: {}, playerId: {}, judgeId: {}", activityId, playerId, currentJudgeId);
+            Long activityId = ((Number) playerData.get("activity_id")).longValue();
+            Long playerId = ((Number) playerData.get("player_id")).longValue();
+            log.info("鏌ヨ鍒扮殑鏁版嵁 - activityId: {}, playerId: {}, judgeId: {}, stageId: {}", activityId, playerId, currentJudgeId, stageId);
             
-            // 楠岃瘉璇勫鏄惁鏈夋潈闄愯瘎鍒嗘娲诲姩
+            // 楠岃瘉璇勫鏄惁鏈夋潈闄愯瘎鍒嗘娲诲姩鍜岄樁娈�
             if (!userContextUtil.isCurrentUserJudgeForActivity(activityId)) {
                 throw new RuntimeException("褰撳墠璇勫鏃犳潈闄愯瘎鍒嗘娲诲姩");
             }
             
+            // 楠岃瘉璇勫鏄惁鏈夋潈闄愯瘎鍒嗘闃舵
+            String verifyJudgeStageIdSql = "SELECT COUNT(*) FROM t_activity_judge WHERE activity_id = ? AND judge_id = ? AND stage_id = ?";
+            Number judgeStageCountNumber = jdbcTemplate.queryForObject(verifyJudgeStageIdSql, Number.class, activityId, currentJudgeId, stageId);
+            Integer judgeStageCount = judgeStageCountNumber != null ? judgeStageCountNumber.intValue() : 0;
+            if (judgeStageCount == null || judgeStageCount == 0) {
+                throw new RuntimeException("褰撳墠璇勫鏃犳潈闄愯瘎鍒嗘闃舵锛宻tageId: " + stageId);
+            }
+            
             // 鏌ヨ娲诲姩鐨勮瘎鍒嗘ā鏉縄D
             String queryActivitySql = "SELECT rating_scheme_id FROM t_activity WHERE id = ?";
-            Long ratingSchemeId = jdbcTemplate.queryForObject(queryActivitySql, Long.class, activityId);
+            Number ratingSchemeIdNumber = jdbcTemplate.queryForObject(queryActivitySql, Number.class, activityId);
+            Long ratingSchemeId = ratingSchemeIdNumber != null ? ratingSchemeIdNumber.longValue() : null;
             log.info("鏌ヨ鍒扮殑ratingSchemeId: {}", ratingSchemeId);
             
             if (ratingSchemeId == null) {
@@ -95,10 +114,10 @@
                 // 鍒犻櫎宸叉湁鐨勮瘎鍒嗛」
                 activityPlayerRatingItemRepository.deleteByActivityPlayerRatingId(rating.getId());
             } else {
-                // 鍒涘缓鏂扮殑璇勫垎璁板綍
-                rating = new ActivityPlayerRating(activityId, activityPlayerId, playerId, currentJudgeId, ratingSchemeId);
+                // 鍒涘缓鏂扮殑璇勫垎璁板綍锛屼娇鐢ㄥ墠绔紶閫掔殑stageId
+                rating = new ActivityPlayerRating(activityId, activityPlayerId, stageId, playerId, currentJudgeId, ratingSchemeId);
                 rating = activityPlayerRatingRepository.save(rating);
-                log.info("鍒涘缓鏂扮殑璇勫垎璁板綍锛孖D: {}", rating.getId());
+                log.info("鍒涘缓鏂扮殑璇勫垎璁板綍锛孖D: {}, stageId: {}", rating.getId(), stageId);
             }
             
             // 淇濆瓨璇勫垎椤�
@@ -118,30 +137,32 @@
                 
                 // 鍒涘缓璇勫垎椤硅褰�
                 ActivityPlayerRatingItem ratingItemEntity = new ActivityPlayerRatingItem(
+                        activityId,
+                        activityPlayerId,
                         rating.getId(),
+                        stageId, // 浣跨敤鍓嶇浼犻�掔殑stageId
+                        playerId,
+                        currentJudgeId,
+                        ratingSchemeId,
                         itemId,
-                        ratingItem.getName(),
-                        BigDecimal.ONE, // 榛樿鏉冮噸涓�1
-                        BigDecimal.valueOf(ratingItem.getMaxScore()),
                         score
                 );
-                ratingItemEntity.setRemark(input.getComment());
                 
                 activityPlayerRatingItemRepository.save(ratingItemEntity);
                 
-                // 绱姞鍔犳潈寰楀垎
-                if (ratingItemEntity.getWeightedScore() != null) {
-                    totalScore = totalScore.add(ratingItemEntity.getWeightedScore());
+                // 绱姞寰楀垎
+                if (score != null) {
+                    totalScore = totalScore.add(score);
                 }
                 
-                log.info("淇濆瓨璇勫垎椤圭洰: itemId={}, score={}, weightedScore={}", 
-                        itemId, score, ratingItemEntity.getWeightedScore());
+                log.info("淇濆瓨璇勫垎椤圭洰: itemId={}, score={}", 
+                        itemId, score);
             }
             
             // 鏇存柊鎬诲垎鍜岀姸鎬�
             rating.setTotalScore(totalScore);
-            rating.setStatus(1); // 宸茶瘎鍒�
-            rating.setRemark(input.getComment());
+            rating.setState(1); // 宸茶瘎鍒�
+            rating.setFeedback(input.getComment());
             activityPlayerRatingRepository.save(rating);
             
             log.info("璇勫垎淇濆瓨瀹屾垚锛屾�诲垎: {}", totalScore);
@@ -185,7 +206,10 @@
      */
     public CurrentJudgeRatingResponse getCurrentJudgeRating(Long activityPlayerId) {
         Long currentJudgeId = userContextUtil.getCurrentJudgeId();
+        log.info("getCurrentJudgeRating - activityPlayerId: {}, currentJudgeId: {}", activityPlayerId, currentJudgeId);
+        
         if (currentJudgeId == null) {
+            log.warn("getCurrentJudgeRating - currentJudgeId is null");
             return null;
         }
         
@@ -193,29 +217,51 @@
                 .findByActivityPlayerIdAndJudgeId(activityPlayerId, currentJudgeId);
         
         if (!ratingOpt.isPresent()) {
+            log.info("getCurrentJudgeRating - No rating found for activityPlayerId: {} and judgeId: {}", activityPlayerId, currentJudgeId);
             return null;
         }
+        
+        log.info("getCurrentJudgeRating - Found rating with id: {}", ratingOpt.get().getId());
         
         ActivityPlayerRating rating = ratingOpt.get();
         CurrentJudgeRatingResponse response = new CurrentJudgeRatingResponse();
         response.setId(rating.getId());
         response.setTotalScore(rating.getTotalScore());
-        response.setStatus(rating.getStatus());
-        response.setRemark(rating.getRemark());
+        response.setStatus(rating.getState());
+        response.setRemark(rating.getFeedback());
         
         // 鑾峰彇璇勫垎椤�
         List<ActivityPlayerRatingItem> items = activityPlayerRatingItemRepository
                 .findByActivityPlayerRatingId(rating.getId());
-        
-        List<CurrentJudgeRatingResponse.CurrentJudgeRatingItemResponse> itemResponses = items.stream()
-                .map(item -> new CurrentJudgeRatingResponse.CurrentJudgeRatingItemResponse(
-                        item.getRatingItemId(),
-                        item.getRatingItemName(),
-                        item.getScore(),
-                        item.getWeightedScore()
-                ))
+
+        List<Long> ratingItemIds = items.stream()
+                .map(ActivityPlayerRatingItem::getRatingItemId)
+                .filter(java.util.Objects::nonNull)
+                .distinct()
                 .collect(java.util.stream.Collectors.toList());
-        
+
+        java.util.Map<Long, RatingItem> ratingItemMap = ratingItemIds.isEmpty()
+                ? java.util.Collections.emptyMap()
+                : ratingItemRepository.findAllById(ratingItemIds).stream()
+                        .collect(java.util.stream.Collectors.toMap(RatingItem::getId, java.util.function.Function.identity()));
+
+        List<CurrentJudgeRatingResponse.CurrentJudgeRatingItemResponse> itemResponses = items.stream()
+                .map(item -> {
+                    RatingItem ratingItem = ratingItemMap.get(item.getRatingItemId());
+                    String name = ratingItem != null ? ratingItem.getName() : "";
+                    BigDecimal maxScore = ratingItem != null && ratingItem.getMaxScore() != null
+                            ? BigDecimal.valueOf(ratingItem.getMaxScore()) : null;
+                    return new CurrentJudgeRatingResponse.CurrentJudgeRatingItemResponse(
+                            item.getRatingItemId(),
+                            name,
+                            item.getScore(),
+                            item.getScore(),
+                            maxScore
+                    );
+                })
+                .collect(java.util.stream.Collectors.toList());
+
+        response.setRatedAt(rating.getUpdateTime() != null ? rating.getUpdateTime().toString() : null);
         response.setItems(itemResponses);
         return response;
     }
@@ -236,44 +282,85 @@
      * 鑾峰彇鎸囧畾閫夋墜鐨勬墍鏈夎瘎濮旇瘎鍒嗙姸鎬�
      */
     public List<JudgeRatingStatusResponse> getAllJudgeRatingsForPlayer(Long activityPlayerId) {
-        // 棣栧厛鑾峰彇娲诲姩ID
-        String activitySql = "SELECT activity_id FROM t_activity_player WHERE id = ?";
-        Long activityId = jdbcTemplate.queryForObject(activitySql, Long.class, activityPlayerId);
+        log.info("寮�濮嬭幏鍙栭�夋墜璇勫璇勫垎鐘舵�侊紝activityPlayerId: {}", activityPlayerId);
         
-        if (activityId == null) {
+        // 棣栧厛鑾峰彇娲诲姩ID鍜岄樁娈礗D
+        String activityPlayerSql = "SELECT activity_id, stage_id FROM t_activity_player WHERE id = ?";
+        Map<String, Object> activityPlayerData = jdbcTemplate.queryForMap(activityPlayerSql, activityPlayerId);
+        
+        if (activityPlayerData == null) {
             throw new RuntimeException("鏈壘鍒版椿鍔ㄩ�夋墜璁板綍锛宎ctivityPlayerId: " + activityPlayerId);
         }
         
-        // 鑾峰彇娲诲姩鐨勬墍鏈夎瘎濮�
+        Long activityId = ((Number) activityPlayerData.get("activity_id")).longValue();
+        Long stageId = ((Number) activityPlayerData.get("stage_id")).longValue();
+        
+        log.info("鎵惧埌娲诲姩ID: {}, 闃舵ID: {}", activityId, stageId);
+        
+        // 鑾峰彇鎸囧畾闃舵鐨勮瘎濮旓紙閫氳繃stageId杩囨护锛岀‘淇濆敮涓�鎬э級
         String judgesSql = "SELECT j.id, j.name FROM t_judge j " +
                           "JOIN t_activity_judge aj ON j.id = aj.judge_id " +
-                          "WHERE aj.activity_id = ? ORDER BY j.name";
+                          "WHERE aj.activity_id = ? AND aj.stage_id = ? ORDER BY j.name";
         
-        List<Map<String, Object>> judgeRows = jdbcTemplate.queryForList(judgesSql, activityId);
+        List<Map<String, Object>> judgeRows = jdbcTemplate.queryForList(judgesSql, activityId, stageId);
+        log.info("鎵惧埌璇勫鏁伴噺: {}", judgeRows.size());
         
         Long currentJudgeId = userContextUtil.getCurrentJudgeId();
         
-        return judgeRows.stream().map(row -> {
+        List<JudgeRatingStatusResponse> result = judgeRows.stream().map(row -> {
             Long judgeId = ((Number) row.get("id")).longValue();
             String judgeName = (String) row.get("name");
             
-            // 鏌ユ壘璇ヨ瘎濮旂殑璇勫垎
-            Optional<ActivityPlayerRating> ratingOpt = activityPlayerRatingRepository
-                    .findByActivityPlayerIdAndJudgeId(activityPlayerId, judgeId);
+            // 鍙煡鎵炬湁鏁堢殑璇勫垎璁板綍锛坰tate=1锛夛紝閬垮厤閲嶅璁$畻
+            String ratingCountSql = "SELECT COUNT(*) FROM t_activity_player_rating WHERE activity_player_id = ? AND judge_id = ? AND state = 1";
+            Integer ratingCount = jdbcTemplate.queryForObject(ratingCountSql, Integer.class, activityPlayerId, judgeId);
             
+            Boolean hasRated = ratingCount != null && ratingCount > 0; // 璇勫娆℃暟>0琛ㄧず宸茶瘎瀹�
+            String ratingTime = null;
             BigDecimal totalScore = null;
-            Integer status = 0;
             
-            if (ratingOpt.isPresent()) {
-                ActivityPlayerRating rating = ratingOpt.get();
-                totalScore = rating.getTotalScore();
-                status = rating.getStatus();
+            // 濡傛灉宸茶瘎鍒嗭紝鑾峰彇鏈�鏂扮殑鏈夋晥璇勫垎璁板綍
+            if (hasRated) {
+                // 鑾峰彇鏈�鏂扮殑鏈夋晥璇勫垎璁板綍锛堟寜鏇存柊鏃堕棿鍊掑簭锛屽彇绗竴鏉★級
+                String latestRatingSql = "SELECT * FROM t_activity_player_rating " +
+                                       "WHERE activity_player_id = ? AND judge_id = ? AND state = 1 " +
+                                       "ORDER BY update_time DESC LIMIT 1";
+                
+                try {
+                    Map<String, Object> ratingRow = jdbcTemplate.queryForMap(latestRatingSql, activityPlayerId, judgeId);
+                    if (ratingRow != null) {
+                        totalScore = (BigDecimal) ratingRow.get("total_score");
+                        Object updateTimeObj = ratingRow.get("update_time");
+                        if (updateTimeObj != null) {
+                            ratingTime = updateTimeObj.toString();
+                        }
+                    }
+                } catch (Exception e) {
+                    // 濡傛灉鏌ヨ澶辫触锛屼娇鐢╮epository鏂规硶浣滀负澶囬��
+                    Optional<ActivityPlayerRating> ratingOpt = activityPlayerRatingRepository
+                            .findByActivityPlayerIdAndJudgeId(activityPlayerId, judgeId);
+                    
+                    if (ratingOpt.isPresent()) {
+                        ActivityPlayerRating rating = ratingOpt.get();
+                        // 鍙湁褰撹褰曠姸鎬佷负1鏃舵墠浣跨敤
+                        if (rating.getState() != null && rating.getState() == 1) {
+                            totalScore = rating.getTotalScore();
+                            if (rating.getUpdateTime() != null) {
+                                ratingTime = rating.getUpdateTime().toString();
+                            }
+                        }
+                    }
+                }
             }
             
-            Boolean isCurrentJudge = judgeId.equals(currentJudgeId);
-            
-            return new JudgeRatingStatusResponse(judgeId, judgeName, totalScore, status, isCurrentJudge);
+            JudgeRatingStatusResponse response = new JudgeRatingStatusResponse(judgeId, judgeName, hasRated, ratingTime, totalScore);
+            log.info("璇勫 {} (ID: {}) 璇勫垎鐘舵��: hasRated={}, totalScore={}, ratingTime={}", 
+                    judgeName, judgeId, hasRated, totalScore, ratingTime);
+            return response;
         }).collect(java.util.stream.Collectors.toList());
+        
+        log.info("杩斿洖璇勫璇勫垎鐘舵�佸垪琛紝鎬绘暟: {}", result.size());
+        return result;
     }
 
     /**
@@ -289,8 +376,74 @@
         return new CurrentJudgeInfoResponse(
                 currentJudge.getId(),
                 currentJudge.getName(),
-                currentJudge.getPhone(),
-                currentJudge.getDescription()
+                currentJudge.getTitle(),
+                currentJudge.getCompany()
         );
     }
-}
\ No newline at end of file
+
+    /**
+     * 妫�鏌ヨ瘎濮旀槸鍚﹀湪鎸囧畾姣旇禌闃舵鐨勮瘎濮斿垪琛ㄤ腑
+     */
+    public boolean isJudgeInActivity(Long stageId, Long judgeId) {
+        try {
+            return activityJudgeRepository.existsByStageIdAndJudgeId(stageId, judgeId);
+        } catch (Exception e) {
+            log.error("妫�鏌ヨ瘎濮旀潈闄愭椂鍙戠敓寮傚父: stageId={}, judgeId={}, error={}", stageId, judgeId, e.getMessage(), e);
+            return false;
+        }
+    }
+
+    /**
+     * 鑾峰彇鎸囧畾璇勫瀵规寚瀹氶�夋墜鐨勮瘎鍒嗘槑缁�
+     */
+    public CurrentJudgeRatingResponse getJudgeRatingDetail(Long activityPlayerId, Long judgeId) {
+        try {
+            Optional<ActivityPlayerRating> ratingOpt = activityPlayerRatingRepository
+                    .findByActivityPlayerIdAndJudgeId(activityPlayerId, judgeId);
+            
+            if (!ratingOpt.isPresent()) {
+                return null;
+            }
+            
+            ActivityPlayerRating rating = ratingOpt.get();
+            
+            // 鏌ヨ璇勫垎鏄庣粏椤�
+            String itemsSql = "SELECT ri.id as rating_item_id, ri.name as rating_item_name, " +
+                             "apri.score, ri.max_score " +
+                             "FROM t_activity_player_rating_item apri " +
+                             "JOIN t_rating_item ri ON apri.rating_item_id = ri.id " +
+                             "WHERE apri.activity_player_rating_id = ? " +
+                             "ORDER BY ri.order_no";
+            
+            List<Map<String, Object>> itemRows = jdbcTemplate.queryForList(itemsSql, rating.getId());
+            
+            List<CurrentJudgeRatingResponse.CurrentJudgeRatingItemResponse> items = itemRows.stream()
+                    .map(row -> new CurrentJudgeRatingResponse.CurrentJudgeRatingItemResponse(
+                            ((Number) row.get("rating_item_id")).longValue(),
+                            (String) row.get("rating_item_name"),
+                            (BigDecimal) row.get("score"),
+                            (BigDecimal) row.get("score"),
+                            row.get("max_score") instanceof BigDecimal
+                                    ? (BigDecimal) row.get("max_score")
+                                    : row.get("max_score") instanceof Number
+                                        ? BigDecimal.valueOf(((Number) row.get("max_score")).doubleValue())
+                                        : null
+                    ))
+                    .collect(java.util.stream.Collectors.toList());
+            
+            CurrentJudgeRatingResponse response = new CurrentJudgeRatingResponse();
+            response.setId(rating.getId());
+            response.setTotalScore(rating.getTotalScore());
+            response.setStatus(rating.getState());
+            response.setRemark(rating.getFeedback());
+            response.setRatedAt(rating.getUpdateTime() != null ? rating.getUpdateTime().toString() : null);
+            response.setItems(items);
+            
+            return response;
+        } catch (Exception e) {
+            log.error("鑾峰彇璇勫璇勫垎鏄庣粏澶辫触: activityPlayerId={}, judgeId={}, error={}", 
+                     activityPlayerId, judgeId, e.getMessage(), e);
+            return null;
+        }
+    }
+}

--
Gitblit v1.8.0