import uuid from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from app.core.deps import get_cra_site_scope, get_current_user, get_db_session, require_api_permission from app.crud import study as study_crud from app.crud import subject_pd as subject_pd_crud from app.schemas.subject_pd import SubjectPdSummaryRead router = APIRouter() async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID): study = await study_crud.get(db, study_id) if not study: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="项目不存在") return study @router.get( "/subject-pds", response_model=list[SubjectPdSummaryRead], dependencies=[Depends(require_api_permission("subject_pds:list"))], ) async def list_study_subject_pds( study_id: uuid.UUID, skip: int = 0, limit: int = 500, db: AsyncSession = Depends(get_db_session), current_user=Depends(get_current_user), ) -> list[SubjectPdSummaryRead]: await _ensure_study_exists(db, study_id) cra_scope = await get_cra_site_scope(db, study_id, current_user) site_ids = cra_scope[0] if cra_scope else None rows = await subject_pd_crud.list_study_subject_pds(db, study_id, skip=skip, limit=limit, site_ids=site_ids) return [ SubjectPdSummaryRead( id=item.id, study_id=item.study_id, subject_id=item.subject_id, subject_no=subject_no, site_id=site_id, pd_no=item.pd_no, status=item.status, description=item.description, updated_at=item.updated_at, ) for item, subject_no, site_id in rows ]