|
@@ -10,6 +10,7 @@ import com.aizuda.boot.modules.gen.service.IGenTemplateService;
|
|
|
import com.aizuda.common.toolkit.CollectionUtils;
|
|
|
import com.aizuda.core.api.ApiAssert;
|
|
|
import com.aizuda.core.bean.BaseEntity;
|
|
|
+import com.aizuda.core.exception.ApiException;
|
|
|
import com.aizuda.service.web.UserSession;
|
|
|
import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
@@ -50,14 +51,18 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
|
|
|
|
@Override
|
|
|
public List<GenVO> preview(GenDTO dto) {
|
|
|
- List<GenTemplate> genTemplates = genTemplateService.listByIds(dto.getTemplateIds());
|
|
|
- ApiAssert.fail(CollectionUtils.isEmpty(genTemplates), "请选择生成模板");
|
|
|
+ List<GenTemplate> genTemplates = genTemplateService.listCheckByIds(dto.getTemplateIds());
|
|
|
|
|
|
- // 生成预览内容
|
|
|
+ // 初始化数据连接配置
|
|
|
ConfigBuilder configBuilder = this.buildConfigBuilder(dto);
|
|
|
- TableInfo tableInfo = this.buildConfigBuilder(dto).getTableInfoList().get(0);
|
|
|
- VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
|
|
|
- templateEngine.init(configBuilder);
|
|
|
+
|
|
|
+ // 查询指定表信息
|
|
|
+ List<TableInfo> tableInfos = configBuilder.getTableInfoList();
|
|
|
+ ApiAssert.fail(CollectionUtils.isEmpty(tableInfos), "未找到指定表信息");
|
|
|
+ TableInfo tableInfo = tableInfos.get(0);
|
|
|
+
|
|
|
+ // 初始化模板引擎
|
|
|
+ VelocityTemplateEngine templateEngine = new VelocityTemplateEngine().init(configBuilder);
|
|
|
return genTemplates.stream().map(t -> {
|
|
|
GenVO vo = new GenVO();
|
|
|
try {
|
|
@@ -75,35 +80,44 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
|
|
|
|
@Override
|
|
|
public void download(HttpServletResponse response, GenDTO dto) {
|
|
|
- List<GenTemplate> genTemplates = genTemplateService.listByIds(dto.getTemplateIds());
|
|
|
- ApiAssert.fail(CollectionUtils.isEmpty(genTemplates), "请选择生成模板");
|
|
|
+ List<GenTemplate> genTemplates = genTemplateService.listCheckByIds(dto.getTemplateIds());
|
|
|
|
|
|
- // 生成预览内容
|
|
|
+ // 初始化数据连接配置
|
|
|
ConfigBuilder configBuilder = this.buildConfigBuilder(dto);
|
|
|
- TableInfo tableInfo = this.buildConfigBuilder(dto).getTableInfoList().get(0);
|
|
|
- VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
|
|
|
- templateEngine.init(configBuilder);
|
|
|
+
|
|
|
+ // 查询指定表信息
|
|
|
+ List<TableInfo> tableInfos = configBuilder.getTableInfoList();
|
|
|
+
|
|
|
+ // 初始化模板引擎
|
|
|
+ VelocityTemplateEngine templateEngine = new VelocityTemplateEngine().init(configBuilder);
|
|
|
|
|
|
// 设置响应内容类型
|
|
|
+ String fileName = "genCode";
|
|
|
+ if (tableInfos.size() < 2) {
|
|
|
+ fileName = dto.getTableName().replaceAll(",", "_");
|
|
|
+ }
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".zip\"");
|
|
|
response.setContentType("application/zip");
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=\"generatedCode.zip\"");
|
|
|
+
|
|
|
// 创建ZIP输出流
|
|
|
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
|
|
|
- // 渲染模板
|
|
|
- for (GenTemplate gt : genTemplates) {
|
|
|
- // 创建输出流
|
|
|
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
- Map<String, Object> objectMap = this.getObjectMap(configBuilder, tableInfo);
|
|
|
- String context = templateEngine.writer(objectMap, tableInfo.getEntityName(), gt.getTplContent());
|
|
|
- byteArrayOutputStream.write(context.getBytes());
|
|
|
-
|
|
|
- // 创建文件内容
|
|
|
- String packageName = configBuilder.getPackageConfig().getParent();
|
|
|
- ZipEntry zipEntry = new ZipEntry(packageName.replace('.', '/') + "/" +
|
|
|
- String.format(gt.getOutFile(), tableInfo.getEntityName()));
|
|
|
- zos.putNextEntry(zipEntry);
|
|
|
- zos.write(byteArrayOutputStream.toByteArray());
|
|
|
- zos.closeEntry();
|
|
|
+ for (TableInfo ti : tableInfos) {
|
|
|
+ // 渲染模板
|
|
|
+ for (GenTemplate gt : genTemplates) {
|
|
|
+ // 创建输出流
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ Map<String, Object> objectMap = this.getObjectMap(configBuilder, ti);
|
|
|
+ String context = templateEngine.writer(objectMap, ti.getEntityName(), gt.getTplContent());
|
|
|
+ byteArrayOutputStream.write(context.getBytes());
|
|
|
+
|
|
|
+ // 创建文件内容
|
|
|
+ String packageName = configBuilder.getPackageConfig().getParent();
|
|
|
+ ZipEntry zipEntry = new ZipEntry(packageName.replace('.', '/') + "/" +
|
|
|
+ String.format(gt.getOutFile(), ti.getEntityName()));
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+ zos.write(byteArrayOutputStream.toByteArray());
|
|
|
+ zos.closeEntry();
|
|
|
+ }
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
ApiAssert.fail("生成文件异常");
|
|
@@ -113,8 +127,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
|
private ConfigBuilder buildConfigBuilder(GenDTO dto) {
|
|
|
DataSourceConfig.Builder dataSource;
|
|
|
if (null != dto.getDatabaseId()) {
|
|
|
- GenDatabase gb = genDatabaseService.getById(dto.getDatabaseId());
|
|
|
- ApiAssert.fail(null == gb, "指定数据源不存在");
|
|
|
+ GenDatabase gb = genDatabaseService.checkById(dto.getDatabaseId());
|
|
|
dataSource = new DataSourceConfig.Builder(gb.url(), gb.getUsername(), gb.getPassword());
|
|
|
} else {
|
|
|
dataSource = new DataSourceConfig.Builder(dsp.getUrl(), dsp.getUsername(), dsp.getPassword());
|
|
@@ -146,11 +159,15 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
|
"update_by", "update_time", "deleted")
|
|
|
.logicDeleteColumnName("deleted")
|
|
|
.superClass(BaseEntity.class);
|
|
|
- // 注入配置
|
|
|
- InjectionConfig.Builder injection = new InjectionConfig.Builder();
|
|
|
- // 模板渲染引擎 new CodeTemplateEngine()
|
|
|
- return new ConfigBuilder(packageInfo.build(), dataSource.build(), strategy.build(), null,
|
|
|
- globalConfig.build(), injection.build());
|
|
|
+ try {
|
|
|
+ // 注入配置
|
|
|
+ InjectionConfig.Builder injection = new InjectionConfig.Builder();
|
|
|
+ // 模板渲染引擎 new CodeTemplateEngine()
|
|
|
+ return new ConfigBuilder(packageInfo.build(), dataSource.build(), strategy.build(), null,
|
|
|
+ globalConfig.build(), injection.build());
|
|
|
+ } catch (Throwable t) {
|
|
|
+ throw new ApiException("数据库连接初始化失败");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> getObjectMap(ConfigBuilder config, TableInfo tableInfo) {
|
|
@@ -235,7 +252,7 @@ public class GenTableServiceImpl implements IGenTableService {
|
|
|
customMap = new HashMap<>();
|
|
|
}
|
|
|
String va = annotations.toString();
|
|
|
- if (org.apache.commons.lang3.StringUtils.isNotBlank(va)) {
|
|
|
+ if (StringUtils.isNotBlank(va)) {
|
|
|
va += lnt;
|
|
|
tableInfo.addImportPackages("com.aizuda.core.validation.Create");
|
|
|
} else {
|