#!/usr/bin/env python # -*- coding: utf-8 -*- from fastapi import APIRouter, Depends from sqlalchemy import asc from sqlalchemy.ext.asyncio import AsyncSession from crud.sysdata.region import crud_region from schemas.sysdata.region import RegionItemList from utils.depends import get_async_db router = APIRouter() @router.get("/regions", response_model=RegionItemList, summary="地区列表(级联选择用)", description="地区列表,默认选择省列表。") async def get_regions(page: int = None, size: int = None, code: str = "", level: int = 1, db: AsyncSession = Depends(get_async_db)): filters = {"level": level} if code: filters["pcode"] = code if level != 1: filters["level"] = level # 翻页 if ((page is not None) and (page > 0)) \ and ((size is not None) and (size > 0)): offset = (page - 1) * size else: offset = size = None total, items = await crud_region.find_all(db, filters=filters, offset=offset, limit=size, order_by=[asc("id")]) return {"total": total, "data": items}