package com.iailab.module.infra.controller.admin.codegen; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.ZipUtil; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.module.infra.controller.admin.codegen.vo.CodegenCreateListReqVO; import com.iailab.module.infra.controller.admin.codegen.vo.CodegenDetailRespVO; import com.iailab.module.infra.controller.admin.codegen.vo.CodegenPreviewRespVO; import com.iailab.module.infra.controller.admin.codegen.vo.CodegenUpdateReqVO; import com.iailab.module.infra.controller.admin.codegen.vo.table.CodegenTablePageReqVO; import com.iailab.module.infra.controller.admin.codegen.vo.table.CodegenTableRespVO; import com.iailab.module.infra.controller.admin.codegen.vo.table.DatabaseTableRespVO; import com.iailab.module.infra.convert.codegen.CodegenConvert; import com.iailab.module.infra.dal.dataobject.codegen.CodegenColumnDO; import com.iailab.module.infra.dal.dataobject.codegen.CodegenTableDO; import com.iailab.module.infra.service.codegen.CodegenService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; import static com.iailab.framework.common.pojo.CommonResult.success; import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static com.iailab.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment; @Tag(name = "管理后台 - 代码生成器") @RestController @RequestMapping("/infra/codegen") @Validated public class CodegenController { @Resource private CodegenService codegenService; @GetMapping("/db/table/list") @Operation(summary = "获得数据库自带的表定义列表", description = "会过滤掉已经导入 Codegen 的表") @Parameters({ @Parameter(name = "dataSourceConfigId", description = "数据源配置的编号", required = true, example = "1"), @Parameter(name = "name", description = "表名,模糊匹配", example = "iailab"), @Parameter(name = "comment", description = "描述,模糊匹配", example = "平台") }) @PreAuthorize("@ss.hasPermission('infra:codegen:query')") public CommonResult> getDatabaseTableList( @RequestParam(value = "dataSourceConfigId") Long dataSourceConfigId, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "comment", required = false) String comment) { return success(codegenService.getDatabaseTableList(dataSourceConfigId, name, comment)); } @GetMapping("/table/list") @Operation(summary = "获得表定义列表") @Parameter(name = "dataSourceConfigId", description = "数据源配置的编号", required = true, example = "1") @PreAuthorize("@ss.hasPermission('infra:codegen:query')") public CommonResult> getCodegenTableList(@RequestParam(value = "dataSourceConfigId") Long dataSourceConfigId) { List list = codegenService.getCodegenTableList(dataSourceConfigId); return success(BeanUtils.toBean(list, CodegenTableRespVO.class)); } @GetMapping("/table/page") @Operation(summary = "获得表定义分页") @PreAuthorize("@ss.hasPermission('infra:codegen:query')") public CommonResult> getCodegenTablePage(@Valid CodegenTablePageReqVO pageReqVO) { PageResult pageResult = codegenService.getCodegenTablePage(pageReqVO); return success(BeanUtils.toBean(pageResult, CodegenTableRespVO.class)); } @GetMapping("/detail") @Operation(summary = "获得表和字段的明细") @Parameter(name = "tableId", description = "表编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('infra:codegen:query')") public CommonResult getCodegenDetail(@RequestParam("tableId") Long tableId) { CodegenTableDO table = codegenService.getCodegenTable(tableId); List columns = codegenService.getCodegenColumnListByTableId(tableId); // 拼装返回 return success(CodegenConvert.INSTANCE.convert(table, columns)); } @Operation(summary = "基于数据库的表结构,创建代码生成器的表和字段定义") @PostMapping("/create-list") @PreAuthorize("@ss.hasPermission('infra:codegen:create')") public CommonResult> createCodegenList(@Valid @RequestBody CodegenCreateListReqVO reqVO) { return success(codegenService.createCodegenList(getLoginUserId(), reqVO)); } @Operation(summary = "更新数据库的表和字段定义") @PutMapping("/update") @PreAuthorize("@ss.hasPermission('infra:codegen:update')") public CommonResult updateCodegen(@Valid @RequestBody CodegenUpdateReqVO updateReqVO) { codegenService.updateCodegen(updateReqVO); return success(true); } @Operation(summary = "基于数据库的表结构,同步数据库的表和字段定义") @PutMapping("/sync-from-db") @Parameter(name = "tableId", description = "表编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('infra:codegen:update')") public CommonResult syncCodegenFromDB(@RequestParam("tableId") Long tableId) { codegenService.syncCodegenFromDB(tableId); return success(true); } @Operation(summary = "删除数据库的表和字段定义") @DeleteMapping("/delete") @Parameter(name = "tableId", description = "表编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('infra:codegen:delete')") public CommonResult deleteCodegen(@RequestParam("tableId") Long tableId) { codegenService.deleteCodegen(tableId); return success(true); } @Operation(summary = "预览生成代码") @GetMapping("/preview") @Parameter(name = "tableId", description = "表编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('infra:codegen:preview')") public CommonResult> previewCodegen(@RequestParam("tableId") Long tableId) { Map codes = codegenService.generationCodes(tableId); return success(CodegenConvert.INSTANCE.convert(codes)); } @Operation(summary = "下载生成代码") @GetMapping("/download") @Parameter(name = "tableId", description = "表编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('infra:codegen:download')") public void downloadCodegen(@RequestParam("tableId") Long tableId, HttpServletResponse response) throws IOException { // 生成代码 Map codes = codegenService.generationCodes(tableId); // 构建 zip 包 String[] paths = codes.keySet().toArray(new String[0]); ByteArrayInputStream[] ins = codes.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipUtil.zip(outputStream, paths, ins); // 输出 writeAttachment(response, "codegen.zip", outputStream.toByteArray()); } }