estimate_select.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from fastapi import APIRouter, Depends, Query, Request
  2. from fastapi.responses import HTMLResponse
  3. from sqlalchemy.orm import Session
  4. from sqlalchemy import text
  5. from fastapi.templating import Jinja2Templates
  6. from database import SessionLocal
  7. router = APIRouter(prefix="/estimate")
  8. templates = Jinja2Templates(directory="templates")
  9. def get_db():
  10. db = SessionLocal()
  11. try:
  12. yield db
  13. finally:
  14. db.close()
  15. @router.get("/select", response_class=HTMLResponse)
  16. def select_page(
  17. request: Request,
  18. brand: str | None = Query(None),
  19. db: Session = Depends(get_db)
  20. ):
  21. # 默认分类 = 手机
  22. type_name = "手机"
  23. # 左侧品牌
  24. brands = db.execute(text("""
  25. SELECT DISTINCT brand_name
  26. FROM t_machine
  27. WHERE type_name=:t
  28. AND brand_name IS NOT NULL
  29. ORDER BY brand_name
  30. """), {"t": type_name}).fetchall()
  31. # 默认选中第一个品牌
  32. if not brand and brands:
  33. brand = brands[0].brand_name
  34. # 右侧机型列表(不再分系列)
  35. machines = []
  36. if brand:
  37. machines = db.execute(text("""
  38. SELECT id, name,machine_id
  39. FROM t_machine
  40. WHERE type_name=:t
  41. AND brand_name=:b
  42. ORDER BY name
  43. """), {
  44. "t": type_name,
  45. "b": brand
  46. }).fetchall()
  47. return templates.TemplateResponse(
  48. "estimate_select.html",
  49. {
  50. "request": request,
  51. "brands": brands,
  52. "machines": machines,
  53. "current_brand": brand
  54. }
  55. )