package com.dl.generator.controller; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.hutool.core.convert.Convert; import cn.hutool.core.io.IoUtil; import com.dl.common.annotation.Log; import com.dl.common.core.controller.BaseController; import com.dl.common.core.domain.PageQuery; import com.dl.common.core.domain.R; import com.dl.common.core.page.TableDataInfo; import com.dl.common.enums.BusinessType; import com.dl.generator.domain.GenTable; import com.dl.generator.domain.GenTableColumn; import com.dl.generator.service.IGenTableService; import lombok.RequiredArgsConstructor; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 代码生成 操作处理 * * @author Lion Li */ @Validated @RequiredArgsConstructor @RestController @RequestMapping("/tool/gen") public class GenController extends BaseController { private final IGenTableService genTableService; /** * 查询代码生成列表 */ @SaCheckPermission("tool:gen:list") @GetMapping("/list") public TableDataInfo genList(GenTable genTable, PageQuery pageQuery) { return genTableService.selectPageGenTableList(genTable, pageQuery); } /** * 修改代码生成业务 * * @param tableId 表ID */ @SaCheckPermission("tool:gen:query") @GetMapping(value = "/{tableId}") public R> getInfo(@PathVariable Long tableId) { GenTable table = genTableService.selectGenTableById(tableId); List tables = genTableService.selectGenTableAll(); List list = genTableService.selectGenTableColumnListByTableId(tableId); Map map = new HashMap(); map.put("info", table); map.put("rows", list); map.put("tables", tables); return R.ok(map); } /** * 查询数据库列表 */ @SaCheckPermission("tool:gen:list") @GetMapping("/db/list") public TableDataInfo dataList(GenTable genTable, PageQuery pageQuery) { return genTableService.selectPageDbTableList(genTable, pageQuery); } /** * 查询数据表字段列表 * * @param tableId 表ID */ @SaCheckPermission("tool:gen:list") @GetMapping(value = "/column/{tableId}") public TableDataInfo columnList(Long tableId) { TableDataInfo dataInfo = new TableDataInfo<>(); List list = genTableService.selectGenTableColumnListByTableId(tableId); dataInfo.setRows(list); dataInfo.setTotal(list.size()); return dataInfo; } /** * 导入表结构(保存) * * @param tables 表名串 */ @SaCheckPermission("tool:gen:import") @Log(title = "代码生成", businessType = BusinessType.IMPORT) @PostMapping("/importTable") public R importTableSave(String tables) { String[] tableNames = Convert.toStrArray(tables); // 查询表信息 List tableList = genTableService.selectDbTableListByNames(tableNames); genTableService.importGenTable(tableList); return R.ok(); } /** * 修改保存代码生成业务 */ @SaCheckPermission("tool:gen:edit") @Log(title = "代码生成", businessType = BusinessType.UPDATE) @PutMapping public R editSave(@Validated @RequestBody GenTable genTable) { genTableService.validateEdit(genTable); genTableService.updateGenTable(genTable); return R.ok(); } /** * 删除代码生成 * * @param tableIds 表ID串 */ @SaCheckPermission("tool:gen:remove") @Log(title = "代码生成", businessType = BusinessType.DELETE) @DeleteMapping("/{tableIds}") public R remove(@PathVariable Long[] tableIds) { genTableService.deleteGenTableByIds(tableIds); return R.ok(); } /** * 预览代码 * * @param tableId 表ID */ @SaCheckPermission("tool:gen:preview") @GetMapping("/preview/{tableId}") public R> preview(@PathVariable("tableId") Long tableId) throws IOException { Map dataMap = genTableService.previewCode(tableId); return R.ok(dataMap); } /** * 生成代码(下载方式) * * @param tableName 表名 */ @SaCheckPermission("tool:gen:code") @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/download/{tableName}") public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException { byte[] data = genTableService.downloadCode(tableName); genCode(response, data); } /** * 生成代码(自定义路径) * * @param tableName 表名 */ @SaCheckPermission("tool:gen:code") @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/genCode/{tableName}") public R genCode(@PathVariable("tableName") String tableName) { genTableService.generatorCode(tableName); return R.ok(); } /** * 同步数据库 * * @param tableName 表名 */ @SaCheckPermission("tool:gen:edit") @Log(title = "代码生成", businessType = BusinessType.UPDATE) @GetMapping("/synchDb/{tableName}") public R synchDb(@PathVariable("tableName") String tableName) { genTableService.synchDb(tableName); return R.ok(); } /** * 批量生成代码 * * @param tables 表名串 */ @SaCheckPermission("tool:gen:code") @Log(title = "代码生成", businessType = BusinessType.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); } /** * 生成zip文件 */ 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=\"dl.zip\""); response.addHeader("Content-Length", "" + data.length); response.setContentType("application/octet-stream; charset=UTF-8"); IoUtil.write(response.getOutputStream(), false, data); } }