|
@@ -5,13 +5,18 @@ import cn.hutool.core.io.IoUtil;
|
|
import cn.hutool.core.lang.Dict;
|
|
import cn.hutool.core.lang.Dict;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
|
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+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.dromara.common.core.constant.Constants;
|
|
import org.dromara.common.core.constant.Constants;
|
|
-import org.dromara.generator.constant.GenConstants;
|
|
|
|
import org.dromara.common.core.exception.ServiceException;
|
|
import org.dromara.common.core.exception.ServiceException;
|
|
import org.dromara.common.core.utils.StreamUtils;
|
|
import org.dromara.common.core.utils.StreamUtils;
|
|
import org.dromara.common.core.utils.StringUtils;
|
|
import org.dromara.common.core.utils.StringUtils;
|
|
@@ -20,6 +25,7 @@ import org.dromara.common.json.utils.JsonUtils;
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
import org.dromara.common.satoken.utils.LoginHelper;
|
|
import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
|
+import org.dromara.generator.constant.GenConstants;
|
|
import org.dromara.generator.domain.GenTable;
|
|
import org.dromara.generator.domain.GenTable;
|
|
import org.dromara.generator.domain.GenTableColumn;
|
|
import org.dromara.generator.domain.GenTableColumn;
|
|
import org.dromara.generator.mapper.GenTableColumnMapper;
|
|
import org.dromara.generator.mapper.GenTableColumnMapper;
|
|
@@ -27,11 +33,6 @@ import org.dromara.generator.mapper.GenTableMapper;
|
|
import org.dromara.generator.util.GenUtils;
|
|
import org.dromara.generator.util.GenUtils;
|
|
import org.dromara.generator.util.VelocityInitializer;
|
|
import org.dromara.generator.util.VelocityInitializer;
|
|
import org.dromara.generator.util.VelocityUtils;
|
|
import org.dromara.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 org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -40,7 +41,11 @@ import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.StringWriter;
|
|
import java.io.StringWriter;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
@@ -49,7 +54,7 @@ import java.util.zip.ZipOutputStream;
|
|
*
|
|
*
|
|
* @author Lion Li
|
|
* @author Lion Li
|
|
*/
|
|
*/
|
|
-@DS("#header.datasource")
|
|
|
|
|
|
+// @DS("#header.datasource")
|
|
@Slf4j
|
|
@Slf4j
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
@Service
|
|
@Service
|
|
@@ -94,16 +99,19 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
private QueryWrapper<GenTable> buildGenTableQueryWrapper(GenTable genTable) {
|
|
private QueryWrapper<GenTable> buildGenTableQueryWrapper(GenTable genTable) {
|
|
Map<String, Object> params = genTable.getParams();
|
|
Map<String, Object> params = genTable.getParams();
|
|
QueryWrapper<GenTable> wrapper = Wrappers.query();
|
|
QueryWrapper<GenTable> wrapper = Wrappers.query();
|
|
- wrapper.like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName()))
|
|
|
|
|
|
+ wrapper
|
|
|
|
+ .eq(StringUtils.isNotEmpty(genTable.getDataName()),"data_name", genTable.getDataName())
|
|
|
|
+ .like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName()))
|
|
.like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(table_comment)", StringUtils.lowerCase(genTable.getTableComment()))
|
|
.like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(table_comment)", StringUtils.lowerCase(genTable.getTableComment()))
|
|
.between(params.get("beginTime") != null && params.get("endTime") != null,
|
|
.between(params.get("beginTime") != null && params.get("endTime") != null,
|
|
"create_time", params.get("beginTime"), params.get("endTime"));
|
|
"create_time", params.get("beginTime"), params.get("endTime"));
|
|
return wrapper;
|
|
return wrapper;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ @DS("#genTable.dataName")
|
|
@Override
|
|
@Override
|
|
public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable, PageQuery pageQuery) {
|
|
public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable, PageQuery pageQuery) {
|
|
|
|
+ genTable.getParams().put("genTableNames",baseMapper.selectTableNameList(genTable.getDataName()));
|
|
Page<GenTable> page = baseMapper.selectPageDbTableList(pageQuery.build(), genTable);
|
|
Page<GenTable> page = baseMapper.selectPageDbTableList(pageQuery.build(), genTable);
|
|
return TableDataInfo.build(page);
|
|
return TableDataInfo.build(page);
|
|
}
|
|
}
|
|
@@ -112,10 +120,12 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
* 查询据库列表
|
|
* 查询据库列表
|
|
*
|
|
*
|
|
* @param tableNames 表名称组
|
|
* @param tableNames 表名称组
|
|
|
|
+ * @param dataName
|
|
* @return 数据库表集合
|
|
* @return 数据库表集合
|
|
*/
|
|
*/
|
|
|
|
+ @DS("#dataName")
|
|
@Override
|
|
@Override
|
|
- public List<GenTable> selectDbTableListByNames(String[] tableNames) {
|
|
|
|
|
|
+ public List<GenTable> selectDbTableListByNames(String[] tableNames, String dataName) {
|
|
return baseMapper.selectDbTableListByNames(tableNames);
|
|
return baseMapper.selectDbTableListByNames(tableNames);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -166,19 +176,21 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
* 导入表结构
|
|
* 导入表结构
|
|
*
|
|
*
|
|
* @param tableList 导入表列表
|
|
* @param tableList 导入表列表
|
|
|
|
+ * @param dataName
|
|
*/
|
|
*/
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
+ @DSTransactional
|
|
@Override
|
|
@Override
|
|
- public void importGenTable(List<GenTable> tableList) {
|
|
|
|
|
|
+ public void importGenTable(List<GenTable> tableList, String dataName) {
|
|
String operName = LoginHelper.getUsername();
|
|
String operName = LoginHelper.getUsername();
|
|
try {
|
|
try {
|
|
for (GenTable table : tableList) {
|
|
for (GenTable table : tableList) {
|
|
String tableName = table.getTableName();
|
|
String tableName = table.getTableName();
|
|
GenUtils.initTable(table, operName);
|
|
GenUtils.initTable(table, operName);
|
|
|
|
+ table.setDataName(dataName);
|
|
int row = baseMapper.insert(table);
|
|
int row = baseMapper.insert(table);
|
|
if (row > 0) {
|
|
if (row > 0) {
|
|
// 保存列信息
|
|
// 保存列信息
|
|
- List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
|
|
|
|
|
|
+ List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName, dataName);
|
|
List<GenTableColumn> saveColumns = new ArrayList<>();
|
|
List<GenTableColumn> saveColumns = new ArrayList<>();
|
|
for (GenTableColumn column : genTableColumns) {
|
|
for (GenTableColumn column : genTableColumns) {
|
|
GenUtils.initColumnField(column, table);
|
|
GenUtils.initColumnField(column, table);
|
|
@@ -231,14 +243,14 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
/**
|
|
/**
|
|
* 生成代码(下载方式)
|
|
* 生成代码(下载方式)
|
|
*
|
|
*
|
|
- * @param tableName 表名称
|
|
|
|
|
|
+ * @param tableId 表名称
|
|
* @return 数据
|
|
* @return 数据
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public byte[] downloadCode(String tableName) {
|
|
|
|
|
|
+ public byte[] downloadCode(Long tableId) {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
- generatorCode(tableName, zip);
|
|
|
|
|
|
+ generatorCode(tableId, zip);
|
|
IoUtil.close(zip);
|
|
IoUtil.close(zip);
|
|
return outputStream.toByteArray();
|
|
return outputStream.toByteArray();
|
|
}
|
|
}
|
|
@@ -246,12 +258,12 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
/**
|
|
/**
|
|
* 生成代码(自定义路径)
|
|
* 生成代码(自定义路径)
|
|
*
|
|
*
|
|
- * @param tableName 表名称
|
|
|
|
|
|
+ * @param tableId 表名称
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public void generatorCode(String tableName) {
|
|
|
|
|
|
+ public void generatorCode(Long tableId) {
|
|
// 查询表信息
|
|
// 查询表信息
|
|
- GenTable table = baseMapper.selectGenTableByName(tableName);
|
|
|
|
|
|
+ GenTable table = baseMapper.selectGenTableById(tableId);
|
|
// 设置主键列信息
|
|
// 设置主键列信息
|
|
setPkColumn(table);
|
|
setPkColumn(table);
|
|
|
|
|
|
@@ -280,16 +292,16 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
/**
|
|
/**
|
|
* 同步数据库
|
|
* 同步数据库
|
|
*
|
|
*
|
|
- * @param tableName 表名称
|
|
|
|
|
|
+ * @param tableId 表名称
|
|
*/
|
|
*/
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
+ @DSTransactional
|
|
@Override
|
|
@Override
|
|
- public void synchDb(String tableName) {
|
|
|
|
- GenTable table = baseMapper.selectGenTableByName(tableName);
|
|
|
|
|
|
+ public void synchDb(Long tableId) {
|
|
|
|
+ GenTable table = baseMapper.selectGenTableById(tableId);
|
|
List<GenTableColumn> tableColumns = table.getColumns();
|
|
List<GenTableColumn> tableColumns = table.getColumns();
|
|
Map<String, GenTableColumn> tableColumnMap = StreamUtils.toIdentityMap(tableColumns, GenTableColumn::getColumnName);
|
|
Map<String, GenTableColumn> tableColumnMap = StreamUtils.toIdentityMap(tableColumns, GenTableColumn::getColumnName);
|
|
|
|
|
|
- List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
|
|
|
|
|
|
+ List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(table.getTableName(), table.getDataName());
|
|
if (CollUtil.isEmpty(dbTableColumns)) {
|
|
if (CollUtil.isEmpty(dbTableColumns)) {
|
|
throw new ServiceException("同步数据失败,原表结构不存在");
|
|
throw new ServiceException("同步数据失败,原表结构不存在");
|
|
}
|
|
}
|
|
@@ -322,22 +334,24 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
List<GenTableColumn> delColumns = StreamUtils.filter(tableColumns, column -> !dbTableColumnNames.contains(column.getColumnName()));
|
|
List<GenTableColumn> delColumns = StreamUtils.filter(tableColumns, column -> !dbTableColumnNames.contains(column.getColumnName()));
|
|
if (CollUtil.isNotEmpty(delColumns)) {
|
|
if (CollUtil.isNotEmpty(delColumns)) {
|
|
List<Long> ids = StreamUtils.toList(delColumns, GenTableColumn::getColumnId);
|
|
List<Long> ids = StreamUtils.toList(delColumns, GenTableColumn::getColumnId);
|
|
- genTableColumnMapper.deleteBatchIds(ids);
|
|
|
|
|
|
+ if (CollUtil.isNotEmpty(ids)) {
|
|
|
|
+ genTableColumnMapper.deleteBatchIds(ids);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 批量生成代码(下载方式)
|
|
* 批量生成代码(下载方式)
|
|
*
|
|
*
|
|
- * @param tableNames 表数组
|
|
|
|
|
|
+ * @param tableIds 表ID数组
|
|
* @return 数据
|
|
* @return 数据
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public byte[] downloadCode(String[] tableNames) {
|
|
|
|
|
|
+ public byte[] downloadCode(String[] tableIds) {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
- for (String tableName : tableNames) {
|
|
|
|
- generatorCode(tableName, zip);
|
|
|
|
|
|
+ for (String tableId : tableIds) {
|
|
|
|
+ generatorCode(Long.parseLong(tableId), zip);
|
|
}
|
|
}
|
|
IoUtil.close(zip);
|
|
IoUtil.close(zip);
|
|
return outputStream.toByteArray();
|
|
return outputStream.toByteArray();
|
|
@@ -346,9 +360,9 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
/**
|
|
/**
|
|
* 查询表信息并生成代码
|
|
* 查询表信息并生成代码
|
|
*/
|
|
*/
|
|
- private void generatorCode(String tableName, ZipOutputStream zip) {
|
|
|
|
|
|
+ private void generatorCode(Long tableId, ZipOutputStream zip) {
|
|
// 查询表信息
|
|
// 查询表信息
|
|
- GenTable table = baseMapper.selectGenTableByName(tableName);
|
|
|
|
|
|
+ GenTable table = baseMapper.selectGenTableById(tableId);
|
|
List<Long> menuIds = new ArrayList<>();
|
|
List<Long> menuIds = new ArrayList<>();
|
|
for (int i = 0; i < 6; i++) {
|
|
for (int i = 0; i < 6; i++) {
|
|
menuIds.add(identifierGenerator.nextId(null).longValue());
|
|
menuIds.add(identifierGenerator.nextId(null).longValue());
|