controller.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, get_current_user
  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 NoticeCreateSchema, NoticeOutSchema, NoticeQueryParam, NoticeUpdateSchema
  13. from .service import NoticeService
  14. NoticeRouter = APIRouter(route_class=OperationLogRoute, prefix="/notice", tags=["公告通知"])
  15. @NoticeRouter.get(
  16. "/detail/{id}",
  17. summary="获取公告详情",
  18. description="获取公告详情",
  19. response_model=ResponseSchema[NoticeOutSchema],
  20. )
  21. async def get_obj_detail_controller(
  22. id: Annotated[int, Path(description="公告ID")],
  23. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:detail"]))],
  24. ) -> JSONResponse:
  25. """
  26. 获取公告详情。
  27. 参数:
  28. - id (int): 公告ID。
  29. - auth (AuthSchema): 认证信息模型。
  30. 返回:
  31. - JSONResponse: 包含公告详情的响应模型。
  32. """
  33. result_dict = await NoticeService.get_notice_detail_service(id=id, auth=auth)
  34. log.info(f"获取公告详情成功 {id}")
  35. return SuccessResponse(data=result_dict, msg="获取公告详情成功")
  36. @NoticeRouter.get(
  37. "/list",
  38. summary="查询公告",
  39. description="查询公告",
  40. response_model=ResponseSchema[list[NoticeOutSchema]],
  41. )
  42. async def get_obj_list_controller(
  43. page: Annotated[PaginationQueryParam, Depends()],
  44. search: Annotated[NoticeQueryParam, Depends()],
  45. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:query"]))],
  46. ) -> JSONResponse:
  47. """
  48. 查询公告。
  49. 参数:
  50. - page (PaginationQueryParam): 分页查询参数模型。
  51. - search (NoticeQueryParam): 查询公告参数模型。
  52. - auth (AuthSchema): 认证信息模型。
  53. 返回:
  54. - JSONResponse: 包含分页公告详情的响应模型。
  55. """
  56. result_dict = await NoticeService.get_notice_page_service(
  57. auth=auth,
  58. page_no=page.page_no,
  59. page_size=page.page_size,
  60. search=search,
  61. order_by=page.order_by,
  62. )
  63. log.info("查询公告列表成功")
  64. return SuccessResponse(data=result_dict, msg="查询公告列表成功")
  65. @NoticeRouter.post(
  66. "/create",
  67. summary="创建公告",
  68. description="创建公告",
  69. response_model=ResponseSchema[NoticeOutSchema],
  70. )
  71. async def create_obj_controller(
  72. data: NoticeCreateSchema,
  73. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:create"]))],
  74. ) -> JSONResponse:
  75. """
  76. 创建公告。
  77. 参数:
  78. - data (NoticeCreateSchema): 创建公告负载模型。
  79. - auth (AuthSchema): 认证信息模型。
  80. 返回:
  81. - JSONResponse: 包含创建公告结果的响应模型。
  82. """
  83. result_dict = await NoticeService.create_notice_service(auth=auth, data=data)
  84. log.info(f"创建公告成功: {result_dict}")
  85. return SuccessResponse(data=result_dict, msg="创建公告成功")
  86. @NoticeRouter.put(
  87. "/update/{id}",
  88. summary="修改公告",
  89. description="修改公告",
  90. response_model=ResponseSchema[NoticeOutSchema],
  91. )
  92. async def update_obj_controller(
  93. data: NoticeUpdateSchema,
  94. id: Annotated[int, Path(description="公告ID")],
  95. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:update"]))],
  96. ) -> JSONResponse:
  97. """
  98. 修改公告。
  99. 参数:
  100. - data (NoticeUpdateSchema): 修改公告负载模型。
  101. - id (int): 公告ID。
  102. - auth (AuthSchema): 认证信息模型。
  103. 返回:
  104. - JSONResponse: 包含修改公告结果的响应模型。
  105. """
  106. result_dict = await NoticeService.update_notice_service(auth=auth, id=id, data=data)
  107. log.info(f"修改公告成功: {result_dict}")
  108. return SuccessResponse(data=result_dict, msg="修改公告成功")
  109. @NoticeRouter.delete(
  110. "/delete",
  111. summary="删除公告",
  112. description="删除公告",
  113. response_model=ResponseSchema[None],
  114. )
  115. async def delete_obj_controller(
  116. ids: Annotated[list[int], Body(description="ID列表")],
  117. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:delete"]))],
  118. ) -> JSONResponse:
  119. """
  120. 删除公告。
  121. 参数:
  122. - ids (list[int]): 公告ID列表。
  123. - auth (AuthSchema): 认证信息模型。
  124. 返回:
  125. - JSONResponse: 包含删除公告结果的响应模型。
  126. """
  127. await NoticeService.delete_notice_service(auth=auth, ids=ids)
  128. log.info(f"删除公告成功: {ids}")
  129. return SuccessResponse(msg="删除公告成功")
  130. @NoticeRouter.patch(
  131. "/available/setting",
  132. summary="批量修改公告状态",
  133. description="批量修改公告状态",
  134. response_model=ResponseSchema[None],
  135. )
  136. async def batch_set_available_obj_controller(
  137. data: BatchSetAvailable,
  138. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:patch"]))],
  139. ) -> JSONResponse:
  140. """
  141. 批量修改公告状态。
  142. 参数:
  143. - data (BatchSetAvailable): 批量修改公告状态负载模型。
  144. - auth (AuthSchema): 认证信息模型。
  145. 返回:
  146. - JSONResponse: 包含批量修改公告状态结果的响应模型。
  147. """
  148. await NoticeService.set_notice_available_service(auth=auth, data=data)
  149. log.info(f"批量修改公告状态成功: {data.ids}")
  150. return SuccessResponse(msg="批量修改公告状态成功")
  151. @NoticeRouter.post(
  152. "/export",
  153. summary="导出公告",
  154. description="导出公告",
  155. response_model=ResponseSchema[None],
  156. )
  157. async def export_obj_list_controller(
  158. search: Annotated[NoticeQueryParam, Depends()],
  159. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:notice:export"]))],
  160. ) -> StreamingResponse:
  161. """
  162. 导出公告。
  163. 参数:
  164. - search (NoticeQueryParam): 查询公告参数模型。
  165. - auth (AuthSchema): 认证信息模型。
  166. 返回:
  167. - StreamingResponse: 包含导出公告的流式响应模型。
  168. """
  169. result_dict_list = await NoticeService.get_notice_list_service(search=search, auth=auth)
  170. export_result = await NoticeService.export_notice_service(notice_list=result_dict_list)
  171. log.info("导出公告成功")
  172. return StreamResponse(
  173. data=bytes2file_response(export_result),
  174. media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  175. headers={"Content-Disposition": "attachment; filename=notice.xlsx"},
  176. )
  177. @NoticeRouter.get(
  178. "/available",
  179. summary="获取全局启用公告",
  180. description="获取全局启用公告",
  181. response_model=ResponseSchema[list[NoticeOutSchema]],
  182. )
  183. async def get_obj_list_available_controller(
  184. auth: Annotated[AuthSchema, Depends(get_current_user)],
  185. ) -> JSONResponse:
  186. """
  187. 获取全局启用公告。
  188. 参数:
  189. - auth (AuthSchema): 认证信息模型。
  190. 返回:
  191. - JSONResponse: 包含分页已启用公告详情的响应模型。
  192. """
  193. result_dict = await NoticeService.get_notice_available_page_service(auth=auth)
  194. log.info("查询已启用公告列表成功")
  195. return SuccessResponse(data=result_dict, msg="查询已启用公告列表成功")