search.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. from fastapi import APIRouter, Depends, Query
  5. from sqlalchemy import asc
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from common.const import PERIODS, SUBJECTS
  8. from crud.school import crud_school, crud_grade, crud_class, crud_system
  9. from crud.sysdata.region import crud_region
  10. from crud.sysdata.role import crud_role
  11. from models.school import School, SchoolGrade, SchoolClass
  12. from models.sysdata.region import Region
  13. from models.sysdata.role import Role
  14. from models.user import Admin
  15. from schemas.base import ReturnField
  16. from schemas.school.school import (SchoolItemList, GradeItemList, SubjectList, PeriodList,
  17. YearsList, SchoolSystemList, ClassSearchItemList)
  18. from schemas.sysdata.region import RegionItemList
  19. from schemas.sysdata.role import RoleList
  20. from utils.depends import get_current_user, get_async_db
  21. router = APIRouter()
  22. @router.get("/schools/search",
  23. summary="学校列表",
  24. response_model=SchoolItemList,
  25. response_model_exclude_none=True)
  26. async def get_schools_search(name: str = "",
  27. res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"),
  28. db: AsyncSession = Depends(get_async_db),
  29. current_user: Admin = Depends(get_current_user)):
  30. _q = []
  31. if name:
  32. _q.append(School.name.like(f"{name}%"))
  33. _, db_schools = await crud_school.find_all(db, filters=_q, return_fields=res)
  34. return {"data": db_schools}
  35. @router.get("/grades/search",
  36. summary="年级列表",
  37. response_model=GradeItemList,
  38. response_model_exclude_none=True)
  39. async def get_grades_search(name: str = "",
  40. sid: int = 0,
  41. res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"),
  42. db: AsyncSession = Depends(get_async_db),
  43. current_user: Admin = Depends(get_current_user)):
  44. _q = []
  45. if name:
  46. _q.append(SchoolGrade.name.like(f"{name}%"))
  47. if sid:
  48. _q.append(SchoolGrade.school_id == sid)
  49. total, db_grades = await crud_grade.find_all(db,
  50. filters=_q,
  51. return_fields=res,
  52. order_by=[asc("order")])
  53. return {"data": db_grades}
  54. @router.get("/classes/search",
  55. summary="班级列表",
  56. response_model=ClassSearchItemList,
  57. response_model_exclude_none=True)
  58. async def get_classes_search(name: str = "",
  59. gid: str = "",
  60. res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"),
  61. db: AsyncSession = Depends(get_async_db),
  62. current_user: Admin = Depends(get_current_user)):
  63. _q = []
  64. if name:
  65. _q.append(SchoolClass.name.like(f"{name}%"))
  66. if gid:
  67. _q.append(SchoolClass.grade_id == int(gid))
  68. _, db_classes = await crud_class.find_all(db,
  69. filters=_q,
  70. return_fields=res,
  71. order_by=[asc("id")])
  72. return {"data": db_classes}
  73. @router.get("/roles/search",
  74. summary="职务列表",
  75. response_model=RoleList,
  76. response_model_exclude_none=True)
  77. async def get_roles_search(name: str = "",
  78. platform: str = "app",
  79. res: ReturnField = Query(["id", "name"], description="控制返回字段,字段逗号分隔"),
  80. db: AsyncSession = Depends(get_async_db),
  81. current_user: Admin = Depends(get_current_user)):
  82. _q = []
  83. if name:
  84. _q.append(Role.name.like(f"{name}%"))
  85. if platform:
  86. _q.append(Role.platform == platform)
  87. total, items = await crud_role.find_all(db, filters=_q, return_fields=res, order_by=[asc("id")])
  88. return {"data": items, "total": total}
  89. @router.get("/subjects/search",
  90. summary="学科列表",
  91. response_model=SubjectList,
  92. response_model_exclude_none=True)
  93. async def get_subjects_search(current_user: Admin = Depends(get_current_user)):
  94. data = [{"name": item} for item in SUBJECTS]
  95. return {"data": data}
  96. @router.get("/periods/search",
  97. summary="学段列表",
  98. response_model=PeriodList,
  99. response_model_exclude_none=True)
  100. async def get_periods_search(current_user: Admin = Depends(get_current_user)):
  101. data = [{"name": item} for item in PERIODS]
  102. return {"data": data}
  103. @router.get("/schoolsystem/search",
  104. summary="学制列表",
  105. response_model=SchoolSystemList,
  106. response_model_exclude_none=True)
  107. async def get_system_search(db: AsyncSession = Depends(get_async_db),
  108. current_user: Admin = Depends(get_current_user)):
  109. _, items = await crud_system.find_all(db, filters={}, order_by=[asc("id")])
  110. return {"data": items}
  111. @router.get("/years/search",
  112. summary="年份列表",
  113. response_model=YearsList,
  114. response_model_exclude_none=True)
  115. async def get_years_search(current_user: Admin = Depends(get_current_user)):
  116. now = datetime.datetime.now().year + 1
  117. data = [{"name": item} for item in range(now - 10, now)]
  118. data.reverse()
  119. return {"data": data}
  120. @router.get("/regions/search",
  121. summary="地区列表(省份列表)",
  122. response_model=RegionItemList,
  123. response_model_exclude_none=True)
  124. async def get_regions_search(name: str = "",
  125. res: ReturnField = Query(["id", "name", "code"],
  126. description="控制返回字段,字段逗号分隔"),
  127. db: AsyncSession = Depends(get_async_db),
  128. current_user: Admin = Depends(get_current_user)):
  129. _q = [Region.level == 1]
  130. if name:
  131. _q.append(Region.name.like(f"{name}%"))
  132. _, db_regions = await crud_region.find_all(db,
  133. filters=_q,
  134. return_fields=res,
  135. order_by=[asc("id")])
  136. return {"data": db_regions}