This commit is contained in:
HuangHai
2026-01-20 07:43:13 +08:00
parent 6263487425
commit b0b4533f57
6 changed files with 124 additions and 2 deletions

View File

@@ -13,6 +13,9 @@ from Tools.T6_Export import export_excel, DorisExcelExporter, extract_hourly_pri
import tempfile
import os
import zipfile
import subprocess
from pydantic import BaseModel
from starlette.background import BackgroundTask
from Model.YltAnalyticsModel import (
StationBase,
CompetitorStation,
@@ -95,6 +98,55 @@ async def export_prices_zip():
)
class AiReportRequest(BaseModel):
content: str
@router.post("/api/export/ai-report-docx")
async def export_ai_report_docx(req: AiReportRequest):
content = req.content
if not content:
raise HTTPException(status_code=400, detail="Content is empty")
# Create temp markdown file
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False, encoding="utf-8") as tmp_md:
tmp_md.write(content)
tmp_md_path = tmp_md.name
output_docx_path = tmp_md_path.replace(".md", ".docx")
# Check template
template_path = "static/template/templates.docx"
cmd = ['pandoc', '-s', tmp_md_path, '-o', output_docx_path, '--resource-path=static']
# Only add reference doc if it exists, but the user requested it specifically.
# We'll check if it exists, if not, we might fail or warn, but let's try to include it if possible.
if os.path.exists(template_path):
cmd.extend(['--reference-doc', template_path])
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
# Clean up
if os.path.exists(tmp_md_path):
os.remove(tmp_md_path)
raise HTTPException(status_code=500, detail=f"Pandoc conversion failed: {str(e)}")
def cleanup():
if os.path.exists(tmp_md_path):
os.remove(tmp_md_path)
if os.path.exists(output_docx_path):
os.remove(output_docx_path)
return FileResponse(
output_docx_path,
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
filename="AI分析报告.docx",
background=BackgroundTask(cleanup)
)
@router.get("/api/ai/pricing/strategy-summary")
async def ai_pricing_strategy_summary():
async def generate_stream():