Compare commits
5 Commits
293814f09b
...
d5e235cb94
Author | SHA1 | Date |
---|---|---|
王运杰 | d5e235cb94 | 11 months ago |
王运杰 | 9a02ee9aca | 11 months ago |
hypaas | 442ccb617f | 11 months ago |
hypaas | ef44e9cf93 | 11 months ago |
hypaas | cb4b2b060d | 11 months ago |
17 changed files with 533 additions and 70 deletions
@ -1,10 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<domain-models xmlns="http://hypaas.com/xml/ns/domain-models" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hypaas.com/xml/ns/domain-models http://hypaas.com/xml/ns/domain-models/domain-models_5.2.xsd"> |
||||
<module name="export-entity" package="com.hypass.export"/> |
||||
<module name="export" package="com.hypass.export"/> |
||||
<entity hashAll="false" logUpdates="true" persistable="true" name="ExportTemplate" cacheable="false" lang="java" strategy="SINGLE"> |
||||
<string actOverwrite="false" title="模板编码" required="true" copy="true" massUpdate="false" name="code" isJudg="false"/> |
||||
<string actOverwrite="false" title="模板名称" copy="true" massUpdate="false" name="name" isJudg="false"/> |
||||
<many-to-one title="实体名称" ref="com.hypaas.meta.db.MetaModel" unique="true" name="entityName" copy="true"/> |
||||
<string actOverwrite="false" title="模板编码" required="true" copy="true" unique="true" name="code" isJudg="false"/> |
||||
<string actOverwrite="false" title="模板名称" copy="true" unique="true" name="name" isJudg="false"/> |
||||
<many-to-one title="实体名称" massUpdate="false" ref="com.hypaas.meta.db.MetaModel" unique="false" name="entityName" copy="true"/> |
||||
<enum title="是否默认模板" massUpdate="false" ref="com.export.enums.CommonBoolean" name="isDefaultTemplate" copy="true"/> |
||||
</entity> |
||||
</domain-models> |
||||
|
@ -0,0 +1,31 @@
|
||||
package com.hypass.export.controller; |
||||
|
||||
import com.hypass.export.ExportTemplate; |
||||
import java.lang.reflect.Field; |
||||
|
||||
/** @ClassName Child @Description TODO @Author wyj @Date 2023/12/6 13:48 @Version 1.0 */ |
||||
public class Child extends Parent { |
||||
public String childField = "childField"; |
||||
|
||||
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { |
||||
// 获取 Child 类的对象
|
||||
ExportTemplate exportTemplate = new ExportTemplate(); |
||||
Field field = getDeclaredField(exportTemplate, "updatedBy"); |
||||
System.out.println(field.getName()); |
||||
} |
||||
|
||||
public static Field getDeclaredField(Object object, String fieldName) { |
||||
Field field = null; |
||||
Class<?> clazz = object.getClass(); |
||||
for (; clazz != Object.class; clazz = clazz.getSuperclass()) { |
||||
try { |
||||
field = clazz.getDeclaredField(fieldName); |
||||
return field; |
||||
} catch (NoSuchFieldException e) { |
||||
// 不需要处理
|
||||
// 不断向父类查询是否有某个字段
|
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,223 @@
|
||||
package com.hypass.export.controller; |
||||
|
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.export.enums.CommonBoolean; |
||||
import com.google.inject.Inject; |
||||
import com.hypaas.db.Model; |
||||
import com.hypaas.db.Query; |
||||
import com.hypaas.meta.MetaFiles; |
||||
import com.hypaas.meta.db.MetaFile; |
||||
import com.hypaas.meta.db.MetaModel; |
||||
import com.hypaas.meta.db.repo.MetaModelRepository; |
||||
import com.hypaas.meta.schema.actions.ActionView; |
||||
import com.hypaas.rpc.ActionRequest; |
||||
import com.hypaas.rpc.ActionResponse; |
||||
import com.hypass.export.ExportStrategy; |
||||
import com.hypass.export.ExportTemplate; |
||||
import com.hypass.export.repo.ExportStrategyRepository; |
||||
import com.hypass.export.repo.ExportTemplateRepository; |
||||
import java.io.*; |
||||
import java.lang.reflect.Field; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
import org.apache.commons.collections.CollectionUtils; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
|
||||
public class ExportActionController { |
||||
|
||||
@Inject private ExportTemplateRepository exportTemplateRepository; |
||||
|
||||
@Inject private MetaModelRepository metaModelRepository; |
||||
|
||||
@Inject private ExportStrategyRepository exportStrategyRepository; |
||||
|
||||
@Inject private MetaFiles metaFiles; |
||||
|
||||
public void export(ActionRequest request, ActionResponse response) { |
||||
// 获取model
|
||||
String model = request.getModel(); |
||||
MetaModel entity = |
||||
Query.of(MetaModel.class) |
||||
.filter("self.fullName = :fullName") |
||||
.bind("fullName", model) |
||||
.fetchOne(); |
||||
// 根据model查询对应的模版
|
||||
ExportTemplate exportTemplate = |
||||
Query.of(ExportTemplate.class) |
||||
.filter("self.entityName = :entityName") |
||||
.bind("entityName", entity) |
||||
.fetchOne(); |
||||
if (exportTemplate == null) { |
||||
response.setAlert("该实体模型没有配置导出模版"); |
||||
return; |
||||
} |
||||
// 解析策略
|
||||
List<ExportStrategy> exportStrategys = |
||||
Query.of(ExportStrategy.class) |
||||
.filter("self.templateInfo = :templateInfo") |
||||
.bind("templateInfo", exportTemplate) |
||||
.fetch(); |
||||
if (CollectionUtils.isEmpty(exportStrategys)) { |
||||
response.setAlert("该实体模型没有配置导出模版策略"); |
||||
return; |
||||
} |
||||
// 设置动态头
|
||||
List<List<String>> headList = new ArrayList<>(); |
||||
exportStrategys.forEach( |
||||
exportStrategy -> { |
||||
if (CommonBoolean.IS.equals(exportStrategy.getIsExport())) { |
||||
List<String> head = new ArrayList<>(); |
||||
if ("ManyToOne".equals(exportStrategy.getFieldAttr())) { |
||||
String exportContent = exportStrategy.getExportContent(); |
||||
if (StringUtils.isEmpty(exportContent)) { |
||||
return; |
||||
} |
||||
String[] split = exportContent.split(","); |
||||
for (String s : split) { |
||||
head.add(s); |
||||
headList.add(head); |
||||
} |
||||
} else { |
||||
head.add(exportStrategy.getExportFiledName()); |
||||
headList.add(head); |
||||
} |
||||
} |
||||
}); |
||||
String fullName = entity.getFullName(); |
||||
final Class<Model> modelClass; |
||||
try { |
||||
modelClass = (Class<Model>) Class.forName(fullName); |
||||
} catch (ClassNotFoundException e) { |
||||
throw new IllegalArgumentException("No such model found."); |
||||
} |
||||
Query<Model> of = Query.of(modelClass); |
||||
List<Model> modelDatas = of.fetch(); |
||||
// Dynamic data retrieval and processing
|
||||
List<List<Object>> dataList = |
||||
modelDatas.stream() |
||||
.map( |
||||
data -> { |
||||
List<Object> rowData = new ArrayList<>(); |
||||
for (ExportStrategy strategy : exportStrategys) { |
||||
if (CommonBoolean.NOT.equals(strategy.getIsExport())) continue; |
||||
if (StringUtils.isEmpty(strategy.getExportFiledName())) { |
||||
strategy.setExportFiledName(strategy.getFieldName()); |
||||
} |
||||
Field field = null; |
||||
try { |
||||
field = getDeclaredField(modelClass, strategy.getFieldName()); |
||||
field.setAccessible(true); |
||||
if (processManyToOne(data, rowData, strategy, field)) continue; |
||||
} catch (NoSuchFieldException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
Object value = null; // Handle null values if needed
|
||||
try { |
||||
value = field.get(data); |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
if (value != null && value.getClass().isEnum()) { |
||||
String name = ((Enum<?>) value).name(); |
||||
rowData.add(name); |
||||
} else { |
||||
rowData.add(value); |
||||
} |
||||
} |
||||
return rowData; |
||||
}) |
||||
.collect(Collectors.toList()); |
||||
|
||||
// 创建 OutputStream 对象
|
||||
// 创建临时目录
|
||||
File tempDir = new File(System.getProperty("user.dir"), "temp"); |
||||
if (!tempDir.exists()) { |
||||
tempDir.mkdirs(); |
||||
} |
||||
String fileName = "excel.xlsx"; |
||||
File file = new File(tempDir, fileName); |
||||
// dataList.forEach(data->{
|
||||
// data.forEach(d -> {
|
||||
// System.out.println(d.getClass().getTypeName());
|
||||
// });
|
||||
// });
|
||||
EasyExcel.write(file).head(headList).sheet("实体信息").doWrite(dataList); |
||||
MetaFile upload = null; |
||||
try { |
||||
upload = metaFiles.upload(file); |
||||
} catch (IOException e) { |
||||
tempDir.delete(); |
||||
throw new RuntimeException(e); |
||||
} |
||||
if (upload != null) { |
||||
response.setView( |
||||
ActionView.define("文件导出") |
||||
.add( |
||||
"html", |
||||
"ws/rest/com.hypaas.meta.db.MetaFile/" |
||||
+ upload.getId() |
||||
+ "/content/download?v=" |
||||
+ upload.getVersion()) |
||||
.param("download", "true") |
||||
.map()); |
||||
} |
||||
tempDir.delete(); |
||||
} |
||||
|
||||
private static boolean processManyToOne( |
||||
Model data, List<Object> rowData, ExportStrategy strategy, Field field) |
||||
throws NoSuchFieldException { |
||||
if ("ManyToOne".equals(strategy.getFieldAttr())) { |
||||
String exportContent = strategy.getExportContent(); |
||||
if (StringUtils.isEmpty(exportContent)) return true; |
||||
String[] split = exportContent.split(","); |
||||
for (String s : split) { |
||||
Field subField = getDeclaredField((Class<Model>) field.getType(), s); |
||||
subField.setAccessible(true); |
||||
Object value = null; // Handle null values if needed
|
||||
try { |
||||
Field declaredField = getDeclaredField((Class<Model>) data.getClass(), field.getName()); |
||||
declaredField.setAccessible(true); |
||||
Object o = declaredField.get(data); |
||||
value = subField.get(o); |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
rowData.add(value); |
||||
} |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static Field getDeclaredField(Class<Model> clazz, String fieldName) { |
||||
Field field = null; |
||||
try { |
||||
field = clazz.getDeclaredField(fieldName); |
||||
} catch (NoSuchFieldException e) { |
||||
for (; clazz != Model.class; clazz = (Class<Model>) clazz.getSuperclass()) { |
||||
if (clazz != null) { |
||||
try { |
||||
field = clazz.getDeclaredField(fieldName); |
||||
} catch (NoSuchFieldException ex) { |
||||
continue; |
||||
} |
||||
if (field != null) return field; |
||||
} |
||||
} |
||||
throw new RuntimeException(e); |
||||
} |
||||
return field; |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
MetaModel metaModel = new MetaModel(); |
||||
try { |
||||
Field fullName = metaModel.getClass().getDeclaredField("fullName"); |
||||
System.out.println(111); |
||||
} catch (NoSuchFieldException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,5 @@
|
||||
package com.hypass.export.controller; |
||||
|
||||
public class Parent { |
||||
public String parentField = "parentField"; |
||||
} |
@ -0,0 +1,79 @@
|
||||
package com.hypass.export.model; |
||||
|
||||
import com.hypaas.meta.db.MetaModel; |
||||
import java.util.List; |
||||
|
||||
public class ExportTemplateDetailDTO { |
||||
|
||||
private TemplateBaseInfoDTO templateInfo; |
||||
|
||||
private List<ExportStrategyDTO> strategyList; |
||||
|
||||
public static class TemplateBaseInfoDTO { |
||||
private Long id; |
||||
|
||||
private String code; |
||||
|
||||
private String name; |
||||
|
||||
private MetaModel entityName; |
||||
|
||||
private String isDefaultTemplate; |
||||
|
||||
public Long getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(Long id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getCode() { |
||||
return code; |
||||
} |
||||
|
||||
public void setCode(String code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public MetaModel getEntityName() { |
||||
return entityName; |
||||
} |
||||
|
||||
public void setEntityName(MetaModel entityName) { |
||||
this.entityName = entityName; |
||||
} |
||||
|
||||
public String getIsDefaultTemplate() { |
||||
return isDefaultTemplate; |
||||
} |
||||
|
||||
public void setIsDefaultTemplate(String isDefaultTemplate) { |
||||
this.isDefaultTemplate = isDefaultTemplate; |
||||
} |
||||
} |
||||
|
||||
public TemplateBaseInfoDTO getTemplateInfo() { |
||||
return templateInfo; |
||||
} |
||||
|
||||
public void setTemplateInfo(TemplateBaseInfoDTO templateInfo) { |
||||
this.templateInfo = templateInfo; |
||||
} |
||||
|
||||
public List<ExportStrategyDTO> getStrategyList() { |
||||
return strategyList; |
||||
} |
||||
|
||||
public void setStrategyList(List<ExportStrategyDTO> strategyList) { |
||||
this.strategyList = strategyList; |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
package com.hypass.export.model; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @ClassName SaveExportStrategyDTO @Description TODO @Author wyj @Date 2023/12/2 15:41 @Version 1.0 |
||||
*/ |
||||
public class SaveExportStrategyDTO implements Serializable { |
||||
|
||||
private String templateId; |
||||
|
||||
private List<ExportStrategyDTO> exportStrategyDTOs; |
||||
|
||||
public String getTemplateId() { |
||||
return templateId; |
||||
} |
||||
|
||||
public void setTemplateId(String templateId) { |
||||
this.templateId = templateId; |
||||
} |
||||
|
||||
public List<ExportStrategyDTO> getExportStrategyDTOs() { |
||||
return exportStrategyDTOs; |
||||
} |
||||
|
||||
public void setExportStrategyDTOs(List<ExportStrategyDTO> exportStrategyDTOs) { |
||||
this.exportStrategyDTOs = exportStrategyDTOs; |
||||
} |
||||
} |
@ -1,3 +1,4 @@
|
||||
interfaceList: |
||||
- "/ws/register/register/face" |
||||
- "/ws/register/login/face" |
||||
- "/ws/export/**" |
Loading…
Reference in new issue