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