| | |
| | | import com.rongyichuang.player.entity.Player; |
| | | import com.rongyichuang.player.repository.ActivityPlayerRepository; |
| | | import com.rongyichuang.player.repository.PlayerRepository; |
| | | import com.rongyichuang.activity.repository.ActivityRepository; |
| | | import com.rongyichuang.activity.entity.Activity; |
| | | import com.rongyichuang.common.entity.Media; |
| | | import com.rongyichuang.common.repository.MediaRepository; |
| | | import com.rongyichuang.common.enums.MediaTargetType; |
| | |
| | | |
| | | @Autowired |
| | | private PlayerRepository playerRepository; |
| | | |
| | | @Autowired |
| | | private ActivityRepository activityRepository; |
| | | |
| | | @Autowired |
| | | private MediaRepository mediaRepository; |
| | |
| | | } |
| | | log.info("未发现重复报名"); |
| | | |
| | | // 4. 创建报名记录 |
| | | // 4. 查找第一阶段,如果没有则使用活动本身 |
| | | log.info("查找活动的第一阶段,活动ID: {}", input.getActivityId()); |
| | | Activity firstStage = activityRepository.findFirstStageByActivityId(input.getActivityId()); |
| | | Long stageId; |
| | | if (firstStage != null) { |
| | | stageId = firstStage.getId(); |
| | | log.info("找到第一阶段,阶段ID: {}, 阶段名称: {}", firstStage.getId(), firstStage.getName()); |
| | | } else { |
| | | // 如果没有找到第一阶段,使用活动本身作为阶段 |
| | | stageId = input.getActivityId(); |
| | | log.info("未找到第一阶段,使用活动本身作为阶段,活动ID: {}", input.getActivityId()); |
| | | } |
| | | |
| | | // 5. 创建报名记录 |
| | | log.info("开始创建报名记录"); |
| | | ActivityPlayer activityPlayer = new ActivityPlayer(); |
| | | activityPlayer.setActivityId(input.getActivityId()); |
| | | activityPlayer.setStageId(input.getActivityId()); // 根据文档:如果比赛未定义阶段,stage_id设为activity_id |
| | | activityPlayer.setStageId(stageId); // 绑定到第一阶段或活动本身 |
| | | activityPlayer.setPlayerId(player.getId()); |
| | | activityPlayer.setRegionId(input.getRegionId()); |
| | | activityPlayer.setProjectName(input.getProjectName()); // 设置项目名称 |
| | |
| | | ActivityPlayer savedActivityPlayer = activityPlayerRepository.save(activityPlayer); |
| | | log.info("报名记录创建成功,ID: {}", savedActivityPlayer.getId()); |
| | | |
| | | // 5. 保存其他媒体文件(兼容旧版本) |
| | | // 6. 保存其他媒体文件(兼容旧版本) |
| | | if (input.getMediaFiles() != null && !input.getMediaFiles().isEmpty()) { |
| | | saveMediaFiles(savedActivityPlayer.getId(), input.getMediaFiles()); |
| | | } |
| | | |
| | | // 6. 保存头像媒体记录 |
| | | // 7. 保存头像媒体记录 |
| | | if (input.getPlayerInfo().getAvatarMediaId() != null && !input.getPlayerInfo().getAvatarMediaId().trim().isEmpty()) { |
| | | savePlayerAvatarMedia(player.getId(), input.getPlayerInfo().getAvatarMediaId()); |
| | | } |
| | | |
| | | // 7. 保存附件媒体记录 |
| | | // 8. 保存附件媒体记录 |
| | | if (input.getAttachmentMediaIds() != null && !input.getAttachmentMediaIds().isEmpty()) { |
| | | log.info("开始保存附件媒体记录,报名ID: {}, 附件数量: {}", savedActivityPlayer.getId(), input.getAttachmentMediaIds().size()); |
| | | saveAttachmentMediaRecords(savedActivityPlayer.getId(), input.getAttachmentMediaIds()); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 保存学员头像 |
| | | * 参考judge模块的实现,将已上传的头像媒体文件关联到学员 |
| | | * 审核通过 |
| | | */ |
| | | public Boolean approveActivityPlayer(Long activityPlayerId, String feedback) { |
| | | try { |
| | | Optional<ActivityPlayer> activityPlayerOpt = activityPlayerRepository.findById(activityPlayerId); |
| | | if (!activityPlayerOpt.isPresent()) { |
| | | throw new RuntimeException("报名记录不存在"); |
| | | } |
| | | |
| | | ActivityPlayer activityPlayer = activityPlayerOpt.get(); |
| | | activityPlayer.setState(1); // 1=审核通过 |
| | | activityPlayer.setFeedback(feedback); |
| | | activityPlayerRepository.save(activityPlayer); |
| | | |
| | | log.info("审核通过成功,activityPlayerId: {}", activityPlayerId); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("审核通过失败,activityPlayerId: {}", activityPlayerId, e); |
| | | throw new RuntimeException("审核通过失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 审核驳回 |
| | | */ |
| | | public Boolean rejectActivityPlayer(Long activityPlayerId, String feedback) { |
| | | try { |
| | | Optional<ActivityPlayer> activityPlayerOpt = activityPlayerRepository.findById(activityPlayerId); |
| | | if (!activityPlayerOpt.isPresent()) { |
| | | throw new RuntimeException("报名记录不存在"); |
| | | } |
| | | |
| | | ActivityPlayer activityPlayer = activityPlayerOpt.get(); |
| | | activityPlayer.setState(2); // 2=审核驳回 |
| | | activityPlayer.setFeedback(feedback); |
| | | activityPlayerRepository.save(activityPlayer); |
| | | |
| | | log.info("审核驳回成功,activityPlayerId: {}", activityPlayerId); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("审核驳回失败,activityPlayerId: {}", activityPlayerId, e); |
| | | throw new RuntimeException("审核驳回失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新报名信息 |
| | | */ |
| | | public ActivityRegistrationResponse updateActivityRegistration(Long activityPlayerId, ActivityRegistrationInput input) { |
| | | try { |
| | | log.info("开始更新报名信息,报名ID: {}", activityPlayerId); |
| | | |
| | | // 1. 查找现有报名记录 |
| | | Optional<ActivityPlayer> activityPlayerOpt = activityPlayerRepository.findById(activityPlayerId); |
| | | if (!activityPlayerOpt.isPresent()) { |
| | | throw new RuntimeException("报名记录不存在"); |
| | | } |
| | | |
| | | ActivityPlayer activityPlayer = activityPlayerOpt.get(); |
| | | |
| | | // 2. 更新选手信息 |
| | | Player player = playerRepository.findById(activityPlayer.getPlayerId()).orElse(null); |
| | | if (player != null) { |
| | | player.setName(input.getPlayerInfo().getName()); |
| | | if (input.getPlayerInfo().getGender() != null) { |
| | | player.setGender(input.getPlayerInfo().getGender()); |
| | | } |
| | | if (input.getPlayerInfo().getEducation() != null) { |
| | | player.setEducation(input.getPlayerInfo().getEducation()); |
| | | } |
| | | if (input.getPlayerInfo().getIntroduction() != null) { |
| | | player.setIntroduction(input.getPlayerInfo().getIntroduction()); |
| | | } |
| | | if (input.getPlayerInfo().getDescription() != null) { |
| | | player.setDescription(input.getPlayerInfo().getDescription()); |
| | | } |
| | | playerRepository.save(player); |
| | | |
| | | // 更新用户信息 |
| | | updateUserInfo(player, input); |
| | | } |
| | | |
| | | // 3. 更新报名记录基本信息 |
| | | if (input.getProjectName() != null) { |
| | | activityPlayer.setProjectName(input.getProjectName()); |
| | | } |
| | | if (input.getDescription() != null) { |
| | | activityPlayer.setDescription(input.getDescription()); |
| | | } |
| | | if (input.getRegionId() != null) { |
| | | activityPlayer.setRegionId(input.getRegionId()); |
| | | } |
| | | |
| | | activityPlayerRepository.save(activityPlayer); |
| | | |
| | | // 4. 处理头像更新 |
| | | if (input.getPlayerInfo().getAvatarMediaId() != null && !input.getPlayerInfo().getAvatarMediaId().isEmpty()) { |
| | | // 保存新头像 |
| | | savePlayerAvatarMedia(player.getId(), input.getPlayerInfo().getAvatarMediaId()); |
| | | } |
| | | |
| | | // 5. 处理比赛图片和视频更新 |
| | | if (input.getMediaFiles() != null) { |
| | | // 删除旧的比赛图片和视频记录 |
| | | deleteOldSubmissionMedia(activityPlayerId); |
| | | // 保存新的比赛图片和视频 |
| | | saveMediaFiles(activityPlayerId, input.getMediaFiles()); |
| | | } |
| | | |
| | | // 6. 处理附件更新 |
| | | if (input.getAttachmentMediaIds() != null) { |
| | | // 删除旧附件记录 |
| | | deleteOldAttachmentMedia(activityPlayerId); |
| | | // 保存新附件 |
| | | saveAttachmentMediaRecords(activityPlayerId, input.getAttachmentMediaIds()); |
| | | } |
| | | |
| | | log.info("更新报名信息成功,报名ID: {}", activityPlayerId); |
| | | |
| | | ActivityRegistrationResponse response = new ActivityRegistrationResponse(); |
| | | response.setSuccess(true); |
| | | response.setMessage("更新报名信息成功"); |
| | | response.setRegistrationId(activityPlayerId); |
| | | response.setPlayerId(player.getId()); |
| | | response.setUserId(player.getUserId()); |
| | | response.setActivityPlayerId(activityPlayerId); |
| | | |
| | | return response; |
| | | |
| | | } catch (Exception e) { |
| | | log.error("更新报名信息失败,报名ID: {}", activityPlayerId, e); |
| | | throw new RuntimeException("更新报名信息失败: " + e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 删除旧的比赛图片和视频记录 |
| | | */ |
| | | private void deleteOldSubmissionMedia(Long activityPlayerId) { |
| | | try { |
| | | List<Media> oldSubmissionMedia = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.ACTIVITY_PLAYER_SUBMISSION.getValue(), activityPlayerId, 1); |
| | | for (Media media : oldSubmissionMedia) { |
| | | media.setState(0); // 设置为删除状态 |
| | | mediaRepository.save(media); |
| | | } |
| | | log.info("删除旧比赛图片/视频记录成功,报名ID: {}, 删除数量: {}", activityPlayerId, oldSubmissionMedia.size()); |
| | | } catch (Exception e) { |
| | | log.error("删除旧比赛图片/视频记录失败,报名ID: {}", activityPlayerId, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除旧的附件媒体记录 |
| | | */ |
| | | private void deleteOldAttachmentMedia(Long activityPlayerId) { |
| | | try { |
| | | List<Media> oldAttachments = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | 5, activityPlayerId, 1); // target_type=5 是附件 |
| | | for (Media oldAttachment : oldAttachments) { |
| | | oldAttachment.setState(0); // 设置为删除状态 |
| | | mediaRepository.save(oldAttachment); |
| | | } |
| | | log.info("删除旧附件记录成功,报名ID: {}, 删除数量: {}", activityPlayerId, oldAttachments.size()); |
| | | } catch (Exception e) { |
| | | log.error("删除旧附件记录失败,报名ID: {}", activityPlayerId, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新审核意见 |
| | | */ |
| | | public Boolean updateFeedback(Long activityPlayerId, String feedback) { |
| | | try { |
| | | Optional<ActivityPlayer> activityPlayerOpt = activityPlayerRepository.findById(activityPlayerId); |
| | | if (!activityPlayerOpt.isPresent()) { |
| | | throw new RuntimeException("报名记录不存在"); |
| | | } |
| | | |
| | | ActivityPlayer activityPlayer = activityPlayerOpt.get(); |
| | | activityPlayer.setFeedback(feedback); |
| | | activityPlayerRepository.save(activityPlayer); |
| | | |
| | | log.info("更新审核意见成功,activityPlayerId: {}", activityPlayerId); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("更新审核意见失败,activityPlayerId: {}", activityPlayerId, e); |
| | | throw new RuntimeException("更新审核意见失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存用户头像 |
| | | * 将已上传的头像媒体文件关联到用户 |
| | | */ |
| | | private void savePlayerAvatar(Long playerId, Long avatarMediaId) { |
| | | try { |
| | | // 查找现有的头像记录并删除(确保一个学员只有一个头像) |
| | | mediaRepository.deleteByTargetTypeAndTargetId(MediaTargetType.STUDENT_AVATAR.getValue(), playerId); |
| | | // 获取player对应的用户ID |
| | | Optional<Player> playerOpt = playerRepository.findById(playerId); |
| | | if (!playerOpt.isPresent()) { |
| | | log.warn("未找到学员记录,学员ID: {}", playerId); |
| | | return; |
| | | } |
| | | |
| | | Player player = playerOpt.get(); |
| | | Long userId = player.getUserId(); |
| | | if (userId == null) { |
| | | log.warn("学员未关联用户,学员ID: {}", playerId); |
| | | return; |
| | | } |
| | | |
| | | // 查找现有的头像记录并删除(确保一个用户只有一个头像) |
| | | mediaRepository.deleteByTargetTypeAndTargetId(MediaTargetType.USER_AVATAR.getValue(), userId); |
| | | |
| | | // 更新媒体文件的target信息 |
| | | Media avatarMedia = mediaRepository.findById(avatarMediaId).orElse(null); |
| | | if (avatarMedia != null) { |
| | | avatarMedia.setTargetType(MediaTargetType.STUDENT_AVATAR.getValue()); |
| | | avatarMedia.setTargetId(playerId); |
| | | avatarMedia.setTargetType(MediaTargetType.USER_AVATAR.getValue()); |
| | | avatarMedia.setTargetId(userId); |
| | | mediaRepository.save(avatarMedia); |
| | | log.info("学员头像保存成功,学员ID: {}, 媒体ID: {}", playerId, avatarMediaId); |
| | | log.info("用户头像保存成功,用户ID: {}, 媒体ID: {}", userId, avatarMediaId); |
| | | } else { |
| | | log.warn("未找到头像媒体文件,媒体ID: {}", avatarMediaId); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("保存学员头像时发生错误,学员ID: {}, 媒体ID: {}", playerId, avatarMediaId, e); |
| | | log.error("保存用户头像时发生错误,学员ID: {}, 媒体ID: {}", playerId, avatarMediaId, e); |
| | | } |
| | | } |
| | | |
| | |
| | | MediaSaveInput input = new MediaSaveInput(); |
| | | input.setTargetType("player"); |
| | | input.setTargetId(playerId); |
| | | input.setUrl(avatarMediaId); // COS路径 |
| | | input.setPath(avatarMediaId); // COS路径 |
| | | input.setFileName("avatar.jpg"); // 默认头像文件名 |
| | | input.setFileExt("jpg"); // 文件扩展名 |
| | | input.setFileSize(0L); // 文件大小暂时设为0 |
| | |
| | | MediaSaveInput input = new MediaSaveInput(); |
| | | input.setTargetType("activity_player"); |
| | | input.setTargetId(activityPlayerId); |
| | | input.setUrl(mediaId); // COS路径 |
| | | input.setPath(mediaId); // COS路径 |
| | | input.setFileName("attachment"); // 默认附件文件名 |
| | | input.setFileExt("jpg"); // 文件扩展名 |
| | | input.setFileSize(0L); // 文件大小暂时设为0 |