controller.py 5.2 KB

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