|
|
@@ -19,11 +19,38 @@ def get_db():
|
|
|
finally:
|
|
|
db.close()
|
|
|
|
|
|
-
|
|
|
-# ================= 页面 =================
|
|
|
+# ================= 获取所有机型 =================
|
|
|
@router.get("/simulate", response_class=HTMLResponse)
|
|
|
-def simulate(machine_id: int, db: Session = Depends(get_db)):
|
|
|
+def simulate(db: Session = Depends(get_db)):
|
|
|
+ # 获取所有有模板的机型
|
|
|
+ machines = db.execute(text("""
|
|
|
+ SELECT machine_id, name
|
|
|
+ FROM t_machine
|
|
|
+ WHERE machine_id IN (SELECT DISTINCT machine_id FROM machine_temp)
|
|
|
+ """)).fetchall()
|
|
|
+
|
|
|
+ html = "<h2>估价模拟</h2>"
|
|
|
+ html += '<form method="get" action="/estimate/simulate_one">'
|
|
|
|
|
|
+ # 展示机型选择
|
|
|
+ html += '<label>选择机型:</label><br>'
|
|
|
+ html += '<select name="machine_id">'
|
|
|
+ for machine in machines:
|
|
|
+ html += f'<option value="{machine.machine_id}">{machine.name}</option>'
|
|
|
+ html += '</select><br>'
|
|
|
+
|
|
|
+ html += '<button type="submit">选择机型</button></form>'
|
|
|
+
|
|
|
+ return html
|
|
|
+
|
|
|
+
|
|
|
+# ================= 获取机型模板和选项 =================
|
|
|
+@router.get("/simulate_one", response_class=HTMLResponse)
|
|
|
+def simulate(
|
|
|
+ machine_id: int,
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ # 获取机型对应的模板
|
|
|
row = db.execute(text("""
|
|
|
SELECT estimate_packet
|
|
|
FROM machine_temp
|
|
|
@@ -32,9 +59,9 @@ def simulate(machine_id: int, db: Session = Depends(get_db)):
|
|
|
|
|
|
tpl = json.loads(row.estimate_packet)
|
|
|
|
|
|
- html = "<h2>估价模拟</h2>"
|
|
|
+ html = f"<h2>估价模拟 - {tpl['templateId']}</h2>"
|
|
|
|
|
|
- html += '<form method="post">'
|
|
|
+ html += '<form method="post" action="/estimate/simulate">'
|
|
|
|
|
|
for step in tpl["template"]:
|
|
|
html += f"<h3>{step['stepName']}</h3>"
|
|
|
@@ -61,21 +88,30 @@ def simulate(machine_id: int, db: Session = Depends(get_db)):
|
|
|
|
|
|
|
|
|
|
|
|
-# ================= 提交估价 =================
|
|
|
+# ================= 提交估价并计算价格 =================
|
|
|
@router.post("/simulate", response_class=HTMLResponse)
|
|
|
def simulate_calc(
|
|
|
machine_id: int = Form(...),
|
|
|
option_ids: list[str] = Form([]),
|
|
|
db: Session = Depends(get_db)
|
|
|
):
|
|
|
- base_price = 1000.0
|
|
|
+ # 获取基准价格
|
|
|
+ base_price_row = db.execute(text("""
|
|
|
+ SELECT base_price
|
|
|
+ FROM machine_base_price
|
|
|
+ WHERE machine_id=:mid
|
|
|
+ """), {"mid": machine_id}).fetchone()
|
|
|
|
|
|
+ # base_price = base_price_row.base_price
|
|
|
+ base_price = base_price_row.base_price if base_price_row else 1000.0
|
|
|
+ # 获取选项因素
|
|
|
factors = db.execute(text("""
|
|
|
- SELECT option_id,factor,absolute_deduct
|
|
|
- FROM price_option_factor
|
|
|
+ SELECT option_id, factor, absolute_deduct
|
|
|
+ FROM price_option_factor
|
|
|
WHERE option_id IN :ids
|
|
|
"""), {"ids": tuple(option_ids)}).fetchall()
|
|
|
|
|
|
+ # 计算价格
|
|
|
price = base_price
|
|
|
|
|
|
for f in factors:
|
|
|
@@ -84,24 +120,48 @@ def simulate_calc(
|
|
|
if f.absolute_deduct:
|
|
|
price -= float(f.absolute_deduct)
|
|
|
|
|
|
- # 机型调节
|
|
|
- adjust = db.execute(text("""
|
|
|
+ # 逐步应用调节因子
|
|
|
+
|
|
|
+ # 1. 获取全局调节因子
|
|
|
+ global_adjust = db.execute(text("""
|
|
|
+ SELECT factor FROM price_adjust_factor
|
|
|
+ WHERE level='global'
|
|
|
+ """)).fetchone()
|
|
|
+
|
|
|
+ if global_adjust:
|
|
|
+ price *= float(global_adjust.factor)
|
|
|
+
|
|
|
+ # 2. 获取品牌调节因子
|
|
|
+ brand_adjust = db.execute(text("""
|
|
|
+ SELECT factor FROM price_adjust_factor
|
|
|
+ WHERE level='brand'
|
|
|
+ AND ref_id=(
|
|
|
+ SELECT brand_id FROM t_machine WHERE machine_id=:mid
|
|
|
+ )
|
|
|
+ """), {"mid": machine_id}).fetchone()
|
|
|
+
|
|
|
+ if brand_adjust:
|
|
|
+ price *= float(brand_adjust.factor)
|
|
|
+
|
|
|
+ # 3. 获取机型调节因子
|
|
|
+ machine_adjust = db.execute(text("""
|
|
|
SELECT factor FROM price_adjust_factor
|
|
|
WHERE level='machine'
|
|
|
AND ref_id=:mid
|
|
|
"""), {"mid": machine_id}).fetchone()
|
|
|
|
|
|
- if adjust:
|
|
|
- price *= float(adjust.factor)
|
|
|
+ if machine_adjust:
|
|
|
+ price *= float(machine_adjust.factor)
|
|
|
|
|
|
html = f"""
|
|
|
<h2>估价结果</h2>
|
|
|
选择项:{",".join(option_ids)}<br>
|
|
|
- 估价:{round(price,2)} 元<br>
|
|
|
- <a href="/estimate/simulate?machine_id={machine_id}">返回</a>
|
|
|
+ 估价:{round(price, 2)} 元<br>
|
|
|
+ <a href="/simulate?machine_id={machine_id}">返回</a>
|
|
|
"""
|
|
|
|
|
|
return html
|
|
|
+
|
|
|
@router.post("/estimate2", response_class=HTMLResponse)
|
|
|
def estimate_submit(
|
|
|
request: Request,
|