history.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """
  2. 分析历史记录 API — 管理各类分析报告历史
  3. """
  4. from fastapi import APIRouter, Query
  5. from app.services import history_service
  6. from app.utils.response import success_response, error_response
  7. router = APIRouter(prefix="/history", tags=["分析历史"])
  8. @router.get("/list")
  9. async def list_history(
  10. type: str = Query(None, description="类型: sentiment/data_analysis/buffett/chat"),
  11. limit: int = Query(20, ge=1, le=100),
  12. ):
  13. """获取历史记录列表"""
  14. result = await history_service.get_history_list(analysis_type=type, limit=limit)
  15. if not result["success"]:
  16. return error_response(code=500, message=result.get("error", "查询失败"))
  17. return success_response(data={"items": result["items"], "total": result["total"]})
  18. @router.get("/{record_id}")
  19. async def get_history(record_id: int):
  20. """获取历史记录详情"""
  21. result = await history_service.get_history_detail(record_id)
  22. if not result["success"]:
  23. return error_response(code=404, message=result.get("error", "记录不存在"))
  24. return success_response(data=result["record"])
  25. @router.delete("/{record_id}")
  26. async def delete_history(record_id: int):
  27. """删除单条历史记录"""
  28. result = await history_service.delete_history(record_id)
  29. if not result["success"]:
  30. return error_response(code=404, message=result.get("error", "删除失败"))
  31. return success_response(message=result["message"])
  32. @router.post("/clear")
  33. async def clear_history(
  34. type: str = Query(None, description="仅清除指定类型"),
  35. ):
  36. """清空今日历史"""
  37. result = await history_service.clear_today_history(analysis_type=type)
  38. if not result["success"]:
  39. return error_response(code=500, message=result.get("error", "清除失败"))
  40. return success_response(message=f"已清除 {result['count']} 条记录")