from typing import List from fastapi import APIRouter, Query, Depends from sqlalchemy.ext.asyncio import AsyncSession from app.core.redis import get_redis_client from app.core.database import get_db from redis.asyncio import Redis from app.utils.response import success from app.schemas.common import ApiResponse from app.schemas.slot_refresh_status import RefreshBase, RefreshFail, RefreshStatusOut from app.schemas.slot_snapshot import SlotOverviewOut, SlotSnapshotCreate, SlotSnapshotOut from app.services.slot_refresh_status_service import SlotRefreshStatusService from app.services.slot_snapshot_service import SlotSnapshotService public_router = APIRouter() protected_router = APIRouter() admin_required_router = APIRouter() @public_router.get("/slots/latest", summary="查询最近的slot", tags=["Slot数据"], response_model=ApiResponse[SlotSnapshotOut]) async def slots_latest_get( country: str = Query("", description="目的国家"), city: str = Query("", description="递交城市"), visa_type: str = Query("", description="签证类型"), db: AsyncSession = Depends(get_db) ): res = await SlotSnapshotService.latest_for(db, country, city, visa_type) return success(data=res) @public_router.get("/slots/overview", summary="查询最近的slot", tags=["Slot数据"], response_model=ApiResponse[List[SlotOverviewOut]]) async def slots_latest_get( city: str = Query("", description="递交城市"), db: AsyncSession = Depends(get_db) ): res = await SlotSnapshotService.get_slot_overview(db, city) return success(data=res) @admin_required_router.post("/slots/report", summary="上报最近的slot", tags=["Slot数据"], response_model=ApiResponse[SlotSnapshotOut]) async def slots_report( payload: SlotSnapshotCreate, db: AsyncSession = Depends(get_db), redis_client: Redis = Depends(get_redis_client) ): res = await SlotSnapshotService.report(db, redis_client, payload) return success(data=res) @admin_required_router.post("/slot_refresh/start", summary="刷新slot开始", tags=["Slot Monitor 监控"], response_model=ApiResponse[RefreshStatusOut]) async def slot_refresh_start(data: RefreshBase, db: AsyncSession = Depends(get_db) ): data = await SlotRefreshStatusService.refresh_start(db, data) return success(data=data) @admin_required_router.post("/slot_refresh/success", summary="刷新slot成功", tags=["Slot Monitor 监控"], response_model=ApiResponse[RefreshStatusOut]) async def slot_refresh_success(data: RefreshBase, db: AsyncSession = Depends(get_db) ): data = await SlotRefreshStatusService.refresh_success(db, data) return success(data=data) @admin_required_router.post("/slot_refresh/fail", summary="刷新slot失败", tags=["Slot Monitor 监控"], response_model=ApiResponse[RefreshStatusOut]) async def slot_refresh_fail(data: RefreshFail,db: AsyncSession = Depends(get_db) ): data = await SlotRefreshStatusService.refresh_fail(db, data) return success(data=data) @admin_required_router.get("/slot_refresh/status", summary="查询刷新纪录", tags=["Slot Monitor 监控"], response_model=ApiResponse[List[RefreshStatusOut]]) async def slot_refresh_status(db: AsyncSession = Depends(get_db)): data = await SlotRefreshStatusService.list_all(db) return success(data=data)