内容安全审核
This commit is contained in:
43
service/device_action_service.py
Normal file
43
service/device_action_service.py
Normal file
@ -0,0 +1,43 @@
|
||||
from ds.db import db
|
||||
from schema.device_action_schema import DeviceActionCreate, DeviceActionResponse
|
||||
from mysql.connector import Error as MySQLError
|
||||
|
||||
|
||||
# 新增设备操作记录
|
||||
def add_device_action(client_ip: str, action: int) -> DeviceActionResponse:
|
||||
"""
|
||||
新增设备操作记录(内部方法、非接口)
|
||||
:param action_data: 含client_ip和action(0/1)
|
||||
:return: 新增的完整记录
|
||||
"""
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 插入SQL(id自增、依赖数据库自动生成)
|
||||
insert_query = """
|
||||
INSERT INTO device_action
|
||||
(client_ip, action, created_at, updated_at)
|
||||
VALUES (%s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
cursor.execute(insert_query, (
|
||||
client_ip,
|
||||
action
|
||||
))
|
||||
conn.commit()
|
||||
|
||||
# 获取新增记录(通过自增ID查询)
|
||||
new_id = cursor.lastrowid
|
||||
cursor.execute("SELECT * FROM device_action WHERE id = %s", (new_id,))
|
||||
new_action = cursor.fetchone()
|
||||
|
||||
return DeviceActionResponse(**new_action)
|
||||
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"新增记录失败: {str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
Reference in New Issue
Block a user