controller.py 9.2 KB

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