controller.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # -*- coding: utf-8 -*-
  2. from typing import List
  3. from fastapi import APIRouter, Depends, Body, Path
  4. from fastapi.responses import JSONResponse
  5. from app.common.response import SuccessResponse, StreamResponse
  6. from app.core.dependencies import AuthPermission
  7. from app.core.base_params import PaginationQueryParam
  8. from app.common.request import PaginationService
  9. from app.core.router_class import OperationLogRoute
  10. from app.utils.common_util import bytes2file_response
  11. from app.core.logger import log
  12. from app.api.v1.module_system.auth.schema import AuthSchema
  13. from .schema import GenTableSchema, GenTableQueryParam
  14. from .service import GenTableService
  15. GenRouter = APIRouter(route_class=OperationLogRoute, prefix='/gencode', tags=["代码生成模块"])
  16. @GenRouter.get("/list", summary="查询代码生成业务表列表", description="查询代码生成业务表列表")
  17. async def gen_table_list_controller(
  18. page: PaginationQueryParam = Depends(),
  19. search: GenTableQueryParam = Depends(),
  20. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:query"]))
  21. ) -> JSONResponse:
  22. """
  23. 查询代码生成业务表列表
  24. 参数:
  25. - page (PaginationQueryParam): 分页查询参数
  26. - search (GenTableQueryParam): 搜索参数
  27. - auth (AuthSchema): 认证信息模型
  28. 返回:
  29. - JSONResponse: 包含查询结果和分页信息的JSON响应
  30. """
  31. result_dict_list = await GenTableService.get_gen_table_list_service(auth=auth, search=search)
  32. result_dict = await PaginationService.paginate(data_list=result_dict_list, page_no=page.page_no, page_size=page.page_size)
  33. log.info('获取代码生成业务表列表成功')
  34. return SuccessResponse(data=result_dict, msg="获取代码生成业务表列表成功")
  35. @GenRouter.get("/db/list", summary="查询数据库表列表", description="查询数据库表列表")
  36. async def get_gen_db_table_list_controller(
  37. page: PaginationQueryParam = Depends(),
  38. search: GenTableQueryParam = Depends(),
  39. auth: AuthSchema = Depends(AuthPermission(["module_generator:dblist:query"]))
  40. ) -> JSONResponse:
  41. """
  42. 查询数据库表列表
  43. 参数:
  44. - page (PaginationQueryParam): 分页查询参数
  45. - search (GenTableQueryParam): 搜索参数
  46. - auth (AuthSchema): 认证信息模型
  47. 返回:
  48. - JSONResponse: 包含查询结果和分页信息的JSON响应
  49. """
  50. result_dict_list = await GenTableService.get_gen_db_table_list_service(auth=auth, search=search)
  51. result_dict = await PaginationService.paginate(data_list=result_dict_list, page_no=page.page_no, page_size=page.page_size)
  52. log.info('获取数据库表列表成功')
  53. return SuccessResponse(data=result_dict, msg="获取数据库表列表成功")
  54. @GenRouter.post("/import", summary="导入表结构", description="导入表结构")
  55. async def import_gen_table_controller(
  56. table_names: List[str] = Body(..., description="表名列表"),
  57. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:import"])),
  58. ) -> JSONResponse:
  59. """
  60. 导入表结构
  61. 参数:
  62. - table_names (List[str]): 表名列表
  63. - auth (AuthSchema): 认证信息模型
  64. 返回:
  65. - JSONResponse: 包含导入结果和导入的表结构列表的JSON响应
  66. """
  67. add_gen_table_list = await GenTableService.get_gen_db_table_list_by_name_service(auth, table_names)
  68. result = await GenTableService.import_gen_table_service(auth, add_gen_table_list)
  69. log.info('导入表结构成功')
  70. return SuccessResponse(msg="导入表结构成功", data=result)
  71. @GenRouter.get("/detail/{table_id}", summary="获取业务表详细信息", description="获取业务表详细信息")
  72. async def gen_table_detail_controller(
  73. table_id: int = Path(..., description="业务表ID"),
  74. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:query"]))
  75. ) -> JSONResponse:
  76. """
  77. 获取业务表详细信息
  78. 参数:
  79. - table_id (int): 业务表ID
  80. - auth (AuthSchema): 认证信息模型
  81. 返回:
  82. - JSONResponse: 包含业务表详细信息的JSON响应
  83. """
  84. gen_table_detail_result = await GenTableService.get_gen_table_detail_service(auth, table_id)
  85. log.info(f'获取table_id为{table_id}的信息成功')
  86. return SuccessResponse(data=gen_table_detail_result, msg="获取业务表详细信息成功")
  87. @GenRouter.post("/create", summary="创建表结构", description="创建表结构")
  88. async def create_table_controller(
  89. sql: str = Body(..., description="SQL语句,用于创建表结构"),
  90. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:create"])),
  91. ) -> JSONResponse:
  92. """
  93. 创建表结构
  94. 参数:
  95. - sql (str): SQL语句,用于创建表结构
  96. - auth (AuthSchema): 认证信息模型
  97. 返回:
  98. - JSONResponse: 包含创建结果的JSON响应
  99. """
  100. result = await GenTableService.create_table_service(auth, sql)
  101. log.info('创建表结构成功')
  102. return SuccessResponse(msg="创建表结构成功", data=result)
  103. @GenRouter.put("/update/{table_id}", summary="编辑业务表信息", description="编辑业务表信息")
  104. async def update_gen_table_controller(
  105. table_id: int = Path(..., description="业务表ID"),
  106. data: GenTableSchema = Body(..., description="业务表信息"),
  107. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:update"])),
  108. ) -> JSONResponse:
  109. """
  110. 编辑业务表信息
  111. 参数:
  112. - table_id (int): 业务表ID
  113. - data (GenTableSchema): 业务表信息模型
  114. - auth (AuthSchema): 认证信息模型
  115. 返回:
  116. - JSONResponse: 包含编辑结果的JSON响应
  117. """
  118. result_dict = await GenTableService.update_gen_table_service(auth, data, table_id)
  119. log.info('编辑业务表信息成功')
  120. return SuccessResponse(data=result_dict, msg="编辑业务表信息成功")
  121. @GenRouter.delete("/delete", summary="删除业务表信息", description="删除业务表信息")
  122. async def delete_gen_table_controller(
  123. ids: List[int] = Body(..., description="业务表ID列表"),
  124. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:delete"]))
  125. ) -> JSONResponse:
  126. """
  127. 删除业务表信息
  128. 参数:
  129. - ids (List[int]): 业务表ID列表
  130. - auth (AuthSchema): 认证信息模型
  131. 返回:
  132. - JSONResponse: 包含删除结果的JSON响应
  133. """
  134. result = await GenTableService.delete_gen_table_service(auth, ids)
  135. log.info('删除业务表信息成功')
  136. return SuccessResponse(msg="删除业务表信息成功", data=result)
  137. @GenRouter.patch("/batch/output", summary="批量生成代码", description="批量生成代码")
  138. async def batch_gen_code_controller(
  139. table_names: List[str] = Body(..., description="表名列表"),
  140. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:patch"]))
  141. ) -> StreamResponse:
  142. """
  143. 批量生成代码
  144. 参数:
  145. - table_names (List[str]): 表名列表
  146. - auth (AuthSchema): 认证信息模型
  147. 返回:
  148. - StreamResponse: 包含批量生成代码的ZIP文件流响应
  149. """
  150. batch_gen_code_result = await GenTableService.batch_gen_code_service(auth, table_names)
  151. log.info(f'批量生成代码成功,表名列表:{table_names}')
  152. return StreamResponse(
  153. data=bytes2file_response(batch_gen_code_result),
  154. media_type='application/zip',
  155. headers={'Content-Disposition': 'attachment; filename=code.zip'}
  156. )
  157. @GenRouter.post("/output/{table_name}", summary="生成代码到指定路径", description="生成代码到指定路径")
  158. async def gen_code_local_controller(
  159. table_name: str = Path(..., description="表名"),
  160. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:code"]))
  161. ) -> JSONResponse:
  162. """
  163. 生成代码到指定路径
  164. 参数:
  165. - table_name (str): 表名
  166. - auth (AuthSchema): 认证信息模型
  167. 返回:
  168. - JSONResponse: 包含生成结果的JSON响应
  169. """
  170. result = await GenTableService.generate_code_service(auth, table_name)
  171. log.info(f'生成代码,表名:{table_name},到指定路径成功')
  172. return SuccessResponse(msg="生成代码到指定路径成功", data=result)
  173. @GenRouter.get("/preview/{table_id}", summary="预览代码", description="预览代码")
  174. async def preview_code_controller(
  175. table_id: int = Path(..., description="业务表ID"),
  176. auth: AuthSchema = Depends(AuthPermission(["module_generator:gencode:query"]))
  177. ) -> JSONResponse:
  178. """
  179. 预览代码
  180. 参数:
  181. - table_id (int): 业务表ID
  182. - auth (AuthSchema): 认证信息模型
  183. 返回:
  184. - JSONResponse: 包含预览代码的JSON响应
  185. """
  186. preview_code_result = await GenTableService.preview_code_service(auth, table_id)
  187. log.info(f'预览代码,表id:{table_id},成功')
  188. return SuccessResponse(data=preview_code_result, msg="预览代码成功")
  189. @GenRouter.post("/sync_db/{table_name}", summary="同步数据库", description="同步数据库")
  190. async def sync_db_controller(
  191. table_name: str = Path(..., description="表名"),
  192. auth: AuthSchema = Depends(AuthPermission(["module_generator:db:sync"]))
  193. ) -> JSONResponse:
  194. """
  195. 同步数据库
  196. 参数:
  197. - table_name (str): 表名
  198. - auth (AuthSchema): 认证信息模型
  199. 返回:
  200. - JSONResponse: 包含同步数据库结果的JSON响应
  201. """
  202. result = await GenTableService.sync_db_service(auth, table_name)
  203. log.info(f'同步数据库,表名:{table_name},成功')
  204. return SuccessResponse(msg="同步数据库成功", data=result)