controller.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. from typing import Annotated
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
  6. from app.core.base_params import PaginationQueryParam
  7. from app.core.dependencies import AuthPermission
  8. from app.core.logger import log
  9. from app.core.router_class import OperationLogRoute
  10. from app.utils.common_util import bytes2file_response
  11. from .schema import (
  12. GenCreateTableSqlBody,
  13. GenDBTableSchema,
  14. GenSyncPreviewSchema,
  15. GenTableOutSchema,
  16. GenTableQueryParam,
  17. GenTableSchema,
  18. )
  19. from .service import GenTableService
  20. GenRouter = APIRouter(route_class=OperationLogRoute, prefix="/gencode", tags=["代码生成模块"])
  21. @GenRouter.get(
  22. "/list",
  23. summary="查询代码生成业务表列表",
  24. description="查询代码生成业务表列表",
  25. response_model=ResponseSchema[list[GenTableOutSchema]],
  26. )
  27. async def gen_table_list_controller(
  28. page: Annotated[PaginationQueryParam, Depends()],
  29. search: Annotated[GenTableQueryParam, Depends()],
  30. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
  31. ) -> JSONResponse:
  32. """
  33. 查询代码生成业务表列表
  34. 参数:
  35. - page (PaginationQueryParam): 分页查询参数
  36. - search (GenTableQueryParam): 搜索参数
  37. - auth (AuthSchema): 认证信息模型
  38. 返回:
  39. - JSONResponse: 包含查询结果和分页信息的JSON响应
  40. """
  41. order_by = [{"created_time": "desc"}]
  42. if page.order_by:
  43. order_by = page.order_by
  44. result_dict = await GenTableService.get_gen_table_page_service(
  45. auth=auth,
  46. page_no=page.page_no,
  47. page_size=page.page_size,
  48. search=search,
  49. order_by=order_by,
  50. )
  51. log.info("获取代码生成业务表列表成功")
  52. return SuccessResponse(data=result_dict, msg="获取代码生成业务表列表成功")
  53. @GenRouter.get(
  54. "/db/list",
  55. summary="查询数据库表列表",
  56. description="查询数据库表列表",
  57. response_model=ResponseSchema[list[GenDBTableSchema]],
  58. )
  59. async def get_gen_db_table_list_controller(
  60. page: Annotated[PaginationQueryParam, Depends()],
  61. search: Annotated[GenTableQueryParam, Depends()],
  62. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:dblist:query"]))],
  63. ) -> JSONResponse:
  64. """
  65. 查询数据库表列表
  66. 参数:
  67. - page (PaginationQueryParam): 分页查询参数
  68. - search (GenTableQueryParam): 搜索参数
  69. - auth (AuthSchema): 认证信息模型
  70. 返回:
  71. - JSONResponse: 包含查询结果和分页信息的JSON响应
  72. """
  73. # 优化:数据库侧分页(MySQL information_schema / Postgres pg_catalog),避免全量反射导致卡顿
  74. result_dict = await GenTableService.get_gen_db_table_page_service(
  75. auth=auth,
  76. page_no=page.page_no,
  77. page_size=page.page_size,
  78. search=search,
  79. )
  80. log.info("获取数据库表列表成功")
  81. return SuccessResponse(data=result_dict, msg="获取数据库表列表成功")
  82. @GenRouter.post(
  83. "/import",
  84. summary="导入表结构",
  85. description="导入表结构",
  86. response_model=ResponseSchema[bool],
  87. )
  88. async def import_gen_table_controller(
  89. table_names: Annotated[list[str], Body(description="表名列表")],
  90. auth: Annotated[
  91. AuthSchema,
  92. Depends(AuthPermission(["module_generator:gencode:import"])),
  93. ],
  94. ) -> JSONResponse:
  95. """
  96. 导入表结构
  97. 参数:
  98. - table_names (List[str]): 表名列表
  99. - auth (AuthSchema): 认证信息模型
  100. 返回:
  101. - JSONResponse: 包含导入结果和导入的表结构列表的JSON响应
  102. """
  103. add_gen_table_list = await GenTableService.get_gen_db_table_list_by_name_service(
  104. auth, table_names
  105. )
  106. result = await GenTableService.import_gen_table_service(auth, add_gen_table_list)
  107. log.info("导入表结构成功")
  108. return SuccessResponse(msg="导入表结构成功", data=result)
  109. @GenRouter.get(
  110. "/detail/{table_id}",
  111. summary="获取业务表详细信息",
  112. description="获取业务表详细信息",
  113. response_model=ResponseSchema[GenTableOutSchema],
  114. )
  115. async def gen_table_detail_controller(
  116. table_id: Annotated[int, Path(description="业务表ID")],
  117. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
  118. ) -> JSONResponse:
  119. """
  120. 获取业务表详细信息
  121. 参数:
  122. - table_id (int): 业务表ID
  123. - auth (AuthSchema): 认证信息模型
  124. 返回:
  125. - JSONResponse: 包含业务表详细信息的JSON响应
  126. """
  127. gen_table_detail_result = await GenTableService.get_gen_table_detail_service(auth, table_id)
  128. log.info(f"获取table_id为{table_id}的信息成功")
  129. return SuccessResponse(data=gen_table_detail_result, msg="获取业务表详细信息成功")
  130. @GenRouter.post(
  131. "/create",
  132. summary="创建表结构",
  133. description="创建表结构",
  134. response_model=ResponseSchema[bool],
  135. )
  136. async def create_table_controller(
  137. body: GenCreateTableSqlBody,
  138. auth: Annotated[
  139. AuthSchema,
  140. Depends(AuthPermission(["module_generator:gencode:create"])),
  141. ],
  142. ) -> JSONResponse:
  143. """
  144. 创建表结构
  145. 参数:
  146. - body (GenCreateTableSqlBody): 含 `sql` 字段的请求体(与前端 `data: { sql }` 一致)
  147. - auth (AuthSchema): 认证信息模型
  148. 返回:
  149. - JSONResponse: 包含创建结果的JSON响应
  150. """
  151. result = await GenTableService.create_table_service(auth, body.sql)
  152. log.info("创建表结构成功")
  153. return SuccessResponse(msg="创建表结构成功", data=result)
  154. @GenRouter.put(
  155. "/update/{table_id}",
  156. summary="编辑业务表信息",
  157. description="编辑业务表信息",
  158. response_model=ResponseSchema[GenTableOutSchema],
  159. )
  160. async def update_gen_table_controller(
  161. table_id: Annotated[int, Path(description="业务表ID")],
  162. data: Annotated[GenTableSchema, Body(description="业务表信息")],
  163. auth: Annotated[
  164. AuthSchema,
  165. Depends(AuthPermission(["module_generator:gencode:update"])),
  166. ],
  167. ) -> JSONResponse:
  168. """
  169. 编辑业务表信息
  170. 参数:
  171. - table_id (int): 业务表ID
  172. - data (GenTableSchema): 业务表信息模型
  173. - auth (AuthSchema): 认证信息模型
  174. 返回:
  175. - JSONResponse: 包含编辑结果的JSON响应
  176. """
  177. result_dict = await GenTableService.update_gen_table_service(auth, data, table_id)
  178. log.info("编辑业务表信息成功")
  179. return SuccessResponse(data=result_dict, msg="编辑业务表信息成功")
  180. @GenRouter.delete(
  181. "/delete",
  182. summary="删除业务表信息",
  183. description="删除业务表信息",
  184. response_model=ResponseSchema[None],
  185. )
  186. async def delete_gen_table_controller(
  187. ids: Annotated[list[int], Body(description="业务表ID列表")],
  188. auth: Annotated[
  189. AuthSchema,
  190. Depends(AuthPermission(["module_generator:gencode:delete"])),
  191. ],
  192. ) -> JSONResponse:
  193. """
  194. 删除业务表信息
  195. 参数:
  196. - ids (List[int]): 业务表ID列表
  197. - auth (AuthSchema): 认证信息模型
  198. 返回:
  199. - JSONResponse: 包含删除结果的JSON响应
  200. """
  201. result = await GenTableService.delete_gen_table_service(auth, ids)
  202. log.info("删除业务表信息成功")
  203. return SuccessResponse(msg="删除业务表信息成功", data=result)
  204. @GenRouter.patch(
  205. "/batch/output",
  206. summary="批量生成代码",
  207. description="批量生成代码",
  208. )
  209. async def batch_gen_code_controller(
  210. table_names: Annotated[list[str], Body(description="表名列表")],
  211. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:operate"]))],
  212. ) -> StreamResponse:
  213. """
  214. 批量生成代码
  215. 参数:
  216. - table_names (List[str]): 表名列表
  217. - auth (AuthSchema): 认证信息模型
  218. 返回:
  219. - StreamResponse: 包含批量生成代码的ZIP文件流响应
  220. """
  221. batch_gen_code_result = await GenTableService.batch_gen_code_service(auth, table_names)
  222. log.info(f"批量生成代码成功,表名列表:{table_names}")
  223. return StreamResponse(
  224. data=bytes2file_response(batch_gen_code_result),
  225. media_type="application/zip",
  226. headers={"Content-Disposition": "attachment; filename=code.zip"},
  227. )
  228. @GenRouter.post(
  229. "/output/{table_name}",
  230. summary="生成代码到指定路径",
  231. description="生成代码到指定路径",
  232. response_model=ResponseSchema[bool],
  233. )
  234. async def gen_code_local_controller(
  235. table_name: Annotated[str, Path(description="表名")],
  236. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:code"]))],
  237. ) -> JSONResponse:
  238. """
  239. 生成代码到指定路径
  240. 参数:
  241. - table_name (str): 表名
  242. - auth (AuthSchema): 认证信息模型
  243. 返回:
  244. - JSONResponse: 包含生成结果的JSON响应
  245. """
  246. result = await GenTableService.generate_code_service(auth, table_name)
  247. log.info(f"生成代码,表名:{table_name},到指定路径成功")
  248. return SuccessResponse(msg="生成代码到指定路径成功", data=result)
  249. @GenRouter.get(
  250. "/preview/{table_id}",
  251. summary="预览代码",
  252. description="预览代码",
  253. response_model=ResponseSchema[dict],
  254. )
  255. async def preview_code_controller(
  256. table_id: Annotated[int, Path(description="业务表ID")],
  257. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
  258. ) -> JSONResponse:
  259. """
  260. 预览代码
  261. 参数:
  262. - table_id (int): 业务表ID
  263. - auth (AuthSchema): 认证信息模型
  264. 返回:
  265. - JSONResponse: 包含预览代码的JSON响应
  266. """
  267. preview_code_result = await GenTableService.preview_code_service(auth, table_id)
  268. log.info(f"预览代码,表id:{table_id},成功")
  269. return SuccessResponse(data=preview_code_result, msg="预览代码成功")
  270. @GenRouter.post(
  271. "/sync_db/{table_name}",
  272. summary="同步数据库",
  273. description="同步数据库",
  274. response_model=ResponseSchema[None],
  275. )
  276. async def sync_db_controller(
  277. table_name: Annotated[str, Path(description="表名")],
  278. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:db:sync"]))],
  279. ) -> JSONResponse:
  280. """
  281. 同步数据库
  282. 参数:
  283. - table_name (str): 表名
  284. - auth (AuthSchema): 认证信息模型
  285. 返回:
  286. - JSONResponse: 包含同步数据库结果的JSON响应
  287. """
  288. result = await GenTableService.sync_db_service(auth, table_name)
  289. log.info(f"同步数据库,表名:{table_name},成功")
  290. return SuccessResponse(msg="同步数据库成功", data=result)
  291. @GenRouter.get(
  292. "/sync_db/preview/{table_name}",
  293. summary="同步数据库差异预览",
  294. description="同步数据库前差异预览(主表 + 可选子表),不落库",
  295. response_model=ResponseSchema[GenSyncPreviewSchema],
  296. )
  297. async def sync_db_preview_controller(
  298. table_name: Annotated[str, Path(description="表名")],
  299. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:db:sync"]))],
  300. ) -> JSONResponse:
  301. """
  302. 同步数据库前差异预览(主表 + 可选子表),不落库。
  303. 参数:
  304. - table_name (str): 物理表名。
  305. - auth (AuthSchema): 认证信息。
  306. 返回:
  307. - JSONResponse: 成功响应,data 为预览结构。
  308. """
  309. result = await GenTableService.sync_db_preview_service(auth, table_name)
  310. return SuccessResponse(msg="获取同步差异预览成功", data=result)