controller.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. from typing import Annotated
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse, StreamingResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
  6. from app.core.base_params import PaginationQueryParam
  7. from app.core.base_schema import BatchSetAvailable
  8. from app.core.dependencies import AuthPermission
  9. from app.core.logger import log
  10. from app.core.router_class import OperationLogRoute
  11. from app.utils.common_util import bytes2file_response
  12. from .schema import (
  13. PositionCreateSchema,
  14. PositionOutSchema,
  15. PositionQueryParam,
  16. PositionUpdateSchema,
  17. )
  18. from .service import PositionService
  19. PositionRouter = APIRouter(route_class=OperationLogRoute, prefix="/position", tags=["岗位管理"])
  20. @PositionRouter.get(
  21. "/list",
  22. summary="查询岗位",
  23. description="查询岗位",
  24. response_model=ResponseSchema[list[PositionOutSchema]],
  25. )
  26. async def get_obj_list_controller(
  27. page: Annotated[PaginationQueryParam, Depends()],
  28. search: Annotated[PositionQueryParam, Depends()],
  29. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:query"]))],
  30. ) -> JSONResponse:
  31. """
  32. 查询岗位列表
  33. 参数:
  34. - page (PaginationQueryParam): 分页查询参数
  35. - search (PositionQueryParam): 查询参数
  36. - auth (AuthSchema): 认证信息模型
  37. 返回:
  38. - JSONResponse: 分页查询结果
  39. """
  40. order_by = [{"order": "asc"}]
  41. if page.order_by:
  42. order_by = page.order_by
  43. result_dict = await PositionService.get_position_page_service(
  44. auth=auth,
  45. page_no=page.page_no,
  46. page_size=page.page_size,
  47. search=search,
  48. order_by=order_by,
  49. )
  50. log.info("查询岗位列表成功")
  51. return SuccessResponse(data=result_dict, msg="查询岗位列表成功")
  52. @PositionRouter.get(
  53. "/detail/{id}",
  54. summary="查询岗位详情",
  55. description="查询岗位详情",
  56. response_model=ResponseSchema[PositionOutSchema],
  57. )
  58. async def get_obj_detail_controller(
  59. id: Annotated[int, Path(description="岗位ID")],
  60. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:detail"]))],
  61. ) -> JSONResponse:
  62. """
  63. 查询岗位详情
  64. 参数:
  65. - id (int): 岗位ID
  66. - auth (AuthSchema): 认证信息模型
  67. 返回:
  68. - JSONResponse: 岗位详情对象
  69. """
  70. result_dict = await PositionService.get_position_detail_service(id=id, auth=auth)
  71. log.info(f"查询岗位详情成功 {id}")
  72. return SuccessResponse(data=result_dict, msg="获取岗位详情成功")
  73. @PositionRouter.post(
  74. "/create",
  75. summary="创建岗位",
  76. description="创建岗位",
  77. response_model=ResponseSchema[PositionOutSchema],
  78. )
  79. async def create_obj_controller(
  80. data: PositionCreateSchema,
  81. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:create"]))],
  82. ) -> JSONResponse:
  83. """
  84. 创建岗位
  85. 参数:
  86. - data (PositionCreateSchema): 创建岗位模型
  87. - auth (AuthSchema): 认证信息模型
  88. 返回:
  89. - JSONResponse: 岗位详情对象
  90. """
  91. result_dict = await PositionService.create_position_service(data=data, auth=auth)
  92. log.info(f"创建岗位成功: {result_dict}")
  93. return SuccessResponse(data=result_dict, msg="创建岗位成功")
  94. @PositionRouter.put(
  95. "/update/{id}",
  96. summary="修改岗位",
  97. description="修改岗位",
  98. response_model=ResponseSchema[PositionOutSchema],
  99. )
  100. async def update_obj_controller(
  101. data: PositionUpdateSchema,
  102. id: Annotated[int, Path(description="岗位ID")],
  103. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:update"]))],
  104. ) -> JSONResponse:
  105. """
  106. 修改岗位
  107. 参数:
  108. - data (PositionUpdateSchema): 修改岗位模型
  109. - id (int): 岗位ID
  110. - auth (AuthSchema): 认证信息模型
  111. 返回:
  112. - JSONResponse: 岗位详情对象
  113. """
  114. result_dict = await PositionService.update_position_service(id=id, data=data, auth=auth)
  115. log.info(f"修改岗位成功: {result_dict}")
  116. return SuccessResponse(data=result_dict, msg="修改岗位成功")
  117. @PositionRouter.delete(
  118. "/delete",
  119. summary="删除岗位",
  120. description="删除岗位",
  121. response_model=ResponseSchema[None],
  122. )
  123. async def delete_obj_controller(
  124. ids: Annotated[list[int], Body(description="ID列表")],
  125. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:delete"]))],
  126. ) -> JSONResponse:
  127. """
  128. 删除岗位
  129. 参数:
  130. - ids (list[int]): ID列表
  131. - auth (AuthSchema): 认证信息模型
  132. 返回:
  133. - JSONResponse: 成功消息
  134. """
  135. await PositionService.delete_position_service(ids=ids, auth=auth)
  136. log.info(f"删除岗位成功: {ids}")
  137. return SuccessResponse(msg="删除岗位成功")
  138. @PositionRouter.patch(
  139. "/available/setting",
  140. summary="批量修改岗位状态",
  141. description="批量修改岗位状态",
  142. response_model=ResponseSchema[None],
  143. )
  144. async def batch_set_available_obj_controller(
  145. data: BatchSetAvailable,
  146. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:patch"]))],
  147. ) -> JSONResponse:
  148. """
  149. 批量修改岗位状态
  150. 参数:
  151. - data (BatchSetAvailable): 批量修改岗位状态模型
  152. - auth (AuthSchema): 认证信息模型
  153. 返回:
  154. - JSONResponse: 成功消息
  155. """
  156. await PositionService.set_position_available_service(data=data, auth=auth)
  157. log.info(f"批量修改岗位状态成功: {data.ids}")
  158. return SuccessResponse(msg="批量修改岗位状态成功")
  159. @PositionRouter.post(
  160. "/export",
  161. summary="导出岗位",
  162. description="导出岗位",
  163. response_model=ResponseSchema[None],
  164. )
  165. async def export_obj_list_controller(
  166. search: Annotated[PositionQueryParam, Depends()],
  167. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:position:export"]))],
  168. ) -> StreamingResponse:
  169. """
  170. 导出岗位
  171. 参数:
  172. - search (PositionQueryParam): 查询参数
  173. - auth (AuthSchema): 认证信息模型
  174. 返回:
  175. - StreamingResponse: 岗位Excel文件流
  176. """
  177. position_query_result = await PositionService.get_position_list_service(
  178. search=search, auth=auth
  179. )
  180. position_export_result = await PositionService.export_position_list_service(
  181. position_list=position_query_result
  182. )
  183. log.info("导出岗位成功")
  184. return StreamResponse(
  185. data=bytes2file_response(position_export_result),
  186. media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  187. headers={"Content-Disposition": "attachment; filename=position.xlsx"},
  188. )