47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
|
||
|
|
from app.models.training_record import TrainingRecord
|
||
|
|
|
||
|
|
|
||
|
|
class EvaluationRepository:
|
||
|
|
"""评价仓储:负责完整训练结束后的 training_record 读写。"""
|
||
|
|
|
||
|
|
def __init__(self, db: Session) -> None:
|
||
|
|
self.db = db
|
||
|
|
|
||
|
|
def create_record(self, record: TrainingRecord) -> TrainingRecord:
|
||
|
|
"""评价保存:把 AI 评分报告保存为训练记录。"""
|
||
|
|
self.db.add(record)
|
||
|
|
self.db.flush()
|
||
|
|
return record
|
||
|
|
|
||
|
|
def get_by_session(self, session_id: int, user_id: str) -> TrainingRecord | None:
|
||
|
|
"""评价读取:按会话 ID 和外部 user_id 查询训练记录。"""
|
||
|
|
stmt = select(TrainingRecord).where(
|
||
|
|
TrainingRecord.session_id == session_id,
|
||
|
|
TrainingRecord.external_user_id == user_id,
|
||
|
|
)
|
||
|
|
return self.db.scalar(stmt)
|
||
|
|
|
||
|
|
def get_owned_record(self, evaluation_id: int, user_id: str) -> TrainingRecord | None:
|
||
|
|
"""评价归属校验:按训练记录 ID 和外部 user_id 查询记录。"""
|
||
|
|
stmt = select(TrainingRecord).where(
|
||
|
|
TrainingRecord.id == evaluation_id,
|
||
|
|
TrainingRecord.external_user_id == user_id,
|
||
|
|
)
|
||
|
|
return self.db.scalar(stmt)
|
||
|
|
|
||
|
|
def list_by_user(self, user_id: str) -> list[TrainingRecord]:
|
||
|
|
"""历史评价:按外部 user_id 查询完整训练后的评价记录。"""
|
||
|
|
stmt = (
|
||
|
|
select(TrainingRecord)
|
||
|
|
.where(TrainingRecord.external_user_id == user_id)
|
||
|
|
.order_by(TrainingRecord.created_at.desc())
|
||
|
|
)
|
||
|
|
return list(self.db.scalars(stmt).all())
|
||
|
|
|
||
|
|
def flush(self) -> None:
|
||
|
|
"""记录更新:刷新 PDF 路径等派生字段。"""
|
||
|
|
self.db.flush()
|