refactor(代码生成): 优化代码下载的路径默认为 admin , up screw 0.0.6
(cherry picked from commit 582bbcabbea1e84ef4b33c78c0e8f54897106299)
This commit is contained in:
1 parent
310b16ba7b
commit
c647864b9b
4 files changed
+184
-190
No files matched your search
@@ -16,7 +16,7 @@
|
||||
|
||||
<properties>
|
||||
<magic.version>2.0.2</magic.version>
|
||||
<screw.version>0.0.5</screw.version>
|
||||
<screw.version>0.0.6</screw.version>
|
||||
<db2.version>11.5.0.0</db2.version>
|
||||
<anyline.version>8.7.1-jdk17-20232101</anyline.version>
|
||||
<configuration.version>1.10</configuration.version>
|
||||
|
||||
+179
-184
@@ -60,211 +60,206 @@ import java.util.*;
|
||||
@RequiredArgsConstructor
|
||||
public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable> implements GenTableService {
|
||||
|
||||
/**
|
||||
* 默认配置信息
|
||||
*/
|
||||
private static final String CONFIG_PATH = "template/config.json";
|
||||
/**
|
||||
* 默认配置信息
|
||||
*/
|
||||
private static final String CONFIG_PATH = "template/config.json";
|
||||
|
||||
private final GenTableColumnService columnService;
|
||||
private final GenTableColumnService columnService;
|
||||
|
||||
private final GenGroupService genGroupService;
|
||||
private final GenGroupService genGroupService;
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*
|
||||
* @return map
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> getGeneratorConfig() {
|
||||
ClassPathResource classPathResource = new ClassPathResource(CONFIG_PATH);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(IoUtil.readUtf8(classPathResource.getStream()));
|
||||
return jsonObject.getRaw();
|
||||
}
|
||||
/**
|
||||
* 获取配置信息
|
||||
* @return map
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> getGeneratorConfig() {
|
||||
ClassPathResource classPathResource = new ClassPathResource(CONFIG_PATH);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(IoUtil.readUtf8(classPathResource.getStream()));
|
||||
return jsonObject.getRaw();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询表ddl 语句
|
||||
*
|
||||
* @param dsName 数据源名称
|
||||
* @param tableName 表名称
|
||||
* @return ddl 语句
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String queryTableDdl(String dsName, String tableName) throws Exception {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
Table table = ServiceProxy.metadata().table(tableName); // 获取表结构
|
||||
table.execute(false);// 不执行SQL
|
||||
ServiceProxy.ddl().create(table);
|
||||
return table.getDdl();// 返回创建表的DDL
|
||||
}
|
||||
/**
|
||||
* 查询表ddl 语句
|
||||
* @param dsName 数据源名称
|
||||
* @param tableName 表名称
|
||||
* @return ddl 语句
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String queryTableDdl(String dsName, String tableName) throws Exception {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
Table table = ServiceProxy.metadata().table(tableName); // 获取表结构
|
||||
table.execute(false);// 不执行SQL
|
||||
ServiceProxy.ddl().create(table);
|
||||
return table.getDdl();// 返回创建表的DDL
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询表的全部字段
|
||||
*
|
||||
* @param dsName 数据源
|
||||
* @param tableName 表名称
|
||||
* @return column
|
||||
*/
|
||||
@Override
|
||||
public List<String> queryTableColumn(String dsName, String tableName) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
return ServiceProxy.metadata().columns(tableName).values().stream().map(Column::getName).toList();
|
||||
}
|
||||
/**
|
||||
* 查询表的全部字段
|
||||
* @param dsName 数据源
|
||||
* @param tableName 表名称
|
||||
* @return column
|
||||
*/
|
||||
@Override
|
||||
public List<String> queryTableColumn(String dsName, String tableName) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
return ServiceProxy.metadata().columns(tableName).values().stream().map(Column::getName).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询对应数据源的表
|
||||
*
|
||||
* @param page 分页信息
|
||||
* @param table 查询条件
|
||||
* @return 表
|
||||
*/
|
||||
@Override
|
||||
public IPage queryTablePage(Page<Table> page, GenTable table) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(table.getDsName());
|
||||
List<Table> tableList = ServiceProxy.metadata().tables().values().stream()
|
||||
.filter(t -> {
|
||||
if (StrUtil.isBlank(table.getTableName())) {
|
||||
return true;
|
||||
}
|
||||
return StrUtil.containsIgnoreCase(t.getName(false), table.getTableName());
|
||||
}).toList();
|
||||
/**
|
||||
* 查询对应数据源的表
|
||||
* @param page 分页信息
|
||||
* @param table 查询条件
|
||||
* @return 表
|
||||
*/
|
||||
@Override
|
||||
public IPage queryTablePage(Page<Table> page, GenTable table) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(table.getDsName());
|
||||
List<Table> tableList = ServiceProxy.metadata().tables().values().stream().filter(t -> {
|
||||
if (StrUtil.isBlank(table.getTableName())) {
|
||||
return true;
|
||||
}
|
||||
return StrUtil.containsIgnoreCase(t.getName(false), table.getTableName());
|
||||
}).toList();
|
||||
|
||||
// 根据 page 进行分页
|
||||
List<Table> records = CollUtil.page((int) page.getCurrent() - 1, (int) page.getSize(), tableList);
|
||||
page.setTotal(tableList.size());
|
||||
page.setRecords(records);
|
||||
return page;
|
||||
}
|
||||
// 根据 page 进行分页
|
||||
List<Table> records = CollUtil.page((int) page.getCurrent() - 1, (int) page.getSize(), tableList);
|
||||
page.setTotal(tableList.size());
|
||||
page.setRecords(records);
|
||||
return page;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询数据源里面的全部表
|
||||
*
|
||||
* @param dsName 数据源名称
|
||||
* @return table
|
||||
*/
|
||||
@Override
|
||||
public List<String> queryTableList(String dsName) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
return ServiceProxy.metadata().tables().values().stream().map(Table::getName).toList();
|
||||
}
|
||||
/**
|
||||
* 查询数据源里面的全部表
|
||||
* @param dsName 数据源名称
|
||||
* @return table
|
||||
*/
|
||||
@Override
|
||||
public List<String> queryTableList(String dsName) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
return ServiceProxy.metadata().tables().values().stream().map(Table::getName).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询表信息(列),然后插入到中间表中
|
||||
*
|
||||
* @param dsName 数据源
|
||||
* @param tableName 表名
|
||||
* @return GenTable
|
||||
*/
|
||||
@Override
|
||||
public GenTable queryOrBuildTable(String dsName, String tableName) {
|
||||
GenTable genTable = baseMapper.selectOne(
|
||||
Wrappers.<GenTable>lambdaQuery().eq(GenTable::getTableName, tableName).eq(GenTable::getDsName, dsName));
|
||||
// 如果 genTable 为空, 执行导入
|
||||
if (Objects.isNull(genTable)) {
|
||||
genTable = this.tableImport(dsName, tableName);
|
||||
}
|
||||
/**
|
||||
* 查询表信息(列),然后插入到中间表中
|
||||
* @param dsName 数据源
|
||||
* @param tableName 表名
|
||||
* @return GenTable
|
||||
*/
|
||||
@Override
|
||||
public GenTable queryOrBuildTable(String dsName, String tableName) {
|
||||
GenTable genTable = baseMapper.selectOne(
|
||||
Wrappers.<GenTable>lambdaQuery().eq(GenTable::getTableName, tableName).eq(GenTable::getDsName, dsName));
|
||||
// 如果 genTable 为空, 执行导入
|
||||
if (Objects.isNull(genTable)) {
|
||||
genTable = this.tableImport(dsName, tableName);
|
||||
}
|
||||
|
||||
List<GenTableColumnEntity> fieldList = columnService.list(Wrappers.<GenTableColumnEntity>lambdaQuery()
|
||||
.eq(GenTableColumnEntity::getDsName, dsName)
|
||||
.eq(GenTableColumnEntity::getTableName, tableName)
|
||||
.orderByAsc(GenTableColumnEntity::getSort));
|
||||
genTable.setFieldList(fieldList);
|
||||
List<GenTableColumnEntity> fieldList = columnService.list(Wrappers.<GenTableColumnEntity>lambdaQuery()
|
||||
.eq(GenTableColumnEntity::getDsName, dsName)
|
||||
.eq(GenTableColumnEntity::getTableName, tableName)
|
||||
.orderByAsc(GenTableColumnEntity::getSort));
|
||||
genTable.setFieldList(fieldList);
|
||||
|
||||
// 查询模板分组信息
|
||||
List<GenGroupEntity> groupEntities = genGroupService.list();
|
||||
genTable.setGroupList(groupEntities);
|
||||
return genTable;
|
||||
}
|
||||
// 查询模板分组信息
|
||||
List<GenGroupEntity> groupEntities = genGroupService.list();
|
||||
genTable.setGroupList(groupEntities);
|
||||
return genTable;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
protected GenTable tableImport(String dsName, String tableName) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
protected GenTable tableImport(String dsName, String tableName) {
|
||||
// 手动切换数据源
|
||||
DynamicDataSourceContextHolder.push(dsName);
|
||||
|
||||
// 查询表是否存在
|
||||
GenTable table = new GenTable();
|
||||
// 从数据库获取表信息
|
||||
AnylineService service = ServiceProxy.service();
|
||||
Table tableMetadata = service.metadata().table(tableName);
|
||||
Database database = service.metadata().database();
|
||||
// 获取默认表配置信息 ()
|
||||
Map<String, Object> generatorConfig = getGeneratorConfig();
|
||||
// 查询表是否存在
|
||||
GenTable table = new GenTable();
|
||||
// 从数据库获取表信息
|
||||
AnylineService service = ServiceProxy.service();
|
||||
Table tableMetadata = service.metadata().table(tableName);
|
||||
Database database = service.metadata().database();
|
||||
// 获取默认表配置信息 ()
|
||||
Map<String, Object> generatorConfig = getGeneratorConfig();
|
||||
|
||||
JSONObject project = (JSONObject) generatorConfig.get("project");
|
||||
JSONObject developer = (JSONObject) generatorConfig.get("developer");
|
||||
JSONObject project = (JSONObject) generatorConfig.get("project");
|
||||
JSONObject developer = (JSONObject) generatorConfig.get("developer");
|
||||
|
||||
table.setPackageName(project.getStr("packageName"));
|
||||
table.setVersion(project.getStr("version"));
|
||||
table.setBackendPath(project.getStr("backendPath"));
|
||||
table.setFrontendPath(project.getStr("frontendPath"));
|
||||
table.setAuthor(developer.getStr("author"));
|
||||
table.setEmail(developer.getStr("email"));
|
||||
table.setTableName(tableName);
|
||||
table.setDsName(dsName);
|
||||
table.setTableComment(tableMetadata.getComment());
|
||||
table.setPackageName(project.getStr("packageName"));
|
||||
table.setVersion(project.getStr("version"));
|
||||
table.setBackendPath(project.getStr("backendPath"));
|
||||
table.setFrontendPath(project.getStr("frontendPath"));
|
||||
table.setAuthor(developer.getStr("author"));
|
||||
table.setEmail(developer.getStr("email"));
|
||||
table.setTableName(tableName);
|
||||
table.setDsName(dsName);
|
||||
table.setTableComment(tableMetadata.getComment());
|
||||
|
||||
table.setDbType(database.getDatabase().title());
|
||||
table.setFormLayout(FormLayoutEnum.TWO.getValue());
|
||||
table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.getValue());
|
||||
table.setClassName(NamingCase.toPascalCase(tableName));
|
||||
table.setModuleName(GenKit.getModuleName(table.getPackageName()));
|
||||
table.setFunctionName(GenKit.getFunctionName(tableName));
|
||||
table.setCreateTime(LocalDateTime.now());
|
||||
table.setDbType(database.getDatabase().title());
|
||||
table.setFormLayout(FormLayoutEnum.TWO.getValue());
|
||||
table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.getValue());
|
||||
table.setClassName(NamingCase.toPascalCase(tableName));
|
||||
// 模块名称默认为 admin
|
||||
table.setModuleName("admin");
|
||||
table.setFunctionName(GenKit.getFunctionName(tableName));
|
||||
table.setCreateTime(LocalDateTime.now());
|
||||
|
||||
// 使用默认数据源
|
||||
DynamicDataSourceContextHolder.clear();
|
||||
this.save(table);
|
||||
// 使用默认数据源
|
||||
DynamicDataSourceContextHolder.clear();
|
||||
this.save(table);
|
||||
|
||||
// 获取原生字段数据
|
||||
List<GenTableColumnEntity> tableFieldList = getGenTableColumnEntities(dsName, tableName, tableMetadata);
|
||||
// 获取原生字段数据
|
||||
List<GenTableColumnEntity> tableFieldList = getGenTableColumnEntities(dsName, tableName, tableMetadata);
|
||||
|
||||
// 初始化字段数据
|
||||
columnService.initFieldList(tableFieldList);
|
||||
// 保存列数据
|
||||
columnService.saveOrUpdateBatch(tableFieldList);
|
||||
// 初始化字段数据
|
||||
columnService.initFieldList(tableFieldList);
|
||||
// 保存列数据
|
||||
columnService.saveOrUpdateBatch(tableFieldList);
|
||||
|
||||
table.setFieldList(tableFieldList);
|
||||
return table;
|
||||
}
|
||||
table.setFieldList(tableFieldList);
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表字段信息
|
||||
*
|
||||
* @param dsName 数据源信息
|
||||
* @param tableName 表名称
|
||||
* @param tableMetadata 表的元数据
|
||||
* @return list
|
||||
*/
|
||||
private static @NotNull List<GenTableColumnEntity> getGenTableColumnEntities(String dsName, String tableName, Table tableMetadata) {
|
||||
List<GenTableColumnEntity> tableFieldList = new ArrayList<>();
|
||||
LinkedHashMap<String, Column> columns = tableMetadata.getColumns();
|
||||
columns.forEach((columnName, column) -> {
|
||||
GenTableColumnEntity genTableColumnEntity = new GenTableColumnEntity();
|
||||
genTableColumnEntity.setTableName(tableName);
|
||||
genTableColumnEntity.setDsName(dsName);
|
||||
genTableColumnEntity.setFieldName(column.getName());
|
||||
genTableColumnEntity.setFieldComment(column.getComment());
|
||||
genTableColumnEntity.setFieldType(column.getTypeName());
|
||||
genTableColumnEntity.setPrimaryPk(column.isPrimaryKey() == 1 ? BoolFillEnum.TRUE.getValue() : BoolFillEnum.FALSE.getValue());
|
||||
genTableColumnEntity.setAutoFill(AutoFillEnum.DEFAULT.name());
|
||||
genTableColumnEntity.setFormItem(BoolFillEnum.TRUE.getValue());
|
||||
genTableColumnEntity.setGridItem(BoolFillEnum.TRUE.getValue());
|
||||
/**
|
||||
* 获取表字段信息
|
||||
* @param dsName 数据源信息
|
||||
* @param tableName 表名称
|
||||
* @param tableMetadata 表的元数据
|
||||
* @return list
|
||||
*/
|
||||
private static @NotNull List<GenTableColumnEntity> getGenTableColumnEntities(String dsName, String tableName,
|
||||
Table tableMetadata) {
|
||||
List<GenTableColumnEntity> tableFieldList = new ArrayList<>();
|
||||
LinkedHashMap<String, Column> columns = tableMetadata.getColumns();
|
||||
columns.forEach((columnName, column) -> {
|
||||
GenTableColumnEntity genTableColumnEntity = new GenTableColumnEntity();
|
||||
genTableColumnEntity.setTableName(tableName);
|
||||
genTableColumnEntity.setDsName(dsName);
|
||||
genTableColumnEntity.setFieldName(column.getName());
|
||||
genTableColumnEntity.setFieldComment(column.getComment());
|
||||
genTableColumnEntity.setFieldType(column.getTypeName());
|
||||
genTableColumnEntity.setPrimaryPk(
|
||||
column.isPrimaryKey() == 1 ? BoolFillEnum.TRUE.getValue() : BoolFillEnum.FALSE.getValue());
|
||||
genTableColumnEntity.setAutoFill(AutoFillEnum.DEFAULT.name());
|
||||
genTableColumnEntity.setFormItem(BoolFillEnum.TRUE.getValue());
|
||||
genTableColumnEntity.setGridItem(BoolFillEnum.TRUE.getValue());
|
||||
|
||||
// 审计字段处理
|
||||
if (EnumUtil.contains(CommonColumnFiledEnum.class, column.getName())) {
|
||||
CommonColumnFiledEnum commonColumnFiledEnum = CommonColumnFiledEnum.valueOf(column.getName());
|
||||
genTableColumnEntity.setFormItem(commonColumnFiledEnum.getFormItem());
|
||||
genTableColumnEntity.setGridItem(commonColumnFiledEnum.getGridItem());
|
||||
genTableColumnEntity.setAutoFill(commonColumnFiledEnum.getAutoFill());
|
||||
genTableColumnEntity.setSort(commonColumnFiledEnum.getSort());
|
||||
}
|
||||
tableFieldList.add(genTableColumnEntity);
|
||||
});
|
||||
return tableFieldList;
|
||||
}
|
||||
// 审计字段处理
|
||||
if (EnumUtil.contains(CommonColumnFiledEnum.class, column.getName())) {
|
||||
CommonColumnFiledEnum commonColumnFiledEnum = CommonColumnFiledEnum.valueOf(column.getName());
|
||||
genTableColumnEntity.setFormItem(commonColumnFiledEnum.getFormItem());
|
||||
genTableColumnEntity.setGridItem(commonColumnFiledEnum.getGridItem());
|
||||
genTableColumnEntity.setAutoFill(commonColumnFiledEnum.getAutoFill());
|
||||
genTableColumnEntity.setSort(commonColumnFiledEnum.getSort());
|
||||
}
|
||||
tableFieldList.add(genTableColumnEntity);
|
||||
});
|
||||
return tableFieldList;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-3
@@ -22,6 +22,7 @@ import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.smallbun.screw.core.constant.DefaultConstants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenGroupEntity;
|
||||
@@ -54,8 +55,6 @@ import java.util.Set;
|
||||
public class GenTemplateServiceImpl extends ServiceImpl<GenTemplateMapper, GenTemplateEntity>
|
||||
implements GenTemplateService {
|
||||
|
||||
private static final String URL = "https://git.pig4cloud.com/pig/CGTM/raw/master";
|
||||
|
||||
private final GenTemplateGroupMapper genTemplateGroupMapper;
|
||||
|
||||
private final GenGroupMapper genGroupMapper;
|
||||
@@ -138,7 +137,7 @@ public class GenTemplateServiceImpl extends ServiceImpl<GenTemplateMapper, GenTe
|
||||
* @return {@link String }
|
||||
*/
|
||||
private String getCGTMFile(String fileName) {
|
||||
HttpResponse response = HttpRequest.get(String.format("%s/%s", URL, fileName))
|
||||
HttpResponse response = HttpRequest.get(String.format("%s/CGTM/raw/master/%s", DefaultConstants.CGTM_URL, fileName))
|
||||
.execute();
|
||||
|
||||
if (response.getStatus() == HttpStatus.HTTP_OK || StrUtil.isNotBlank(response.body())) {
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
"project": {
|
||||
"packageName": "com.pig4cloud.pigx",
|
||||
"version": "1.0.0",
|
||||
"backendPath": "admin",
|
||||
"backendPath": "pigx",
|
||||
"frontendPath": "pigx-ui"
|
||||
},
|
||||
"developer": {
|
||||
"author": "pig",
|
||||
"email": "sw@mail.pigx.vip"
|
||||
"email": "sw@pigx.vip"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user