diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/helper/DataBaseHelper.java b/ruoyi-common/src/main/java/com/ruoyi/common/helper/DataBaseHelper.java index 55a3a41c8..c1adea31e 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/helper/DataBaseHelper.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/helper/DataBaseHelper.java @@ -23,7 +23,7 @@ public class DataBaseHelper { /** * 获取当前数据库类型 */ - public static DataBaseType getDataBasyType() { + public static DataBaseType getDataBaseType() { DynamicRoutingDataSource ds = (DynamicRoutingDataSource) SpringUtils.getBean(DataSource.class); DataSource dataSource = ds.determineDataSource(); try { @@ -36,23 +36,23 @@ public class DataBaseHelper { } public static boolean isMySql() { - return DataBaseType.MY_SQL == getDataBasyType(); + return DataBaseType.MY_SQL == getDataBaseType(); } public static boolean isOracle() { - return DataBaseType.ORACLE == getDataBasyType(); + return DataBaseType.ORACLE == getDataBaseType(); } public static boolean isPostgerSql() { - return DataBaseType.POSTGRE_SQL == getDataBasyType(); + return DataBaseType.POSTGRE_SQL == getDataBaseType(); } public static boolean isSqlServer() { - return DataBaseType.SQL_SERVER == getDataBasyType(); + return DataBaseType.SQL_SERVER == getDataBaseType(); } public static String findInSet(Object var1, String var2) { - DataBaseType dataBasyType = getDataBasyType(); + DataBaseType dataBasyType = getDataBaseType(); String var = Convert.toStr(var1); if (dataBasyType == DataBaseType.SQL_SERVER) { // charindex(',100,' , ',0,100,101,') <> 0 diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java index 37119a5a8..29bfe0de8 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java @@ -1,8 +1,6 @@ package com.ruoyi.generator.mapper; import com.baomidou.mybatisplus.annotation.InterceptorIgnore; -import com.baomidou.mybatisplus.core.conditions.Wrapper; -import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.core.mapper.BaseMapperPlus; import com.ruoyi.generator.domain.GenTable; @@ -18,15 +16,15 @@ import java.util.List; @InterceptorIgnore(dataPermission = "true") public interface GenTableMapper extends BaseMapperPlus { - Page selectPageDbTableList(@Param("page") Page page, @Param(Constants.WRAPPER) Wrapper queryWrapper); + Page selectPageDbTableList(@Param("page") Page page, @Param("genTable") GenTable genTable); /** * 查询据库列表 * - * @param queryWrapper 查询条件 + * @param genTable 查询条件 * @return 数据库表集合 */ - List selectDbTableList(@Param(Constants.WRAPPER) Wrapper queryWrapper); + List selectDbTableList(GenTable genTable); /** * 查询据库列表 diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableColumnMapper.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableColumnMapper.java deleted file mode 100644 index b14f2e6c1..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableColumnMapper.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.ruoyi.generator.mapper; - -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; -import com.ruoyi.common.core.mapper.BaseMapperPlus; -import com.ruoyi.generator.domain.GenTableColumn; - -import java.util.List; - -/** - * 业务字段 数据层 - * - * @author Lion Li - */ -@InterceptorIgnore(dataPermission = "true") -public interface OracleGenTableColumnMapper extends BaseMapperPlus { - /** - * 根据表名称查询列信息 - * - * @param tableName 表名称 - * @return 列信息 - */ - List selectDbTableColumnsByName(String tableName); - -} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableMapper.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableMapper.java deleted file mode 100644 index 23a5fedf8..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableMapper.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.ruoyi.generator.mapper; - -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; -import com.baomidou.mybatisplus.core.conditions.Wrapper; -import com.baomidou.mybatisplus.core.toolkit.Constants; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.ruoyi.common.core.mapper.BaseMapperPlus; -import com.ruoyi.generator.domain.GenTable; -import org.apache.ibatis.annotations.Param; - -import java.util.List; - -/** - * 业务 数据层 - * - * @author Lion Li - */ -@InterceptorIgnore(dataPermission = "true") -public interface OracleGenTableMapper extends BaseMapperPlus { - - Page selectPageDbTableList(@Param("page") Page page, @Param(Constants.WRAPPER) Wrapper queryWrapper); - - /** - * 查询据库列表 - * - * @param queryWrapper 查询条件 - * @return 数据库表集合 - */ - List selectDbTableList(@Param(Constants.WRAPPER) Wrapper queryWrapper); - - /** - * 查询据库列表 - * - * @param tableNames 表名称组 - * @return 数据库表集合 - */ - List selectDbTableListByNames(String[] tableNames); - - /** - * 查询所有表信息 - * - * @return 表信息集合 - */ - List selectGenTableAll(); - - /** - * 查询表ID业务信息 - * - * @param id 业务ID - * @return 业务信息 - */ - GenTable selectGenTableById(Long id); - - /** - * 查询表名称业务信息 - * - * @param tableName 表名称 - * @return 业务信息 - */ - GenTable selectGenTableByName(String tableName); - -} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/BaseGenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/BaseGenTableServiceImpl.java deleted file mode 100644 index 6d3034fc6..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/BaseGenTableServiceImpl.java +++ /dev/null @@ -1,319 +0,0 @@ -package com.ruoyi.generator.service; - -import cn.hutool.core.io.IoUtil; -import cn.hutool.core.lang.Dict; -import cn.hutool.core.util.ObjectUtil; -import com.baomidou.dynamic.datasource.annotation.DS; -import com.ruoyi.common.constant.GenConstants; -import com.ruoyi.common.core.domain.PageQuery; -import com.ruoyi.common.core.page.TableDataInfo; -import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.helper.DataBaseHelper; -import com.ruoyi.common.utils.JsonUtils; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.common.utils.spring.SpringUtils; -import com.ruoyi.generator.domain.GenTable; -import com.ruoyi.generator.domain.GenTableColumn; -import com.ruoyi.generator.util.VelocityUtils; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Service; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.zip.ZipOutputStream; - -/** - * 业务 服务层实现 - * - * @author Lion Li - */ -@DS("#header.datasource") -@Primary -@Service -public class BaseGenTableServiceImpl implements IGenTableService { - - public BaseGenTableServiceImpl getService() { - if (DataBaseHelper.isMySql()) { - return SpringUtils.getBean(GenTableServiceImpl.class); - } else if (DataBaseHelper.isOracle()) { - return SpringUtils.getBean(OracleGenTableServiceImpl.class); - } else if (DataBaseHelper.isPostgerSql()) { - throw new ServiceException("当前数据库类型不支持!!!"); - } else if (DataBaseHelper.isSqlServer()) { - throw new ServiceException("当前数据库类型不支持!!!"); - } else { - throw new ServiceException("当前数据库类型不支持!!!"); - } - } - - /** - * 查询业务字段列表 - * - * @param tableId 业务字段编号 - * @return 业务字段集合 - */ - @Override - public List selectGenTableColumnListByTableId(Long tableId) { - return getService().selectGenTableColumnListByTableId(tableId); - } - - /** - * 查询业务信息 - * - * @param id 业务ID - * @return 业务信息 - */ - @Override - public GenTable selectGenTableById(Long id) { - return getService().selectGenTableById(id); - } - - @Override - public TableDataInfo selectPageGenTableList(GenTable genTable, PageQuery pageQuery) { - return getService().selectPageGenTableList(genTable, pageQuery); - } - - /** - * 查询业务列表 - * - * @param genTable 业务信息 - * @return 业务集合 - */ - @Override - public List selectGenTableList(GenTable genTable) { - return getService().selectGenTableList(genTable); - } - - - @Override - public TableDataInfo selectPageDbTableList(GenTable genTable, PageQuery pageQuery){ - return getService().selectPageDbTableList(genTable, pageQuery); - } - - /** - * 查询据库列表 - * - * @param genTable 业务信息 - * @return 数据库表集合 - */ - @Override - public List selectDbTableList(GenTable genTable) { - return getService().selectDbTableList(genTable); - } - - /** - * 查询据库列表 - * - * @param tableNames 表名称组 - * @return 数据库表集合 - */ - @Override - public List selectDbTableListByNames(String[] tableNames) { - return getService().selectDbTableListByNames(tableNames); - } - - /** - * 查询所有表信息 - * - * @return 表信息集合 - */ - @Override - public List selectGenTableAll() { - return getService().selectGenTableAll(); - } - - /** - * 修改业务 - * - * @param genTable 业务信息 - * @return 结果 - */ - @Override - public void updateGenTable(GenTable genTable) { - getService().updateGenTable(genTable); - } - - /** - * 删除业务对象 - * - * @param tableIds 需要删除的数据ID - * @return 结果 - */ - @Override - public void deleteGenTableByIds(Long[] tableIds) { - getService().deleteGenTableByIds(tableIds); - } - - /** - * 导入表结构 - * - * @param tableList 导入表列表 - */ - @Override - public void importGenTable(List tableList) { - getService().importGenTable(tableList); - } - - /** - * 预览代码 - * - * @param tableId 表编号 - * @return 预览数据列表 - */ - @Override - public Map previewCode(Long tableId) { - return getService().previewCode(tableId); - } - - /** - * 生成代码(下载方式) - * - * @param tableName 表名称 - * @return 数据 - */ - @Override - public byte[] downloadCode(String tableName) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ZipOutputStream zip = new ZipOutputStream(outputStream); - generatorCode(tableName, zip); - IoUtil.close(zip); - return outputStream.toByteArray(); - } - - /** - * 生成代码(自定义路径) - * - * @param tableName 表名称 - */ - @Override - public void generatorCode(String tableName) { - getService().generatorCode(tableName); - } - - /** - * 同步数据库 - * - * @param tableName 表名称 - */ - @Override - public void synchDb(String tableName) { - getService().synchDb(tableName); - } - - /** - * 批量生成代码(下载方式) - * - * @param tableNames 表数组 - * @return 数据 - */ - @Override - public byte[] downloadCode(String[] tableNames) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ZipOutputStream zip = new ZipOutputStream(outputStream); - for (String tableName : tableNames) { - generatorCode(tableName, zip); - } - IoUtil.close(zip); - return outputStream.toByteArray(); - } - - /** - * 查询表信息并生成代码 - */ - public void generatorCode(String tableName, ZipOutputStream zip) { - getService().generatorCode(tableName, zip); - } - - /** - * 修改保存参数校验 - * - * @param genTable 业务信息 - */ - @Override - public void validateEdit(GenTable genTable) { - if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) { - String options = JsonUtils.toJsonString(genTable.getParams()); - Dict paramsObj = JsonUtils.parseMap(options); - if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_CODE))) { - throw new ServiceException("树编码字段不能为空"); - } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_PARENT_CODE))) { - throw new ServiceException("树父编码字段不能为空"); - } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_NAME))) { - throw new ServiceException("树名称字段不能为空"); - } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) { - if (StringUtils.isEmpty(genTable.getSubTableName())) { - throw new ServiceException("关联子表的表名不能为空"); - } else if (StringUtils.isEmpty(genTable.getSubTableFkName())) { - throw new ServiceException("子表关联的外键名不能为空"); - } - } - } - } - - /** - * 设置主键列信息 - * - * @param table 业务表信息 - */ - public void setPkColumn(GenTable table) { - for (GenTableColumn column : table.getColumns()) { - if (column.isPk()) { - table.setPkColumn(column); - break; - } - } - if (ObjectUtil.isNull(table.getPkColumn())) { - table.setPkColumn(table.getColumns().get(0)); - } - if (GenConstants.TPL_SUB.equals(table.getTplCategory())) { - for (GenTableColumn column : table.getSubTable().getColumns()) { - if (column.isPk()) { - table.getSubTable().setPkColumn(column); - break; - } - } - if (ObjectUtil.isNull(table.getSubTable().getPkColumn())) { - table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0)); - } - } - } - - /** - * 设置代码生成其他选项值 - * - * @param genTable 设置后的生成对象 - */ - public void setTableFromOptions(GenTable genTable) { - Dict paramsObj = JsonUtils.parseMap(genTable.getOptions()); - if (ObjectUtil.isNotNull(paramsObj)) { - String treeCode = paramsObj.getStr(GenConstants.TREE_CODE); - String treeParentCode = paramsObj.getStr(GenConstants.TREE_PARENT_CODE); - String treeName = paramsObj.getStr(GenConstants.TREE_NAME); - String parentMenuId = paramsObj.getStr(GenConstants.PARENT_MENU_ID); - String parentMenuName = paramsObj.getStr(GenConstants.PARENT_MENU_NAME); - - genTable.setTreeCode(treeCode); - genTable.setTreeParentCode(treeParentCode); - genTable.setTreeName(treeName); - genTable.setParentMenuId(parentMenuId); - 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); - } -} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java index f957009fe..ac0c6befa 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java @@ -2,12 +2,15 @@ package com.ruoyi.generator.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IoUtil; -import com.baomidou.mybatisplus.core.conditions.Wrapper; +import cn.hutool.core.lang.Dict; +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.constant.Constants; +import com.ruoyi.common.constant.GenConstants; import com.ruoyi.common.core.domain.PageQuery; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.exception.ServiceException; @@ -29,6 +32,8 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.springframework.stereotype.Service; +import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -43,10 +48,11 @@ import java.util.zip.ZipOutputStream; * * @author Lion Li */ +@DS("#header.datasource") @Slf4j @RequiredArgsConstructor @Service -public class GenTableServiceImpl extends BaseGenTableServiceImpl { +public class GenTableServiceImpl implements IGenTableService { private final GenTableMapper baseMapper; private final GenTableColumnMapper genTableColumnMapper; @@ -107,7 +113,7 @@ public class GenTableServiceImpl extends BaseGenTableServiceImpl { @Override public TableDataInfo selectPageDbTableList(GenTable genTable, PageQuery pageQuery) { - Page page = baseMapper.selectPageDbTableList(pageQuery.build(), this.buildDbTableQueryWrapper(genTable)); + Page page = baseMapper.selectPageDbTableList(pageQuery.build(), genTable); return TableDataInfo.build(page); } @@ -119,22 +125,7 @@ public class GenTableServiceImpl extends BaseGenTableServiceImpl { */ @Override public List selectDbTableList(GenTable genTable) { - return baseMapper.selectDbTableList(this.buildDbTableQueryWrapper(genTable)); - } - - private Wrapper buildDbTableQueryWrapper(GenTable genTable) { - Map params = genTable.getParams(); - QueryWrapper wrapper = Wrappers.query(); - wrapper.apply("table_schema = (select database())") - .notLike("table_name", "xxl_job_") - .notLike("table_name", "gen_") - .notInSql("table_name", "select table_name from gen_table") - .like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName())) - .like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(table_comment)", StringUtils.lowerCase(genTable.getTableComment())) - .between(params.get("beginTime") != null && params.get("endTime") != null, - "create_time", params.get("beginTime"), params.get("endTime")) - .orderByDesc("create_time"); - return wrapper; + return baseMapper.selectDbTableList(genTable); } /** @@ -251,6 +242,21 @@ public class GenTableServiceImpl extends BaseGenTableServiceImpl { return dataMap; } + /** + * 生成代码(下载方式) + * + * @param tableName 表名称 + * @return 数据 + */ + @Override + public byte[] downloadCode(String tableName) { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream zip = new ZipOutputStream(outputStream); + generatorCode(tableName, zip); + IoUtil.close(zip); + return outputStream.toByteArray(); + } + /** * 生成代码(自定义路径) * @@ -339,10 +345,26 @@ public class GenTableServiceImpl extends BaseGenTableServiceImpl { } /** - * 查询表信息并生成代码 + * 批量生成代码(下载方式) + * + * @param tableNames 表数组 + * @return 数据 */ @Override - public void generatorCode(String tableName, ZipOutputStream zip) { + public byte[] downloadCode(String[] tableNames) { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream zip = new ZipOutputStream(outputStream); + for (String tableName : tableNames) { + generatorCode(tableName, zip); + } + IoUtil.close(zip); + return outputStream.toByteArray(); + } + + /** + * 查询表信息并生成代码 + */ + private void generatorCode(String tableName, ZipOutputStream zip) { // 查询表信息 GenTable table = baseMapper.selectGenTableByName(tableName); // 设置主子表信息 @@ -374,6 +396,60 @@ public class GenTableServiceImpl extends BaseGenTableServiceImpl { } } + /** + * 修改保存参数校验 + * + * @param genTable 业务信息 + */ + @Override + public void validateEdit(GenTable genTable) { + if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) { + String options = JsonUtils.toJsonString(genTable.getParams()); + Dict paramsObj = JsonUtils.parseMap(options); + if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_CODE))) { + throw new ServiceException("树编码字段不能为空"); + } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_PARENT_CODE))) { + throw new ServiceException("树父编码字段不能为空"); + } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_NAME))) { + throw new ServiceException("树名称字段不能为空"); + } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) { + if (StringUtils.isEmpty(genTable.getSubTableName())) { + throw new ServiceException("关联子表的表名不能为空"); + } else if (StringUtils.isEmpty(genTable.getSubTableFkName())) { + throw new ServiceException("子表关联的外键名不能为空"); + } + } + } + } + + /** + * 设置主键列信息 + * + * @param table 业务表信息 + */ + public void setPkColumn(GenTable table) { + for (GenTableColumn column : table.getColumns()) { + if (column.isPk()) { + table.setPkColumn(column); + break; + } + } + if (ObjectUtil.isNull(table.getPkColumn())) { + table.setPkColumn(table.getColumns().get(0)); + } + if (GenConstants.TPL_SUB.equals(table.getTplCategory())) { + for (GenTableColumn column : table.getSubTable().getColumns()) { + if (column.isPk()) { + table.getSubTable().setPkColumn(column); + break; + } + } + if (ObjectUtil.isNull(table.getSubTable().getPkColumn())) { + table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0)); + } + } + } + /** * 设置主子表信息 * @@ -386,4 +462,41 @@ public class GenTableServiceImpl extends BaseGenTableServiceImpl { } } + /** + * 设置代码生成其他选项值 + * + * @param genTable 设置后的生成对象 + */ + public void setTableFromOptions(GenTable genTable) { + Dict paramsObj = JsonUtils.parseMap(genTable.getOptions()); + if (ObjectUtil.isNotNull(paramsObj)) { + String treeCode = paramsObj.getStr(GenConstants.TREE_CODE); + String treeParentCode = paramsObj.getStr(GenConstants.TREE_PARENT_CODE); + String treeName = paramsObj.getStr(GenConstants.TREE_NAME); + String parentMenuId = paramsObj.getStr(GenConstants.PARENT_MENU_ID); + String parentMenuName = paramsObj.getStr(GenConstants.PARENT_MENU_NAME); + + genTable.setTreeCode(treeCode); + genTable.setTreeParentCode(treeParentCode); + genTable.setTreeName(treeName); + genTable.setParentMenuId(parentMenuId); + 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); + } } + diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/OracleGenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/OracleGenTableServiceImpl.java deleted file mode 100644 index 79550d596..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/OracleGenTableServiceImpl.java +++ /dev/null @@ -1,391 +0,0 @@ -package com.ruoyi.generator.service; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.io.IoUtil; -import com.baomidou.mybatisplus.core.conditions.Wrapper; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.ruoyi.common.constant.Constants; -import com.ruoyi.common.core.domain.PageQuery; -import com.ruoyi.common.core.page.TableDataInfo; -import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.helper.LoginHelper; -import com.ruoyi.common.utils.JsonUtils; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.common.utils.file.FileUtils; -import com.ruoyi.generator.domain.GenTable; -import com.ruoyi.generator.domain.GenTableColumn; -import com.ruoyi.generator.mapper.OracleGenTableColumnMapper; -import com.ruoyi.generator.mapper.OracleGenTableMapper; -import com.ruoyi.generator.util.GenUtils; -import com.ruoyi.generator.util.VelocityInitializer; -import com.ruoyi.generator.util.VelocityUtils; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.Velocity; -import org.springframework.stereotype.Service; - -import java.io.IOException; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -/** - * 业务 服务层实现 - * - * @author Lion Li - */ -@Slf4j -@RequiredArgsConstructor -@Service -public class OracleGenTableServiceImpl extends BaseGenTableServiceImpl { - - private final OracleGenTableMapper baseMapper; - private final OracleGenTableColumnMapper genTableColumnMapper; - - /** - * 查询业务字段列表 - * - * @param tableId 业务字段编号 - * @return 业务字段集合 - */ - @Override - public List selectGenTableColumnListByTableId(Long tableId) { - return genTableColumnMapper.selectList(new LambdaQueryWrapper() - .eq(GenTableColumn::getTableId, tableId) - .orderByAsc(GenTableColumn::getSort)); - } - /** - * 查询业务信息 - * - * @param id 业务ID - * @return 业务信息 - */ - @Override - public GenTable selectGenTableById(Long id) { - GenTable genTable = baseMapper.selectGenTableById(id); - setTableFromOptions(genTable); - return genTable; - } - - @Override - public TableDataInfo selectPageGenTableList(GenTable genTable, PageQuery pageQuery) { - Page page = baseMapper.selectPage(pageQuery.build(), this.buildGenTableQueryWrapper(genTable)); - return TableDataInfo.build(page); - } - - /** - * 查询业务列表 - * - * @param genTable 业务信息 - * @return 业务集合 - */ - @Override - public List selectGenTableList(GenTable genTable) { - return baseMapper.selectList(this.buildGenTableQueryWrapper(genTable)); - } - - private QueryWrapper buildGenTableQueryWrapper(GenTable genTable) { - Map params = genTable.getParams(); - QueryWrapper wrapper = Wrappers.query(); - wrapper.like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName())) - .like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(table_comment)", StringUtils.lowerCase(genTable.getTableComment())) - .between(params.get("beginTime") != null && params.get("endTime") != null, - "create_time", params.get("beginTime"), params.get("endTime")); - return wrapper; - } - - - @Override - public TableDataInfo selectPageDbTableList(GenTable genTable, PageQuery pageQuery) { - Page page = baseMapper.selectPageDbTableList(pageQuery.build(), this.buildDbTableQueryWrapper(genTable)); - return TableDataInfo.build(page); - } - - /** - * 查询据库列表 - * - * @param genTable 业务信息 - * @return 数据库表集合 - */ - @Override - public List selectDbTableList(GenTable genTable) { - return baseMapper.selectDbTableList(this.buildDbTableQueryWrapper(genTable)); - } - - private Wrapper buildDbTableQueryWrapper(GenTable genTable) { - Map params = genTable.getParams(); - QueryWrapper wrapper = Wrappers.query(); - wrapper.apply("dt.table_name = dtc.table_name") - .apply("dt.table_name = uo.object_name") - .eq("uo.object_type ", "TABLE") - .notLike("dt.table_name", "XXL_JOB_") - .notLike("dt.table_name", "GEN_") - .notInSql("lower(dt.table_name)", "select table_name from gen_table") - .like(StringUtils.isNotBlank(genTable.getTableName()), "lower(dt.table_name)", StringUtils.lowerCase(genTable.getTableName())) - .like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(dt.table_comment)", StringUtils.lowerCase(genTable.getTableComment())) - .between(params.get("beginTime") != null && params.get("endTime") != null, - "create_time", params.get("beginTime"), params.get("endTime")) - .orderByDesc("create_time"); - return wrapper; - } - - /** - * 查询据库列表 - * - * @param tableNames 表名称组 - * @return 数据库表集合 - */ - @Override - public List selectDbTableListByNames(String[] tableNames) { - return baseMapper.selectDbTableListByNames(tableNames); - } - - /** - * 查询所有表信息 - * - * @return 表信息集合 - */ - @Override - public List selectGenTableAll() { - return baseMapper.selectGenTableAll(); - } - - /** - * 修改业务 - * - * @param genTable 业务信息 - * @return 结果 - */ - @Override - public void updateGenTable(GenTable genTable) { - String options = JsonUtils.toJsonString(genTable.getParams()); - genTable.setOptions(options); - int row = baseMapper.updateById(genTable); - if (row > 0) { - for (GenTableColumn cenTableColumn : genTable.getColumns()) { - genTableColumnMapper.updateById(cenTableColumn); - } - } - } - - /** - * 删除业务对象 - * - * @param tableIds 需要删除的数据ID - * @return 结果 - */ - @Override - public void deleteGenTableByIds(Long[] tableIds) { - List ids = Arrays.asList(tableIds); - baseMapper.deleteBatchIds(ids); - genTableColumnMapper.delete(new LambdaQueryWrapper().in(GenTableColumn::getTableId, ids)); - } - - /** - * 导入表结构 - * - * @param tableList 导入表列表 - */ - @Override - public void importGenTable(List tableList) { - String operName = LoginHelper.getUsername(); - try { - for (GenTable table : tableList) { - String tableName = table.getTableName(); - GenUtils.initTable(table, operName); - int row = baseMapper.insert(table); - if (row > 0) { - // 保存列信息 - List genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); - List saveColumns = new ArrayList<>(); - for (GenTableColumn column : genTableColumns) { - GenUtils.initColumnField(column, table); - saveColumns.add(column); - } - if (CollUtil.isNotEmpty(saveColumns)) { - genTableColumnMapper.insertBatch(saveColumns); - } - } - } - } catch (Exception e) { - throw new ServiceException("导入失败:" + e.getMessage()); - } - } - - /** - * 预览代码 - * - * @param tableId 表编号 - * @return 预览数据列表 - */ - @Override - public Map previewCode(Long tableId) { - Map dataMap = new LinkedHashMap<>(); - // 查询表信息 - GenTable table = baseMapper.selectGenTableById(tableId); - // 设置主子表信息 - setSubTable(table); - // 设置主键列信息 - setPkColumn(table); - VelocityInitializer.initVelocity(); - - VelocityContext context = VelocityUtils.prepareContext(table); - - // 获取模板列表 - List 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; - } - - /** - * 生成代码(自定义路径) - * - * @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 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()); - } - } - } - } - - /** - * 同步数据库 - * - * @param tableName 表名称 - */ - @Override - public void synchDb(String tableName) { - GenTable table = baseMapper.selectGenTableByName(tableName); - List tableColumns = table.getColumns(); - Map tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity())); - - List dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); - if (CollUtil.isEmpty(dbTableColumns)) { - throw new ServiceException("同步数据失败,原表结构不存在"); - } - List dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); - - List 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 delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList()); - if (CollUtil.isNotEmpty(delColumns)) { - List ids = delColumns.stream().map(GenTableColumn::getColumnId).collect(Collectors.toList()); - genTableColumnMapper.deleteBatchIds(ids); - } - } - - /** - * 查询表信息并生成代码 - */ - @Override - public void generatorCode(String tableName, ZipOutputStream zip) { - // 查询表信息 - GenTable table = baseMapper.selectGenTableByName(tableName); - // 设置主子表信息 - setSubTable(table); - // 设置主键列信息 - setPkColumn(table); - - VelocityInitializer.initVelocity(); - - VelocityContext context = VelocityUtils.prepareContext(table); - - // 获取模板列表 - List 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); - } - } - } - - /** - * 设置主子表信息 - * - * @param table 业务表信息 - */ - public void setSubTable(GenTable table) { - String subTableName = table.getSubTableName(); - if (StringUtils.isNotEmpty(subTableName)) { - table.setSubTable(baseMapper.selectGenTableByName(subTableName)); - } - } - -} diff --git a/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml index 7ef974458..4614dd41c 100644 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -30,9 +30,41 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" diff --git a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml index cb437b4ca..306f7af27 100644 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml +++ b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml @@ -54,30 +54,125 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select lower(temp.column_name) as column_name, - (case when (temp.nullable = 'N' and temp.constraint_type != 'P') then '1' else null end) as is_required, - (case when temp.constraint_type = 'P' then '1' else '0' end) as is_pk, - temp.column_id as sort, - temp.comments as column_comment, - (case when temp.constraint_type = 'P' then '1' else '0' end) as is_increment, - lower(temp.data_type) as column_type - from ( - select col.column_id, col.column_name,col.nullable, col.data_type, colc.comments, uc.constraint_type - , row_number() over (partition by col.column_name order by uc.constraint_type desc) as row_flg - from user_tab_columns col - left join user_col_comments colc on colc.table_name = col.table_name and colc.column_name = col.column_name - left join user_cons_columns ucc on ucc.table_name = col.table_name and ucc.column_name = col.column_name - left join user_constraints uc on uc.constraint_name = ucc.constraint_name - where col.table_name = upper(#{tableName}) - ) temp - WHERE temp.row_flg = 1 - ORDER BY temp.column_id - - - diff --git a/ruoyi-generator/src/main/resources/mapper/generator/OracleGenTableMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/OracleGenTableMapper.xml deleted file mode 100644 index 5bf1a2006..000000000 --- a/ruoyi-generator/src/main/resources/mapper/generator/OracleGenTableMapper.xml +++ /dev/null @@ -1,117 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -