controller.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Depends, UploadFile, Body, Path, Query
  3. from fastapi.responses import StreamingResponse, JSONResponse
  4. from redis.asyncio import Redis
  5. from app.common.response import SuccessResponse, StreamResponse
  6. from app.core.dependencies import AuthPermission, redis_getter
  7. from app.api.v1.module_system.auth.schema import AuthSchema
  8. from app.core.base_params import PaginationQueryParam
  9. from app.utils.common_util import bytes2file_response
  10. from app.core.logger import log
  11. from app.core.base_schema import BatchSetAvailable
  12. from .service import BizMecService
  13. from .schema import BizMecCreateSchema, BizMecUpdateSchema, BizMecQueryParam
  14. BizMecRouter = APIRouter(prefix='/mec', tags=["机构信息模块"])
  15. @BizMecRouter.get("/detail/{id}", summary="获取机构信息详情", description="获取机构信息详情")
  16. async def get_mec_detail_controller(
  17. id: int = Path(..., description="ID"),
  18. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:query"]))
  19. ) -> JSONResponse:
  20. """获取机构信息详情接口"""
  21. result_dict = await BizMecService.detail_mec_service(auth=auth, id=id)
  22. log.info(f"获取机构信息详情成功 {id}")
  23. return SuccessResponse(data=result_dict, msg="获取机构信息详情成功")
  24. @BizMecRouter.get("/detailForNo/{mec_no}", summary="获取机构信息详情", description="获取机构信息详情")
  25. async def get_mec_detail_controller(
  26. mec_no: str = Path(..., description="MecNo"),
  27. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:query"]))
  28. ) -> JSONResponse:
  29. """获取机构信息详情接口"""
  30. result_dict = await BizMecService.detail_mec_service_for_no(auth=auth, mec_no=mec_no)
  31. log.info(f"获取机构信息详情成功 {id}")
  32. return SuccessResponse(data=result_dict, msg="获取机构信息详情成功")
  33. @BizMecRouter.get("/list", summary="查询机构信息列表", description="查询机构信息列表")
  34. async def get_mec_list_controller(
  35. page: PaginationQueryParam = Depends(),
  36. search: BizMecQueryParam = Depends(),
  37. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:query"]))
  38. ) -> JSONResponse:
  39. """查询机构信息列表接口(数据库分页)"""
  40. result_dict = await BizMecService.page_mec_service(
  41. auth=auth,
  42. page_no=page.page_no if page.page_no is not None else 1,
  43. page_size=page.page_size if page.page_size is not None else 10,
  44. search=search,
  45. order_by=page.order_by
  46. )
  47. log.info("查询机构信息列表成功")
  48. return SuccessResponse(data=result_dict, msg="查询机构信息列表成功")
  49. @BizMecRouter.post("/create", summary="创建机构信息", description="创建机构信息")
  50. async def create_mec_controller(
  51. data: BizMecCreateSchema,
  52. redis: Redis = Depends(redis_getter),
  53. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:create"]))
  54. ) -> JSONResponse:
  55. """创建机构信息接口"""
  56. result_dict = await BizMecService.create_mec_service(auth=auth, data=data,redis=redis)
  57. log.info("创建机构信息成功")
  58. return SuccessResponse(data=result_dict, msg="创建机构信息成功")
  59. @BizMecRouter.put("/update/{id}", summary="修改机构信息", description="修改机构信息")
  60. async def update_mec_controller(
  61. data: BizMecUpdateSchema,
  62. id: int = Path(..., description="ID"),
  63. redis: Redis = Depends(redis_getter),
  64. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:update"]))
  65. ) -> JSONResponse:
  66. """修改机构信息接口"""
  67. result_dict = await BizMecService.update_mec_service(auth=auth, id=id, data=data,redis=redis)
  68. log.info("修改机构信息成功")
  69. return SuccessResponse(data=result_dict, msg="修改机构信息成功")
  70. @BizMecRouter.delete("/delete", summary="删除机构信息", description="删除机构信息")
  71. async def delete_mec_controller(
  72. ids: list[int] = Body(..., description="ID列表"),
  73. redis: Redis = Depends(redis_getter),
  74. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:delete"]))
  75. ) -> JSONResponse:
  76. """删除机构信息接口"""
  77. await BizMecService.delete_mec_service(auth=auth, ids=ids,redis=redis)
  78. log.info(f"删除机构信息成功: {ids}")
  79. return SuccessResponse(msg="删除机构信息成功")
  80. @BizMecRouter.patch("/available/setting", summary="批量修改机构信息状态", description="批量修改机构信息状态")
  81. async def batch_set_available_mec_controller(
  82. data: BatchSetAvailable,
  83. redis: Redis = Depends(redis_getter),
  84. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:patch"]))
  85. ) -> JSONResponse:
  86. """批量修改机构信息状态接口"""
  87. await BizMecService.set_available_mec_service(auth=auth, data=data,redis=redis)
  88. log.info(f"批量修改机构信息状态成功: {data.ids}")
  89. return SuccessResponse(msg="批量修改机构信息状态成功")
  90. @BizMecRouter.post('/export', summary="导出机构信息", description="导出机构信息")
  91. async def export_mec_list_controller(
  92. search: BizMecQueryParam = Depends(),
  93. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:export"]))
  94. ) -> StreamingResponse:
  95. """导出机构信息接口"""
  96. result_dict_list = await BizMecService.list_mec_service(search=search, auth=auth)
  97. export_result = await BizMecService.batch_export_mec_service(obj_list=result_dict_list)
  98. log.info('导出机构信息成功')
  99. return StreamResponse(
  100. data=bytes2file_response(export_result),
  101. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  102. headers={
  103. 'Content-Disposition': 'attachment; filename=biz_mec.xlsx'
  104. }
  105. )
  106. @BizMecRouter.post('/import', summary="导入机构信息", description="导入机构信息")
  107. async def import_mec_list_controller(
  108. file: UploadFile,
  109. auth: AuthSchema = Depends(AuthPermission(["module_business:mec:import"]))
  110. ) -> JSONResponse:
  111. """导入机构信息接口"""
  112. batch_import_result = await BizMecService.batch_import_mec_service(file=file, auth=auth, update_support=True)
  113. log.info("导入机构信息成功")
  114. return SuccessResponse(data=batch_import_result, msg="导入机构信息成功")
  115. @BizMecRouter.post('/download/template', summary="获取机构信息导入模板", description="获取机构信息导入模板", dependencies=[Depends(AuthPermission(["module_business:mec:download"]))])
  116. async def export_mec_template_controller() -> StreamingResponse:
  117. """获取机构信息导入模板接口"""
  118. import_template_result = await BizMecService.import_template_download_mec_service()
  119. log.info('获取机构信息导入模板成功')
  120. return StreamResponse(
  121. data=bytes2file_response(import_template_result),
  122. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  123. headers={'Content-Disposition': 'attachment; filename=biz_mec_template.xlsx'}
  124. )