- 新增 POST /api/alarms/batch-delete、/api/bluetooth/batch-delete、 /api/heartbeats/batch-delete 批量删除端点 (最多500条) - 四个页面表格添加全选复选框和"删除选中"按钮 - 提取通用 toggleAllCheckboxes/updateSelCount/_batchDelete 函数 - 数据日志页面根据当前查询类型自动路由到对应的批量删除API via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
185 lines
7.3 KiB
Python
185 lines
7.3 KiB
Python
"""
|
||
Bluetooth Router - 蓝牙数据接口
|
||
API endpoints for querying Bluetooth punch and location records.
|
||
"""
|
||
|
||
import math
|
||
from datetime import datetime
|
||
from typing import Literal
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||
from sqlalchemy import func, select
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
||
from app.database import get_db
|
||
from app.models import BluetoothRecord
|
||
from app.schemas import (
|
||
APIResponse,
|
||
BluetoothRecordResponse,
|
||
PaginatedList,
|
||
)
|
||
from app.services import device_service
|
||
|
||
router = APIRouter(prefix="/api/bluetooth", tags=["Bluetooth / 蓝牙数据"])
|
||
|
||
|
||
@router.get(
|
||
"",
|
||
response_model=APIResponse[PaginatedList[BluetoothRecordResponse]],
|
||
summary="获取蓝牙记录列表 / List bluetooth records",
|
||
)
|
||
async def list_bluetooth_records(
|
||
device_id: int | None = Query(default=None, description="设备ID / Device ID"),
|
||
record_type: str | None = Query(default=None, description="记录类型 / Record type (punch/location)"),
|
||
beacon_mac: str | None = Query(default=None, description="信标MAC / Beacon MAC filter"),
|
||
start_time: datetime | None = Query(default=None, description="开始时间 / Start time (ISO 8601)"),
|
||
end_time: datetime | None = Query(default=None, description="结束时间 / End time (ISO 8601)"),
|
||
sort_order: Literal["asc", "desc"] = Query(default="desc", description="排序方向 / Sort order (asc/desc)"),
|
||
page: int = Query(default=1, ge=1, description="页码 / Page number"),
|
||
page_size: int = Query(default=20, ge=1, le=100, description="每页数量 / Items per page"),
|
||
db: AsyncSession = Depends(get_db),
|
||
):
|
||
"""
|
||
获取蓝牙数据记录列表,支持按设备、记录类型、信标MAC、时间范围过滤。
|
||
List Bluetooth records with filters for device, record type, beacon MAC, and time range.
|
||
"""
|
||
query = select(BluetoothRecord)
|
||
count_query = select(func.count(BluetoothRecord.id))
|
||
|
||
if device_id is not None:
|
||
query = query.where(BluetoothRecord.device_id == device_id)
|
||
count_query = count_query.where(BluetoothRecord.device_id == device_id)
|
||
|
||
if record_type:
|
||
query = query.where(BluetoothRecord.record_type == record_type)
|
||
count_query = count_query.where(BluetoothRecord.record_type == record_type)
|
||
|
||
if beacon_mac:
|
||
query = query.where(BluetoothRecord.beacon_mac == beacon_mac)
|
||
count_query = count_query.where(BluetoothRecord.beacon_mac == beacon_mac)
|
||
|
||
if start_time:
|
||
query = query.where(BluetoothRecord.recorded_at >= start_time)
|
||
count_query = count_query.where(BluetoothRecord.recorded_at >= start_time)
|
||
|
||
if end_time:
|
||
query = query.where(BluetoothRecord.recorded_at <= end_time)
|
||
count_query = count_query.where(BluetoothRecord.recorded_at <= end_time)
|
||
|
||
total_result = await db.execute(count_query)
|
||
total = total_result.scalar() or 0
|
||
|
||
offset = (page - 1) * page_size
|
||
order = BluetoothRecord.recorded_at.asc() if sort_order == "asc" else BluetoothRecord.recorded_at.desc()
|
||
query = query.order_by(order).offset(offset).limit(page_size)
|
||
result = await db.execute(query)
|
||
records = list(result.scalars().all())
|
||
|
||
return APIResponse(
|
||
data=PaginatedList(
|
||
items=[BluetoothRecordResponse.model_validate(r) for r in records],
|
||
total=total,
|
||
page=page,
|
||
page_size=page_size,
|
||
total_pages=math.ceil(total / page_size) if total else 0,
|
||
)
|
||
)
|
||
|
||
|
||
@router.get(
|
||
"/device/{device_id}",
|
||
response_model=APIResponse[PaginatedList[BluetoothRecordResponse]],
|
||
summary="获取设备蓝牙记录 / Get bluetooth records for device",
|
||
)
|
||
async def device_bluetooth_records(
|
||
device_id: int,
|
||
record_type: str | None = Query(default=None, description="记录类型 / Record type (punch/location)"),
|
||
start_time: datetime | None = Query(default=None, description="开始时间 / Start time"),
|
||
end_time: datetime | None = Query(default=None, description="结束时间 / End time"),
|
||
page: int = Query(default=1, ge=1, description="页码"),
|
||
page_size: int = Query(default=20, ge=1, le=100, description="每页数量"),
|
||
db: AsyncSession = Depends(get_db),
|
||
):
|
||
"""
|
||
获取指定设备的蓝牙数据记录。
|
||
Get Bluetooth records for a specific device.
|
||
"""
|
||
device = await device_service.get_device(db, device_id)
|
||
if device is None:
|
||
raise HTTPException(status_code=404, detail=f"Device {device_id} not found / 未找到设备{device_id}")
|
||
|
||
query = select(BluetoothRecord).where(BluetoothRecord.device_id == device_id)
|
||
count_query = select(func.count(BluetoothRecord.id)).where(BluetoothRecord.device_id == device_id)
|
||
|
||
if record_type:
|
||
query = query.where(BluetoothRecord.record_type == record_type)
|
||
count_query = count_query.where(BluetoothRecord.record_type == record_type)
|
||
|
||
if start_time:
|
||
query = query.where(BluetoothRecord.recorded_at >= start_time)
|
||
count_query = count_query.where(BluetoothRecord.recorded_at >= start_time)
|
||
|
||
if end_time:
|
||
query = query.where(BluetoothRecord.recorded_at <= end_time)
|
||
count_query = count_query.where(BluetoothRecord.recorded_at <= end_time)
|
||
|
||
total_result = await db.execute(count_query)
|
||
total = total_result.scalar() or 0
|
||
|
||
offset = (page - 1) * page_size
|
||
query = query.order_by(BluetoothRecord.recorded_at.desc()).offset(offset).limit(page_size)
|
||
result = await db.execute(query)
|
||
records = list(result.scalars().all())
|
||
|
||
return APIResponse(
|
||
data=PaginatedList(
|
||
items=[BluetoothRecordResponse.model_validate(r) for r in records],
|
||
total=total,
|
||
page=page,
|
||
page_size=page_size,
|
||
total_pages=math.ceil(total / page_size) if total else 0,
|
||
)
|
||
)
|
||
|
||
|
||
@router.post(
|
||
"/batch-delete",
|
||
response_model=APIResponse[dict],
|
||
summary="批量删除蓝牙记录 / Batch delete bluetooth records",
|
||
)
|
||
async def batch_delete_bluetooth(
|
||
body: dict,
|
||
db: AsyncSession = Depends(get_db),
|
||
):
|
||
"""批量删除蓝牙记录,最多500条。 / Batch delete bluetooth records (max 500)."""
|
||
record_ids = body.get("record_ids", [])
|
||
if not record_ids:
|
||
raise HTTPException(status_code=400, detail="record_ids is required")
|
||
if len(record_ids) > 500:
|
||
raise HTTPException(status_code=400, detail="Max 500 records per request")
|
||
result = await db.execute(
|
||
select(BluetoothRecord).where(BluetoothRecord.id.in_(record_ids))
|
||
)
|
||
records = list(result.scalars().all())
|
||
for r in records:
|
||
await db.delete(r)
|
||
await db.flush()
|
||
return APIResponse(data={"deleted": len(records)})
|
||
|
||
|
||
# NOTE: /{record_id} must be after /device/{device_id} to avoid route conflicts
|
||
@router.get(
|
||
"/{record_id}",
|
||
response_model=APIResponse[BluetoothRecordResponse],
|
||
summary="获取蓝牙记录详情 / Get bluetooth record",
|
||
)
|
||
async def get_bluetooth_record(record_id: int, db: AsyncSession = Depends(get_db)):
|
||
"""按ID获取蓝牙记录详情 / Get bluetooth record details by ID."""
|
||
result = await db.execute(
|
||
select(BluetoothRecord).where(BluetoothRecord.id == record_id)
|
||
)
|
||
record = result.scalar_one_or_none()
|
||
if record is None:
|
||
raise HTTPException(status_code=404, detail=f"Bluetooth record {record_id} not found")
|
||
return APIResponse(data=BluetoothRecordResponse.model_validate(record))
|