| | |
| | | package com.iplatform.base.controller; |
| | | |
| | | import com.insurance.common.annotation.Log; |
| | | import com.insurance.common.core.domain.AjaxResult; |
| | | import com.insurance.common.core.page.TableDataInfo; |
| | | import com.insurance.common.enums.BusinessType; |
| | | import com.iplatform.base.SystemController; |
| | | import com.iplatform.core.util.CharsetKit; |
| | | import com.iplatform.core.util.Convert; |
| | | import com.iplatform.mybatis.domain.GenTable; |
| | | import com.iplatform.mybatis.domain.GenTableColumn; |
| | | import com.iplatform.mybatis.domain.TableDataInfo; |
| | | import com.iplatform.mybatis.service.IGenTableColumnService; |
| | | import com.iplatform.mybatis.service.IGenTableService; |
| | | import com.iplatform.mybatis.service.MetaDataServiceImpl; |
| | | import com.iplatform.mybatis.util.VelocityInitializer; |
| | | import com.iplatform.mybatis.util.VelocityUtils; |
| | | import com.walker.infrastructure.utils.StringUtils; |
| | | import com.walker.web.ResponseValue; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.velocity.Template; |
| | | import org.apache.velocity.VelocityContext; |
| | | import org.apache.velocity.app.Velocity; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.DeleteMapping; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.StringWriter; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * 代码生成 操作处理。 |
| | |
| | | */ |
| | | //@Api(value = "代码生成管理", tags = {"代码生成管理"}) |
| | | @RestController |
| | | @RequestMapping("/tool/gen") |
| | | @RequestMapping("/tool/generate") |
| | | public class GenController extends SystemController |
| | | { |
| | | @Autowired |
| | |
| | | |
| | | @Autowired |
| | | private IGenTableColumnService genTableColumnService; |
| | | |
| | | @Autowired |
| | | private MetaDataServiceImpl metaDataService; |
| | | |
| | | /** |
| | | * 查询代码生成列表 |
| | |
| | | */ |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:query')") |
| | | @GetMapping(value = "/{tableId}") |
| | | public AjaxResult getInfo(@PathVariable Long tableId) |
| | | public ResponseValue getInfo(@PathVariable Long tableId) |
| | | { |
| | | GenTable table = genTableService.selectGenTableById(tableId); |
| | | List<GenTable> tables = genTableService.selectGenTableAll(); |
| | |
| | | map.put("info", table); |
| | | map.put("rows", list); |
| | | map.put("tables", tables); |
| | | return success(map); |
| | | // return success(map); |
| | | return ResponseValue.success(map); |
| | | } |
| | | |
| | | /** |
| | |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:import')") |
| | | // @Log(title = "代码生成", businessType = BusinessType.IMPORT) |
| | | @PostMapping("/importTable") |
| | | public AjaxResult importTableSave(String tables) |
| | | public ResponseValue importTableSave(String tables) |
| | | { |
| | | String[] tableNames = Convert.toStrArray(tables); |
| | | // 查询表信息 |
| | | List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames); |
| | | genTableService.importGenTable(tableList); |
| | | return success(); |
| | | return ResponseValue.success(); |
| | | } |
| | | |
| | | /** |
| | |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:edit')") |
| | | // @Log(title = "代码生成", businessType = BusinessType.UPDATE) |
| | | @PutMapping |
| | | public AjaxResult editSave(@Validated @RequestBody GenTable genTable) |
| | | public ResponseValue editSave(@Validated @RequestBody GenTable genTable) |
| | | { |
| | | genTableService.validateEdit(genTable); |
| | | genTableService.updateGenTable(genTable); |
| | | return success(); |
| | | return ResponseValue.success(); |
| | | } |
| | | |
| | | /** |
| | |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:remove')") |
| | | // @Log(title = "代码生成", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{tableIds}") |
| | | public AjaxResult remove(@PathVariable Long[] tableIds) |
| | | public ResponseValue remove(@PathVariable Long[] tableIds) |
| | | { |
| | | genTableService.deleteGenTableByIds(tableIds); |
| | | return success(); |
| | | return ResponseValue.success(); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:preview')") |
| | | @GetMapping("/preview/{tableId}") |
| | | public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException |
| | | public ResponseValue preview(@PathVariable("tableId") Long tableId) throws IOException |
| | | { |
| | | Map<String, String> dataMap = genTableService.previewCode(tableId); |
| | | return success(dataMap); |
| | | return ResponseValue.success(dataMap); |
| | | } |
| | | |
| | | /** |
| | | * 生成代码(下载方式) |
| | | */ |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:code')") |
| | | @Log(title = "代码生成", businessType = BusinessType.GENCODE) |
| | | // @Log(title = "代码生成", businessType = BusinessType.GENCODE) |
| | | @GetMapping("/download/{tableName}") |
| | | public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException |
| | | { |
| | |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:code')") |
| | | // @Log(title = "代码生成", businessType = BusinessType.GENCODE) |
| | | @GetMapping("/genCode/{tableName}") |
| | | public AjaxResult genCode(@PathVariable("tableName") String tableName) |
| | | public ResponseValue genCode(@PathVariable("tableName") String tableName) |
| | | { |
| | | genTableService.generatorCode(tableName); |
| | | return success(); |
| | | return ResponseValue.success(); |
| | | } |
| | | |
| | | /** |
| | |
| | | // @PreAuthorize("@ss.hasPermi('tool:gen:edit')") |
| | | // @Log(title = "代码生成", businessType = BusinessType.UPDATE) |
| | | @GetMapping("/synchDb/{tableName}") |
| | | public AjaxResult synchDb(@PathVariable("tableName") String tableName) |
| | | public ResponseValue synchDb(@PathVariable("tableName") String tableName) |
| | | { |
| | | genTableService.synchDb(tableName); |
| | | return success(); |
| | | return ResponseValue.success(); |
| | | } |
| | | |
| | | /** |
| | |
| | | response.setContentType("application/octet-stream; charset=UTF-8"); |
| | | IOUtils.write(data, response.getOutputStream()); |
| | | } |
| | | |
| | | /** |
| | | * 响应请求分页数据 |
| | | */ |
| | | @SuppressWarnings({ "rawtypes"}) |
| | | protected TableDataInfo getDataTable(List<?> list) |
| | | { |
| | | TableDataInfo rspData = new TableDataInfo(); |
| | | rspData.setCode(HttpStatus.OK.value()); |
| | | rspData.setMsg("查询成功"); |
| | | rspData.setRows(list); |
| | | // rspData.setTotal(page.getTotal()); |
| | | rspData.setTotal(0); |
| | | return rspData; |
| | | } |
| | | |
| | | /** |
| | | * 生成指定表结构代码,支持多数据库。 |
| | | * @param response |
| | | * @param tableName 表名,可以是精确的表名,如:s_ment,也可以是前缀,如:s_ |
| | | * @param isPrecision 是否精确查询(单表) |
| | | * @param packageName |
| | | * @throws IOException |
| | | * @date 2024-02-20 |
| | | */ |
| | | @GetMapping("/batchDownloadGenCode") |
| | | public void batchDownloadGenCode(HttpServletResponse response, String tableName, boolean isPrecision, String packageName) throws IOException{ |
| | | List<GenTable> list = this.metaDataService.queryDatabaseTableInfo(tableName, isPrecision, packageName, null, null, null); |
| | | if(StringUtils.isEmptyList(list)){ |
| | | logger.error("未查找到任何表信息,无法生存代码,tableName={}", tableName); |
| | | return; |
| | | } |
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| | | ZipOutputStream zip = new ZipOutputStream(outputStream); |
| | | for(GenTable table : list){ |
| | | VelocityInitializer.initVelocity(); |
| | | VelocityContext context = VelocityUtils.prepareContext(table); |
| | | |
| | | // 获取模板列表 |
| | | List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); |
| | | for (String template : templates) { |
| | | // 渲染模板 |
| | | StringWriter sw = new StringWriter(); |
| | | Template tpl = Velocity.getTemplate(template, CharsetKit.UTF_8); |
| | | tpl.merge(context, sw); |
| | | try { |
| | | // 添加到zip |
| | | zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table))); |
| | | IOUtils.write(sw.toString(), zip, CharsetKit.UTF_8); |
| | | IOUtils.closeQuietly(sw); |
| | | zip.flush(); |
| | | zip.closeEntry(); |
| | | } |
| | | catch (IOException e) { |
| | | logger.error("渲染模板失败,表名:" + table.getTableName(), e); |
| | | } |
| | | } |
| | | } |
| | | IOUtils.closeQuietly(zip); |
| | | byte[] data = outputStream.toByteArray(); |
| | | genCode(response, data); |
| | | } |
| | | } |