"""临时联调数据:为能耗报表验证准备 pms_dev 数据,并打印清理所需的 ID。 产出: - 1 个临时用户(role=2,总管理员)→ 用于自签 JWT - 1 个临时项目(org=1)+ 2 个临时柜子(含板/仓体)→ 用于验证项目/柜分组 - 6 条 charge_records(3 柜 × 2 天)→ 用于验证分组与趋势 所有 ID 写入 temp_ids.json,供 cleanup_temp_data.py 精确删除。 """ import json import os import pymysql HERE = os.path.dirname(os.path.abspath(__file__)) DB = dict(host="10.8.0.252", user="root", password="Hbhyg731024@", database="pms_dev") conn = pymysql.connect(**DB) cur = conn.cursor() created = { "user_id": None, "project_id": None, "cabinet_ids": [], "board_ids": [], "compartment_ids": [], "record_ids": [], } # ---------------------------------------------------------------- 临时总管理员 cur.execute( "INSERT INTO users (phone, name, password, role, organization_id, status) " "VALUES (%s, %s, %s, %s, %s, %s)", ("19900000001", "能耗验证临时管理员", "x", 2, None, 1), ) created["user_id"] = cur.lastrowid # ------------------------------------------------- 临时项目(org=1,验证项目分组) cur.execute( "INSERT INTO projects (organization_id, name) VALUES (%s, %s)", (1, "能耗验证临时项目"), ) temp_project_id = cur.lastrowid created["project_id"] = temp_project_id # ------------------------------------- 临时柜子 ×2(挂临时项目;imei 15 位、abstract_id 唯一) for idx, name in enumerate(["能耗验证临时柜A", "能耗验证临时柜B"], start=1): cur.execute( "INSERT INTO cabinets (project_id, abstract_id, imei, name, status) " "VALUES (%s, %s, %s, %s, %s)", (temp_project_id, f"99-0000000{idx}", f"99900000000000{idx}", name, 1), ) cabinet_id = cur.lastrowid created["cabinet_ids"].append(cabinet_id) cur.execute( "INSERT INTO cabin_boards (cabinet_id, board_id, status) VALUES (%s, %s, %s)", (cabinet_id, 1, 1), ) board_id = cur.lastrowid created["board_ids"].append(board_id) cur.execute( "INSERT INTO compartments (cabin_board_id, channel_id, status) VALUES (%s, %s, %s)", (board_id, 1, 0), ) created["compartment_ids"].append(cur.lastrowid) comp_a, comp_b = created["compartment_ids"] cab_a, cab_b = created["cabinet_ids"] # ----------------------------------------------------------- 6 条充电记录 # 3 个柜子(已有柜 1 + 临时柜 A/B)× 2 天(今天 / 昨天): # 柜1 今天 220 Wh 昨天 1000 Wh (项目 1「联调测试项目」) # 柜A 今天 330 Wh 昨天 2000 Wh (临时项目) # 柜B 今天 440 Wh 昨天 3000 Wh (临时项目) # 期望:今日合计 990 Wh;本月合计 990 + 6000 = 6990 Wh plan = [ (1, 1, 220, "CURDATE()"), (1, 1, 1000, "DATE_SUB(CURDATE(), INTERVAL 1 DAY)"), (cab_a, comp_a, 330, "CURDATE()"), (cab_a, comp_a, 2000, "DATE_SUB(CURDATE(), INTERVAL 1 DAY)"), (cab_b, comp_b, 440, "CURDATE()"), (cab_b, comp_b, 3000, "DATE_SUB(CURDATE(), INTERVAL 1 DAY)"), ] for cabinet_id, compartment_id, energy, day_expr in plan: cur.execute( "INSERT INTO charge_records " "(compartment_id, cabinet_id, start_time, end_time, energy, max_temperature, status) " f"VALUES (%s, %s, {day_expr} + INTERVAL 10 HOUR, {day_expr} + INTERVAL 12 HOUR, %s, %s, %s)", (compartment_id, cabinet_id, energy, 29815, 0), ) created["record_ids"].append(cur.lastrowid) conn.commit() with open(os.path.join(HERE, "temp_ids.json"), "w", encoding="utf-8") as fh: json.dump(created, fh, ensure_ascii=False, indent=2) print(json.dumps(created, ensure_ascii=False, indent=2)) cur.close() conn.close()