package com.rongyichuang.player.service;
|
|
import com.rongyichuang.player.dto.response.*;
|
import com.rongyichuang.rating.dto.response.RatingItemResponse;
|
import com.rongyichuang.rating.entity.RatingScheme;
|
import com.rongyichuang.rating.repository.RatingSchemeRepository;
|
import com.rongyichuang.common.entity.Media;
|
import com.rongyichuang.common.repository.MediaRepository;
|
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.PersistenceContext;
|
import org.springframework.stereotype.Service;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.List;
|
import java.util.ArrayList;
|
import java.util.stream.Collectors;
|
|
/**
|
* 比赛报名详情服务(用于评分页面)
|
*/
|
@Service
|
public class ActivityPlayerDetailService {
|
|
private static final Logger log = LoggerFactory.getLogger(ActivityPlayerDetailService.class);
|
|
@PersistenceContext
|
private EntityManager em;
|
|
private final MediaRepository mediaRepository;
|
private final RatingSchemeRepository ratingSchemeRepository;
|
|
public ActivityPlayerDetailService(MediaRepository mediaRepository,
|
RatingSchemeRepository ratingSchemeRepository) {
|
this.mediaRepository = mediaRepository;
|
this.ratingSchemeRepository = ratingSchemeRepository;
|
}
|
|
/**
|
* 获取比赛报名详情(用于评分)
|
* @param activityPlayerId 报名记录ID
|
* @return 报名详情
|
*/
|
public ActivityPlayerDetailResponse getDetailForRating(Long activityPlayerId) {
|
log.info("获取报名详情用于评分,activityPlayerId: {}", activityPlayerId);
|
|
// 查询基本信息
|
String sql = """
|
SELECT ap.id, ap.description, ap.activity_id,
|
p.id as player_id, p.name as player_name, p.phone, p.description as player_desc,
|
a.name as activity_name, a.rating_scheme_id
|
FROM t_avtivity_player ap
|
JOIN t_player p ON p.id = ap.player_id
|
JOIN t_activity a ON a.id = ap.activity_id
|
WHERE ap.id = ?
|
""";
|
|
@SuppressWarnings("unchecked")
|
List<Object[]> results = em.createNativeQuery(sql)
|
.setParameter(1, activityPlayerId)
|
.getResultList();
|
|
if (results.isEmpty()) {
|
log.warn("未找到报名记录,activityPlayerId: {}", activityPlayerId);
|
return null;
|
}
|
|
Object[] row = results.get(0);
|
ActivityPlayerDetailResponse response = new ActivityPlayerDetailResponse();
|
response.setId(activityPlayerId);
|
response.setDescription(row[1] != null ? row[1].toString() : "");
|
response.setActivityName(row[7] != null ? row[7].toString() : "");
|
|
// 构建学员信息
|
PlayerInfoResponse playerInfo = new PlayerInfoResponse();
|
playerInfo.setId(row[3] != null ? Long.valueOf(row[3].toString()) : null);
|
playerInfo.setName(row[4] != null ? row[4].toString() : "");
|
playerInfo.setPhone(row[5] != null ? row[5].toString() : "");
|
playerInfo.setDescription(row[6] != null ? row[6].toString() : "");
|
|
// 查询学员头像(target_type=5)
|
if (playerInfo.getId() != null) {
|
List<Media> avatarMedias = mediaRepository.findByTargetTypeAndTargetIdAndState(5, playerInfo.getId(), 1);
|
if (!avatarMedias.isEmpty()) {
|
Media avatar = avatarMedias.get(0);
|
String avatarUrl = avatar.getPath();
|
playerInfo.setAvatarUrl(avatarUrl);
|
log.info("找到学员头像: {}", avatarUrl);
|
}
|
}
|
response.setPlayerInfo(playerInfo);
|
|
// 查询提交的资料(target_type=4)
|
List<Media> submissionMedias = mediaRepository.findByTargetTypeAndTargetIdAndState(4, activityPlayerId, 1);
|
List<SubmissionMediaResponse> submissionFiles = submissionMedias.stream()
|
.map(this::convertToSubmissionMedia)
|
.collect(Collectors.toList());
|
response.setSubmissionFiles(submissionFiles);
|
log.info("找到提交资料 {} 个", submissionFiles.size());
|
|
// 查询评分模板
|
Long ratingSchemeId = row[8] != null ? Long.valueOf(row[8].toString()) : null;
|
if (ratingSchemeId != null) {
|
RatingFormResponse ratingForm = buildRatingForm(ratingSchemeId);
|
response.setRatingForm(ratingForm);
|
}
|
|
return response;
|
}
|
|
/**
|
* 转换媒体文件为提交资料响应
|
*/
|
private SubmissionMediaResponse convertToSubmissionMedia(Media media) {
|
SubmissionMediaResponse response = new SubmissionMediaResponse();
|
response.setId(media.getId());
|
response.setName(media.getName());
|
response.setUrl(media.getPath());
|
response.setFileExt(media.getFileExt());
|
response.setFileSize(media.getFileSize() != null ? media.getFileSize().longValue() : null);
|
response.setMediaType(media.getMediaType());
|
return response;
|
}
|
|
/**
|
* 构建评分表单
|
*/
|
private RatingFormResponse buildRatingForm(Long ratingSchemeId) {
|
log.info("构建评分表单,ratingSchemeId: {}", ratingSchemeId);
|
|
RatingScheme scheme = ratingSchemeRepository.findById(ratingSchemeId).orElse(null);
|
if (scheme == null) {
|
log.warn("未找到评分模板,ratingSchemeId: {}", ratingSchemeId);
|
return null;
|
}
|
|
RatingFormResponse response = new RatingFormResponse();
|
response.setSchemeId(scheme.getId());
|
response.setSchemeName(scheme.getName());
|
response.setTotalMaxScore(scheme.getTotalScore());
|
|
// 转换评分项目
|
if (scheme.getItems() != null) {
|
List<RatingItemResponse> items = scheme.getItems().stream()
|
.map(RatingItemResponse::new)
|
.collect(Collectors.toList());
|
response.setItems(items);
|
}
|
|
log.info("评分模板构建完成,包含 {} 个评分项目",
|
response.getItems() != null ? response.getItems().size() : 0);
|
|
return response;
|
}
|
}
|