From ff9482780d639863ddc88a11749cd425bbc69712 Mon Sep 17 00:00:00 2001 From: wangfq Date: Mon, 15 Jun 2026 10:02:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20tb=5Fstate=5Ftst=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20detector=5Fserial=20=E5=AD=97=E6=AE=B5=20+=20tb=5Fpending=5F?= =?UTF-8?q?detector=20=E8=BE=85=E5=8A=A9=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - tb_state_tst DDL 增加 detector_serial VARCHAR(45) 列 + V2.4.0 迁移 - 新建 tb_pending_detector 表,用于 web 端暂存待插入的序列号 - insert_test_result / insert_wave_data 插入前从辅助表读取序列号(消费后清除) - 新增 get_pending_detector_serial() 查询函数 --- src/models.py | 57 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/src/models.py b/src/models.py index 2b7e07d..1afaf04 100644 --- a/src/models.py +++ b/src/models.py @@ -100,6 +100,7 @@ async def _create_tables(pool: aiomysql.Pool): CREATE TABLE IF NOT EXISTS `tb_state_tst` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `dnt_id` INT NOT NULL COMMENT 'FK → dnt_info.id', + `detector_serial` VARCHAR(45) DEFAULT '' COMMENT '车检器序列号', `dpg430_addr` TINYINT DEFAULT 0, `pcnum` VARCHAR(10) DEFAULT '' COMMENT '批次号', `serialnum` INT DEFAULT 0 COMMENT '流水号', @@ -320,6 +321,26 @@ async def _create_tables(pool: aiomysql.Pool): except Exception: pass + # V2.4.0 迁移:tb_state_tst 增加车检器序列号 + for col, col_def in [ + ("detector_serial", "VARCHAR(45) DEFAULT '' COMMENT '车检器序列号'"), + ]: + try: + await cur.execute( + f"ALTER TABLE `tb_state_tst` ADD COLUMN `{col}` {col_def}" + ) + except Exception: + pass + + # 13. 待插入的车检器序列号表 (V2.4.0) + await cur.execute(""" + CREATE TABLE IF NOT EXISTS `tb_pending_detector` ( + `dnt_id` INT PRIMARY KEY COMMENT 'FK → dnt_info.id', + `detector_serial` VARCHAR(45) DEFAULT '' COMMENT '待插入的车检器序列号', + `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 + """) + # 11. 设备事件日志表 await cur.execute(""" CREATE TABLE IF NOT EXISTS `tb_device_log` ( @@ -544,18 +565,19 @@ async def insert_test_result(dnt_id: int, dpg430_addr: int, pcnum: str, relay_code: int = 0): """插入测试结果到 tb_state_tst""" coil_id, simulate_car_id = await get_fixture_coil_car_ids(dnt_id) + detector_serial = await get_pending_detector_serial(dnt_id) pool = await get_pool() async with pool.acquire() as conn: async with conn.cursor() as cur: await cur.execute( """INSERT INTO tb_state_tst - (dnt_id, dpg430_addr, pcnum, serialnum, sub_type, str_type, + (dnt_id, detector_serial, dpg430_addr, pcnum, serialnum, sub_type, str_type, test_mode, data_source, iffinish, fault_info, relay_out, ppvalue, idle_freq, enter_freq, exit_freq, enter_dist, exit_dist, enter_speed, exit_speed, relay_code, coil_id, simulate_car_id) - VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", - (dnt_id, dpg430_addr, pcnum, serialnum, sub_type, str_type, + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", + (dnt_id, detector_serial, dpg430_addr, pcnum, serialnum, sub_type, str_type, test_mode, data_source, iffinish, fault_info, relay_out, ppvalue, idle_freq, enter_freq, exit_freq, enter_dist, exit_dist, enter_speed, exit_speed, @@ -573,19 +595,20 @@ async def insert_wave_data(dnt_id: int, dpg430_addr: int, coil_id, simulate_car_id = await get_fixture_coil_car_ids(dnt_id) dev_type = await get_fixture_dev_type(dnt_id) str_type = await get_dev_type_name(dev_type) if dev_type else "" + detector_serial = await get_pending_detector_serial(dnt_id) pool = await get_pool() async with pool.acquire() as conn: async with conn.cursor() as cur: await cur.execute( """INSERT INTO tb_state_tst - (dnt_id, dpg430_addr, sub_type, str_type, + (dnt_id, detector_serial, dpg430_addr, sub_type, str_type, test_mode, data_source, relay_out, relay_code, remain_count, work_freq, curr_dist, speed, near_dist, far_dist, b4_enter_dist, b4_leave_dist, coil_id, simulate_car_id) - VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", - (dnt_id, dpg430_addr, dev_type, str_type, + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", + (dnt_id, detector_serial, dpg430_addr, dev_type, str_type, 1, "B4", relay_out, relay_code, remain_count, work_freq, curr_dist, speed, @@ -838,3 +861,25 @@ async def get_vehicle_base_test_by_type(type_num: int) -> dict | None: (type_num,), ) return await cur.fetchone() + + +# ─── tb_pending_detector ─────────────────────────────────────────── + +async def get_pending_detector_serial(dnt_id: int) -> str: + """获取并清除待插入的车检器序列号(一次性消费)""" + pool = await get_pool() + async with pool.acquire() as conn: + async with conn.cursor(aiomysql.DictCursor) as cur: + await cur.execute( + "SELECT detector_serial FROM tb_pending_detector WHERE dnt_id=%s", + (dnt_id,), + ) + row = await cur.fetchone() + if row: + # 清除已消费的记录 + await cur.execute( + "DELETE FROM tb_pending_detector WHERE dnt_id=%s", + (dnt_id,), + ) + return row["detector_serial"] or "" + return ""