| | |
| | | package com.ruoyi.project.tool.gen.service;
|
| | |
|
| | | import java.io.ByteArrayOutputStream;
|
| | | import java.io.File;
|
| | | import java.io.IOException;
|
| | | import java.io.StringWriter;
|
| | | import java.util.LinkedHashMap;
|
| | |
| | | import com.alibaba.fastjson.JSONObject;
|
| | | import com.ruoyi.common.constant.Constants;
|
| | | import com.ruoyi.common.constant.GenConstants;
|
| | | import com.ruoyi.common.core.text.CharsetKit;
|
| | | import com.ruoyi.common.exception.CustomException;
|
| | | import com.ruoyi.common.utils.SecurityUtils;
|
| | | import com.ruoyi.common.utils.StringUtils;
|
| | | import com.ruoyi.common.utils.file.FileUtils;
|
| | | import com.ruoyi.project.tool.gen.domain.GenTable;
|
| | | import com.ruoyi.project.tool.gen.domain.GenTableColumn;
|
| | | import com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper;
|
| | |
| | | }
|
| | |
|
| | | /**
|
| | | * 生成代码
|
| | | * 生成代码(下载方式)
|
| | | *
|
| | | * @param tableName 表名称
|
| | | * @return 数据
|
| | | */
|
| | | @Override
|
| | | public byte[] generatorCode(String tableName)
|
| | | public byte[] downloadCode(String tableName)
|
| | | {
|
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
| | | ZipOutputStream zip = new ZipOutputStream(outputStream);
|
| | |
| | | }
|
| | |
|
| | | /**
|
| | | * 批量生成代码
|
| | | * 生成代码(自定义路径)
|
| | | * |
| | | * @param tableName 表名称
|
| | | * @return 数据
|
| | | */
|
| | | @Override
|
| | | public void generatorCode(String tableName)
|
| | | {
|
| | | // 查询表信息
|
| | | GenTable table = genTableMapper.selectGenTableByName(tableName);
|
| | | // 查询列信息
|
| | | List<GenTableColumn> columns = table.getColumns();
|
| | | setPkColumn(table, columns);
|
| | |
|
| | | VelocityInitializer.initVelocity();
|
| | |
|
| | | VelocityContext context = VelocityUtils.prepareContext(table);
|
| | |
|
| | | // 获取模板列表
|
| | | List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
|
| | | for (String template : templates)
|
| | | {
|
| | | if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
|
| | | {
|
| | | // 渲染模板
|
| | | StringWriter sw = new StringWriter();
|
| | | Template tpl = Velocity.getTemplate(template, Constants.UTF8);
|
| | | tpl.merge(context, sw);
|
| | | try
|
| | | {
|
| | | String path = getGenPath(table, template);
|
| | | FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
|
| | | }
|
| | | catch (IOException e)
|
| | | {
|
| | | throw new CustomException("渲染模板失败,表名:" + table.getTableName());
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * 批量生成代码(下载方式)
|
| | | *
|
| | | * @param tableNames 表数组
|
| | | * @return 数据
|
| | | */
|
| | | @Override
|
| | | public byte[] generatorCode(String[] tableNames)
|
| | | public byte[] downloadCode(String[] tableNames)
|
| | | {
|
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
| | | ZipOutputStream zip = new ZipOutputStream(outputStream);
|
| | |
| | | genTable.setParentMenuName(parentMenuName);
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取代码生成地址
|
| | | * |
| | | * @param table 业务表信息
|
| | | * @param template 模板文件路径
|
| | | * @return 生成地址
|
| | | */
|
| | | public static String getGenPath(GenTable table, String template)
|
| | | {
|
| | | String genPath = table.getGenPath();
|
| | | if (StringUtils.equals(genPath, "/"))
|
| | | {
|
| | | return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
|
| | | }
|
| | | return genPath + File.separator + VelocityUtils.getFileName(template, table);
|
| | | }
|
| | | } |