GenController.java 8.1 KB

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