feat: 添加网页进行访问查询

This commit is contained in:
2025-12-27 00:12:28 +08:00
parent 04fe771e8f
commit 3fbb216aa9
6 changed files with 245 additions and 5 deletions

View File

@@ -28,7 +28,6 @@ class AmmeterUsageModel:
@classmethod
async def insert(cls, data: dict):
print(data)
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
await conn.execute(
@@ -53,3 +52,68 @@ class AmmeterUsageModel:
),
)
await conn.commit()
@classmethod
async def exec(cls, sql: str, *args, **kwargs):
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
await conn.execute(sql, *args, **kwargs)
await conn.commit()
@classmethod
async def get_recent(cls, room_id: int, days: int = 30):
"""查询最近N天数据"""
now = int(time.time() * 1000)
start = now - days * 24 * 60 * 60 * 1000
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
cur = await conn.execute(
"""
SELECT created_at, left_ele, left_free_ele, left_money
FROM ammeter_usage
WHERE room_id = ?
AND created_at >= ?
ORDER BY created_at ASC
""",
(room_id, start),
)
rows = await cur.fetchall()
return [dict(r) for r in rows]
@classmethod
async def get_range(cls, room_id: int, start: int, end: int):
"""按时间区间查询"""
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
cur = await conn.execute(
"""
SELECT *
FROM ammeter_usage
WHERE room_id = ?
AND created_at BETWEEN ? AND ?
ORDER BY created_at ASC
""",
(room_id, start, end),
)
rows = await cur.fetchall()
return [dict(r) for r in rows]
@classmethod
async def list(cls, room_id: int, page: int = 1, limit: int = 20):
"""分页查询,用于表格展示"""
offset = (page - 1) * limit
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
cur = await conn.execute(
"""
SELECT *
FROM ammeter_usage
WHERE room_id = ?
ORDER BY created_at DESC
LIMIT ? OFFSET ?
""",
(room_id, limit, offset),
)
rows = await cur.fetchall()
return [dict(r) for r in rows]