controller.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. RoleCreateSchema,
  14. RoleOutSchema,
  15. RolePermissionSettingSchema,
  16. RoleQueryParam,
  17. RoleUpdateSchema,
  18. )
  19. from .service import RoleService
  20. RoleRouter = APIRouter(route_class=OperationLogRoute, prefix="/role", tags=["角色管理"])
  21. @RoleRouter.get(
  22. "/list",
  23. summary="查询角色",
  24. description="查询角色",
  25. response_model=ResponseSchema[list[RoleOutSchema]],
  26. )
  27. async def get_obj_list_controller(
  28. page: Annotated[PaginationQueryParam, Depends()],
  29. search: Annotated[RoleQueryParam, Depends()],
  30. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:query"]))],
  31. ) -> JSONResponse:
  32. """
  33. 查询角色
  34. 参数:
  35. - page (PaginationQueryParam): 分页查询参数模型
  36. - search (RoleQueryParam): 查询参数模型
  37. - auth (AuthSchema): 认证信息模型
  38. 返回:
  39. - JSONResponse: 分页查询结果JSON响应
  40. """
  41. order_by = [{"order": "asc"}]
  42. if page.order_by:
  43. order_by = page.order_by
  44. result_dict = await RoleService.get_role_page_service(
  45. auth=auth,
  46. page_no=page.page_no,
  47. page_size=page.page_size,
  48. search=search,
  49. order_by=order_by,
  50. )
  51. log.info("查询角色成功")
  52. return SuccessResponse(data=result_dict, msg="查询角色成功")
  53. @RoleRouter.get(
  54. "/detail/{id}",
  55. summary="查询角色详情",
  56. description="查询角色详情",
  57. response_model=ResponseSchema[RoleOutSchema],
  58. )
  59. async def get_obj_detail_controller(
  60. id: Annotated[int, Path(description="角色ID")],
  61. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:detail"]))],
  62. ) -> JSONResponse:
  63. """
  64. 查询角色详情
  65. 参数:
  66. - id (int): 角色ID
  67. - auth (AuthSchema): 认证信息模型
  68. 返回:
  69. - JSONResponse: 角色详情JSON响应
  70. """
  71. result_dict = await RoleService.get_role_detail_service(id=id, auth=auth)
  72. log.info(f"获取角色详情成功 {id}")
  73. return SuccessResponse(data=result_dict, msg="获取角色详情成功")
  74. @RoleRouter.post(
  75. "/create",
  76. summary="创建角色",
  77. description="创建角色",
  78. response_model=ResponseSchema[RoleOutSchema],
  79. )
  80. async def create_obj_controller(
  81. data: RoleCreateSchema,
  82. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:create"]))],
  83. ) -> JSONResponse:
  84. """
  85. 创建角色
  86. 参数:
  87. - data (RoleCreateSchema): 创建角色模型
  88. - auth (AuthSchema): 认证信息模型
  89. 返回:
  90. - JSONResponse: 创建角色JSON响应
  91. """
  92. result_dict = await RoleService.create_role_service(data=data, auth=auth)
  93. log.info(f"创建角色成功: {result_dict}")
  94. return SuccessResponse(data=result_dict, msg="创建角色成功")
  95. @RoleRouter.put(
  96. "/update/{id}",
  97. summary="修改角色",
  98. description="修改角色",
  99. response_model=ResponseSchema[RoleOutSchema],
  100. )
  101. async def update_obj_controller(
  102. data: RoleUpdateSchema,
  103. id: Annotated[int, Path(description="角色ID")],
  104. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:update"]))],
  105. ) -> JSONResponse:
  106. """
  107. 修改角色
  108. 参数:
  109. - data (RoleUpdateSchema): 修改角色模型
  110. - id (int): 角色ID
  111. - auth (AuthSchema): 认证信息模型
  112. 返回:
  113. - JSONResponse: 修改角色JSON响应
  114. """
  115. result_dict = await RoleService.update_role_service(id=id, data=data, auth=auth)
  116. log.info(f"修改角色成功: {result_dict}")
  117. return SuccessResponse(data=result_dict, msg="修改角色成功")
  118. @RoleRouter.delete(
  119. "/delete",
  120. summary="删除角色",
  121. description="删除角色",
  122. response_model=ResponseSchema[None],
  123. )
  124. async def delete_obj_controller(
  125. ids: Annotated[list[int], Body(description="ID列表")],
  126. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:delete"]))],
  127. ) -> JSONResponse:
  128. """
  129. 删除角色
  130. 参数:
  131. - ids (list[int]): ID列表
  132. - auth (AuthSchema): 认证信息模型
  133. 返回:
  134. - JSONResponse: 删除角色JSON响应
  135. """
  136. await RoleService.delete_role_service(ids=ids, auth=auth)
  137. log.info(f"删除角色成功: {ids}")
  138. return SuccessResponse(msg="删除角色成功")
  139. @RoleRouter.patch(
  140. "/available/setting",
  141. summary="批量修改角色状态",
  142. description="批量修改角色状态",
  143. response_model=ResponseSchema[None],
  144. )
  145. async def batch_set_available_obj_controller(
  146. data: BatchSetAvailable,
  147. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:patch"]))],
  148. ) -> JSONResponse:
  149. """
  150. 批量修改角色状态
  151. 参数:
  152. - data (BatchSetAvailable): 批量修改角色状态模型
  153. - auth (AuthSchema): 认证信息模型
  154. 返回:
  155. - JSONResponse: 批量修改角色状态JSON响应
  156. """
  157. await RoleService.set_role_available_service(data=data, auth=auth)
  158. log.info(f"批量修改角色状态成功: {data.ids}")
  159. return SuccessResponse(msg="批量修改角色状态成功")
  160. @RoleRouter.patch(
  161. "/permission/setting",
  162. summary="角色授权",
  163. description="角色授权",
  164. response_model=ResponseSchema[None],
  165. )
  166. async def set_role_permission_controller(
  167. data: RolePermissionSettingSchema,
  168. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:permission"]))],
  169. ) -> JSONResponse:
  170. """
  171. 角色授权
  172. 参数:
  173. - data (RolePermissionSettingSchema): 角色授权模型
  174. - auth (AuthSchema): 认证信息模型
  175. 返回:
  176. - JSONResponse: 角色授权JSON响应
  177. """
  178. await RoleService.set_role_permission_service(data=data, auth=auth)
  179. log.info(f"设置角色权限成功: {data}")
  180. return SuccessResponse(msg="授权角色成功")
  181. @RoleRouter.post(
  182. "/export",
  183. summary="导出角色",
  184. description="导出角色",
  185. response_model=ResponseSchema[None],
  186. )
  187. async def export_obj_list_controller(
  188. search: Annotated[RoleQueryParam, Depends()],
  189. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:role:export"]))],
  190. ) -> StreamingResponse:
  191. """
  192. 导出角色
  193. 参数:
  194. - search (RoleQueryParam): 查询参数模型
  195. - auth (AuthSchema): 认证信息模型
  196. 返回:
  197. - StreamingResponse: 导出角色流响应
  198. """
  199. role_query_result = await RoleService.get_role_list_service(search=search, auth=auth)
  200. role_export_result = await RoleService.export_role_list_service(role_list=role_query_result)
  201. log.info("导出角色成功")
  202. return StreamResponse(
  203. data=bytes2file_response(role_export_result),
  204. media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  205. headers={"Content-Disposition": "attachment; filename=role.xlsx"},
  206. )