controller.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse, StreamingResponse
  4. from app.common.response import StreamResponse, SuccessResponse
  5. from app.core.base_params import PaginationQueryParam
  6. from app.core.dependencies import AuthPermission, get_current_user
  7. from app.core.base_schema import BatchSetAvailable
  8. from app.core.logger import log
  9. from app.common.request import PaginationService
  10. from app.core.router_class import OperationLogRoute
  11. from app.utils.common_util import bytes2file_response
  12. from ..auth.schema import AuthSchema
  13. from .service import NoticeService
  14. from .schema import (
  15. NoticeCreateSchema,
  16. NoticeUpdateSchema,
  17. NoticeQueryParam
  18. )
  19. NoticeRouter = APIRouter(route_class=OperationLogRoute, prefix="/notice", tags=["公告通知"])
  20. @NoticeRouter.get("/detail/{id}", summary="获取公告详情", description="获取公告详情")
  21. async def get_obj_detail_controller(
  22. id: int = Path(..., description="公告ID"),
  23. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:query"]))
  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("/list", summary="查询公告", description="查询公告")
  37. async def get_obj_list_controller(
  38. page: PaginationQueryParam = Depends(),
  39. search: NoticeQueryParam = Depends(),
  40. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:query"]))
  41. ) -> JSONResponse:
  42. """
  43. 查询公告。
  44. 参数:
  45. - page (PaginationQueryParam): 分页查询参数模型。
  46. - search (NoticeQueryParam): 查询公告参数模型。
  47. - auth (AuthSchema): 认证信息模型。
  48. 返回:
  49. - JSONResponse: 包含分页公告详情的响应模型。
  50. """
  51. result_dict_list = await NoticeService.get_notice_list_service(auth=auth, search=search, order_by=page.order_by)
  52. result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
  53. log.info(f"查询公告列表成功")
  54. return SuccessResponse(data=result_dict, msg="查询公告列表成功")
  55. @NoticeRouter.post("/create", summary="创建公告", description="创建公告")
  56. async def create_obj_controller(
  57. data: NoticeCreateSchema,
  58. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:create"]))
  59. ) -> JSONResponse:
  60. """
  61. 创建公告。
  62. 参数:
  63. - data (NoticeCreateSchema): 创建公告负载模型。
  64. - auth (AuthSchema): 认证信息模型。
  65. 返回:
  66. - JSONResponse: 包含创建公告结果的响应模型。
  67. """
  68. result_dict = await NoticeService.create_notice_service(auth=auth, data=data)
  69. log.info(f"创建公告成功: {result_dict}")
  70. return SuccessResponse(data=result_dict, msg="创建公告成功")
  71. @NoticeRouter.put("/update/{id}", summary="修改公告", description="修改公告")
  72. async def update_obj_controller(
  73. data: NoticeUpdateSchema,
  74. id: int = Path(..., description="公告ID"),
  75. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:update"]))
  76. ) -> JSONResponse:
  77. """
  78. 修改公告。
  79. 参数:
  80. - data (NoticeUpdateSchema): 修改公告负载模型。
  81. - id (int): 公告ID。
  82. - auth (AuthSchema): 认证信息模型。
  83. 返回:
  84. - JSONResponse: 包含修改公告结果的响应模型。
  85. """
  86. result_dict = await NoticeService.update_notice_service(auth=auth, id=id, data=data)
  87. log.info(f"修改公告成功: {result_dict}")
  88. return SuccessResponse(data=result_dict, msg="修改公告成功")
  89. @NoticeRouter.delete("/delete", summary="删除公告", description="删除公告")
  90. async def delete_obj_controller(
  91. ids: list[int] = Body(..., description="ID列表"),
  92. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:delete"]))
  93. ) -> JSONResponse:
  94. """
  95. 删除公告。
  96. 参数:
  97. - ids (list[int]): 公告ID列表。
  98. - auth (AuthSchema): 认证信息模型。
  99. 返回:
  100. - JSONResponse: 包含删除公告结果的响应模型。
  101. """
  102. await NoticeService.delete_notice_service(auth=auth, ids=ids)
  103. log.info(f"删除公告成功: {ids}")
  104. return SuccessResponse(msg="删除公告成功")
  105. @NoticeRouter.patch("/available/setting", summary="批量修改公告状态", description="批量修改公告状态")
  106. async def batch_set_available_obj_controller(
  107. data: BatchSetAvailable,
  108. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:patch"]))
  109. ) -> JSONResponse:
  110. """
  111. 批量修改公告状态。
  112. 参数:
  113. - data (BatchSetAvailable): 批量修改公告状态负载模型。
  114. - auth (AuthSchema): 认证信息模型。
  115. 返回:
  116. - JSONResponse: 包含批量修改公告状态结果的响应模型。
  117. """
  118. await NoticeService.set_notice_available_service(auth=auth, data=data)
  119. log.info(f"批量修改公告状态成功: {data.ids}")
  120. return SuccessResponse(msg="批量修改公告状态成功")
  121. @NoticeRouter.post('/export', summary="导出公告", description="导出公告")
  122. async def export_obj_list_controller(
  123. search: NoticeQueryParam = Depends(),
  124. auth: AuthSchema = Depends(AuthPermission(["module_system:notice:export"]))
  125. ) -> StreamingResponse:
  126. """
  127. 导出公告。
  128. 参数:
  129. - search (NoticeQueryParam): 查询公告参数模型。
  130. - auth (AuthSchema): 认证信息模型。
  131. 返回:
  132. - StreamingResponse: 包含导出公告的流式响应模型。
  133. """
  134. result_dict_list = await NoticeService.get_notice_list_service(search=search, auth=auth)
  135. export_result = await NoticeService.export_notice_service(notice_list=result_dict_list)
  136. log.info('导出公告成功')
  137. return StreamResponse(
  138. data=bytes2file_response(export_result),
  139. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  140. headers = {'Content-Disposition': 'attachment; filename=notice.xlsx'}
  141. )
  142. @NoticeRouter.get("/available", summary="获取全局启用公告", description="获取全局启用公告")
  143. async def get_obj_list_available_controller(
  144. auth: AuthSchema = Depends(get_current_user)
  145. ) -> JSONResponse:
  146. """
  147. 获取全局启用公告。
  148. 参数:
  149. - auth (AuthSchema): 认证信息模型。
  150. 返回:
  151. - JSONResponse: 包含分页已启用公告详情的响应模型。
  152. """
  153. result_dict_list = await NoticeService.get_notice_list_available_service(auth=auth)
  154. result_dict = await PaginationService.paginate(data_list= result_dict_list)
  155. log.info(f"查询已启用公告列表成功")
  156. return SuccessResponse(data=result_dict, msg="查询已启用公告列表成功")