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.DynamicRoutingDataSource;
|
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.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.beans.factory.annotation.Autowired;
|
import org.springframework.context.annotation.Primary;
|
import org.springframework.stereotype.Service;
|
|
import javax.sql.DataSource;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.sql.DatabaseMetaData;
|
import java.sql.SQLException;
|
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 {
|
|
@Autowired
|
private DataSource dataSource;
|
|
public BaseGenTableServiceImpl getService() {
|
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) this.dataSource;
|
DataSource dataSource = ds.determineDataSource();
|
try {
|
DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
|
String databaseProductName = metaData.getDatabaseProductName();
|
if ("MySQL".equals(databaseProductName)) {
|
return SpringUtils.getBean(GenTableServiceImpl.class);
|
} else if ("Oracle".equals(databaseProductName)) {
|
return SpringUtils.getBean(OracleGenTableServiceImpl.class);
|
} else if ("PostgreSQL".equals(databaseProductName)) {
|
|
} else if ("Microsoft SQL Server".equals(databaseProductName)) {
|
|
} else {
|
throw new ServiceException("当前数据库类型不支持 => " + databaseProductName);
|
}
|
} catch (SQLException e) {
|
throw new ServiceException(e.getMessage());
|
}
|
return null;
|
}
|
|
/**
|
* 查询业务字段列表
|
*
|
* @param tableId 业务字段编号
|
* @return 业务字段集合
|
*/
|
@Override
|
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId) {
|
return getService().selectGenTableColumnListByTableId(tableId);
|
}
|
|
/**
|
* 查询业务信息
|
*
|
* @param id 业务ID
|
* @return 业务信息
|
*/
|
@Override
|
public GenTable selectGenTableById(Long id) {
|
return getService().selectGenTableById(id);
|
}
|
|
@Override
|
public TableDataInfo<GenTable> selectPageGenTableList(GenTable genTable, PageQuery pageQuery) {
|
return getService().selectPageGenTableList(genTable, pageQuery);
|
}
|
|
/**
|
* 查询业务列表
|
*
|
* @param genTable 业务信息
|
* @return 业务集合
|
*/
|
@Override
|
public List<GenTable> selectGenTableList(GenTable genTable) {
|
return getService().selectGenTableList(genTable);
|
}
|
|
|
@Override
|
public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable, PageQuery pageQuery){
|
return getService().selectPageDbTableList(genTable, pageQuery);
|
}
|
|
/**
|
* 查询据库列表
|
*
|
* @param genTable 业务信息
|
* @return 数据库表集合
|
*/
|
@Override
|
public List<GenTable> selectDbTableList(GenTable genTable) {
|
return getService().selectDbTableList(genTable);
|
}
|
|
/**
|
* 查询据库列表
|
*
|
* @param tableNames 表名称组
|
* @return 数据库表集合
|
*/
|
@Override
|
public List<GenTable> selectDbTableListByNames(String[] tableNames) {
|
return getService().selectDbTableListByNames(tableNames);
|
}
|
|
/**
|
* 查询所有表信息
|
*
|
* @return 表信息集合
|
*/
|
@Override
|
public List<GenTable> 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<GenTable> tableList) {
|
getService().importGenTable(tableList);
|
}
|
|
/**
|
* 预览代码
|
*
|
* @param tableId 表编号
|
* @return 预览数据列表
|
*/
|
@Override
|
public Map<String, String> 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);
|
}
|
}
|