GenController.java 6.6 KB

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