Преглед изворни кода

refactor(game-event): 优化项目统计数据计算逻辑

- 修改 calculateProjectStats 方法签名,添加 eventId 参数
- 更新 GameProjectStatsVo 实体类字段命名,统一为 totalParticipants 和 completedParticipants
- 重构数据库查询逻辑,按项目分类动态计算参赛总数(运动员或队伍)
- 在分页查询和列表查询中均添加统计数据批量计算功能
- 优化 SQL 查询语句,通过子查询精确统计各类项目参与人数
zhou пре 3 дана
родитељ
комит
d697e2393c

+ 3 - 5
ruoyi-modules/ruoyi-game-event/src/main/java/org/dromara/system/domain/vo/GameProjectStatsVo.java

@@ -9,10 +9,8 @@ import java.io.Serializable;
 @Data
 public class GameProjectStatsVo implements Serializable {
     private Long projectId;
-    /** 运动员数量 */
-    private Integer athleteCount;
-    /** 队伍数量 */
-    private Integer teamCount;
+    /** 参赛总数(根据项目类型自动计算运动员或队伍数) */
+    private Integer totalParticipants;
     /** 完赛数量 */
-    private Integer completedCount;
+    private Integer completedParticipants;
 }

+ 1 - 1
ruoyi-modules/ruoyi-game-event/src/main/java/org/dromara/system/mapper/GameScoreMapper.java

@@ -23,7 +23,7 @@ public interface GameScoreMapper extends BaseMapperPlus<GameScore, GameScoreVo>
     /**
      * 批量统计项目的参赛和完赛情况
      */
-    List<GameProjectStatsVo> selectStatsByProjectIds(@Param("projectIds") List<Long> projectIds);
+    List<GameProjectStatsVo> selectStatsByProjectIds(@Param("eventId") Long eventId, @Param("projectIds") List<Long> projectIds);
 
     /**
      * 获取赛事下每个项目成绩最高的前三名

+ 7 - 12
ruoyi-modules/ruoyi-game-event/src/main/java/org/dromara/system/service/impl/GameEventProjectServiceImpl.java

@@ -133,7 +133,7 @@ public class GameEventProjectServiceImpl implements IGameEventProjectService {
             });
 
         // 批量计算统计数据
-        calculateProjectStats(result.getRecords());
+        calculateProjectStats(bo.getEventId(), result.getRecords());
 
         return TableDataInfo.build(result);
     }
@@ -141,7 +141,7 @@ public class GameEventProjectServiceImpl implements IGameEventProjectService {
     /**
      * 批量计算项目的统计数据(参赛总数、完赛数、未完赛数)
      */
-    private void calculateProjectStats(List<GameEventProjectVo> projectList) {
+    private void calculateProjectStats(Long eventId, List<GameEventProjectVo> projectList) {
         if (CollectionUtils.isEmpty(projectList)) {
             return;
         }
@@ -156,7 +156,7 @@ public class GameEventProjectServiceImpl implements IGameEventProjectService {
         }
 
         // 数据库聚合查询统计数据
-        List<GameProjectStatsVo> statsList = gameScoreMapper.selectStatsByProjectIds(projectIds);
+        List<GameProjectStatsVo> statsList = gameScoreMapper.selectStatsByProjectIds(eventId, projectIds);
 
         // 转换为 Map 方便查找
         Map<Long, GameProjectStatsVo> statsMap = statsList.stream()
@@ -166,15 +166,8 @@ public class GameEventProjectServiceImpl implements IGameEventProjectService {
         projectList.forEach(vo -> {
             GameProjectStatsVo stats = statsMap.get(vo.getProjectId());
             if (stats != null) {
-                // 根据归类决定总数
-                if ("0".equals(vo.getClassification())) {
-                    // 个人项目
-                    vo.setTotalParticipants(stats.getAthleteCount());
-                } else {
-                    // 团体项目
-                    vo.setTotalParticipants(stats.getTeamCount());
-                }
-                vo.setCompletedParticipants(stats.getCompletedCount());
+                vo.setTotalParticipants(stats.getTotalParticipants());
+                vo.setCompletedParticipants(stats.getCompletedParticipants());
             } else {
                 vo.setTotalParticipants(0);
                 vo.setCompletedParticipants(0);
@@ -262,6 +255,8 @@ public class GameEventProjectServiceImpl implements IGameEventProjectService {
                     vo.setRefereeGroups(refereeList);
                 });
         });
+        // 批量计算统计数据
+        calculateProjectStats(bo.getEventId(), list);
         return list;
     }
 

+ 21 - 8
ruoyi-modules/ruoyi-game-event/src/main/resources/mapper/system/GameScoreMapper.xml

@@ -7,19 +7,32 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     <!--- 根据项目Id统计该项目参赛运动员和参赛队伍的数量以及完/未完赛数-->
     <select id="selectStatsByProjectIds" resultType="org.dromara.system.domain.vo.GameProjectStatsVo">
         SELECT
-            project_id as projectId,
-            COUNT(DISTINCT athlete_id) as athleteCount,
-            COUNT(DISTINCT team_id) as teamCount,
-            SUM(CASE WHEN (individual_performance > 0) OR (team_performance > 0) THEN 1 ELSE 0 END) as completedCount
-        FROM game_score
-        WHERE del_flag = '0'
+            p.project_id as projectId,
+            (CASE
+                WHEN p.classification = '0' THEN
+                    (SELECT COUNT(*) FROM game_athlete a
+                     WHERE a.event_id = #{eventId} AND a.del_flag = '0'
+                       AND JSON_CONTAINS(a.project_value, CAST(p.project_id AS CHAR))
+                    )
+                ELSE
+                    (SELECT COUNT(*) FROM game_team t
+                     WHERE t.event_id = #{eventId} AND t.del_flag = '0'
+                       AND JSON_CONTAINS(t.project_value, CAST(p.project_id AS CHAR))
+                    )
+            END) as totalParticipants,
+            (SELECT COUNT(DISTINCT (CASE WHEN p.classification = '0' THEN s.athlete_id ELSE s.team_id END))
+             FROM game_score s
+             WHERE s.project_id = p.project_id AND s.del_flag = '0'
+               AND ((s.individual_performance > 0) OR (s.team_performance > 0))
+            ) as completedParticipants
+        FROM game_event_project p
+        WHERE p.event_id = #{eventId}
         <if test="projectIds != null and projectIds.size > 0">
-            AND project_id IN
+            AND p.project_id IN
             <foreach item="projectId" collection="projectIds" separator="," open="(" close=")">
                 #{projectId}
             </foreach>
         </if>
-        GROUP BY project_id
     </select>
 
     <select id="listAppScoreByEventIdGroupProjectId" resultType="org.dromara.system.domain.GameScore">