controller.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from typing import Annotated, Optional
  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. DepartmentUpdateSchema,
  12. DepartmentDetailOutSchema,
  13. DepartmentOperationOutSchema,
  14. )
  15. from .service import DepartmentService
  16. DepartmentRouter = APIRouter(
  17. route_class=OperationLogRoute,
  18. prefix="/department",
  19. tags=["部门管理"],
  20. )
  21. @DepartmentRouter.post(
  22. "",
  23. summary="创建部门",
  24. description="创建部门 (alipay.commerce.ec.department.create)",
  25. response_model=ResponseSchema[DepartmentOperationOutSchema],
  26. )
  27. async def create_department_controller(
  28. data: DepartmentCreateSchema,
  29. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:create"]))],
  30. ) -> JSONResponse:
  31. """创建部门"""
  32. result = await DepartmentService.create_department_service(auth=auth, data=data)
  33. log.info(f"创建部门成功: {data.department_name}, department_id={result.department_id}")
  34. return SuccessResponse(data=result, msg="创建部门成功")
  35. @DepartmentRouter.delete(
  36. "/{department_id}",
  37. summary="删除部门",
  38. description="删除部门 (alipay.commerce.ec.department.delete)",
  39. response_model=ResponseSchema[DepartmentOperationOutSchema],
  40. )
  41. async def delete_department_controller(
  42. department_id: Annotated[str, Path(description="部门ID")],
  43. enterprise_id: Annotated[str, Query(description="企业ID")],
  44. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:delete"]))],
  45. ) -> JSONResponse:
  46. """删除部门"""
  47. result = await DepartmentService.delete_department_service(
  48. auth=auth, department_id=department_id, enterprise_id=enterprise_id
  49. )
  50. log.info(f"删除部门成功: department_id={department_id}")
  51. return SuccessResponse(data=result, msg="删除部门成功")
  52. @DepartmentRouter.put(
  53. "/{department_id}",
  54. summary="更新部门",
  55. description="更新部门 (alipay.commerce.ec.department.info.modify)",
  56. response_model=ResponseSchema[DepartmentOperationOutSchema],
  57. )
  58. async def update_department_controller(
  59. department_id: Annotated[str, Path(description="部门ID")],
  60. enterprise_id: Annotated[str, Query(description="企业ID")],
  61. data: DepartmentUpdateSchema,
  62. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:update"]))],
  63. ) -> JSONResponse:
  64. """更新部门"""
  65. result = await DepartmentService.update_department_service(
  66. auth=auth, department_id=department_id, enterprise_id=enterprise_id, data=data
  67. )
  68. log.info(f"更新部门成功: department_id={department_id}")
  69. return SuccessResponse(data=result, msg="更新部门成功")
  70. @DepartmentRouter.get(
  71. "/{department_id}",
  72. summary="查询部门详情",
  73. description="查询部门详情 (alipay.commerce.ec.department.info.query)",
  74. response_model=ResponseSchema[DepartmentDetailOutSchema],
  75. )
  76. async def get_department_controller(
  77. department_id: Annotated[str, Path(description="部门ID")],
  78. enterprise_id: Annotated[str, Query(description="企业ID")],
  79. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:detail"]))],
  80. ) -> JSONResponse:
  81. """查询部门详情"""
  82. result = await DepartmentService.get_department_service(
  83. auth=auth, department_id=department_id, enterprise_id=enterprise_id
  84. )
  85. log.info(f"查询部门详情成功: department_id={department_id}")
  86. return SuccessResponse(data=result, msg="查询部门详情成功")
  87. @DepartmentRouter.get(
  88. "/{department_id}/sub",
  89. summary="查询子部门列表",
  90. description="查询子部门列表 (alipay.commerce.ec.department.sublist.query)",
  91. )
  92. async def get_sub_departments_controller(
  93. department_id: Annotated[str, Path(description="部门ID")],
  94. enterprise_id: Annotated[str, Query(description="企业ID")],
  95. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))],
  96. ) -> JSONResponse:
  97. """查询子部门列表"""
  98. result = await DepartmentService.get_sub_departments_service(
  99. auth=auth, parent_department_id=department_id, enterprise_id=enterprise_id
  100. )
  101. log.info(f"查询子部门列表成功: department_id={department_id}")
  102. return SuccessResponse(data=result, msg="查询子部门列表成功")
  103. @DepartmentRouter.get(
  104. "",
  105. summary="查询部门列表",
  106. description="分页查询部门列表",
  107. )
  108. async def list_department_controller(
  109. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))],
  110. page_no: Annotated[int, Query(description="页码")] = 1,
  111. page_size: Annotated[int, Query(description="每页数量")] = 20,
  112. enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
  113. department_name: Annotated[str | None, Query(description="部门名称")] = None,
  114. department_code: Annotated[str | None, Query(description="部门编码")] = None,
  115. parent_department_id: Annotated[str | None, Query(description="上级部门ID")] = None,
  116. status: Annotated[str | None, Query(description="状态")] = None,
  117. ) -> JSONResponse:
  118. """查询部门列表"""
  119. search: dict = {}
  120. if enterprise_id:
  121. search["enterprise_id"] = enterprise_id
  122. if department_name:
  123. search["department_name"] = department_name
  124. if department_code:
  125. search["department_code"] = department_code
  126. if parent_department_id is not None:
  127. search["parent_department_id"] = parent_department_id
  128. if status:
  129. search["status"] = status
  130. result = await DepartmentService.list_service(
  131. auth=auth, page_no=page_no, page_size=page_size, search=search
  132. )
  133. log.info(f"查询部门列表成功")
  134. return SuccessResponse(data=result, msg="查询部门列表成功")
  135. @DepartmentRouter.get(
  136. "/tree/list",
  137. summary="获取部门树形结构",
  138. description="获取部门树形结构",
  139. )
  140. async def get_department_tree_controller(
  141. enterprise_id: Annotated[str, Query(description="企业ID")],
  142. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))],
  143. ) -> JSONResponse:
  144. """获取部门树形结构"""
  145. result = await DepartmentService.get_department_tree_service(
  146. auth=auth, enterprise_id=enterprise_id
  147. )
  148. log.info(f"获取部门树形结构成功: enterprise_id={enterprise_id}")
  149. return SuccessResponse(data=result, msg="获取部门树形结构成功")