| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from fastapi import APIRouter, Depends, Query, Request
- from fastapi.responses import HTMLResponse
- from sqlalchemy.orm import Session
- from sqlalchemy import text
- from fastapi.templating import Jinja2Templates
- from database import SessionLocal
- router = APIRouter(prefix="/estimate")
- templates = Jinja2Templates(directory="templates")
- def get_db():
- db = SessionLocal()
- try:
- yield db
- finally:
- db.close()
- @router.get("/select", response_class=HTMLResponse)
- def select_page(
- request: Request,
- brand: str | None = Query(None),
- db: Session = Depends(get_db)
- ):
- # 默认分类 = 手机
- type_name = "手机"
- # 左侧品牌
- brands = db.execute(text("""
- SELECT DISTINCT brand_name
- FROM t_machine
- WHERE type_name=:t
- AND brand_name IS NOT NULL
- ORDER BY brand_name
- """), {"t": type_name}).fetchall()
- # 默认选中第一个品牌
- if not brand and brands:
- brand = brands[0].brand_name
- # 右侧机型列表(不再分系列)
- machines = []
- if brand:
- machines = db.execute(text("""
- SELECT id, name,machine_id
- FROM t_machine
- WHERE type_name=:t
- AND brand_name=:b
- ORDER BY name
- """), {
- "t": type_name,
- "b": brand
- }).fetchall()
- return templates.TemplateResponse(
- "estimate_select.html",
- {
- "request": request,
- "brands": brands,
- "machines": machines,
- "current_brand": brand
- }
- )
|