|
@@ -1,6 +1,18 @@
|
|
|
package com.yaoyicloud.render;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
+import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
|
|
|
/**
|
|
|
* ServiceProviderInfo渲染器
|
|
@@ -14,21 +26,53 @@ public final class ServiceProviderInfoRender extends AbstractRender {
|
|
|
|
|
|
/**
|
|
|
* Docx 渲染
|
|
|
- *
|
|
|
+ *
|
|
|
* @param info 数据
|
|
|
* @param templateFileContent 模板内容
|
|
|
* @return 本地文件目录
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public String renderDocx(ServiceProviderInfo info, Byte[] templateFileContent) throws IOException {
|
|
|
- // json序列化 info
|
|
|
+ public String renderDocx(ServiceProviderInfo info, byte[] templateFileContent) throws IOException {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ SimpleModule module = new SimpleModule();
|
|
|
+ module.addSerializer(new ServiceProviderInfoSerializer(ServiceProviderInfo.class));
|
|
|
+ mapper.registerModule(module);
|
|
|
+
|
|
|
+ String jsonStr = mapper.writeValueAsString(info);
|
|
|
+
|
|
|
// 注: 报告模板的模板变量按照json序列化的结果命名
|
|
|
- // 用POI 渲染模板
|
|
|
- // 保存到 cwd 目录,文件名使用 ServiceProviderInfo + uuid
|
|
|
// 注: 目前的实现假设:一个session对应一个cwd目录
|
|
|
-
|
|
|
- this.docxResultPath = this.renderDocx(info.toString() /* Fake */, templateFileContent, cwd + "/1.docx");
|
|
|
+ this.docxResultPath =
|
|
|
+ this.renderDocx(jsonStr, templateFileContent, Paths.get(cwd, UUID.randomUUID().toString() + ".docx").toString());
|
|
|
return this.docxResultPath;
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * 通过序列化和反序列化
|
|
|
+ * 1. 默认显示规则的数据转换
|
|
|
+ * 2. 避免TL处理模板面对复杂数据类型
|
|
|
+ */
|
|
|
+ public class ServiceProviderInfoSerializer extends StdSerializer<ServiceProviderInfo> {
|
|
|
+
|
|
|
+ public ServiceProviderInfoSerializer() {
|
|
|
+ this(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ServiceProviderInfoSerializer(Class<ServiceProviderInfo> t) {
|
|
|
+ super(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serialize(ServiceProviderInfo value, JsonGenerator jgen, SerializerProvider provider)
|
|
|
+ throws IOException, JsonProcessingException {
|
|
|
+
|
|
|
+ jgen.writeStartObject();
|
|
|
+ jgen.writeStringField("name", value.getName() == null ? "-" : value.getName());
|
|
|
+ jgen.writeStringField("type", value.getType() == null ? "-" : value.getType());
|
|
|
+ jgen.writeStringField("reportDate", value.getReportDate() == null ? ""
|
|
|
+ : value.getReportDate().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
|
|
|
+ jgen.writeStringField("tenantName", value.getTenantName());
|
|
|
+ jgen.writeEndObject();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|