GenController.java 7.2 KB

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