import { graphqlRequest } from '@/config/api'
|
|
// GraphQL 查询语句
|
|
// 获取进行中的比赛列表(包括阶段)
|
const GET_ACTIVE_ACTIVITIES_QUERY = `
|
query GetActiveActivities {
|
allActivities {
|
id
|
pid
|
name
|
state
|
parent {
|
id
|
name
|
}
|
}
|
}
|
`
|
|
// 获取比赛项目列表(分页)
|
const GET_COMPETITION_PROJECTS_QUERY = `
|
query GetCompetitionProjects($activityId: ID, $page: Int, $size: Int) {
|
activityPlayerApplications(activityId: $activityId, page: $page, size: $size) {
|
id
|
playerName
|
activityName
|
projectName
|
phone
|
applyTime
|
state
|
}
|
}
|
`
|
|
// 获取项目详情
|
export const GET_PROJECT_DETAIL_QUERY = `
|
query GetProjectDetail($id: ID!) {
|
activityPlayerDetail(id: $id) {
|
id
|
playerInfo {
|
id
|
name
|
phone
|
gender
|
education
|
introduction
|
avatarUrl
|
birthday
|
}
|
regionInfo {
|
name
|
}
|
activityName
|
projectName
|
description
|
submissionFiles {
|
id
|
name
|
url
|
thumbUrl
|
fileExt
|
fileSize
|
mediaType
|
}
|
ratingForm {
|
schemeId
|
schemeName
|
totalMaxScore
|
items {
|
id
|
name
|
maxScore
|
orderNo
|
}
|
}
|
state
|
feedback
|
}
|
}
|
`
|
|
// 获取评审统计信息
|
export const GET_RATING_STATS_QUERY = `
|
query GetRatingStats($activityPlayerId: ID!) {
|
judgeRatingsForPlayer(activityPlayerId: $activityPlayerId) {
|
judgeId
|
judgeName
|
hasRated
|
totalScore
|
ratingTime
|
}
|
averageScoreForPlayer(activityPlayerId: $activityPlayerId)
|
}
|
`
|
|
// 获取当前评委的评分信息
|
const GET_CURRENT_JUDGE_RATING_QUERY = `
|
query GetCurrentJudgeRating($activityPlayerId: ID!) {
|
currentJudgeRating(activityPlayerId: $activityPlayerId) {
|
id
|
totalScore
|
comments
|
ratingItems {
|
itemId
|
itemName
|
score
|
maxScore
|
}
|
}
|
}
|
`
|
|
// 提交评分
|
const SUBMIT_RATING_MUTATION = `
|
mutation SubmitRating($input: ActivityPlayerRatingInput!) {
|
saveActivityPlayerRating(input: $input)
|
}
|
`
|
|
// API 函数
|
|
/**
|
* 获取进行中的比赛阶段列表(state=1且pid>0的比赛阶段)
|
* 按比赛分组排序,显示格式为"比赛名 + 阶段名"
|
*/
|
export const getActiveActivities = async () => {
|
try {
|
const data = await graphqlRequest(GET_ACTIVE_ACTIVITIES_QUERY)
|
|
// 过滤出state=1且pid>0的比赛阶段
|
const stages = data.allActivities.filter(activity =>
|
activity.state === 1 && activity.pid > 0
|
)
|
|
// 按比赛ID(pid)分组排序,确保同一比赛的不同阶段放在一起
|
stages.sort((a, b) => {
|
// 首先按比赛ID排序
|
if (a.pid !== b.pid) {
|
return a.pid - b.pid
|
}
|
// 同一比赛内按阶段ID排序
|
return a.id - b.id
|
})
|
|
return stages
|
} catch (error) {
|
console.error('获取活动列表失败:', error)
|
throw new Error(error.message || '获取活动列表失败')
|
}
|
}
|
|
/**
|
* 获取比赛项目列表
|
*/
|
export const getCompetitionProjects = async (activityId, page = 0, size = 10) => {
|
try {
|
const data = await graphqlRequest(GET_COMPETITION_PROJECTS_QUERY, {
|
activityId,
|
page,
|
size
|
})
|
|
const projects = data.activityPlayerApplications || []
|
|
// 为每个项目获取评分统计
|
const enrichedProjects = await Promise.all(
|
projects.map(async (project) => {
|
try {
|
const stats = await getRatingStats(project.id)
|
return {
|
id: project.id,
|
playerName: project.playerName,
|
activityName: project.activityName,
|
phone: project.phone,
|
applyTime: project.applyTime,
|
state: project.state,
|
projectName: project.projectName || project.playerName + '的项目', // 使用真实项目名称,如果为空则使用默认名称
|
ratingCount: stats.ratingCount,
|
averageScore: stats.averageScore
|
}
|
} catch (error) {
|
console.warn(`Failed to get rating stats for project ${project.id}:`, error)
|
return {
|
id: project.id,
|
playerName: project.playerName,
|
activityName: project.activityName,
|
phone: project.phone,
|
applyTime: project.applyTime,
|
state: project.state,
|
projectName: project.projectName || project.playerName + '的项目', // 使用真实项目名称,如果为空则使用默认名称
|
ratingCount: 0,
|
averageScore: 0
|
}
|
}
|
})
|
)
|
|
// 模拟分页信息,因为API返回的是数组而不是分页对象
|
const totalElements = projects.length
|
const totalPages = Math.ceil(totalElements / size)
|
|
return {
|
content: enrichedProjects,
|
totalElements,
|
totalPages,
|
number: page,
|
size
|
}
|
} catch (error) {
|
console.error('Error fetching competition projects:', error)
|
throw error
|
}
|
}
|
|
/**
|
* 获取项目详情
|
*/
|
export const getProjectDetail = async (id) => {
|
try {
|
const data = await graphqlRequest(GET_PROJECT_DETAIL_QUERY, { id })
|
return data.activityPlayerDetail
|
} catch (error) {
|
throw new Error(error.message || '获取项目详情失败')
|
}
|
}
|
|
/**
|
* 获取评审统计信息
|
*/
|
export const getRatingStats = async (activityPlayerId) => {
|
try {
|
const data = await graphqlRequest(GET_RATING_STATS_QUERY, { activityPlayerId })
|
|
const ratings = data.judgeRatingsForPlayer || []
|
const averageScore = data.averageScoreForPlayer || 0
|
|
// 只计算已评分的评委
|
const ratedJudges = ratings.filter(rating => rating.hasRated)
|
|
return {
|
ratingCount: ratedJudges.length,
|
averageScore: averageScore,
|
ratings: ratings
|
}
|
} catch (error) {
|
console.error('Error fetching rating stats:', error)
|
// 返回默认值而不是抛出错误,避免影响主要功能
|
return {
|
ratingCount: 0,
|
averageScore: 0,
|
ratings: []
|
}
|
}
|
}
|
|
/**
|
* 获取当前评委的评分信息
|
*/
|
export const getCurrentJudgeRating = async (activityPlayerId) => {
|
try {
|
const data = await graphqlRequest(GET_CURRENT_JUDGE_RATING_QUERY, { activityPlayerId })
|
return data.currentJudgeRating
|
} catch (error) {
|
throw new Error(error.message || '获取当前评委评分失败')
|
}
|
}
|
|
/**
|
* 提交评分
|
*/
|
export const submitRating = async (ratingInput) => {
|
try {
|
const data = await graphqlRequest(SUBMIT_RATING_MUTATION, { input: ratingInput })
|
return data.saveActivityPlayerRating
|
} catch (error) {
|
throw new Error(error.message || '提交评分失败')
|
}
|
}
|