2026-03-11 16:28:04 +08:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
|
from fastapi.responses import FileResponse
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.config import settings
|
|
|
|
|
from app.core.database import get_db
|
|
|
|
|
from app.models.report import ExportReport
|
2026-03-13 14:48:32 +08:00
|
|
|
from app.models.report import ReportType
|
2026-03-11 16:28:04 +08:00
|
|
|
from app.repositories.case_repo import CaseRepository
|
|
|
|
|
from app.schemas.report import ReportCreate, ReportOut, ReportListOut
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/cases/{case_id}/reports", response_model=ReportOut, status_code=201)
|
|
|
|
|
async def generate_report(case_id: UUID, body: ReportCreate, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
repo = CaseRepository(db)
|
|
|
|
|
case = await repo.get(case_id)
|
|
|
|
|
if not case:
|
|
|
|
|
raise HTTPException(404, "案件不存在")
|
2026-03-13 14:48:32 +08:00
|
|
|
if body.report_type == ReportType.word:
|
|
|
|
|
raise HTTPException(400, "当前仅支持 PDF 和 Excel 报告导出")
|
2026-03-11 16:28:04 +08:00
|
|
|
|
|
|
|
|
from app.services.report_service import generate_report as gen
|
|
|
|
|
report = await gen(case_id, body, db)
|
|
|
|
|
return report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/cases/{case_id}/reports", response_model=ReportListOut)
|
|
|
|
|
async def list_reports(case_id: UUID, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
select(ExportReport)
|
2026-03-13 14:48:32 +08:00
|
|
|
.where(ExportReport.case_id == case_id, ExportReport.report_type != ReportType.word)
|
2026-03-11 16:28:04 +08:00
|
|
|
.order_by(ExportReport.created_at.desc())
|
|
|
|
|
)
|
|
|
|
|
items = list(result.scalars().all())
|
|
|
|
|
return ReportListOut(items=items, total=len(items))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/reports/{report_id}/download")
|
|
|
|
|
async def download_report(report_id: UUID, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
report = await db.get(ExportReport, report_id)
|
|
|
|
|
if not report:
|
|
|
|
|
raise HTTPException(404, "报告不存在")
|
|
|
|
|
full_path = settings.upload_path / report.file_path
|
|
|
|
|
if not full_path.exists():
|
|
|
|
|
raise HTTPException(404, "报告文件不存在")
|
|
|
|
|
return FileResponse(full_path, filename=full_path.name)
|