analysis.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. 智能股票分析助手 — 分析报告API路由(兼容层)
  3. 旧版 POST 生成报告已移除;列表与详情优先走分析历史表,其次兼容旧版 AnalysisReport 表。
  4. """
  5. from fastapi import APIRouter, Query
  6. from fastapi.responses import JSONResponse
  7. from app.services import analysis_service, history_service
  8. from app.utils.response import success_response, error_response
  9. router = APIRouter(prefix="/analysis", tags=["分析报告"])
  10. @router.post("/report/{code}")
  11. async def generate_report_removed(
  12. code: str,
  13. user_id: str = Query(default="default", description="用户标识"),
  14. report_type: str = Query(default="full", description="报告类型: full/quick"),
  15. ):
  16. """已废弃:请使用 AI 对话助手或个股页 AI 分析 Tab。"""
  17. return JSONResponse(
  18. status_code=410,
  19. content=error_response(
  20. code=410,
  21. message=(
  22. "该接口已移除。请使用 POST /api/v1/chat/stream(AI 对话助手),"
  23. "或个股分析页的舆情 / 数据 / 巴菲特流式分析。"
  24. ),
  25. data={"replacement_chat": "/api/v1/chat/stream", "stock_code": code},
  26. ),
  27. )
  28. @router.get("/report/{report_id}")
  29. async def get_report(report_id: int):
  30. """获取指定记录:优先 analysis_history,其次旧版 analysis_reports 表。"""
  31. if report_id <= 0:
  32. return error_response(code=400, message="无效的报告ID")
  33. hist = await history_service.get_history_detail(report_id)
  34. if hist.get("success"):
  35. rec = hist["record"]
  36. return success_response(
  37. data={
  38. "report": rec,
  39. "source": "history",
  40. },
  41. message="查询成功",
  42. )
  43. legacy = await analysis_service.get_report(report_id)
  44. if legacy.get("success"):
  45. return success_response(
  46. data={
  47. "report": legacy["report"],
  48. "source": "legacy_analysis_report",
  49. },
  50. message="查询成功(旧版报告表)",
  51. )
  52. return error_response(code=404, message=hist.get("error") or legacy.get("error") or "记录不存在")
  53. @router.get("/reports")
  54. async def list_reports(
  55. user_id: str = Query(default="default", description="用户标识"),
  56. limit: int = Query(default=20, ge=1, le=100, description="最大返回数量"),
  57. ):
  58. """分析报告历史列表 — 对应 analysis_history(各 AI 分析类型)。"""
  59. result = await history_service.get_history_list(
  60. analysis_type=None, user_id=user_id, limit=limit
  61. )
  62. if not result["success"]:
  63. return error_response(code=500, message=result.get("error", "查询失败"))
  64. items = result["items"]
  65. return success_response(
  66. data={
  67. "reports": items,
  68. "items": items,
  69. "total": result["total"],
  70. },
  71. message=f"共 {result['total']} 条记录",
  72. )