Search in sources :

Example 1 with GenTable

use of com.ruoyi.generator.domain.GenTable in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
public void synchDb(String tableName) {
    GenTable table = baseMapper.selectGenTableByName(tableName);
    List<GenTableColumn> tableColumns = table.getColumns();
    Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
    List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
    if (CollUtil.isEmpty(dbTableColumns)) {
        throw new ServiceException("同步数据失败,原表结构不存在");
    }
    List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    List<GenTableColumn> saveColumns = new ArrayList<>();
    dbTableColumns.forEach(column -> {
        GenUtils.initColumnField(column, table);
        if (tableColumnMap.containsKey(column.getColumnName())) {
            GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName());
            column.setColumnId(prevColumn.getColumnId());
            if (column.isList()) {
                // 如果是列表,继续保留查询方式/字典类型选项
                column.setDictType(prevColumn.getDictType());
                column.setQueryType(prevColumn.getQueryType());
            }
            if (StringUtils.isNotEmpty(prevColumn.getIsRequired()) && !column.isPk() && (column.isInsert() || column.isEdit()) && ((column.isUsableColumn()) || (!column.isSuperColumn()))) {
                // 如果是(新增/修改&非主键/非忽略及父属性),继续保留必填/显示类型选项
                column.setIsRequired(prevColumn.getIsRequired());
                column.setHtmlType(prevColumn.getHtmlType());
            }
            genTableColumnMapper.updateById(column);
        } else {
            genTableColumnMapper.insert(column);
        }
    });
    if (CollUtil.isNotEmpty(saveColumns)) {
        genTableColumnMapper.insertBatch(saveColumns);
    }
    List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
    if (CollUtil.isNotEmpty(delColumns)) {
        List<Long> ids = delColumns.stream().map(GenTableColumn::getColumnId).collect(Collectors.toList());
        genTableColumnMapper.deleteBatchIds(ids);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) JsonUtils(com.ruoyi.common.utils.JsonUtils) java.util(java.util) TableDataInfo(com.ruoyi.common.core.page.TableDataInfo) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectUtil(cn.hutool.core.util.ObjectUtil) VelocityUtils(com.ruoyi.generator.util.VelocityUtils) ServiceException(com.ruoyi.common.exception.ServiceException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) IdUtil(cn.hutool.core.util.IdUtil) DS(com.baomidou.dynamic.datasource.annotation.DS) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Function(java.util.function.Function) PageQuery(com.ruoyi.common.core.domain.PageQuery) Template(org.apache.velocity.Template) StringUtils(com.ruoyi.common.utils.StringUtils) Service(org.springframework.stereotype.Service) ZipEntry(java.util.zip.ZipEntry) FileUtils(com.ruoyi.common.utils.file.FileUtils) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) IoUtil(cn.hutool.core.io.IoUtil) Snowflake(cn.hutool.core.lang.Snowflake) GenConstants(com.ruoyi.common.constant.GenConstants) StringWriter(java.io.StringWriter) Dict(cn.hutool.core.lang.Dict) IOException(java.io.IOException) GenUtils(com.ruoyi.generator.util.GenUtils) LoginHelper(com.ruoyi.common.helper.LoginHelper) GenTableMapper(com.ruoyi.generator.mapper.GenTableMapper) VelocityContext(org.apache.velocity.VelocityContext) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) VelocityInitializer(com.ruoyi.generator.util.VelocityInitializer) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) CollUtil(cn.hutool.core.collection.CollUtil) Slf4j(lombok.extern.slf4j.Slf4j) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumnMapper(com.ruoyi.generator.mapper.GenTableColumnMapper) Velocity(org.apache.velocity.app.Velocity) Constants(com.ruoyi.common.constant.Constants) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn)

Example 2 with GenTable

use of com.ruoyi.generator.domain.GenTable in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenTableServiceImpl method previewCode.

/**
 * 预览代码
 *
 * @param tableId 表编号
 * @return 预览数据列表
 */
