controller.py 6.1 KB

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