controller.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. from typing import Annotated
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.common.response import ResponseSchema, SuccessResponse
  6. from app.core.base_schema import BatchSetAvailable
  7. from app.core.dependencies import AuthPermission
  8. from app.core.logger import log
  9. from app.core.router_class import OperationLogRoute
  10. from .schema import DeptCreateSchema, DeptOutSchema, DeptQueryParam, DeptUpdateSchema
  11. from .service import DeptService
  12. DeptRouter = APIRouter(route_class=OperationLogRoute, prefix="/dept", tags=["部门管理"])
  13. @DeptRouter.get(
  14. "/tree",
  15. summary="查询部门树",
  16. description="查询部门树",
  17. response_model=ResponseSchema[list[DeptOutSchema]],
  18. )
  19. async def get_dept_tree_controller(
  20. search: Annotated[DeptQueryParam, Depends()],
  21. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:dept:query"]))],
  22. ) -> JSONResponse:
  23. """
  24. 查询部门树
  25. 参数:
  26. - search (DeptQueryParam): 查询参数模型
  27. - auth (AuthSchema): 认证信息模型
  28. 返回:
  29. - JSONResponse: 包含部门树的响应模型
  30. 异常:
  31. - CustomException: 查询部门树失败时抛出异常。
  32. """
  33. order_by = [{"order": "asc"}]
  34. result_dict_list = await DeptService.get_dept_tree_service(
  35. search=search, auth=auth, order_by=order_by
  36. )
  37. log.info("查询部门树成功")
  38. return SuccessResponse(data=result_dict_list, msg="查询部门树成功")
  39. @DeptRouter.get(
  40. "/detail/{id}",
  41. summary="查询部门详情",
  42. description="查询部门详情",
  43. response_model=ResponseSchema[DeptOutSchema],
  44. )
  45. async def get_obj_detail_controller(
  46. id: Annotated[int, Path(description="部门ID")],
  47. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:dept:detail"]))],
  48. ) -> JSONResponse:
  49. """
  50. 查询部门详情
  51. 参数:
  52. - id (int): 部门ID
  53. - auth (AuthSchema): 认证信息模型
  54. 返回:
  55. - JSONResponse: 包含部门详情的响应模型
  56. 异常:
  57. - CustomException: 查询部门详情失败时抛出异常。
  58. """
  59. result_dict = await DeptService.get_dept_detail_service(id=id, auth=auth)
  60. log.info(f"查询部门详情成功 {id}")
  61. return SuccessResponse(data=result_dict, msg="查询部门详情成功")
  62. @DeptRouter.post(
  63. "/create",
  64. summary="创建部门",
  65. description="创建部门",
  66. response_model=ResponseSchema[DeptOutSchema],
  67. )
  68. async def create_obj_controller(
  69. data: DeptCreateSchema,
  70. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:dept:create"]))],
  71. ) -> JSONResponse:
  72. """
  73. 创建部门
  74. 参数:
  75. - data (DeptCreateSchema): 创建部门负载模型
  76. - auth (AuthSchema): 认证信息模型
  77. 返回:
  78. - JSONResponse: 包含创建部门结果的响应模型
  79. 异常:
  80. - CustomException: 创建部门失败时抛出异常。
  81. """
  82. result_dict = await DeptService.create_dept_service(data=data, auth=auth)
  83. log.info(f"创建部门成功: {result_dict}")
  84. return SuccessResponse(data=result_dict, msg="创建部门成功")
  85. @DeptRouter.put(
  86. "/update/{id}",
  87. summary="修改部门",
  88. description="修改部门",
  89. response_model=ResponseSchema[DeptOutSchema],
  90. )
  91. async def update_obj_controller(
  92. data: DeptUpdateSchema,
  93. id: Annotated[int, Path(description="部门ID")],
  94. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:dept:update"]))],
  95. ) -> JSONResponse:
  96. """
  97. 修改部门
  98. 参数:
  99. - data (DeptUpdateSchema): 修改部门负载模型
  100. - id (int): 部门ID
  101. - auth (AuthSchema): 认证信息模型
  102. 返回:
  103. - JSONResponse: 包含修改部门结果的响应模型
  104. 异常:
  105. - CustomException: 修改部门失败时抛出异常。
  106. """
  107. result_dict = await DeptService.update_dept_service(auth=auth, id=id, data=data)
  108. log.info(f"修改部门成功: {result_dict}")
  109. return SuccessResponse(data=result_dict, msg="修改部门成功")
  110. @DeptRouter.delete(
  111. "/delete",
  112. summary="删除部门",
  113. description="删除部门",
  114. response_model=ResponseSchema[None],
  115. )
  116. async def delete_obj_controller(
  117. ids: Annotated[list[int], Body(description="ID列表")],
  118. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:dept:delete"]))],
  119. ) -> JSONResponse:
  120. """
  121. 删除部门
  122. 参数:
  123. - ids (list[int]): 部门ID列表
  124. - auth (AuthSchema): 认证信息模型
  125. 返回:
  126. - JSONResponse: 包含删除部门结果的响应模型
  127. 异常:
  128. - CustomException: 删除部门失败时抛出异常。
  129. """
  130. await DeptService.delete_dept_service(ids=ids, auth=auth)
  131. log.info(f"删除部门成功: {ids}")
  132. return SuccessResponse(msg="删除部门成功")
  133. @DeptRouter.patch(
  134. "/available/setting",
  135. summary="批量修改部门状态",
  136. description="批量修改部门状态",
  137. response_model=ResponseSchema[None],
  138. )
  139. async def batch_set_available_obj_controller(
  140. data: BatchSetAvailable,
  141. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:dept:patch"]))],
  142. ) -> JSONResponse:
  143. """
  144. 批量修改部门状态
  145. 参数:
  146. - data (BatchSetAvailable): 批量修改部门状态负载模型
  147. - auth (AuthSchema): 认证信息模型
  148. 返回:
  149. - JSONResponse: 包含批量修改部门状态结果的响应模型
  150. 异常:
  151. - CustomException: 批量修改部门状态失败时抛出异常。
  152. """
  153. await DeptService.batch_set_available_service(data=data, auth=auth)
  154. log.info(f"批量修改部门状态成功: {data.ids}")
  155. return SuccessResponse(msg="批量修改部门状态成功")