Search in sources :

Example 6 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-fast by yangzongzhuan.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
@Transactional
public void synchDb(String tableName) {
    GenTable table = genTableMapper.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 (StringUtils.isEmpty(dbTableColumns)) {
        throw new ServiceException("同步数据失败,原表结构不存在");
    }
    List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    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.updateGenTableColumn(column);
        } else {
            genTableColumnMapper.insertGenTableColumn(column);
        }
    });
    List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
    if (StringUtils.isNotEmpty(delColumns)) {
        genTableColumnMapper.deleteGenTableColumns(delColumns);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetKit(com.ruoyi.common.core.text.CharsetKit) ServiceException(com.ruoyi.common.exception.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) VelocityUtils(com.ruoyi.project.tool.gen.util.VelocityUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) GenTableMapper(com.ruoyi.project.tool.gen.mapper.GenTableMapper) Function(java.util.function.Function) GenTableColumnMapper(com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper) Template(org.apache.velocity.Template) LinkedHashMap(java.util.LinkedHashMap) StringUtils(com.ruoyi.common.utils.StringUtils) Service(org.springframework.stereotype.Service) Map(java.util.Map) SecurityUtils(com.ruoyi.common.utils.SecurityUtils) ZipEntry(java.util.zip.ZipEntry) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) GenConstants(com.ruoyi.common.constant.GenConstants) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) JSONObject(com.alibaba.fastjson.JSONObject) Velocity(org.apache.velocity.app.Velocity) GenUtils(com.ruoyi.project.tool.gen.util.GenUtils) Transactional(org.springframework.transaction.annotation.Transactional) Constants(com.ruoyi.common.constant.Constants) VelocityInitializer(com.ruoyi.project.tool.gen.util.VelocityInitializer) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-fast by yangzongzhuan.

the class GenTableServiceImpl method selectGenTableById.

/**
 * 查询业务信息
 *
 * @param id 业务ID
 * @return 业务信息
 */
@Override
public GenTable selectGenTableById(Long id) {
    GenTable genTable = genTableMapper.selectGenTableById(id);
    setTableFromOptions(genTable);
    return genTable;
}
Also used : GenTable(com.ruoyi.project.tool.gen.domain.GenTable)

Example 8 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-fast by yangzongzhuan.

the class GenController method getInfo.

/**
 * 修改代码生成业务
 */
@PreAuthorize("@ss.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);
}
Also used : GenTable(com.ruoyi.project.tool.gen.domain.GenTable) HashMap(java.util.HashMap) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 9 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-fast by yangzongzhuan.

the class GenTableServiceImpl method importGenTable.

/**
 * 导入表结构
 *
 * @param tableList 导入表列表
 */
@Override
@Transactional
public void importGenTable(List<GenTable> tableList) {
    String operName = SecurityUtils.getUsername();
    try {
        for (GenTable table : tableList) {
            String tableName = table.getTableName();
            GenUtils.initTable(table, operName);
            int row = genTableMapper.insertGenTable(table);
            if (row > 0) {
                // 保存列信息
                List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                for (GenTableColumn column : genTableColumns) {
                    GenUtils.initColumnField(column, table);
                    genTableColumnMapper.insertGenTableColumn(column);
                }
            }
        }
    } catch (Exception e) {
        throw new ServiceException("导入失败:" + e.getMessage());
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-fast by yangzongzhuan.

the class GenTableServiceImpl method generatorCode.

/**
 * 查询表信息并生成代码
 */
private void generatorCode(String tableName, ZipOutputStream zip) {
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    // 设置主子表信息
    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)));
            IOUtils.write(sw.toString(), zip, Constants.UTF8);
            IOUtils.closeQuietly(sw);
            zip.flush();
            zip.closeEntry();
        } catch (IOException e) {
            log.error("渲染模板失败,表名:" + table.getTableName(), e);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Aggregations

GenTable (com.ruoyi.project.tool.gen.domain.GenTable)18 GenTableColumn (com.ruoyi.project.tool.gen.domain.GenTableColumn)8 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)8 Template (org.apache.velocity.Template)8 VelocityContext (org.apache.velocity.VelocityContext)8 ServiceException (com.ruoyi.common.exception.ServiceException)6 File (java.io.File)4 LinkedHashMap (java.util.LinkedHashMap)4 ZipEntry (java.util.zip.ZipEntry)4 Transactional (org.springframework.transaction.annotation.Transactional)4 JSON (com.alibaba.fastjson.JSON)2 JSONObject (com.alibaba.fastjson.JSONObject)2 Constants (com.ruoyi.common.constant.Constants)2 GenConstants (com.ruoyi.common.constant.GenConstants)2 CharsetKit (com.ruoyi.common.core.text.CharsetKit)2 SecurityUtils (com.ruoyi.common.utils.SecurityUtils)2 StringUtils (com.ruoyi.common.utils.StringUtils)2 GenTableColumnMapper (com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper)2 GenTableMapper (com.ruoyi.project.tool.gen.mapper.GenTableMapper)2