controller.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. from typing import Annotated
  2. from fastapi import APIRouter, Depends, Path, Query
  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.dependencies import AuthPermission
  7. from app.core.logger import log
  8. from app.core.router_class import OperationLogRoute
  9. from .schema import (
  10. DepartmentCreateSchema,
  11. DepartmentMoveSchema,
  12. DepartmentOutSchema,
  13. DepartmentTreeSchema,
  14. DepartmentUpdateSchema,
  15. )
  16. from .service import DepartmentService
  17. DepartmentRouter = APIRouter(
  18. route_class=OperationLogRoute,
  19. prefix="/department",
  20. tags=["部门管理"],
  21. )
  22. @DepartmentRouter.post(
  23. "",
  24. summary="创建部门",
  25. description="创建一个新的部门",
  26. response_model=ResponseSchema[DepartmentOutSchema],
  27. )
  28. async def create_department_controller(
  29. data: DepartmentCreateSchema,
  30. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:create"]))],
  31. ) -> JSONResponse:
  32. """创建部门"""
  33. result = await DepartmentService.create_service(auth=auth, data=data)
  34. log.info(f"创建部门成功: {data.name}, department_id={result.department_id}")
  35. return SuccessResponse(data=result, msg="创建部门成功")
  36. @DepartmentRouter.get(
  37. "",
  38. summary="查询部门列表",
  39. description="分页查询部门列表(平铺)",
  40. response_model=ResponseSchema[DepartmentOutSchema],
  41. )
  42. async def list_department_controller(
  43. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))],
  44. page_no: Annotated[int, Query(description="页码")] = 1,
  45. page_size: Annotated[int, Query(description="每页数量")] = 100,
  46. name: Annotated[str | None, Query(description="部门名称")] = None,
  47. code: Annotated[str | None, Query(description="部门编码")] = None,
  48. status: Annotated[str | None, Query(description="状态")] = None,
  49. ) -> JSONResponse:
  50. """查询部门列表"""
  51. search = {}
  52. if name:
  53. search["name"] = name
  54. if code:
  55. search["code"] = code
  56. if status:
  57. search["status"] = status
  58. result = await DepartmentService.list_service(
  59. auth=auth, page_no=page_no, page_size=page_size, search=search
  60. )
  61. return SuccessResponse(data=result, msg="查询部门列表成功")
  62. @DepartmentRouter.get(
  63. "/tree",
  64. summary="获取部门树",
  65. description="获取部门树形结构",
  66. response_model=ResponseSchema[DepartmentTreeSchema],
  67. )
  68. async def tree_department_controller(
  69. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:tree"]))],
  70. ) -> JSONResponse:
  71. """获取部门树形结构"""
  72. result = await DepartmentService.tree_service(auth=auth)
  73. return SuccessResponse(data=result, msg="获取部门树成功")
  74. @DepartmentRouter.get(
  75. "/{department_id}",
  76. summary="查询部门详情",
  77. description="根据 department_id 查询部门详情",
  78. response_model=ResponseSchema[DepartmentOutSchema],
  79. )
  80. async def get_detail_controller(
  81. department_id: Annotated[str, Path(description="部门ID")],
  82. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:detail"]))],
  83. ) -> JSONResponse:
  84. """查询部门详情"""
  85. result = await DepartmentService.detail_service(auth=auth, department_id=department_id)
  86. log.info(f"查询部门详情成功: {department_id}")
  87. return SuccessResponse(data=result, msg="查询部门详情成功")
  88. @DepartmentRouter.get(
  89. "/{department_id}/children",
  90. summary="获取子部门",
  91. description="获取指定部门的子部门列表",
  92. response_model=ResponseSchema[DepartmentOutSchema],
  93. )
  94. async def get_children_controller(
  95. department_id: Annotated[str, Path(description="部门ID")],
  96. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:children"]))],
  97. ) -> JSONResponse:
  98. """获取子部门列表"""
  99. result = await DepartmentService.children_service(auth=auth, department_id=department_id)
  100. return SuccessResponse(data=result, msg="获取子部门成功")
  101. @DepartmentRouter.get(
  102. "/options",
  103. summary="获取部门选项",
  104. description="获取部门选择选项,用于上级部门选择",
  105. response_model=ResponseSchema[list],
  106. )
  107. async def get_department_options_controller(
  108. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:options"]))],
  109. ) -> JSONResponse:
  110. """获取部门选择选项"""
  111. result = await DepartmentService.options_service(auth=auth)
  112. return SuccessResponse(data=result, msg="获取部门选项成功")
  113. @DepartmentRouter.put(
  114. "/{department_id}",
  115. summary="更新部门",
  116. description="更新部门信息",
  117. response_model=ResponseSchema[DepartmentOutSchema],
  118. )
  119. async def update_department_controller(
  120. department_id: Annotated[str, Path(description="部门ID")],
  121. data: DepartmentUpdateSchema,
  122. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:update"]))],
  123. ) -> JSONResponse:
  124. """更新部门"""
  125. result = await DepartmentService.update_service(
  126. auth=auth, department_id=department_id, data=data
  127. )
  128. log.info(f"更新部门成功: {department_id}")
  129. return SuccessResponse(data=result, msg="更新部门成功")
  130. @DepartmentRouter.put(
  131. "/{department_id}/move",
  132. summary="移动部门",
  133. description="移动部门(变更父级)",
  134. response_model=ResponseSchema[DepartmentOutSchema],
  135. )
  136. async def move_department_controller(
  137. department_id: Annotated[str, Path(description="部门ID")],
  138. data: DepartmentMoveSchema,
  139. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:move"]))],
  140. ) -> JSONResponse:
  141. """移动部门"""
  142. result = await DepartmentService.move_service(
  143. auth=auth, department_id=department_id, parent_id=data.parent_id
  144. )
  145. log.info(f"移动部门成功: {department_id} -> {data.parent_id}")
  146. return SuccessResponse(data=result, msg="移动部门成功")
  147. @DepartmentRouter.delete(
  148. "/{department_id}",
  149. summary="删除部门",
  150. description="删除部门",
  151. response_model=ResponseSchema[None],
  152. )
  153. async def delete_department_controller(
  154. department_id: Annotated[str, Path(description="部门ID")],
  155. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:delete"]))],
  156. ) -> JSONResponse:
  157. """删除部门"""
  158. await DepartmentService.delete_service(auth=auth, department_id=department_id)
  159. log.info(f"删除部门成功: {department_id}")
  160. return SuccessResponse(data=None, msg="删除部门成功")