GenController.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.ruoyi.gen.controller;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.apache.commons.io.IOUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.validation.annotation.Validated;
  10. import org.springframework.web.bind.annotation.DeleteMapping;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.PathVariable;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.PutMapping;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.ruoyi.common.core.text.Convert;
  19. import com.ruoyi.common.core.web.controller.BaseController;
  20. import com.ruoyi.common.core.web.domain.AjaxResult;
  21. import com.ruoyi.common.core.web.page.TableDataInfo;
  22. import com.ruoyi.common.log.annotation.Log;
  23. import com.ruoyi.common.log.enums.BusinessType;
  24. import com.ruoyi.common.security.annotation.PreAuthorize;
  25. import com.ruoyi.gen.domain.GenTable;
  26. import com.ruoyi.gen.domain.GenTableColumn;
  27. import com.ruoyi.gen.service.IGenTableColumnService;
  28. import com.ruoyi.gen.service.IGenTableService;
  29. /**
  30. * 代码生成 操作处理
  31. *
  32. * @author ruoyi
  33. */
  34. @RequestMapping("/gen")
  35. @RestController
  36. public class GenController extends BaseController
  37. {
  38. @Autowired
  39. private IGenTableService genTableService;
  40. @Autowired
  41. private IGenTableColumnService genTableColumnService;
  42. /**
  43. * 查询代码生成列表
  44. */
  45. @PreAuthorize(hasPermi = "tool:gen:list")
  46. @GetMapping("/list")
  47. public TableDataInfo genList(GenTable genTable)
  48. {
  49. startPage();
  50. List<GenTable> list = genTableService.selectGenTableList(genTable);
  51. return getDataTable(list);
  52. }
  53. /**
  54. * 修改代码生成业务
  55. */
  56. @PreAuthorize(hasPermi = "tool:gen:query")
  57. @GetMapping(value = "/{talbleId}")
  58. public AjaxResult getInfo(@PathVariable Long talbleId)
  59. {
  60. GenTable table = genTableService.selectGenTableById(talbleId);
  61. List<GenTable> tables = genTableService.selectGenTableAll();
  62. List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
  63. Map<String, Object> map = new HashMap<String, Object>();
  64. map.put("info", table);
  65. map.put("rows", list);
  66. map.put("tables", tables);
  67. return AjaxResult.success(map);
  68. }
  69. /**
  70. * 查询数据库列表
  71. */
  72. @PreAuthorize(hasPermi = "tool:gen:list")
  73. @GetMapping("/db/list")
  74. public TableDataInfo dataList(GenTable genTable)
  75. {
  76. startPage();
  77. List<GenTable> list = genTableService.selectDbTableList(genTable);
  78. return getDataTable(list);
  79. }
  80. /**
  81. * 查询数据表字段列表
  82. */
  83. @GetMapping(value = "/column/{talbleId}")
  84. public TableDataInfo columnList(Long tableId)
  85. {
  86. TableDataInfo dataInfo = new TableDataInfo();
  87. List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
  88. dataInfo.setRows(list);
  89. dataInfo.setTotal(list.size());
  90. return dataInfo;
  91. }
  92. /**
  93. * 导入表结构(保存)
  94. */
  95. @PreAuthorize(hasPermi = "tool:gen:import")
  96. @Log(title = "代码生成", businessType = BusinessType.IMPORT)
  97. @PostMapping("/importTable")
  98. public AjaxResult importTableSave(String tables)
  99. {
  100. String[] tableNames = Convert.toStrArray(tables);
  101. // 查询表信息
  102. List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
  103. genTableService.importGenTable(tableList);
  104. return AjaxResult.success();
  105. }
  106. /**
  107. * 修改保存代码生成业务
  108. */
  109. @PreAuthorize(hasPermi = "tool:gen:edit")
  110. @Log(title = "代码生成", businessType = BusinessType.UPDATE)
  111. @PutMapping
  112. public AjaxResult editSave(@Validated @RequestBody GenTable genTable)
  113. {
  114. genTableService.validateEdit(genTable);
  115. genTableService.updateGenTable(genTable);
  116. return AjaxResult.success();
  117. }
  118. /**
  119. * 删除代码生成
  120. */
  121. @PreAuthorize(hasPermi = "tool:gen:remove")
  122. @Log(title = "代码生成", businessType = BusinessType.DELETE)
  123. @DeleteMapping("/{tableIds}")
  124. public AjaxResult remove(@PathVariable Long[] tableIds)
  125. {
  126. genTableService.deleteGenTableByIds(tableIds);
  127. return AjaxResult.success();
  128. }
  129. /**
  130. * 预览代码
  131. */
  132. @PreAuthorize(hasPermi = "tool:gen:preview")
  133. @GetMapping("/preview/{tableId}")
  134. public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException
  135. {
  136. Map<String, String> dataMap = genTableService.previewCode(tableId);
  137. return AjaxResult.success(dataMap);
  138. }
  139. /**
  140. * 生成代码(下载方式)
  141. */
  142. @PreAuthorize(hasPermi = "tool:gen:code")
  143. @Log(title = "代码生成", businessType = BusinessType.GENCODE)
  144. @GetMapping("/download/{tableName}")
  145. public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
  146. {
  147. byte[] data = genTableService.downloadCode(tableName);
  148. genCode(response, data);
  149. }
  150. /**
  151. * 生成代码(自定义路径)
  152. */
  153. @PreAuthorize(hasPermi = "tool:gen:code")
  154. @Log(title = "代码生成", businessType = BusinessType.GENCODE)
  155. @GetMapping("/genCode/{tableName}")
  156. public AjaxResult genCode(@PathVariable("tableName") String tableName)
  157. {
  158. genTableService.generatorCode(tableName);
  159. return AjaxResult.success();
  160. }
  161. /**
  162. * 同步数据库
  163. */
  164. @PreAuthorize(hasPermi = "tool:gen:edit")
  165. @Log(title = "代码生成", businessType = BusinessType.UPDATE)
  166. @GetMapping("/synchDb/{tableName}")
  167. public AjaxResult synchDb(@PathVariable("tableName") String tableName)
  168. {
  169. genTableService.synchDb(tableName);
  170. return AjaxResult.success();
  171. }
  172. /**
  173. * 批量生成代码
  174. */
  175. @PreAuthorize(hasPermi = "tool:gen:code")
  176. @Log(title = "代码生成", businessType = BusinessType.GENCODE)
  177. @GetMapping("/batchGenCode")
  178. public void batchGenCode(HttpServletResponse response, String tables) throws IOException
  179. {
  180. String[] tableNames = Convert.toStrArray(tables);
  181. byte[] data = genTableService.downloadCode(tableNames);
  182. genCode(response, data);
  183. }
  184. /**
  185. * 生成zip文件
  186. */
  187. private void genCode(HttpServletResponse response, byte[] data) throws IOException
  188. {
  189. response.reset();
  190. response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
  191. response.addHeader("Content-Length", "" + data.length);
  192. response.setContentType("application/octet-stream; charset=UTF-8");
  193. IOUtils.write(data, response.getOutputStream());
  194. }
  195. }