#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime from fastapi import APIRouter, Depends, Query from sqlalchemy import asc from sqlalchemy.ext.asyncio import AsyncSession from common.const import PERIODS, SUBJECTS from crud.school import crud_school, crud_grade, crud_class, crud_system from crud.sysdata.region import crud_region from crud.sysdata.role import crud_role from models.school import School, SchoolGrade, SchoolClass from models.sysdata.region import Region from models.sysdata.role import Role from models.user import Admin from schemas.base import ReturnField from schemas.school.school import (SchoolItemList, GradeItemList, SubjectList, PeriodList, YearsList, SchoolSystemList, ClassSearchItemList) from schemas.sysdata.region import RegionItemList from schemas.sysdata.role import RoleList from utils.depends import get_current_user, get_async_db router = APIRouter() @router.get("/schools/search", summary="学校列表", response_model=SchoolItemList, response_model_exclude_none=True) async def get_schools_search(name: str = "", res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [] if name: _q.append(School.name.like(f"{name}%")) _, db_schools = await crud_school.find_all(db, filters=_q, return_fields=res) return {"data": db_schools} @router.get("/grades/search", summary="年级列表", response_model=GradeItemList, response_model_exclude_none=True) async def get_grades_search(name: str = "", sid: int = 0, res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [] if name: _q.append(SchoolGrade.name.like(f"{name}%")) if sid: _q.append(SchoolGrade.school_id == sid) total, db_grades = await crud_grade.find_all(db, filters=_q, return_fields=res, order_by=[asc("order")]) return {"data": db_grades} @router.get("/classes/search", summary="班级列表", response_model=ClassSearchItemList, response_model_exclude_none=True) async def get_classes_search(name: str = "", gid: str = "", res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [] if name: _q.append(SchoolClass.name.like(f"{name}%")) if gid: _q.append(SchoolClass.grade_id == int(gid)) _, db_classes = await crud_class.find_all(db, filters=_q, return_fields=res, order_by=[asc("id")]) return {"data": db_classes} @router.get("/roles/search", summary="职务列表", response_model=RoleList, response_model_exclude_none=True) async def get_roles_search(name: str = "", platform: str = "app", res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [] if name: _q.append(Role.name.like(f"{name}%")) if platform: _q.append(Role.platform == platform) total, items = await crud_role.find_all(db, filters=_q, return_fields=res, order_by=[asc("id")]) return {"data": items, "total": total} @router.get("/subjects/search", summary="学科列表", response_model=SubjectList, response_model_exclude_none=True) async def get_subjects_search(current_user: Admin = Depends(get_current_user)): data = [{"name": item} for item in SUBJECTS] return {"data": data} @router.get("/periods/search", summary="学段列表", response_model=PeriodList, response_model_exclude_none=True) async def get_periods_search(current_user: Admin = Depends(get_current_user)): data = [{"name": item} for item in PERIODS] return {"data": data} @router.get("/schoolsystem/search", summary="学制列表", response_model=SchoolSystemList, response_model_exclude_none=True) async def get_system_search(db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _, items = await crud_system.find_all(db, filters={}, order_by=[asc("id")]) return {"data": items} @router.get("/years/search", summary="年份列表", response_model=YearsList, response_model_exclude_none=True) async def get_years_search(current_user: Admin = Depends(get_current_user)): now = datetime.datetime.now().year + 1 data = [{"name": item} for item in range(now - 10, now)] data.reverse() return {"data": data} @router.get("/regions/search", summary="地区列表(省份列表)", response_model=RegionItemList, response_model_exclude_none=True) async def get_regions_search(name: str = "", res: ReturnField = Query(["id", "name", "code"], description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [Region.level == 1] if name: _q.append(Region.name.like(f"{name}%")) _, db_regions = await crud_region.find_all(db, filters=_q, return_fields=res, order_by=[asc("id")]) return {"data": db_regions}