| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- from typing import Annotated
- from fastapi import APIRouter, Body, Depends, Path
- from fastapi.responses import JSONResponse
- from app.api.v1.module_system.auth.schema import AuthSchema
- from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
- from app.core.base_params import PaginationQueryParam
- from app.core.dependencies import AuthPermission
- from app.core.logger import log
- from app.core.router_class import OperationLogRoute
- from app.utils.common_util import bytes2file_response
- from .schema import (
- GenCreateTableSqlBody,
- GenDBTableSchema,
- GenSyncPreviewSchema,
- GenTableOutSchema,
- GenTableQueryParam,
- GenTableSchema,
- )
- from .service import GenTableService
- GenRouter = APIRouter(route_class=OperationLogRoute, prefix="/gencode", tags=["代码生成模块"])
- @GenRouter.get(
- "/list",
- summary="查询代码生成业务表列表",
- description="查询代码生成业务表列表",
- response_model=ResponseSchema[list[GenTableOutSchema]],
- )
- async def gen_table_list_controller(
- page: Annotated[PaginationQueryParam, Depends()],
- search: Annotated[GenTableQueryParam, Depends()],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
- ) -> JSONResponse:
- """
- 查询代码生成业务表列表
- 参数:
- - page (PaginationQueryParam): 分页查询参数
- - search (GenTableQueryParam): 搜索参数
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含查询结果和分页信息的JSON响应
- """
- order_by = [{"created_time": "desc"}]
- if page.order_by:
- order_by = page.order_by
- result_dict = await GenTableService.get_gen_table_page_service(
- auth=auth,
- page_no=page.page_no,
- page_size=page.page_size,
- search=search,
- order_by=order_by,
- )
- log.info("获取代码生成业务表列表成功")
- return SuccessResponse(data=result_dict, msg="获取代码生成业务表列表成功")
- @GenRouter.get(
- "/db/list",
- summary="查询数据库表列表",
- description="查询数据库表列表",
- response_model=ResponseSchema[list[GenDBTableSchema]],
- )
- async def get_gen_db_table_list_controller(
- page: Annotated[PaginationQueryParam, Depends()],
- search: Annotated[GenTableQueryParam, Depends()],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:dblist:query"]))],
- ) -> JSONResponse:
- """
- 查询数据库表列表
- 参数:
- - page (PaginationQueryParam): 分页查询参数
- - search (GenTableQueryParam): 搜索参数
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含查询结果和分页信息的JSON响应
- """
- # 优化:数据库侧分页(MySQL information_schema / Postgres pg_catalog),避免全量反射导致卡顿
- result_dict = await GenTableService.get_gen_db_table_page_service(
- auth=auth,
- page_no=page.page_no,
- page_size=page.page_size,
- search=search,
- )
- log.info("获取数据库表列表成功")
- return SuccessResponse(data=result_dict, msg="获取数据库表列表成功")
- @GenRouter.post(
- "/import",
- summary="导入表结构",
- description="导入表结构",
- response_model=ResponseSchema[bool],
- )
- async def import_gen_table_controller(
- table_names: Annotated[list[str], Body(description="表名列表")],
- auth: Annotated[
- AuthSchema,
- Depends(AuthPermission(["module_generator:gencode:import"])),
- ],
- ) -> JSONResponse:
- """
- 导入表结构
- 参数:
- - table_names (List[str]): 表名列表
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含导入结果和导入的表结构列表的JSON响应
- """
- add_gen_table_list = await GenTableService.get_gen_db_table_list_by_name_service(
- auth, table_names
- )
- result = await GenTableService.import_gen_table_service(auth, add_gen_table_list)
- log.info("导入表结构成功")
- return SuccessResponse(msg="导入表结构成功", data=result)
- @GenRouter.get(
- "/detail/{table_id}",
- summary="获取业务表详细信息",
- description="获取业务表详细信息",
- response_model=ResponseSchema[GenTableOutSchema],
- )
- async def gen_table_detail_controller(
- table_id: Annotated[int, Path(description="业务表ID")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
- ) -> JSONResponse:
- """
- 获取业务表详细信息
- 参数:
- - table_id (int): 业务表ID
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含业务表详细信息的JSON响应
- """
- gen_table_detail_result = await GenTableService.get_gen_table_detail_service(auth, table_id)
- log.info(f"获取table_id为{table_id}的信息成功")
- return SuccessResponse(data=gen_table_detail_result, msg="获取业务表详细信息成功")
- @GenRouter.post(
- "/create",
- summary="创建表结构",
- description="创建表结构",
- response_model=ResponseSchema[bool],
- )
- async def create_table_controller(
- body: GenCreateTableSqlBody,
- auth: Annotated[
- AuthSchema,
- Depends(AuthPermission(["module_generator:gencode:create"])),
- ],
- ) -> JSONResponse:
- """
- 创建表结构
- 参数:
- - body (GenCreateTableSqlBody): 含 `sql` 字段的请求体(与前端 `data: { sql }` 一致)
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含创建结果的JSON响应
- """
- result = await GenTableService.create_table_service(auth, body.sql)
- log.info("创建表结构成功")
- return SuccessResponse(msg="创建表结构成功", data=result)
- @GenRouter.put(
- "/update/{table_id}",
- summary="编辑业务表信息",
- description="编辑业务表信息",
- response_model=ResponseSchema[GenTableOutSchema],
- )
- async def update_gen_table_controller(
- table_id: Annotated[int, Path(description="业务表ID")],
- data: Annotated[GenTableSchema, Body(description="业务表信息")],
- auth: Annotated[
- AuthSchema,
- Depends(AuthPermission(["module_generator:gencode:update"])),
- ],
- ) -> JSONResponse:
- """
- 编辑业务表信息
- 参数:
- - table_id (int): 业务表ID
- - data (GenTableSchema): 业务表信息模型
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含编辑结果的JSON响应
- """
- result_dict = await GenTableService.update_gen_table_service(auth, data, table_id)
- log.info("编辑业务表信息成功")
- return SuccessResponse(data=result_dict, msg="编辑业务表信息成功")
- @GenRouter.delete(
- "/delete",
- summary="删除业务表信息",
- description="删除业务表信息",
- response_model=ResponseSchema[None],
- )
- async def delete_gen_table_controller(
- ids: Annotated[list[int], Body(description="业务表ID列表")],
- auth: Annotated[
- AuthSchema,
- Depends(AuthPermission(["module_generator:gencode:delete"])),
- ],
- ) -> JSONResponse:
- """
- 删除业务表信息
- 参数:
- - ids (List[int]): 业务表ID列表
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含删除结果的JSON响应
- """
- result = await GenTableService.delete_gen_table_service(auth, ids)
- log.info("删除业务表信息成功")
- return SuccessResponse(msg="删除业务表信息成功", data=result)
- @GenRouter.patch(
- "/batch/output",
- summary="批量生成代码",
- description="批量生成代码",
- )
- async def batch_gen_code_controller(
- table_names: Annotated[list[str], Body(description="表名列表")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:operate"]))],
- ) -> StreamResponse:
- """
- 批量生成代码
- 参数:
- - table_names (List[str]): 表名列表
- - auth (AuthSchema): 认证信息模型
- 返回:
- - StreamResponse: 包含批量生成代码的ZIP文件流响应
- """
- batch_gen_code_result = await GenTableService.batch_gen_code_service(auth, table_names)
- log.info(f"批量生成代码成功,表名列表:{table_names}")
- return StreamResponse(
- data=bytes2file_response(batch_gen_code_result),
- media_type="application/zip",
- headers={"Content-Disposition": "attachment; filename=code.zip"},
- )
- @GenRouter.post(
- "/output/{table_name}",
- summary="生成代码到指定路径",
- description="生成代码到指定路径",
- response_model=ResponseSchema[bool],
- )
- async def gen_code_local_controller(
- table_name: Annotated[str, Path(description="表名")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:code"]))],
- ) -> JSONResponse:
- """
- 生成代码到指定路径
- 参数:
- - table_name (str): 表名
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含生成结果的JSON响应
- """
- result = await GenTableService.generate_code_service(auth, table_name)
- log.info(f"生成代码,表名:{table_name},到指定路径成功")
- return SuccessResponse(msg="生成代码到指定路径成功", data=result)
- @GenRouter.get(
- "/preview/{table_id}",
- summary="预览代码",
- description="预览代码",
- response_model=ResponseSchema[dict],
- )
- async def preview_code_controller(
- table_id: Annotated[int, Path(description="业务表ID")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
- ) -> JSONResponse:
- """
- 预览代码
- 参数:
- - table_id (int): 业务表ID
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含预览代码的JSON响应
- """
- preview_code_result = await GenTableService.preview_code_service(auth, table_id)
- log.info(f"预览代码,表id:{table_id},成功")
- return SuccessResponse(data=preview_code_result, msg="预览代码成功")
- @GenRouter.post(
- "/sync_db/{table_name}",
- summary="同步数据库",
- description="同步数据库",
- response_model=ResponseSchema[None],
- )
- async def sync_db_controller(
- table_name: Annotated[str, Path(description="表名")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:db:sync"]))],
- ) -> JSONResponse:
- """
- 同步数据库
- 参数:
- - table_name (str): 表名
- - auth (AuthSchema): 认证信息模型
- 返回:
- - JSONResponse: 包含同步数据库结果的JSON响应
- """
- result = await GenTableService.sync_db_service(auth, table_name)
- log.info(f"同步数据库,表名:{table_name},成功")
- return SuccessResponse(msg="同步数据库成功", data=result)
- @GenRouter.get(
- "/sync_db/preview/{table_name}",
- summary="同步数据库差异预览",
- description="同步数据库前差异预览(主表 + 可选子表),不落库",
- response_model=ResponseSchema[GenSyncPreviewSchema],
- )
- async def sync_db_preview_controller(
- table_name: Annotated[str, Path(description="表名")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:db:sync"]))],
- ) -> JSONResponse:
- """
- 同步数据库前差异预览(主表 + 可选子表),不落库。
- 参数:
- - table_name (str): 物理表名。
- - auth (AuthSchema): 认证信息。
- 返回:
- - JSONResponse: 成功响应,data 为预览结构。
- """
- result = await GenTableService.sync_db_preview_service(auth, table_name)
- return SuccessResponse(msg="获取同步差异预览成功", data=result)
|