|
...
|
...
|
@@ -4,7 +4,6 @@ import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
...
|
...
|
@@ -13,10 +12,9 @@ import java.util.stream.Collectors; |
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
import com.ruoyi.generator.domain.TableVules;
|
|
|
|
import com.ruoyi.generator.mapper.GenTableMapMapper;
|
|
|
|
import com.zhonglai.luhui.datasource.enums.DataSource;
|
|
|
|
import com.zhonglai.luhui.datasource.enums.DataSourceType;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.utils.GsonConstructor;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.velocity.Template;
|
|
...
|
...
|
@@ -27,8 +25,6 @@ import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
import com.ruoyi.common.constant.GenConstants;
|
|
|
|
import com.ruoyi.common.core.text.CharsetKit;
|
|
...
|
...
|
@@ -58,9 +54,6 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
@Autowired
|
|
|
|
private GenTableColumnMapper genTableColumnMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GenTableMapMapper mapMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 查询业务信息
|
|
|
|
*
|
|
...
|
...
|
@@ -159,38 +152,49 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建表
|
|
|
|
*
|
|
|
|
* @param sql 创建表语句
|
|
|
|
* @return 结果
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean createTable(String sql)
|
|
|
|
{
|
|
|
|
return genTableMapper.createTable(sql) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 导入表结构
|
|
|
|
*
|
|
|
|
* @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());
|
|
|
|
// }
|
|
|
|
public void importGenTable(List<GenTable> tableList, String operName)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -214,7 +218,7 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
VelocityContext context = VelocityUtils.prepareContext(table);
|
|
|
|
|
|
|
|
// 获取模板列表
|
|
|
|
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
|
|
|
|
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getTplWebType());
|
|
|
|
for (String template : templates)
|
|
|
|
{
|
|
|
|
// 渲染模板
|
|
...
|
...
|
@@ -252,36 +256,6 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
{
|
|
|
|
// 查询表信息
|
|
|
|
GenTable table = genTableMapper.selectGenTableByName(tableName);
|
|
|
|
generatorCode(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 从数据库结构生成代码
|
|
|
|
*
|
|
|
|
* @param tableNames 表名称
|
|
|
|
*/
|
|
|
|
@DataSource(value = DataSourceType.SLAVE)
|
|
|
|
@Override
|
|
|
|
public byte[] generatorCodeFromDb(String databaseName,String tableNames,String packageName,String dataSource)
|
|
|
|
{
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
|
|
|
|
|
|
for (String tableName:tableNames.split(","))
|
|
|
|
{
|
|
|
|
// 查询表信息
|
|
|
|
GenTable table = generatorFromTablename(databaseName,tableName,packageName,dataSource);
|
|
|
|
generatorCode(table, zip);
|
|
|
|
}
|
|
|
|
IOUtils.closeQuietly(zip);
|
|
|
|
return outputStream.toByteArray();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void generatorCode(GenTable table)
|
|
|
|
{
|
|
|
|
// 设置主子表信息
|
|
|
|
setSubTable(table);
|
|
|
|
// 设置主键列信息
|
|
...
|
...
|
@@ -292,7 +266,7 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
VelocityContext context = VelocityUtils.prepareContext(table);
|
|
|
|
|
|
|
|
// 获取模板列表
|
|
|
|
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
|
|
|
|
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getTplWebType());
|
|
|
|
for (String template : templates)
|
|
|
|
{
|
|
|
|
if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
|
|
...
|
...
|
@@ -314,97 +288,6 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public GenTable generatorFromTablename(String databaseName,String tableName,String packageName,String dataSource)
|
|
|
|
{
|
|
|
|
List<Map<String,Object>> list = mapMapper.getObjectListBySQL("SELECT COLUMN_NAME,ORDINAL_POSITION,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,CHARACTER_OCTET_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,DATETIME_PRECISION,CHARACTER_SET_NAME,COLLATION_NAME,COLUMN_TYPE,COLUMN_KEY,EXTRA,COLUMN_COMMENT FROM information_schema.columns WHERE table_schema = '"+databaseName+"' AND table_name = '"+tableName+"'");
|
|
|
|
Map<String,Object> map = mapMapper.getObjectListBySQL("SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name='"+tableName+"' AND table_schema='"+databaseName+"'").get(0);
|
|
|
|
|
|
|
|
String[] tbs = tableName.split("_");
|
|
|
|
GenTable genTable = new GenTable();
|
|
|
|
genTable.setTableName(tableName);
|
|
|
|
genTable.setTableComment(map.get("TABLE_COMMENT")+"");
|
|
|
|
genTable.setClassName(GenUtils.convertClassName(tableName));
|
|
|
|
genTable.setTplCategory("crud");
|
|
|
|
genTable.setPackageName(packageName);
|
|
|
|
genTable.setModuleName(tbs[0]);
|
|
|
|
genTable.setBusinessName(genTable.getClassName());
|
|
|
|
genTable.setFunctionName(genTable.getTableComment());
|
|
|
|
genTable.setFunctionAuthor("钟来");
|
|
|
|
genTable.setGenType("1");
|
|
|
|
genTable.setGenPath("/");
|
|
|
|
if(StringUtils.isNotEmpty(dataSource))
|
|
|
|
{
|
|
|
|
genTable.setDataSource("DataSourceType."+dataSource);
|
|
|
|
}
|
|
|
|
List<GenTableColumn> columns = new ArrayList<>();
|
|
|
|
for(Map<String,Object> table:list)
|
|
|
|
{
|
|
|
|
TableVules tableVules = JSONObject.parseObject(JSONObject.toJSONString(table), TableVules.class);
|
|
|
|
GenTableColumn genTableColumn = new GenTableColumn();
|
|
|
|
genTableColumn.setColumnName(tableVules.getColumnName());
|
|
|
|
genTableColumn.setColumnComment(tableVules.getColumnComment());
|
|
|
|
genTableColumn.setColumnType(tableVules.getColumnType());
|
|
|
|
genTableColumn.setJavaType(GenUtils.toSqlToJava(tableVules.getDataType()));
|
|
|
|
genTableColumn.setJavaField(tableVules.getColumnName());
|
|
|
|
genTableColumn.setIsQuery(GenConstants.REQUIRE);
|
|
|
|
if("PRI".equals(tableVules.getColumnKey())) //主键
|
|
|
|
{
|
|
|
|
genTableColumn.setIsPk("1");
|
|
|
|
genTable.setPkColumn(genTableColumn);
|
|
|
|
}
|
|
|
|
if("auto_increment".equals(tableVules.getExtra()))
|
|
|
|
{
|
|
|
|
genTableColumn.setIsIncrement("1");
|
|
|
|
}else {
|
|
|
|
genTableColumn.setIsIncrement("0");
|
|
|
|
}
|
|
|
|
if("NO".equals(tableVules.getIsNullable()))
|
|
|
|
{
|
|
|
|
genTableColumn.setIsRequired("1");
|
|
|
|
}else {
|
|
|
|
genTableColumn.setIsRequired("0");
|
|
|
|
}
|
|
|
|
switch (tableVules.getDataType())
|
|
|
|
{
|
|
|
|
case "int":
|
|
|
|
if(genTableColumn.getColumnName().indexOf("_time")>=0 )
|
|
|
|
{
|
|
|
|
genTableColumn.setQueryType("BETWEEN");
|
|
|
|
}else{
|
|
|
|
genTableColumn.setQueryType("EQ");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "varchar":
|
|
|
|
genTableColumn.setQueryType("LIKE");
|
|
|
|
break;
|
|
|
|
case "bigint":
|
|
|
|
genTableColumn.setQueryType("EQ");
|
|
|
|
break;
|
|
|
|
case "boolean":
|
|
|
|
genTableColumn.setQueryType("EQ");
|
|
|
|
break;
|
|
|
|
case "char":
|
|
|
|
genTableColumn.setQueryType("EQ");
|
|
|
|
break;
|
|
|
|
case "date":
|
|
|
|
genTableColumn.setQueryType("BETWEEN");
|
|
|
|
break;
|
|
|
|
case "datetime":
|
|
|
|
genTableColumn.setQueryType("BETWEEN");
|
|
|
|
break;
|
|
|
|
case "enum":
|
|
|
|
genTableColumn.setQueryType("EQ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
columns.add(genTableColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
genTable.setColumns(columns);
|
|
|
|
return genTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 同步数据库
|
|
|
|
*
|
|
...
|
...
|
@@ -482,48 +365,13 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
/**
|
|
|
|
* 查询表信息并生成代码
|
|
|
|
*/
|
|
|
|
private void generatorCode(String tableName, ZipOutputStream zip)
|
|
|
|
public void generatorCode(String tableName, ZipOutputStream zip)
|
|
|
|
{
|
|
|
|
// 查询表信息
|
|
|
|
GenTable table = genTableMapper.selectGenTableByName(tableName);
|
|
|
|
generatorCode(table,zip);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void generatorCode(GenTable table, ZipOutputStream zip)
|
|
|
|
{
|
|
|
|
// 设置主子表信息
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 修改保存参数校验
|
|
|
|
*
|
|
...
|
...
|
@@ -534,8 +382,8 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
{
|
|
|
|
if (GenConstants.TPL_TREE.equals(genTable.getTplCategory()))
|
|
|
|
{
|
|
|
|
String options = JSON.toJSONString(genTable.getParams());
|
|
|
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
|
|
|
String options = GsonConstructor.get().toJson(genTable.getParams());
|
|
|
|
JSONObject paramsObj = JSON.parseObject(options);
|
|
|
|
if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE)))
|
|
|
|
{
|
|
|
|
throw new ServiceException("树编码字段不能为空");
|
|
...
|
...
|
@@ -619,7 +467,7 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
*/
|
|
|
|
public void setTableFromOptions(GenTable genTable)
|
|
|
|
{
|
|
|
|
JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions());
|
|
|
|
JSONObject paramsObj = JSON.parseObject(genTable.getOptions());
|
|
|
|
if (StringUtils.isNotNull(paramsObj))
|
|
|
|
{
|
|
|
|
String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
|
|
...
|
...
|
@@ -652,4 +500,39 @@ public class GenTableServiceImpl implements IGenTableService |
|
|
|
}
|
|
|
|
return genPath + File.separator + VelocityUtils.getFileName(template, table);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void generatorCode(GenTable table, ZipOutputStream zip)
|
|
|
|
{
|
|
|
|
// 设置主子表信息
|
|
|
|
setSubTable(table);
|
|
|
|
// 设置主键列信息
|
|
|
|
setPkColumn(table);
|
|
|
|
|
|
|
|
VelocityInitializer.initVelocity();
|
|
|
|
|
|
|
|
VelocityContext context = VelocityUtils.prepareContext(table);
|
|
|
|
|
|
|
|
// 获取模板列表
|
|
|
|
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getTplWebType());
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|