controller.py 6.3 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 BizVarDictService
  12. from .schema import BizVarDictCreateSchema, BizVarDictUpdateSchema, BizVarDictQueryParam
  13. BizVarDictRouter = APIRouter(prefix='/vardict', tags=["变量信息模块"])
  14. @BizVarDictRouter.get("/detail/{id}", summary="获取变量信息详情", description="获取变量信息详情")
  15. async def get_vardict_detail_controller(
  16. id: int = Path(..., description="ID"),
  17. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:query"]))
  18. ) -> JSONResponse:
  19. """获取变量信息详情接口"""
  20. result_dict = await BizVarDictService.detail_vardict_service(auth=auth, id=id)
  21. log.info(f"获取变量信息详情成功 {id}")
  22. return SuccessResponse(data=result_dict, msg="获取变量信息详情成功")
  23. @BizVarDictRouter.get("/list", summary="查询变量信息列表", description="查询变量信息列表")
  24. async def get_vardict_list_controller(
  25. page: PaginationQueryParam = Depends(),
  26. search: BizVarDictQueryParam = Depends(),
  27. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:query"]))
  28. ) -> JSONResponse:
  29. """查询变量信息列表接口(数据库分页)"""
  30. result_dict = await BizVarDictService.page_vardict_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. @BizVarDictRouter.post("/create", summary="创建变量信息", description="创建变量信息")
  40. async def create_vardict_controller(
  41. data: BizVarDictCreateSchema,
  42. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:create"]))
  43. ) -> JSONResponse:
  44. """创建变量信息接口"""
  45. result_dict = await BizVarDictService.create_vardict_service(auth=auth, data=data)
  46. log.info("创建变量信息成功")
  47. return SuccessResponse(data=result_dict, msg="创建变量信息成功")
  48. @BizVarDictRouter.put("/update/{id}", summary="修改变量信息", description="修改变量信息")
  49. async def update_vardict_controller(
  50. data: BizVarDictUpdateSchema,
  51. id: int = Path(..., description="ID"),
  52. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:update"]))
  53. ) -> JSONResponse:
  54. """修改变量信息接口"""
  55. result_dict = await BizVarDictService.update_vardict_service(auth=auth, id=id, data=data)
  56. log.info("修改变量信息成功")
  57. return SuccessResponse(data=result_dict, msg="修改变量信息成功")
  58. @BizVarDictRouter.delete("/delete", summary="删除变量信息", description="删除变量信息")
  59. async def delete_vardict_controller(
  60. ids: list[int] = Body(..., description="ID列表"),
  61. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:delete"]))
  62. ) -> JSONResponse:
  63. """删除变量信息接口"""
  64. await BizVarDictService.delete_vardict_service(auth=auth, ids=ids)
  65. log.info(f"删除变量信息成功: {ids}")
  66. return SuccessResponse(msg="删除变量信息成功")
  67. @BizVarDictRouter.patch("/available/setting", summary="批量修改变量信息状态", description="批量修改变量信息状态")
  68. async def batch_set_available_vardict_controller(
  69. data: BatchSetAvailable,
  70. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:patch"]))
  71. ) -> JSONResponse:
  72. """批量修改变量信息状态接口"""
  73. await BizVarDictService.set_available_vardict_service(auth=auth, data=data)
  74. log.info(f"批量修改变量信息状态成功: {data.ids}")
  75. return SuccessResponse(msg="批量修改变量信息状态成功")
  76. @BizVarDictRouter.post('/export', summary="导出变量信息", description="导出变量信息")
  77. async def export_vardict_list_controller(
  78. search: BizVarDictQueryParam = Depends(),
  79. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:export"]))
  80. ) -> StreamingResponse:
  81. """导出变量信息接口"""
  82. result_dict_list = await BizVarDictService.list_vardict_service(search=search, auth=auth)
  83. export_result = await BizVarDictService.batch_export_vardict_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_var_dict.xlsx'
  90. }
  91. )
  92. @BizVarDictRouter.post('/import', summary="导入变量信息", description="导入变量信息")
  93. async def import_vardict_list_controller(
  94. file: UploadFile,
  95. auth: AuthSchema = Depends(AuthPermission(["module_business:vardict:import"]))
  96. ) -> JSONResponse:
  97. """导入变量信息接口"""
  98. batch_import_result = await BizVarDictService.batch_import_vardict_service(file=file, auth=auth, update_support=True)
  99. log.info("导入变量信息成功")
  100. return SuccessResponse(data=batch_import_result, msg="导入变量信息成功")
  101. @BizVarDictRouter.post('/download/template', summary="获取变量信息导入模板", description="获取变量信息导入模板", dependencies=[Depends(AuthPermission(["module_business:vardict:download"]))])
  102. async def export_vardict_template_controller() -> StreamingResponse:
  103. """获取变量信息导入模板接口"""
  104. import_template_result = await BizVarDictService.import_template_download_vardict_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_var_dict_template.xlsx'}
  110. )