|
@@ -0,0 +1,87 @@
|
|
|
+package com.yaoyicloud.render;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import com.deepoove.poi.config.Configure;
|
|
|
+import com.deepoove.poi.config.ConfigureBuilder;
|
|
|
+import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * AuditResult渲染器
|
|
|
+ *
|
|
|
+ */
|
|
|
+public final class AuditResultRender extends AbstractRender {
|
|
|
+
|
|
|
+ public AuditResultRender(String cwd) {
|
|
|
+ super(cwd);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Docx 渲染
|
|
|
+ *
|
|
|
+ * @param info 数据
|
|
|
+ * @param templateFileContent 模板内容
|
|
|
+ * @return 本地文件目录
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public String renderDocx(AuditResult info, byte[] templateFileContent) throws IOException {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ SimpleModule module = new SimpleModule();
|
|
|
+ module.addSerializer(new AuditResultSerializer(AuditResult.class));
|
|
|
+ mapper.registerModule(module);
|
|
|
+
|
|
|
+ String jsonStr = mapper.writeValueAsString(info);
|
|
|
+
|
|
|
+ // 注: 报告模板的模板变量按照json序列化的结果命名
|
|
|
+ // 注: 目前的实现假设:一个session对应一个cwd目录
|
|
|
+ ConfigureBuilder builder = Configure.builder();
|
|
|
+ builder.bind("checkItemScores", new LoopRowTableRenderPolicy());
|
|
|
+ this.docxResultPath = this.renderDocx(jsonStr, templateFileContent, builder,
|
|
|
+ Paths.get(cwd, UUID.randomUUID().toString() + ".docx").toString());
|
|
|
+ return this.docxResultPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 通过序列化和反序列化
|
|
|
+ * 1. 默认显示规则的数据转换
|
|
|
+ * 2. 避免TL处理模板面对复杂数据类型
|
|
|
+ */
|
|
|
+ public class AuditResultSerializer extends StdSerializer<AuditResult> {
|
|
|
+
|
|
|
+ public AuditResultSerializer() {
|
|
|
+ this(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public AuditResultSerializer(Class<AuditResult> t) {
|
|
|
+ super(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serialize(AuditResult value, JsonGenerator jgen, SerializerProvider provider)
|
|
|
+ throws IOException, JsonProcessingException {
|
|
|
+
|
|
|
+ jgen.writeStartObject();
|
|
|
+ jgen.writeStringField("auditResultScore", value.getAuditResultScore());
|
|
|
+ jgen.writeStringField("opinion", value.getOpinion() == null ? "-" : value.getOpinion());
|
|
|
+ jgen.writeStringField("serviceProviderName", value.getServiceProviderName());
|
|
|
+ // Note: ignore reportVersion
|
|
|
+ jgen.writeStringField("auditDate", value.getAuditDate() == null ? ""
|
|
|
+ : value.getAuditDate().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
|
|
|
+ jgen.writeStringField("riskAlert", value.getRiskAlert() == null ? "-" : value.getRiskAlert());
|
|
|
+ jgen.writeStringField("auditResultSuggestion", value.getAuditResultSuggestion() == null ? "-" : value.getAuditResultSuggestion());
|
|
|
+ jgen.writeObjectField("checkItemScores", value.getCheckItemScores());
|
|
|
+ jgen.writeEndObject();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|