中国算力平台算力登记系统2.0
yanzhaofeige
2024-09-30 3c4fee1db116c11d4f04727cfe076d7c94daeaf2
commit | author | age
43dc29 1 package com.odcc.cpzidc.generator.controller;
Y 2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import javax.servlet.http.HttpServletResponse;
9 import org.apache.commons.io.IOUtils;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.security.access.prepost.PreAuthorize;
12 import org.springframework.validation.annotation.Validated;
13 import org.springframework.web.bind.annotation.DeleteMapping;
14 import org.springframework.web.bind.annotation.GetMapping;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.PostMapping;
17 import org.springframework.web.bind.annotation.PutMapping;
18 import org.springframework.web.bind.annotation.RequestBody;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 import org.springframework.web.bind.annotation.RestController;
21 import com.alibaba.druid.DbType;
22 import com.alibaba.druid.sql.SQLUtils;
23 import com.alibaba.druid.sql.ast.SQLStatement;
24 import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
25 import com.odcc.cpzidc.common.annotation.Log;
26 import com.odcc.cpzidc.common.core.controller.BaseController;
27 import com.odcc.cpzidc.common.core.domain.AjaxResult;
28 import com.odcc.cpzidc.common.core.page.TableDataInfo;
29 import com.odcc.cpzidc.common.core.text.Convert;
30 import com.odcc.cpzidc.common.enums.BusinessType;
31 import com.odcc.cpzidc.common.utils.SecurityUtils;
32 import com.odcc.cpzidc.common.utils.sql.SqlUtil;
33 import com.odcc.cpzidc.generator.domain.GenTable;
34 import com.odcc.cpzidc.generator.domain.GenTableColumn;
35 import com.odcc.cpzidc.generator.service.IGenTableColumnService;
36 import com.odcc.cpzidc.generator.service.IGenTableService;
37
38 /**
39  * 代码生成 操作处理
40  * 
41  * @author ruoyi
42  */
43 @RestController
44 @RequestMapping("/tool/gen")
45 public class GenController extends BaseController
46 {
47     @Autowired
48     private IGenTableService genTableService;
49
50     @Autowired
51     private IGenTableColumnService genTableColumnService;
52
53     /**
54      * 查询代码生成列表
55      */
56     @PreAuthorize("@ss.hasPermi('tool:gen:list')")
57     @GetMapping("/list")
58     public TableDataInfo genList(GenTable genTable)
59     {
60         startPage();
61         List<GenTable> list = genTableService.selectGenTableList(genTable);
62         return getDataTable(list);
63     }
64
65     /**
66      * 修改代码生成业务
67      */
68     @PreAuthorize("@ss.hasPermi('tool:gen:query')")
69     @GetMapping(value = "/{tableId}")
70     public AjaxResult getInfo(@PathVariable Long tableId)
71     {
72         GenTable table = genTableService.selectGenTableById(tableId);
73         List<GenTable> tables = genTableService.selectGenTableAll();
74         List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
75         Map<String, Object> map = new HashMap<String, Object>();
76         map.put("info", table);
77         map.put("rows", list);
78         map.put("tables", tables);
79         return success(map);
80     }
81
82     /**
83      * 查询数据库列表
84      */
85     @PreAuthorize("@ss.hasPermi('tool:gen:list')")
86     @GetMapping("/db/list")
87     public TableDataInfo dataList(GenTable genTable)
88     {
89         startPage();
90         List<GenTable> list = genTableService.selectDbTableList(genTable);
91         return getDataTable(list);
92     }
93
94     /**
95      * 查询数据表字段列表
96      */
97     @PreAuthorize("@ss.hasPermi('tool:gen:list')")
98     @GetMapping(value = "/column/{tableId}")
99     public TableDataInfo columnList(Long tableId)
100     {
101         TableDataInfo dataInfo = new TableDataInfo();
102         List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
103         dataInfo.setRows(list);
104         dataInfo.setTotal(list.size());
105         return dataInfo;
106     }
107
108     /**
109      * 导入表结构(保存)
110      */
111     @PreAuthorize("@ss.hasPermi('tool:gen:import')")
112     @Log(title = "代码生成", businessType = BusinessType.IMPORT)
113     @PostMapping("/importTable")
114     public AjaxResult importTableSave(String tables)
115     {
116         String[] tableNames = Convert.toStrArray(tables);
117         // 查询表信息
118         List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
119         genTableService.importGenTable(tableList, SecurityUtils.getUsername());
120         return success();
121     }
122
123     /**
124      * 创建表结构(保存)
125      */
126     @PreAuthorize("@ss.hasRole('admin')")
127     @Log(title = "创建表", businessType = BusinessType.OTHER)
128     @PostMapping("/createTable")
129     public AjaxResult createTableSave(String sql)
130     {
131         try
132         {
133             SqlUtil.filterKeyword(sql);
134             List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, DbType.mysql);
135             List<String> tableNames = new ArrayList<>();
136             for (SQLStatement sqlStatement : sqlStatements)
137             {
138                 if (sqlStatement instanceof MySqlCreateTableStatement)
139                 {
140                     MySqlCreateTableStatement createTableStatement = (MySqlCreateTableStatement) sqlStatement;
141                     if (genTableService.createTable(createTableStatement.toString()))
142                     {
143                         String tableName = createTableStatement.getTableName().replaceAll("`", "");
144                         tableNames.add(tableName);
145                     }
146                 }
147             }
148             List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames.toArray(new String[tableNames.size()]));
149             String operName = SecurityUtils.getUsername();
150             genTableService.importGenTable(tableList, operName);
151             return AjaxResult.success();
152         }
153         catch (Exception e)
154         {
155             logger.error(e.getMessage(), e);
156             return AjaxResult.error("创建表结构异常");
157         }
158     }
159
160     /**
161      * 修改保存代码生成业务
162      */
163     @PreAuthorize("@ss.hasPermi('tool:gen:edit')")
164     @Log(title = "代码生成", businessType = BusinessType.UPDATE)
165     @PutMapping
166     public AjaxResult editSave(@Validated @RequestBody GenTable genTable)
167     {
168         genTableService.validateEdit(genTable);
169         genTableService.updateGenTable(genTable);
170         return success();
171     }
172
173     /**
174      * 删除代码生成
175      */
176     @PreAuthorize("@ss.hasPermi('tool:gen:remove')")
177     @Log(title = "代码生成", businessType = BusinessType.DELETE)
178     @DeleteMapping("/{tableIds}")
179     public AjaxResult remove(@PathVariable Long[] tableIds)
180     {
181         genTableService.deleteGenTableByIds(tableIds);
182         return success();
183     }
184
185     /**
186      * 预览代码
187      */
188     @PreAuthorize("@ss.hasPermi('tool:gen:preview')")
189     @GetMapping("/preview/{tableId}")
190     public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException
191     {
192         Map<String, String> dataMap = genTableService.previewCode(tableId);
193         return success(dataMap);
194     }
195
196     /**
197      * 生成代码(下载方式)
198      */
199     @PreAuthorize("@ss.hasPermi('tool:gen:code')")
200     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
201     @GetMapping("/download/{tableName}")
202     public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
203     {
204         byte[] data = genTableService.downloadCode(tableName);
205         genCode(response, data);
206     }
207
208     /**
209      * 生成代码(自定义路径)
210      */
211     @PreAuthorize("@ss.hasPermi('tool:gen:code')")
212     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
213     @GetMapping("/genCode/{tableName}")
214     public AjaxResult genCode(@PathVariable("tableName") String tableName)
215     {
216         genTableService.generatorCode(tableName);
217         return success();
218     }
219
220     /**
221      * 同步数据库
222      */
223     @PreAuthorize("@ss.hasPermi('tool:gen:edit')")
224     @Log(title = "代码生成", businessType = BusinessType.UPDATE)
225     @GetMapping("/synchDb/{tableName}")
226     public AjaxResult synchDb(@PathVariable("tableName") String tableName)
227     {
228         genTableService.synchDb(tableName);
229         return success();
230     }
231
232     /**
233      * 批量生成代码
234      */
235     @PreAuthorize("@ss.hasPermi('tool:gen:code')")
236     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
237     @GetMapping("/batchGenCode")
238     public void batchGenCode(HttpServletResponse response, String tables) throws IOException
239     {
240         String[] tableNames = Convert.toStrArray(tables);
241         byte[] data = genTableService.downloadCode(tableNames);
242         genCode(response, data);
243     }
244
245     /**
246      * 生成zip文件
247      */
248     private void genCode(HttpServletResponse response, byte[] data) throws IOException
249     {
250         response.reset();
251         response.addHeader("Access-Control-Allow-Origin", "*");
252         response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
253         response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
254         response.addHeader("Content-Length", "" + data.length);
255         response.setContentType("application/octet-stream; charset=UTF-8");
256         IOUtils.write(data, response.getOutputStream());
257     }
258 }