houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.controller.admin.codegen;
H 2
3 import cn.hutool.core.io.IoUtil;
4 import cn.hutool.core.util.ZipUtil;
5 import com.iailab.framework.common.pojo.CommonResult;
6 import com.iailab.framework.common.pojo.PageResult;
7 import com.iailab.framework.common.util.object.BeanUtils;
8 import com.iailab.module.infra.controller.admin.codegen.vo.CodegenCreateListReqVO;
9 import com.iailab.module.infra.controller.admin.codegen.vo.CodegenDetailRespVO;
10 import com.iailab.module.infra.controller.admin.codegen.vo.CodegenPreviewRespVO;
11 import com.iailab.module.infra.controller.admin.codegen.vo.CodegenUpdateReqVO;
12 import com.iailab.module.infra.controller.admin.codegen.vo.table.CodegenTablePageReqVO;
13 import com.iailab.module.infra.controller.admin.codegen.vo.table.CodegenTableRespVO;
14 import com.iailab.module.infra.controller.admin.codegen.vo.table.DatabaseTableRespVO;
15 import com.iailab.module.infra.convert.codegen.CodegenConvert;
16 import com.iailab.module.infra.dal.dataobject.codegen.CodegenColumnDO;
17 import com.iailab.module.infra.dal.dataobject.codegen.CodegenTableDO;
18 import com.iailab.module.infra.service.codegen.CodegenService;
19 import io.swagger.v3.oas.annotations.Operation;
20 import io.swagger.v3.oas.annotations.Parameter;
21 import io.swagger.v3.oas.annotations.Parameters;
22 import io.swagger.v3.oas.annotations.tags.Tag;
23 import org.springframework.security.access.prepost.PreAuthorize;
24 import org.springframework.validation.annotation.Validated;
25 import org.springframework.web.bind.annotation.*;
26
27 import javax.annotation.Resource;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.validation.Valid;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Map;
35
36 import static com.iailab.framework.common.pojo.CommonResult.success;
37 import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
38 import static com.iailab.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment;
39
40 @Tag(name = "管理后台 - 代码生成器")
41 @RestController
42 @RequestMapping("/infra/codegen")
43 @Validated
44 public class CodegenController {
45
46     @Resource
47     private CodegenService codegenService;
48
49     @GetMapping("/db/table/list")
50     @Operation(summary = "获得数据库自带的表定义列表", description = "会过滤掉已经导入 Codegen 的表")
51     @Parameters({
52             @Parameter(name = "dataSourceConfigId", description = "数据源配置的编号", required = true, example = "1"),
53             @Parameter(name = "name", description = "表名,模糊匹配", example = "iailab"),
54             @Parameter(name = "comment", description = "描述,模糊匹配", example = "平台")
55     })
56     @PreAuthorize("@ss.hasPermission('infra:codegen:query')")
57     public CommonResult<List<DatabaseTableRespVO>> getDatabaseTableList(
58             @RequestParam(value = "dataSourceConfigId") Long dataSourceConfigId,
59             @RequestParam(value = "name", required = false) String name,
60             @RequestParam(value = "comment", required = false) String comment) {
61         return success(codegenService.getDatabaseTableList(dataSourceConfigId, name, comment));
62     }
63
64     @GetMapping("/table/list")
65     @Operation(summary = "获得表定义列表")
66     @Parameter(name = "dataSourceConfigId", description = "数据源配置的编号", required = true, example = "1")
67     @PreAuthorize("@ss.hasPermission('infra:codegen:query')")
68     public CommonResult<List<CodegenTableRespVO>> getCodegenTableList(@RequestParam(value = "dataSourceConfigId") Long dataSourceConfigId) {
69         List<CodegenTableDO> list = codegenService.getCodegenTableList(dataSourceConfigId);
70         return success(BeanUtils.toBean(list, CodegenTableRespVO.class));
71     }
72
73     @GetMapping("/table/page")
74     @Operation(summary = "获得表定义分页")
75     @PreAuthorize("@ss.hasPermission('infra:codegen:query')")
76     public CommonResult<PageResult<CodegenTableRespVO>> getCodegenTablePage(@Valid CodegenTablePageReqVO pageReqVO) {
77         PageResult<CodegenTableDO> pageResult = codegenService.getCodegenTablePage(pageReqVO);
78         return success(BeanUtils.toBean(pageResult, CodegenTableRespVO.class));
79     }
80
81     @GetMapping("/detail")
82     @Operation(summary = "获得表和字段的明细")
83     @Parameter(name = "tableId", description = "表编号", required = true, example = "1024")
84     @PreAuthorize("@ss.hasPermission('infra:codegen:query')")
85     public CommonResult<CodegenDetailRespVO> getCodegenDetail(@RequestParam("tableId") Long tableId) {
86         CodegenTableDO table = codegenService.getCodegenTable(tableId);
87         List<CodegenColumnDO> columns = codegenService.getCodegenColumnListByTableId(tableId);
88         // 拼装返回
89         return success(CodegenConvert.INSTANCE.convert(table, columns));
90     }
91
92     @Operation(summary = "基于数据库的表结构,创建代码生成器的表和字段定义")
93     @PostMapping("/create-list")
94     @PreAuthorize("@ss.hasPermission('infra:codegen:create')")
95     public CommonResult<List<Long>> createCodegenList(@Valid @RequestBody CodegenCreateListReqVO reqVO) {
96         return success(codegenService.createCodegenList(getLoginUserId(), reqVO));
97     }
98
99     @Operation(summary = "更新数据库的表和字段定义")
100     @PutMapping("/update")
101     @PreAuthorize("@ss.hasPermission('infra:codegen:update')")
102     public CommonResult<Boolean> updateCodegen(@Valid @RequestBody CodegenUpdateReqVO updateReqVO) {
103         codegenService.updateCodegen(updateReqVO);
104         return success(true);
105     }
106
107     @Operation(summary = "基于数据库的表结构,同步数据库的表和字段定义")
108     @PutMapping("/sync-from-db")
109     @Parameter(name = "tableId", description = "表编号", required = true, example = "1024")
110     @PreAuthorize("@ss.hasPermission('infra:codegen:update')")
111     public CommonResult<Boolean> syncCodegenFromDB(@RequestParam("tableId") Long tableId) {
112         codegenService.syncCodegenFromDB(tableId);
113         return success(true);
114     }
115
116     @Operation(summary = "删除数据库的表和字段定义")
117     @DeleteMapping("/delete")
118     @Parameter(name = "tableId", description = "表编号", required = true, example = "1024")
119     @PreAuthorize("@ss.hasPermission('infra:codegen:delete')")
120     public CommonResult<Boolean> deleteCodegen(@RequestParam("tableId") Long tableId) {
121         codegenService.deleteCodegen(tableId);
122         return success(true);
123     }
124
125     @Operation(summary = "预览生成代码")
126     @GetMapping("/preview")
127     @Parameter(name = "tableId", description = "表编号", required = true, example = "1024")
128     @PreAuthorize("@ss.hasPermission('infra:codegen:preview')")
129     public CommonResult<List<CodegenPreviewRespVO>> previewCodegen(@RequestParam("tableId") Long tableId) {
130         Map<String, String> codes = codegenService.generationCodes(tableId);
131         return success(CodegenConvert.INSTANCE.convert(codes));
132     }
133
134     @Operation(summary = "下载生成代码")
135     @GetMapping("/download")
136     @Parameter(name = "tableId", description = "表编号", required = true, example = "1024")
137     @PreAuthorize("@ss.hasPermission('infra:codegen:download')")
138     public void downloadCodegen(@RequestParam("tableId") Long tableId,
139                                 HttpServletResponse response) throws IOException {
140         // 生成代码
141         Map<String, String> codes = codegenService.generationCodes(tableId);
142         // 构建 zip 包
143         String[] paths = codes.keySet().toArray(new String[0]);
144         ByteArrayInputStream[] ins = codes.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new);
145         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
146         ZipUtil.zip(outputStream, paths, ins);
147         // 输出
148         writeAttachment(response, "codegen.zip", outputStream.toByteArray());
149     }
150
151 }