controller.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Body, Depends, Path, Request, UploadFile
  3. from fastapi.responses import JSONResponse, StreamingResponse
  4. from redis.asyncio.client import Redis
  5. from app.common.request import PaginationService
  6. from app.common.response import StreamResponse, SuccessResponse
  7. from app.core.router_class import OperationLogRoute
  8. from app.utils.common_util import bytes2file_response
  9. from app.core.base_params import PaginationQueryParam
  10. from app.core.dependencies import AuthPermission, redis_getter
  11. from app.core.logger import log
  12. from ..auth.schema import AuthSchema
  13. from .schema import ParamsCreateSchema, ParamsUpdateSchema, ParamsQueryParam
  14. from .service import ParamsService
  15. ParamsRouter = APIRouter(route_class=OperationLogRoute, prefix="/param", tags=["参数管理"])
  16. @ParamsRouter.get("/detail/{id}", summary="获取参数详情", description="获取参数详情")
  17. async def get_type_detail_controller(
  18. id: int = Path(..., description="参数ID"),
  19. auth: AuthSchema = Depends(AuthPermission(["module_system:param:query"]))
  20. ) -> JSONResponse:
  21. """
  22. 获取参数详情
  23. 参数:
  24. - id (int): 参数ID
  25. - auth (AuthSchema): 认证信息模型
  26. 返回:
  27. - JSONResponse: 包含参数详情的 JSON 响应
  28. """
  29. result_dict = await ParamsService.get_obj_detail_service(id=id, auth=auth)
  30. log.info(f"获取参数详情成功 {id}")
  31. return SuccessResponse(data=result_dict, msg="获取参数详情成功")
  32. @ParamsRouter.get("/key/{config_key}", summary="根据配置键获取参数详情", description="根据配置键获取参数详情")
  33. async def get_obj_by_key_controller(
  34. config_key: str = Path(..., description="配置键"),
  35. auth: AuthSchema = Depends(AuthPermission(["module_system:param:query"]))
  36. ) -> JSONResponse:
  37. """
  38. 根据配置键获取参数详情
  39. 参数:
  40. - config_key (str): 配置键
  41. - auth (AuthSchema): 认证信息模型
  42. 返回:
  43. - JSONResponse: 包含参数详情的 JSON 响应
  44. """
  45. result_dict = await ParamsService.get_obj_by_key_service(config_key=config_key, auth=auth)
  46. log.info(f"根据配置键获取参数详情成功 {config_key}")
  47. return SuccessResponse(data=result_dict, msg="根据配置键获取参数详情成功")
  48. @ParamsRouter.get("/value/{config_key}", summary="根据配置键获取参数值", description="根据配置键获取参数值")
  49. async def get_config_value_by_key_controller(
  50. config_key: str = Path(..., description="配置键"),
  51. auth: AuthSchema = Depends(AuthPermission(["module_system:param:query"]))
  52. ) -> JSONResponse:
  53. """
  54. 根据配置键获取参数值
  55. 参数:
  56. - config_key (str): 配置键
  57. - auth (AuthSchema): 认证信息模型
  58. 返回:
  59. - JSONResponse: 包含参数值的 JSON 响应
  60. """
  61. result_value = await ParamsService.get_config_value_by_key_service(config_key=config_key, auth=auth)
  62. log.info(f"根据配置键获取参数值成功 {config_key}")
  63. return SuccessResponse(data=result_value, msg="根据配置键获取参数值成功")
  64. @ParamsRouter.get("/list", summary="获取参数列表", description="获取参数列表")
  65. async def get_obj_list_controller(
  66. auth: AuthSchema = Depends(AuthPermission(["module_system:param:query"])),
  67. page: PaginationQueryParam = Depends(),
  68. search: ParamsQueryParam = Depends(),
  69. ) -> JSONResponse:
  70. """
  71. 获取参数列表
  72. 参数:
  73. - auth (AuthSchema): 认证信息模型
  74. - page (PaginationQueryParam): 分页查询参数模型
  75. - search (ParamsQueryParam): 参数查询参数模型
  76. 返回:
  77. - JSONResponse: 包含参数列表的 JSON 响应
  78. """
  79. result_dict_list = await ParamsService.get_obj_list_service(auth=auth, search=search, order_by=page.order_by)
  80. result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
  81. log.info(f"获取参数列表成功")
  82. return SuccessResponse(data=result_dict, msg="查询参数列表成功")
  83. @ParamsRouter.post("/create", summary="创建参数", description="创建参数")
  84. async def create_obj_controller(
  85. data: ParamsCreateSchema,
  86. redis: Redis = Depends(redis_getter),
  87. auth: AuthSchema = Depends(AuthPermission(["module_system:param:create"]))
  88. ) -> JSONResponse:
  89. """
  90. 创建参数
  91. 参数:
  92. - data (ParamsCreateSchema): 参数创建模型
  93. - redis (Redis): Redis 客户端实例
  94. - auth (AuthSchema): 认证信息模型
  95. 返回:
  96. - JSONResponse: 包含创建参数结果的 JSON 响应
  97. """
  98. result_dict = await ParamsService.create_obj_service(auth=auth, redis=redis, data=data)
  99. log.info(f"创建参数成功: {result_dict}")
  100. return SuccessResponse(data=result_dict, msg="创建参数成功")
  101. @ParamsRouter.put("/update/{id}", summary="修改参数", description="修改参数")
  102. async def update_objs_controller(
  103. data: ParamsUpdateSchema,
  104. id: int = Path(..., description="参数ID"),
  105. redis: Redis = Depends(redis_getter),
  106. auth: AuthSchema = Depends(AuthPermission(["module_system:param:update"]))
  107. ) -> JSONResponse:
  108. """
  109. 修改参数
  110. 参数:
  111. - data (ParamsUpdateSchema): 参数更新模型
  112. - id (int): 参数ID
  113. - redis (Redis): Redis 客户端实例
  114. - auth (AuthSchema): 认证信息模型
  115. 返回:
  116. - JSONResponse: 包含修改参数结果的 JSON 响应
  117. """
  118. result_dict = await ParamsService.update_obj_service(auth=auth, redis=redis, id=id, data=data)
  119. log.info(f"更新参数成功 {result_dict}")
  120. return SuccessResponse(data=result_dict, msg="更新参数成功")
  121. @ParamsRouter.delete("/delete", summary="删除参数", description="删除参数")
  122. async def delete_obj_controller(
  123. redis: Redis = Depends(redis_getter),
  124. ids: list[int] = Body(..., description="ID列表"),
  125. auth: AuthSchema = Depends(AuthPermission(["module_system:param:delete"]))
  126. ) -> JSONResponse:
  127. """
  128. 删除参数
  129. 参数:
  130. - redis (Redis): Redis 客户端实例
  131. - ids (list[int]): 参数ID列表
  132. - auth (AuthSchema): 认证信息模型
  133. 返回:
  134. - JSONResponse: 包含删除参数结果的 JSON 响应
  135. """
  136. await ParamsService.delete_obj_service(auth=auth, redis=redis, ids=ids)
  137. log.info(f"删除参数成功: {ids}")
  138. return SuccessResponse(msg="删除参数成功")
  139. @ParamsRouter.post('/export', summary="导出参数", description="导出参数")
  140. async def export_obj_list_controller(
  141. search: ParamsQueryParam = Depends(),
  142. auth: AuthSchema = Depends(AuthPermission(["module_system:param:export"]))
  143. ) -> StreamingResponse:
  144. """
  145. 导出参数
  146. 参数:
  147. - search (ParamsQueryParam): 参数查询参数模型
  148. - auth (AuthSchema): 认证信息模型
  149. 返回:
  150. - StreamingResponse: 包含导出参数的 Excel 文件流响应
  151. """
  152. result_dict_list = await ParamsService.get_obj_list_service(search=search, auth=auth)
  153. export_result = await ParamsService.export_obj_service(data_list=result_dict_list)
  154. log.info('导出参数成功')
  155. return StreamResponse(
  156. data=bytes2file_response(export_result),
  157. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  158. headers = {
  159. 'Content-Disposition': 'attachment; filename=params.xlsx'
  160. }
  161. )
  162. @ParamsRouter.post("/upload", summary="上传文件", dependencies=[Depends(AuthPermission(["module_system:param:upload"]))])
  163. async def upload_file_controller(
  164. file: UploadFile,
  165. request: Request
  166. ) -> JSONResponse:
  167. """
  168. 上传文件
  169. 参数:
  170. - file (UploadFile): 上传的文件对象
  171. - request (Request): 请求对象
  172. 返回:
  173. - JSONResponse: 包含上传文件结果的 JSON 响应
  174. """
  175. result_str = await ParamsService.upload_service(base_url=str(request.base_url), file=file)
  176. log.info(f"上传文件: {result_str}")
  177. return SuccessResponse(data=result_str, msg='上传文件成功')
  178. @ParamsRouter.get("/info", summary="获取初始化缓存参数", description="获取初始化缓存参数")
  179. async def get_init_obj_controller(
  180. redis: Redis = Depends(redis_getter),
  181. ) -> JSONResponse:
  182. """
  183. 获取初始化缓存参数
  184. 参数:
  185. - redis (Redis): Redis 客户端实例
  186. 返回:
  187. - JSONResponse: 获取初始化缓存参数的 JSON 响应
  188. """
  189. result_dict = await ParamsService.get_init_config_service(redis=redis)
  190. log.info(f"获取初始化缓存参数成功 {result_dict}")
  191. return SuccessResponse(data=result_dict, msg="获取初始化缓存参数成功")