tingwu_new
This commit is contained in:
@@ -127,6 +127,10 @@ class BailianService:
|
||||
evaluation.reasoning = response_content
|
||||
|
||||
evaluation.save()
|
||||
|
||||
# 同步结果到参赛项目 (如果关联了)
|
||||
self._sync_evaluation_to_project(evaluation)
|
||||
|
||||
return evaluation
|
||||
|
||||
except Exception as e:
|
||||
@@ -136,6 +140,103 @@ class BailianService:
|
||||
evaluation.save()
|
||||
return evaluation
|
||||
|
||||
def _sync_evaluation_to_project(self, evaluation: AIEvaluation):
|
||||
"""
|
||||
将AI评估结果同步到关联的参赛项目(评分和评语)
|
||||
"""
|
||||
try:
|
||||
task = evaluation.task
|
||||
if not task.project:
|
||||
return
|
||||
|
||||
project = task.project
|
||||
competition = project.competition
|
||||
|
||||
# 1. 确定评委身份 (Based on Template)
|
||||
# 用户要求:评委显示的是模板名称
|
||||
template_name = evaluation.template.name if evaluation.template else "AI智能评委"
|
||||
# 使用固定前缀 + template_id 确保唯一性,这样同一个模板在不同项目里是同一个评委
|
||||
openid = f"ai_judge_{evaluation.template.id}" if evaluation.template else "ai_judge_default"
|
||||
|
||||
# 延迟导入以避免循环依赖
|
||||
from shop.models import WeChatUser
|
||||
from competition.models import CompetitionEnrollment, Score, Comment, ScoreDimension
|
||||
|
||||
# 获取或创建虚拟评委用户
|
||||
user, created = WeChatUser.objects.get_or_create(
|
||||
openid=openid,
|
||||
defaults={
|
||||
'nickname': template_name,
|
||||
'avatar_url': 'https://ui-avatars.com/api/?name=AI&background=random&color=fff'
|
||||
}
|
||||
)
|
||||
|
||||
# 如果名字不匹配(比如模板改名了),更新它
|
||||
if user.nickname != template_name:
|
||||
user.nickname = template_name
|
||||
user.save(update_fields=['nickname'])
|
||||
|
||||
# 2. 确保评委已报名 (Enrollment)
|
||||
enrollment, _ = CompetitionEnrollment.objects.get_or_create(
|
||||
competition=competition,
|
||||
user=user,
|
||||
defaults={
|
||||
'role': 'judge',
|
||||
'status': 'approved'
|
||||
}
|
||||
)
|
||||
|
||||
# 3. 同步评分 (Score)
|
||||
if evaluation.score is not None:
|
||||
# 尝试找到匹配的维度
|
||||
# 优先级:完全匹配模板名称 > 包含"AI"的维度 > 第一个维度
|
||||
dimensions = competition.score_dimensions.all()
|
||||
target_dimension = None
|
||||
|
||||
for dim in dimensions:
|
||||
if dim.name == template_name:
|
||||
target_dimension = dim
|
||||
break
|
||||
|
||||
if not target_dimension:
|
||||
for dim in dimensions:
|
||||
if "AI" in dim.name.upper():
|
||||
target_dimension = dim
|
||||
break
|
||||
|
||||
# 如果还是没找到,尝试创建一个默认的 "AI评分" 维度?
|
||||
# 或者使用第一个维度。考虑到用户说"对应的AI评分",如果没有对应的,可能需要创建一个?
|
||||
# 为了安全起见,如果找不到明确的AI维度,且存在维度,就用第一个;否则不评分。
|
||||
if not target_dimension and dimensions.exists():
|
||||
target_dimension = dimensions.first()
|
||||
|
||||
if target_dimension:
|
||||
Score.objects.update_or_create(
|
||||
project=project,
|
||||
judge=enrollment,
|
||||
dimension=target_dimension,
|
||||
defaults={'score': evaluation.score}
|
||||
)
|
||||
logger.info(f"Synced AI score {evaluation.score} to project {project.id} dimension {target_dimension.name}")
|
||||
|
||||
# 4. 同步评语 (Comment)
|
||||
if evaluation.evaluation:
|
||||
# 检查是否已存在该评委的评语,避免重复
|
||||
comment = Comment.objects.filter(project=project, judge=enrollment).first()
|
||||
if comment:
|
||||
comment.content = evaluation.evaluation
|
||||
comment.save()
|
||||
else:
|
||||
Comment.objects.create(
|
||||
project=project,
|
||||
judge=enrollment,
|
||||
content=evaluation.evaluation
|
||||
)
|
||||
logger.info(f"Synced AI comment to project {project.id}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to sync evaluation to project: {e}")
|
||||
|
||||
def summarize_task(self, task):
|
||||
"""
|
||||
对转写任务进行总结
|
||||
|
||||
Reference in New Issue
Block a user