mengchang 2 kuukautta sitten
vanhempi
commit
0c9dd7b060

BIN
backend/routers/__pycache__/admin.cpython-310.pyc


BIN
backend/routers/__pycache__/estimate.cpython-310.pyc


+ 75 - 15
backend/routers/estimate.py

@@ -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,

+ 19 - 11
mysql/init.sql

@@ -20,7 +20,8 @@ CREATE TABLE t_machine (
   machine_id BIGINT,
   name VARCHAR(100),
   shrink_name VARCHAR(100),
-  create_time DATETIME DEFAULT CURRENT_TIMESTAMP
+  create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
+  update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );
 
 /* ================= 机型模板 ================= */
@@ -38,23 +39,25 @@ CREATE TABLE machine_temp (
 /* ================= 设备情况项 ================= */
 CREATE TABLE release_option (
   id BIGINT AUTO_INCREMENT PRIMARY KEY,
-  step_id BIGINT COMMENT '1物品信息 2成色情况 3功能情况',
+  step_id BIGINT COMMENT '1成色情况 3功能情况',
   step_name VARCHAR(50),
   option_key_id BIGINT,
   option_key_name VARCHAR(60),
   option_id BIGINT,
   option_name VARCHAR(60),
-  create_time DATETIME DEFAULT CURRENT_TIMESTAMP
+  create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
   update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );
 
 
-/* ================= step1设备选项 ================= */
+/*
+ ================= step1设备选项 ================= 
+ drop base_template_id BIGINT NOT NULL COMMENT '基础模板ID:99181 / 99198 / 99197',
+
+*/
 
 CREATE TABLE step1_attr (
   id BIGINT AUTO_INCREMENT PRIMARY KEY,
-
-  base_template_id BIGINT NOT NULL COMMENT '基础模板ID:99181 / 99198 / 99197',
   step TINYINT DEFAULT 1 COMMENT '固定为 step=1',
 
   attr_key VARCHAR(64) NOT NULL COMMENT 'capacity / color / channel / warranty',
@@ -74,7 +77,6 @@ CREATE TABLE step1_attr (
   create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
   update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 
-  KEY idx_tpl (base_template_id),
   KEY idx_attr (attr_key)
 ) COMMENT='step1 模板属性表';
 
@@ -93,7 +95,9 @@ CREATE TABLE price_option_factor (
   id BIGINT AUTO_INCREMENT PRIMARY KEY,
   option_id BIGINT,
   factor DECIMAL(5,4) DEFAULT 1.0000,
-  absolute_deduct DECIMAL(10,2) DEFAULT 0
+  absolute_deduct DECIMAL(10,2) DEFAULT 0,
+  create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
+  update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );
 
 /* ================= 调节系数 ================= */
@@ -101,7 +105,9 @@ CREATE TABLE price_adjust_factor (
   id BIGINT AUTO_INCREMENT PRIMARY KEY,
   level VARCHAR(32),
   ref_id BIGINT,
-  factor DECIMAL(5,4) DEFAULT 1.0000
+  factor DECIMAL(5,4) DEFAULT 1.0000,
+  create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
+  update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );
 
 /* ================  基础模板 ================= */
@@ -123,7 +129,8 @@ CREATE TABLE estimate_version (
   machine_id BIGINT,
   version_no VARCHAR(32),
   content TEXT,
-  create_time DATETIME DEFAULT CURRENT_TIMESTAMP
+  create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
+  update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );
 
 /* ================= 估价记录 ================= */
@@ -134,5 +141,6 @@ CREATE TABLE estimate_record (
   option_ids TEXT,
   final_price DECIMAL(10,2),
   version_no VARCHAR(32),
-  create_time DATETIME DEFAULT CURRENT_TIMESTAMP
+  create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
+  update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );