代码下载优化

This commit is contained in:
20932067@zju.edu.cn 2021-02-28 21:20:17 +08:00
parent e26b9feb0a
commit 290036d38c
1 changed files with 99 additions and 50 deletions

View File

@ -1,10 +1,11 @@
package com.hchyun.generator.controller;
import java.io.IOException;
import java.io.Serializable;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.hchyun.generator.aop.Log;
@ -55,9 +56,9 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@GetMapping("/list")
public Serializable genList(GenTable genTable) {
startPage();
List<GenTable> list = genTableService.selectGenTableList(genTable);
return getDataTable(list);
startPage();
List<GenTable> list = genTableService.selectGenTableList(genTable);
return getDataTable(list);
}
/**
@ -66,14 +67,14 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:query')")
@GetMapping(value = "/{talbleId}")
public AjaxResult getInfo(@PathVariable Long talbleId) {
GenTable table = genTableService.selectGenTableById(talbleId);
List<GenTable> tables = genTableService.selectGenTableAll();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
Map<String, Object> map = new HashMap<String, Object>();
map.put("info", table);
map.put("rows", list);
map.put("tables", tables);
return AjaxResult.success(map);
GenTable table = genTableService.selectGenTableById(talbleId);
List<GenTable> tables = genTableService.selectGenTableAll();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
Map<String, Object> map = new HashMap<String, Object>();
map.put("info" , table);
map.put("rows" , list);
map.put("tables" , tables);
return AjaxResult.success(map);
}
/**
@ -82,9 +83,9 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@GetMapping("/db/list")
public Serializable dataList(GenTable genTable) {
startPage();
List<GenTable> list = genTableService.selectDbTableList(genTable);
return getDataTable(list);
startPage();
List<GenTable> list = genTableService.selectDbTableList(genTable);
return getDataTable(list);
}
/**
@ -93,25 +94,25 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@GetMapping(value = "/column/{talbleId}")
public Serializable columnList(Long tableId) {
GenTableDataInfo dataInfo = new GenTableDataInfo();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
dataInfo.setRows(list);
dataInfo.setTotal(list.size());
return dataInfo;
GenTableDataInfo dataInfo = new GenTableDataInfo();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
dataInfo.setRows(list);
dataInfo.setTotal(list.size());
return dataInfo;
}
/**
* 导入表结构保存
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@Log(title = "代码生成", businessType = GenBusinessType.IMPORT)
@Log(title = "代码生成" , businessType = GenBusinessType.IMPORT)
@PostMapping("/importTable")
public AjaxResult importTableSave(String tables) {
String[] tableNames = Convert.toStrArray(tables);
// 查询表信息
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
genTableService.importGenTable(tableList);
return AjaxResult.success();
String[] tableNames = Convert.toStrArray(tables);
// 查询表信息
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
genTableService.importGenTable(tableList);
return AjaxResult.success();
}
/**
@ -119,19 +120,19 @@ public class GenController extends GenBaseController {
* 修改保存代码生成业务
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:edit')")
@Log(title = "代码生成", businessType = GenBusinessType.UPDATE)
@Log(title = "代码生成" , businessType = GenBusinessType.UPDATE)
@PutMapping
public AjaxResult editSave(@Validated @RequestBody GenTable genTable) {
genTableService.validateEdit(genTable);
genTableService.updateGenTable(genTable);
return AjaxResult.success();
genTableService.validateEdit(genTable);
genTableService.updateGenTable(genTable);
return AjaxResult.success();
}
/**
* 删除代码生成
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:remove')")
@Log(title = "代码生成", businessType = GenBusinessType.DELETE)
@Log(title = "代码生成" , businessType = GenBusinessType.DELETE)
@DeleteMapping("/{tableIds}")
public AjaxResult remove(@PathVariable Long[] tableIds) {
try {
@ -153,57 +154,105 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:preview')")
@GetMapping("/preview/{tableId}")
public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException {
Map<String, String> dataMap = genTableService.previewCode(tableId);
return AjaxResult.success(dataMap);
Map<String, String> dataMap = genTableService.previewCode(tableId);
return AjaxResult.success(dataMap);
}
/**
* 生成代码下载方式
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = GenBusinessType.GENCODE)
@Log(title = "代码生成" , businessType = GenBusinessType.GENCODE)
@GetMapping("/download/{tableName}")
public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
byte[] data = genTableService.downloadCode(tableName);
genCode(response, data);
}
/**
* 生成代码自定义路径
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = GenBusinessType.GENCODE)
@Log(title = "代码生成" , businessType = GenBusinessType.GENCODE)
@GetMapping("/genCode/{tableName}")
public AjaxResult genCode(@PathVariable("tableName") String tableName) {
boolean start = genTableService.generatorCode(tableName);
if (start) {
return AjaxResult.success();
} else {
return AjaxResult.error("模板渲染失败!");
}
boolean start = genTableService.generatorCode(tableName);
if (start) {
return AjaxResult.success();
} else {
return AjaxResult.error("模板渲染失败!");
}
}
/**
* 同步数据库
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:edit')")
@Log(title = "代码生成", businessType = GenBusinessType.UPDATE)
@Log(title = "代码生成" , businessType = GenBusinessType.UPDATE)
@GetMapping("/synchDb/{tableName}")
public AjaxResult synchDb(@PathVariable("tableName") String tableName) {
genTableService.synchDb(tableName);
return AjaxResult.success();
genTableService.synchDb(tableName);
return AjaxResult.success();
}
/**
* 批量生成代码
*/
@PreAuthorize("@hchyun.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = GenBusinessType.GENCODE)
@Log(title = "代码生成" , businessType = GenBusinessType.GENCODE)
@GetMapping("/batchGenCode")
public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
String[] tableNames = Convert.toStrArray(tables);
byte[] data = genTableService.downloadCode(tableNames);
genCode(response, data);
compress(data);
}
/**
* 将bytes流写入到本地
* @param bytes
*/
public static void compress(byte[] bytes) {
try {
FileOutputStream fileOutputStream = new FileOutputStream("F:\\Work\\表白\\huhyun.zip");
fileOutputStream.write(bytes);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 压缩到文件
*
* @param sourceFileNames 源文件列表
* @param zipFileName 目标压缩文件
*/
public static void compress(List<String> sourceFileNames, String zipFileName) {
byte[] buf = new byte[1024];
File zipFile = new File(zipFileName);
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (int i = 0; i < sourceFileNames.size(); i++) {
File sourceFile = new File(sourceFileNames.get(i));
FileInputStream fis = new FileInputStream(sourceFile);
zos.putNextEntry(new ZipEntry(sourceFile.getName()));
// logge.info("压缩当前文件:{}",sourceFile.getName());
int len;
while ((len = fis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
fis.close();
}
zos.close();
System.out.println("压缩完成");
} catch (Exception e) {
// log.error("压缩文件异常:",e);
}
}
/**
@ -211,10 +260,10 @@ public class GenController extends GenBaseController {
*/
private void genCode(HttpServletResponse response, byte[] data) throws IOException {
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment; filename=\"hchyun.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.addHeader("Access-Control-Allow-Origin" , "*");
response.addHeader("Access-Control-Expose-Headers" , "Content-Disposition");
response.setHeader("Content-Disposition" , "attachment; filename=\"hchyun.zip\"");
response.addHeader("Content-Length" , "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}