controller.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Depends, UploadFile, Body, Path, Query
  3. from fastapi.responses import StreamingResponse, JSONResponse
  4. from app.common.response import SuccessResponse, StreamResponse
  5. from app.core.dependencies import AuthPermission
  6. from app.api.v1.module_system.auth.schema import AuthSchema
  7. from app.core.base_params import PaginationQueryParam
  8. from app.utils.common_util import bytes2file_response
  9. from app.core.logger import log
  10. from app.core.base_schema import BatchSetAvailable
  11. from .service import GatewayService
  12. from .schema import GatewayCreateSchema, GatewayUpdateSchema, GatewayQueryParam
  13. GatewayRouter = APIRouter(prefix='/gateway', tags=["网关信息模块"])
  14. @GatewayRouter.get("/detail/{id}", summary="获取网关信息详情", description="获取网关信息详情")
  15. async def get_gateway_detail_controller(
  16. id: int = Path(..., description="ID"),
  17. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:query"]))
  18. ) -> JSONResponse:
  19. """获取网关信息详情接口"""
  20. result_dict = await GatewayService.detail_gateway_service(auth=auth, id=id)
  21. log.info(f"获取网关信息详情成功 {id}")
  22. return SuccessResponse(data=result_dict, msg="获取网关信息详情成功")
  23. @GatewayRouter.get("/list", summary="查询网关信息列表", description="查询网关信息列表")
  24. async def get_gateway_list_controller(
  25. page: PaginationQueryParam = Depends(),
  26. search: GatewayQueryParam = Depends(),
  27. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:query"]))
  28. ) -> JSONResponse:
  29. """查询网关信息列表接口(数据库分页)"""
  30. result_dict = await GatewayService.page_gateway_service(
  31. auth=auth,
  32. page_no=page.page_no if page.page_no is not None else 1,
  33. page_size=page.page_size if page.page_size is not None else 10,
  34. search=search,
  35. order_by=page.order_by
  36. )
  37. log.info("查询网关信息列表成功")
  38. return SuccessResponse(data=result_dict, msg="查询网关信息列表成功")
  39. @GatewayRouter.post("/create", summary="创建网关信息", description="创建网关信息")
  40. async def create_gateway_controller(
  41. data: GatewayCreateSchema,
  42. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:create"]))
  43. ) -> JSONResponse:
  44. """创建网关信息接口"""
  45. result_dict = await GatewayService.create_gateway_service(auth=auth, data=data)
  46. log.info("创建网关信息成功")
  47. return SuccessResponse(data=result_dict, msg="创建网关信息成功")
  48. @GatewayRouter.put("/update/{id}", summary="修改网关信息", description="修改网关信息")
  49. async def update_gateway_controller(
  50. data: GatewayUpdateSchema,
  51. id: int = Path(..., description="ID"),
  52. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:update"]))
  53. ) -> JSONResponse:
  54. """修改网关信息接口"""
  55. result_dict = await GatewayService.update_gateway_service(auth=auth, id=id, data=data)
  56. log.info("修改网关信息成功")
  57. return SuccessResponse(data=result_dict, msg="修改网关信息成功")
  58. @GatewayRouter.delete("/delete", summary="删除网关信息", description="删除网关信息")
  59. async def delete_gateway_controller(
  60. ids: list[int] = Body(..., description="ID列表"),
  61. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:delete"]))
  62. ) -> JSONResponse:
  63. """删除网关信息接口"""
  64. await GatewayService.delete_gateway_service(auth=auth, ids=ids)
  65. log.info(f"删除网关信息成功: {ids}")
  66. return SuccessResponse(msg="删除网关信息成功")
  67. @GatewayRouter.patch("/available/setting", summary="批量修改网关信息状态", description="批量修改网关信息状态")
  68. async def batch_set_available_gateway_controller(
  69. data: BatchSetAvailable,
  70. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:patch"]))
  71. ) -> JSONResponse:
  72. """批量修改网关信息状态接口"""
  73. await GatewayService.set_available_gateway_service(auth=auth, data=data)
  74. log.info(f"批量修改网关信息状态成功: {data.ids}")
  75. return SuccessResponse(msg="批量修改网关信息状态成功")
  76. @GatewayRouter.post('/export', summary="导出网关信息", description="导出网关信息")
  77. async def export_gateway_list_controller(
  78. search: GatewayQueryParam = Depends(),
  79. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:export"]))
  80. ) -> StreamingResponse:
  81. """导出网关信息接口"""
  82. result_dict_list = await GatewayService.list_gateway_service(search=search, auth=auth)
  83. export_result = await GatewayService.batch_export_gateway_service(obj_list=result_dict_list)
  84. log.info('导出网关信息成功')
  85. return StreamResponse(
  86. data=bytes2file_response(export_result),
  87. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  88. headers={
  89. 'Content-Disposition': 'attachment; filename=biz_gateway.xlsx'
  90. }
  91. )
  92. @GatewayRouter.post('/import', summary="导入网关信息", description="导入网关信息")
  93. async def import_gateway_list_controller(
  94. file: UploadFile,
  95. auth: AuthSchema = Depends(AuthPermission(["module_business:gateway:import"]))
  96. ) -> JSONResponse:
  97. """导入网关信息接口"""
  98. batch_import_result = await GatewayService.batch_import_gateway_service(file=file, auth=auth, update_support=True)
  99. log.info("导入网关信息成功")
  100. return SuccessResponse(data=batch_import_result, msg="导入网关信息成功")
  101. @GatewayRouter.post('/download/template', summary="获取网关信息导入模板", description="获取网关信息导入模板", dependencies=[Depends(AuthPermission(["module_business:gateway:download"]))])
  102. async def export_gateway_template_controller() -> StreamingResponse:
  103. """获取网关信息导入模板接口"""
  104. import_template_result = await GatewayService.import_template_download_gateway_service()
  105. log.info('获取网关信息导入模板成功')
  106. return StreamResponse(
  107. data=bytes2file_response(import_template_result),
  108. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  109. headers={'Content-Disposition': 'attachment; filename=biz_gateway_template.xlsx'}
  110. )