| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- from fastapi import APIRouter, Request, Depends, Form
- from fastapi.responses import HTMLResponse, RedirectResponse
- from sqlalchemy import text # 新增这一行
- from sqlalchemy.orm import Session
- from database import SessionLocal
- import json
- import time
- from datetime import datetime
- from datetime import datetime
- from services.template_builder import build_estimate_template
- router = APIRouter(prefix="/admin", tags=["admin"])
- # ---------- DB ----------
- def get_db():
- db = SessionLocal()
- try:
- yield db
- finally:
- db.close()
- # ---------- 机型管理 ----------
- @router.get("/machines", response_class=HTMLResponse)
- def machine_page(db: Session = Depends(get_db)):
- categories = db.execute(text("""
- SELECT category_id, name FROM t_chx_category
- """)).fetchall()
- machines = db.execute(text("""
- SELECT brand_name, name, type_name
- FROM t_machine
- ORDER BY id DESC
- LIMIT 50
- """)).fetchall()
- html = """
- <h2>机型管理</h2>
- <form method="post" action="/admin/machines/add">
- 分类:
- <select name="type_id">
- """
- for c in categories:
- html += f"<option value='{c.category_id}'>{c.name}</option>"
- html += """
- </select><br><br>
- 品牌名称:<input name="brand_name"><br><br>
- 机型名称:<input name="machine_name"><br><br>
- <button type="submit">新增机型</button>
- </form>
- <hr>
- <ul>
- """
- for m in machines:
- html += f"<li>{m.type_name} | {m.brand_name} - {m.name}</li>"
- html += "</ul>"
- return html
- @router.post("/machines/add")
- def add_machine(
- type_id: int = Form(...),
- brand_name: str = Form(...),
- machine_name: str = Form(...),
- db: Session = Depends(get_db)
- ):
- category = db.execute(
- text("SELECT name FROM t_chx_category WHERE category_id=:id"),
- {"id": type_id}
- ).fetchone()
- machine_id = int(time.time() * 1000)
- brand_id = int(time.time() * 1000)
- db.execute(text("""
- INSERT INTO t_machine
- (
- code,
- type_id,
- type_name,
- brand_id,
- brand_name,
- machine_id,
- name,
- create_time
- )
- VALUES
- (
- 'mobile',
- :type_id,
- :type_name,
- :brand_id,
- :brand_name,
- :machine_id,
- :name,
- :time
- )
- """), {
- "type_id": type_id,
- "type_name": category.name,
- "brand_id": brand_id,
- "brand_name": brand_name,
- "machine_id": machine_id,
- "name": machine_name,
- "time": datetime.now()
- })
- db.commit()
- return RedirectResponse("/admin/machines", status_code=302)
- # ---------- release_option 管理 ----------
- @router.get("/options", response_class=HTMLResponse)
- def option_page(db: Session = Depends(get_db)):
- rows = db.execute(text("""
- SELECT step_name, option_key_name, option_name
- FROM release_option
- ORDER BY step_name, option_key_name
- """)).fetchall()
- # 组装树
- tree = {}
- for r in rows:
- step = r.step_name
- key = r.option_key_name
- opt = r.option_name
- tree.setdefault(step, {})
- if key:
- tree[step].setdefault(key, [])
- if opt:
- tree[step][key].append(opt)
- # 渲染 HTML
- html = "<h2>检测项管理</h2>"
- for step, keys in tree.items():
- html += f"<h3>📂 {step}</h3><ul>"
- for key, opts in keys.items():
- html += f"<li>📁 {key}<ul>"
- for o in opts:
- html += f"<li>✅ {o}</li>"
- html += "</ul></li>"
- html += "</ul>"
- # 表单
- html += """
- <hr>
- <h3>➕ 新增父级</h3>
- <form method="post" action="/admin/options/add">
- 父级名称:<input name="step_name">
- <button type="submit">添加</button>
- </form>
- <h3>➕ 新增子级</h3>
- <form method="post" action="/admin/options/add">
- 父级名称:<input name="step_name">
- 子级名称:<input name="option_key_name">
- <button type="submit">添加</button>
- </form>
- <h3>➕ 新增选项</h3>
- <form method="post" action="/admin/options/add">
- 父级名称:<input name="step_name">
- 子级名称:<input name="option_key_name">
- 选项名称:<input name="option_name">
- <button type="submit">添加</button>
- </form>
- """
- return html
- @router.post("/options/add")
- def add_option(
- step_name: str = Form(...),
- option_key_name: str = Form(""),
- option_name: str = Form(""),
- db: Session = Depends(get_db)
- ):
- db.execute(text("""
- INSERT INTO release_option
- (step_id, step_name, option_key_id, option_key_name, option_id, option_name)
- VALUES
- (
- 2,
- :step,
- UNIX_TIMESTAMP(),
- :key,
- UNIX_TIMESTAMP(),
- :opt
- )
- """), {
- "step": step_name,
- "key": option_key_name,
- "opt": option_name
- })
- db.commit()
- return RedirectResponse("/admin/options", status_code=302)
- # ---------- API ----------
- @router.get("/capacities/{machine_id}")
- def capacities(machine_id: int, db: Session = Depends(get_db)):
- return db.execute(
- "SELECT capacity FROM machine_base_price WHERE machine_id=:id",
- {"id": machine_id}
- ).fetchall()
- @router.post("/release_option/add")
- def add_option(
- step_id: int = Form(...),
- step_name: str = Form(...),
- option_key_name: str = Form(...),
- option_name: str = Form(...),
- db: Session = Depends(get_db)
- ):
- db.execute("""
- INSERT INTO release_option(step_id,step_name,option_key_id,option_key_name,option_id,option_name)
- VALUES (:sid,:sname, UNIX_TIMESTAMP(), :kname, UNIX_TIMESTAMP(), :oname)
- """, {
- "sid": step_id,
- "sname": step_name,
- "kname": option_key_name,
- "oname": option_name
- })
- db.commit()
- return {"ok": True}
- @router.get("/capacities/{machine_id}")
- def capacities(machine_id: int, db: Session = Depends(get_db)):
- return db.execute(
- "SELECT capacity FROM machine_base_price WHERE machine_id=:id",
- {"id": machine_id}
- ).fetchall()
- @router.get("/factors", response_class=HTMLResponse)
- def factors(request: Request, db: Session = Depends(get_db)):
- rows = db.execute(text("""
- SELECT ro.option_name, pf.option_id, pf.factor, pf.absolute_deduct
- FROM price_option_factor pf
- LEFT JOIN release_option ro ON pf.option_id = ro.option_id
- """)).fetchall()
- options = db.execute(text("""
- SELECT DISTINCT option_id, option_name FROM release_option
- """)).fetchall()
- return request.app.templates.TemplateResponse(
- "factors.html",
- {
- "request": request,
- "rows": rows,
- "options": options
- }
- )
- @router.post("/factors/save")
- def save_factor(
- option_id: int = Form(...),
- factor: float = Form(1.0),
- absolute_deduct: float = Form(0),
- db: Session = Depends(get_db)
- ):
- db.execute(text("""
- INSERT INTO price_option_factor(option_id,factor,absolute_deduct)
- VALUES (:oid,:f,:d)
- ON DUPLICATE KEY UPDATE
- factor=:f, absolute_deduct=:d
- """), {
- "oid": option_id,
- "f": factor,
- "d": absolute_deduct
- })
- db.commit()
- return RedirectResponse("/admin/factors", status_code=302)
- @router.get("/adjust", response_class=HTMLResponse)
- def adjust_page(request: Request, db: Session = Depends(get_db)):
- rows = db.execute(text("SELECT * FROM price_adjust_factor")).fetchall()
- return request.app.templates.TemplateResponse(
- "adjust.html",
- {"request": request, "rows": rows}
- )
- @router.post("/adjust/save")
- def save_adjust(
- level: str = Form(...),
- ref_id: int = Form(0),
- factor: float = Form(...),
- db: Session = Depends(get_db)
- ):
- db.execute(text("""
- INSERT INTO price_adjust_factor(level,ref_id,factor)
- VALUES (:l,:r,:f)
- """), {
- "l": level,
- "r": ref_id,
- "f": factor
- })
- db.commit()
- return RedirectResponse("/admin/adjust", status_code=302)
- @router.post("/machine-template/generate")
- def generate_machine_template(
- machine_id: int = Form(...),
- db: Session = Depends(get_db)
- ):
- # 1. release_option
- options = db.execute(text("""
- SELECT step_name, option_key_name, option_id, option_name
- FROM release_option
- ORDER BY step_id, option_key_id, option_id
- """)).fetchall()
- if not options:
- return {"error": "release_option 为空"}
- template = build_estimate_template(options)
- # 2. 写入 machine_temp
- db.execute(text("""
- INSERT INTO machine_temp
- (machine_id, temp_type, estimate_packet, create_time, update_time)
- VALUES (:mid, '00', :json, :now, :now)
- ON DUPLICATE KEY UPDATE
- estimate_packet = :json,
- update_time = :now
- """), {
- "mid": machine_id,
- "json": json.dumps(template, ensure_ascii=False),
- "now": datetime.now()
- })
- db.commit()
- return RedirectResponse("/admin/machine-templates", status_code=302)
- @router.get("/machine-templates", response_class=HTMLResponse)
- def machine_templates(db: Session = Depends(get_db)):
- rows = db.execute(text("""
- SELECT m.machine_id, m.name, m.brand_name,
- mt.id AS temp_id
- FROM t_machine m
- LEFT JOIN machine_temp mt
- ON m.machine_id = mt.machine_id
- AND mt.temp_type = '00'
- ORDER BY m.brand_name, m.name
- LIMIT 100
- """)).fetchall()
- html = "<h2>机型模板管理</h2><table border=1>"
- html += "<tr><th>品牌</th><th>机型</th><th>模板</th><th>操作</th></tr>"
- for r in rows:
- status = "已生成" if r.temp_id else "未生成"
- btn = "重新生成" if r.temp_id else "生成"
- html += f"""
- <tr>
- <td>{r.brand_name}</td>
- <td>{r.name}</td>
- <td>{status}</td>
- <td>
- <form method="post" action="/admin/machine-template/generate">
- <input type="hidden" name="machine_id" value="{r.machine_id}">
- <button type="submit">{btn}</button>
- </form>
- </td>
- </tr>
- """
- html += "</table>"
- return html
- @router.get("/categories", response_class=HTMLResponse)
- def category_page(db: Session = Depends(get_db)):
- rows = db.execute(text("SELECT * FROM t_chx_category")).fetchall()
- html = "<h2>分类管理</h2><ul>"
- for r in rows:
- html += f"<li>{r.name}</li>"
- html += "</ul>"
- html += """
- <h3>新增分类</h3>
- <form method="post">
- 名称:<input name="name"/>
- <button type="submit">添加</button>
- </form>
- """
- return html
- @router.post("/categories")
- def add_category(name: str = Form(...), db: Session = Depends(get_db)):
- db.execute(
- text("INSERT INTO t_chx_category(category_id,name) VALUES (UNIX_TIMESTAMP(),:n)"),
- {"n": name}
- )
- db.commit()
- return RedirectResponse("/admin/categories", 302)
|