Browse Source

feat(game):优化分组运动员分配逻辑并修复性别校验问题

- 引入 Lombok 注解简化构造函数生成
- 注入字典服务以获取性别相关数据
-修复混合性别分组的匹配逻辑
- 优化每组运动员分配人数逻辑,避免超过道次限制
- 增加日志提示当每组人数超过道数时的处理情况
zhou 1 month ago
parent
commit
5489a7f70b

+ 21 - 11
ruoyi-modules/ruoyi-game-event/src/main/java/org/dromara/system/service/impl/GameEventGroupServiceImpl.java

@@ -1,6 +1,7 @@
 package org.dromara.system.service.impl;
 
 import cn.hutool.json.JSONUtil;
+import lombok.RequiredArgsConstructor;
 import org.dromara.common.core.utils.MapstructUtils;
 import org.dromara.common.core.utils.StringUtils;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
@@ -11,23 +12,16 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import lombok.extern.slf4j.Slf4j;
 import org.dromara.system.domain.bo.GameTeamBo;
-import org.dromara.system.domain.vo.GameEventProjectVo;
-import org.dromara.system.service.IGameEventProjectService;
-import org.dromara.system.service.IGameAthleteService;
-import org.dromara.system.service.IGameTeamService;
-import org.dromara.system.domain.vo.GameAthleteVo;
-import org.dromara.system.domain.vo.GameTeamVo;
+import org.dromara.system.domain.vo.*;
+import org.dromara.system.service.*;
 import org.springframework.stereotype.Service;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.InitializingBean;
 import org.dromara.system.domain.bo.GameEventGroupBo;
 import org.dromara.system.domain.constant.GameEventConstant;
-import org.dromara.system.domain.vo.GameEventGroupVo;
 import org.dromara.system.domain.GameEventGroup;
 import org.dromara.system.mapper.GameEventGroupMapper;
-import org.dromara.system.service.IGameEventGroupService;
-import org.dromara.system.service.IGameAthleteCompetitionGroupService;
 import jakarta.annotation.PostConstruct;
 import org.dromara.system.domain.bo.GameAthleteBo;
 import org.dromara.common.core.utils.SpringUtils;
@@ -42,6 +36,7 @@ import java.util.stream.Collectors;
  * @date 2025-07-30
  */
 @Slf4j
+@RequiredArgsConstructor
 @Service
 public class GameEventGroupServiceImpl implements IGameEventGroupService {
 
@@ -57,6 +52,8 @@ public class GameEventGroupServiceImpl implements IGameEventGroupService {
 
     private IGameAthleteCompetitionGroupService gameAthleteCompetitionGroupService;
 
+    private final ISysDictTypeService dictTypeService;
+
     @PostConstruct
     public void init() {
         // 延迟初始化依赖,避免循环依赖
@@ -283,6 +280,8 @@ public class GameEventGroupServiceImpl implements IGameEventGroupService {
         Map<String, Object> result = new HashMap<>();
 
         try {
+            //获取性别字典数据
+            List<SysDictDataVo> genderDict = dictTypeService.selectDictDataByType("sys_group_sex");
             // 获取分组信息
             GameEventGroupVo groupInfo = queryById(groupId);
             if (groupInfo == null) {
@@ -339,8 +338,12 @@ public class GameEventGroupServiceImpl implements IGameEventGroupService {
                     }
 
                     // 检查性别是否匹配
+                    Optional<String> mixGender = genderDict.stream()
+                        .filter(g -> g.getDictLabel().equals("混合"))
+                        .map(SysDictDataVo::getDictValue)
+                        .findFirst();
                     if (StringUtils.isNotBlank(groupInfo.getMemberGender()) &&
-                        !"0".equals(groupInfo.getMemberGender())) {
+                        !mixGender.equals(groupInfo.getMemberGender())) {
                         if (!groupInfo.getMemberGender().equals(
                             athlete.getGender() != null ? athlete.getGender().toString() : null)) {
                             return false;
@@ -370,7 +373,14 @@ public class GameEventGroupServiceImpl implements IGameEventGroupService {
             // 按组别和道次分配运动员
             for (int groupIndex = 1; groupIndex <= groupInfo.getIncludeGroupNum(); groupIndex++) {
                 final int currentGroupIndex = groupIndex; // 创建final变量用于lambda表达式
-                for (int track = 1; track <= groupInfo.getTrackNum(); track++) {
+                // 每组分配 personNum 个运动员,但不超过 trackNum
+                Long personsPerGroup = groupInfo.getPersonNum() != null ? groupInfo.getPersonNum() : groupInfo.getTrackNum();
+                // 如果每组人数超过道数,限制为道数(否则无法显示)
+                if (personsPerGroup > groupInfo.getTrackNum()) {
+                    log.warn("每组人数({})超过道数({}),将限制为道数", personsPerGroup, groupInfo.getTrackNum());
+                    personsPerGroup = groupInfo.getTrackNum();
+                }
+                for (int track = 1; track <= personsPerGroup; track++) {
                     // 寻找可用的运动员
                     GameAthleteVo selectedAthlete = null;
                     int athleteIndex = 0;