@Override
public Map<String, String> previewCode(Long tableId) {
    Map<String, String> dataMap = new LinkedHashMap<>();
    // 查询表信息
    GenTable table = baseMapper.selectGenTableById(tableId);
    Snowflake snowflake = IdUtil.getSnowflake();
    List<Long> menuIds = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
        menuIds.add(snowflake.nextId());
    }
    table.setMenuIds(menuIds);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    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, Constants.UTF8);
        tpl.merge(context, sw);
        dataMap.put(template, sw.toString());
    }
    return dataMap;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) GenTable(com.ruoyi.generator.domain.GenTable) Snowflake(cn.hutool.core.lang.Snowflake)

Example 3 with GenTable

use of com.ruoyi.generator.domain.GenTable in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenTableServiceImpl method generatorCode.

/**
 * 生成代码(自定义路径)
 *
 * @param tableName 表名称
 */
@Override
public void generatorCode(String tableName) {
    // 查询表信息
    GenTable table = baseMapper.selectGenTableByName(tableName);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    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.writeUtf8String(sw.toString(), path);
            } catch (Exception e) {
                throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 4 with GenTable

use of com.ruoyi.generator.domain.GenTable in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenTableServiceImpl method generatorCode.

/**
 * 查询表信息并生成代码
 */
private void generatorCode(String tableName, ZipOutputStream zip) {
    // 查询表信息
    GenTable table = baseMapper.selectGenTableByName(tableName);
    Snowflake snowflake = IdUtil.getSnowflake();
    List<Long> menuIds = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
        menuIds.add(snowflake.nextId());
    }
    table.setMenuIds(menuIds);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    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, Constants.UTF8);
        tpl.merge(context, sw);
        try {
            // 添加到zip
            zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
            IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString());
            IoUtil.close(sw);
            zip.flush();
            zip.closeEntry();
        } catch (IOException e) {
            log.error("渲染模板失败,表名:" + table.getTableName(), e);
        }
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) GenTable(com.ruoyi.generator.domain.GenTable) Snowflake(cn.hutool.core.lang.Snowflake)

Example 5 with GenTable

use of com.ruoyi.generator.domain.GenTable in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenController method getInfo.

/**
 * 修改代码生成业务
 */
@ApiOperation("修改代码生成业务")
@SaCheckPermission("tool:gen:query")
@GetMapping(value = "/{tableId}")
public R<Map<String, Object>> getInfo(@PathVariable Long tableId) {
    GenTable table = genTableService.selectGenTableById(tableId);
    List<GenTable> tables = genTableService.selectGenTableAll();
    List<GenTableColumn> list = genTableService.selectGenTableColumnListByTableId(tableId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("info", table);
    map.put("rows", list);
    map.put("tables", tables);
    return R.ok(map);
}
Also used : GenTable(com.ruoyi.generator.domain.GenTable) HashMap(java.util.HashMap) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) ApiOperation(io.swagger.annotations.ApiOperation) SaCheckPermission(cn.dev33.satoken.annotation.SaCheckPermission)

Aggregations

GenTable (com.ruoyi.generator.domain.GenTable)45 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)20 IOException (java.io.IOException)20 StringWriter (java.io.StringWriter)20 Template (org.apache.velocity.Template)20 VelocityContext (org.apache.velocity.VelocityContext)20 ServiceException (com.ruoyi.common.exception.ServiceException)12 ZipEntry (java.util.zip.ZipEntry)10 File (java.io.File)8 Transactional (org.springframework.transaction.annotation.Transactional)8 LinkedHashMap (java.util.LinkedHashMap)6 Constants (com.ruoyi.common.constant.Constants)5 GenConstants (com.ruoyi.common.constant.GenConstants)5 StringUtils (com.ruoyi.common.utils.StringUtils)5 GenTableColumnMapper (com.ruoyi.generator.mapper.GenTableColumnMapper)5 GenTableMapper (com.ruoyi.generator.mapper.GenTableMapper)5 GenUtils (com.ruoyi.generator.util.GenUtils)5 VelocityInitializer (com.ruoyi.generator.util.VelocityInitializer)5 VelocityUtils (com.ruoyi.generator.util.VelocityUtils)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5