feat: admin用户支持删除测试数据(按条件/日期范围),含确认框+日志

This commit is contained in:
wangfq
2026-06-05 13:44:46 +08:00
parent 0ea3511b90
commit 6a3aaf3c05
4 changed files with 140 additions and 2 deletions

View File

@@ -582,3 +582,48 @@ def delete_vehicle_base_test(test_id: int):
conn.commit()
finally:
conn.close()
# ─── 测试数据删除 ──────────────────────────────────────────────
def delete_test_data(serial: str = "", date_from: str = "",
date_to: str = "", data_source: str = "") -> int:
"""删除符合条件的测试数据,返回删除行数
必须至少提供一个条件serial / date范围 / data_source不允许无条件全删。
"""
conn = get_conn()
try:
with conn.cursor() as cur:
where = []
params = []
if serial:
where.append("t.dnt_id IN (SELECT id FROM dnt_info WHERE serial LIKE %s)")
params.append(f"%{serial}%")
if date_from:
where.append("t.create_time >= %s")
params.append(date_from)
if date_to:
where.append("t.create_time <= %s")
params.append(date_to + " 23:59:59")
if data_source:
where.append("t.data_source = %s")
params.append(data_source)
if not where:
return 0 # 拒绝无条件全删
where_clause = " AND ".join(where)
cur.execute(
f"SELECT COUNT(*) as cnt FROM tb_state_tst t WHERE {where_clause}",
params,
)
cnt = cur.fetchone()["cnt"]
cur.execute(
f"DELETE t FROM tb_state_tst t WHERE {where_clause}", params,
)
conn.commit()
return cnt
finally:
conn.close()