From f04f35b562760afbac0c477357e2a29f77aec3b9 Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期四, 02 十月 2025 13:51:47 +0800
Subject: [PATCH] fix: 修复评审次数重复显示问题

---
 backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerRatingService.java |  173 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 143 insertions(+), 30 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 f3b8ee9..3ddffc1 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 {
-                // 鍒涘缓鏂扮殑璇勫垎璁板綍锛屾殏鏃朵娇鐢�1浣滀负stageId鐨勯粯璁ゅ��
-                rating = new ActivityPlayerRating(activityId, activityPlayerId, 1L, 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);
             }
             
             // 淇濆瓨璇勫垎椤�
@@ -121,7 +140,7 @@
                         activityId,
                         activityPlayerId,
                         rating.getId(),
-                        1L, // stageId锛屾殏鏃朵娇鐢�1
+                        stageId, // 浣跨敤鍓嶇浼犻�掔殑stageId
                         playerId,
                         currentJudgeId,
                         ratingSchemeId,
@@ -238,51 +257,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");
             
-            // 鏌ユ壘璇ヨ瘎濮旂殑璇勫垎璁板綍鏁伴噺锛堜粠t_activity_player_rating琛ㄦ寜activity_player_id鍜宩udge_id鏌ヨ锛�
-            String ratingCountSql = "SELECT COUNT(*) FROM t_activity_player_rating WHERE activity_player_id = ? AND judge_id = ?";
+            // 鍙煡鎵炬湁鏁堢殑璇勫垎璁板綍锛坰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;
             
-            // 濡傛灉宸茶瘎鍒嗭紝鑾峰彇鏈�鏂扮殑璇勫垎璁板綍
+            // 濡傛灉宸茶瘎鍒嗭紝鑾峰彇鏈�鏂扮殑鏈夋晥璇勫垎璁板綍
             if (hasRated) {
-                Optional<ActivityPlayerRating> ratingOpt = activityPlayerRatingRepository
-                        .findByActivityPlayerIdAndJudgeId(activityPlayerId, judgeId);
+                // 鑾峰彇鏈�鏂扮殑鏈夋晥璇勫垎璁板綍锛堟寜鏇存柊鏃堕棿鍊掑簭锛屽彇绗竴鏉★級
+                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";
                 
-                if (ratingOpt.isPresent()) {
-                    ActivityPlayerRating rating = ratingOpt.get();
-                    totalScore = rating.getTotalScore();
-                    if (rating.getUpdateTime() != null) {
-                        ratingTime = rating.getUpdateTime().toString();
+                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();
+                            }
+                        }
                     }
                 }
             }
             
-            return new JudgeRatingStatusResponse(judgeId, judgeName, hasRated, ratingTime, totalScore);
+            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;
     }
 
     /**
@@ -298,8 +351,68 @@
         return new CurrentJudgeInfoResponse(
                 currentJudge.getId(),
                 currentJudge.getName(),
-                currentJudge.getPhone(),
-                currentJudge.getDescription()
+                currentJudge.getTitle(),
+                currentJudge.getCompany()
         );
     }
+
+    /**
+     * 妫�鏌ヨ瘎濮旀槸鍚﹀湪鎸囧畾姣旇禌闃舵鐨勮瘎濮斿垪琛ㄤ腑
+     */
+    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") // weightedScore 鏆傛椂浣跨敤鐩稿悓鍊�
+                    ))
+                    .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.setItems(items);
+            
+            return response;
+        } catch (Exception e) {
+            log.error("鑾峰彇璇勫璇勫垎鏄庣粏澶辫触: activityPlayerId={}, judgeId={}, error={}", 
+                     activityPlayerId, judgeId, e.getMessage(), e);
+            return null;
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0