search.py 5.9 KB

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