report.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from database import MySqlDao
  2. from fastapi import APIRouter, status, HTTPException
  3. import os
  4. from .request_body import ReportRequest
  5. dao =MySqlDao()
  6. router = APIRouter()
  7. @router.post("/report")
  8. async def report(request: ReportRequest):
  9. """获取推荐相关报告接口"""
  10. file_id_record = dao.get_report_file_id(request.cultivacation_id)
  11. if file_id_record.empty:
  12. raise HTTPException(
  13. status_code=status.HTTP_404_NOT_FOUND,
  14. detail="Reports not found"
  15. )
  16. file_id_map = {
  17. '卷烟信息表': file_id_record['product_info_table'].item(),
  18. '品规商户特征关系表': file_id_record['relation_table'].item(),
  19. '相似卷烟表': file_id_record['similarity_product_table'].item()
  20. }
  21. request_data = []
  22. for index, filename in enumerate(file_id_map):
  23. request_data.append(
  24. {
  25. "id": index+1,
  26. "filename": filename,
  27. "file_id": file_id_map.get(filename)
  28. }
  29. )
  30. return {"code": 200, "msg": "success", "data": {"reportInfo": request_data}}
  31. # @router.post("/report")
  32. # async def report_api(request: ReportRequest):
  33. # """获取推荐相关报告接口"""
  34. # files_id_path = os.path.join("./data/reports", request.city_uuid, request.product_code, "files_id.txt")
  35. # if not os.path.exists(files_id_path):
  36. # raise HTTPException(
  37. # status_code=status.HTTP_404_NOT_FOUND,
  38. # detail="Reports not found"
  39. # )
  40. # with open(files_id_path, 'r', encoding="utf-8") as file:
  41. # lines = file.readlines()
  42. # if lines[0].strip() == "failed":
  43. # raise HTTPException(
  44. # status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
  45. # detail="reports upload failed"
  46. # )
  47. # request_data = []
  48. # for index, line in enumerate(lines):
  49. # filename, file_id = line.strip().split(",")
  50. # request_data.append(
  51. # {
  52. # "id": index+1,
  53. # "filename": filename,
  54. # "file_id": file_id
  55. # }
  56. # )
  57. # return {"code": 200, "msg": "success", "data": {"reportInfo": request_data}}