|
@@ -0,0 +1,230 @@
|
|
|
|
+package com.yaoyicloud.render.platform.update;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
+
|
|
|
|
+import com.deepoove.poi.config.Configure;
|
|
|
|
+import com.deepoove.poi.config.ConfigureBuilder;
|
|
|
|
+import com.deepoove.poi.policy.RenderPolicy;
|
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
|
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
|
|
+import com.google.protobuf.InvalidProtocolBufferException;
|
|
|
|
+import com.google.protobuf.MessageOrBuilder;
|
|
|
|
+import com.google.protobuf.util.JsonFormat;
|
|
|
|
+import com.yaoyicloud.config.FilerepoProperties;
|
|
|
|
+import com.yaoyicloud.message.FxyProtos.PublicRecord;
|
|
|
|
+import com.yaoyicloud.render.AbstractNewRender;
|
|
|
|
+
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * PublicRecord渲染器
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+public final class TaxNewRender extends AbstractNewRender {
|
|
|
|
+ private final FilerepoProperties filerepoProperties;
|
|
|
|
+
|
|
|
|
+ public TaxNewRender(String cwd, FilerepoProperties filerepoProperties) {
|
|
|
|
+ super(cwd);
|
|
|
|
+ this.filerepoProperties = filerepoProperties;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected String getBasicPath() throws IOException {
|
|
|
|
+ return filerepoProperties.getBasePath();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected String getReportImagePath() {
|
|
|
|
+ return filerepoProperties.getReportImagePath();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Docx 渲染
|
|
|
|
+ *
|
|
|
|
+ * @param info 数据
|
|
|
|
+ * @param templateFileContent 模板内容
|
|
|
|
+ * @return 本地文件目录
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ public String renderDocx(String info, Map<String, Object> addtionalMap, byte[] templateFileContent,
|
|
|
|
+ String relationId) throws IOException {
|
|
|
|
+ log.info("开始渲染司法风险模块,relationId: {}", relationId);
|
|
|
|
+
|
|
|
|
+ // 配置POI-TL渲染器
|
|
|
|
+ ConfigureBuilder builder = Configure.builder();
|
|
|
|
+ RenderPolicy indicatorsRenderPolicy = this.indicatorsRenderPolicyToProtobuf();
|
|
|
|
+
|
|
|
|
+ builder.bind("dishonestPersons", indicatorsRenderPolicy);
|
|
|
|
+ builder.bind("judicialRiskChecks", indicatorsRenderPolicy);
|
|
|
|
+
|
|
|
|
+ builder.bind("severeViolations", indicatorsRenderPolicy);
|
|
|
|
+ builder.useSpringEL();
|
|
|
|
+
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ Map<String, Object> data = objectMapper.readValue(info, new TypeReference<Map<String, Object>>() {});
|
|
|
|
+ data.replaceAll((k, v) -> v.equals("") ? "-" : v);
|
|
|
|
+
|
|
|
|
+ if (addtionalMap != null) {
|
|
|
|
+ data.putAll(addtionalMap);
|
|
|
|
+ }
|
|
|
|
+ fillBasicDefaultValues(data);
|
|
|
|
+
|
|
|
|
+ Map<String, Object> publicRecordSummary = (Map<String, Object>) data.get("publicRecordSummary");
|
|
|
|
+publicRecordSummary.putIfAbsent("riskSummary", "-");
|
|
|
|
+publicRecordSummary.putIfAbsent("suggestion", "");
|
|
|
|
+ try {
|
|
|
|
+ // 渲染文档
|
|
|
|
+ String resultPath =
|
|
|
|
+ this.renderDocx(data, templateFileContent, builder, relationId, "publicRecord", publicRecordSummary);
|
|
|
|
+ log.info("渲染司法风险模块成功,文件路径: {}", resultPath);
|
|
|
|
+ return resultPath;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("渲染司法风险模块失败,relationId: {}", relationId, e);
|
|
|
|
+ throw new IOException("文档渲染失败", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 填充默认值,确保所有必要字段都存在
|
|
|
|
+ */
|
|
|
|
+ private void fillBasicDefaultValues(Map<String, Object> data) {
|
|
|
|
+
|
|
|
|
+ List<Map<String, Object>> dishonestPersons = (List<Map<String, Object>>) data.get("dishonestPersons");
|
|
|
|
+ if (CollectionUtils.isEmpty(dishonestPersons)) {
|
|
|
|
+ Map<String, Object> defaultDishonestPerson = new HashMap<>();
|
|
|
|
+ defaultDishonestPerson.put("id", "1");
|
|
|
|
+ List<Map<String, Object>> defaultDishonestPersonList = Collections.singletonList(defaultDishonestPerson);
|
|
|
|
+ data.put("dishonestPersons", defaultDishonestPersonList);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<Map<String, Object>> severeViolations = (List<Map<String, Object>>) data.get("severeViolations");
|
|
|
|
+ if (CollectionUtils.isEmpty(severeViolations)) {
|
|
|
|
+ Map<String, Object> defaultSevereViolation = new HashMap<>();
|
|
|
|
+ defaultSevereViolation.put("id", "1");
|
|
|
|
+ List<Map<String, Object>> defaultSevereViolationList = Collections.singletonList(defaultSevereViolation);
|
|
|
|
+ data.put("severeViolations", defaultSevereViolationList);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // /**
|
|
|
|
+ // * Docx 渲染
|
|
|
|
+ // *
|
|
|
|
+ // * @param info 数据
|
|
|
|
+ // * @param templateFileContent 模板内容
|
|
|
|
+ // * @return 本地文件目录
|
|
|
|
+ // * @throws IOException
|
|
|
|
+ // */
|
|
|
|
+ // public String renderDocx(PublicRecord info, Map<String, Object> addtionalMap, byte[] templateFileContent) throws IOException {
|
|
|
|
+ // // TODO: let mapper be package static
|
|
|
|
+ // ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ // SimpleModule module = new SimpleModule();
|
|
|
|
+ // module.addSerializer(new PublicRecordSerializer(PublicRecord.class));
|
|
|
|
+ // mapper.registerModule(module);
|
|
|
|
+ //
|
|
|
|
+ // String jsonStr = mapper.writeValueAsString(info);
|
|
|
|
+ //
|
|
|
|
+ // // 注: 报告模板的模板变量按照json序列化的结果命名
|
|
|
|
+ // // 注: 目前的实现假设:一个session对应一个cwd目录
|
|
|
|
+ // ConfigureBuilder builder = Configure.builder();
|
|
|
|
+ // builder.bind("dishonestPersons", new LoopRowTableRenderPolicy());
|
|
|
|
+ // builder.bind("businessAbnormals", new LoopRowTableRenderPolicy());
|
|
|
|
+ // builder.bind("penaltyRecords", new LoopRowTableRenderPolicy());
|
|
|
|
+ // builder.bind("taxPenalties", new LoopRowTableRenderPolicy());
|
|
|
|
+ // builder.bind("severeViolations", new LoopRowTableRenderPolicy());
|
|
|
|
+ // this.docxResultPath =
|
|
|
|
+ // this.renderDocx(jsonStr, addtionalMap, templateFileContent, builder,
|
|
|
|
+ // Paths.get(cwd, UUID.randomUUID().toString() + ".docx").toString());
|
|
|
|
+ // return this.docxResultPath;
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * 通过序列化和反序列化
|
|
|
|
+ * 1. 进行默认显示规则的数据转换
|
|
|
|
+ * 2. 避免POI-TL处理模板面对复杂数据类型
|
|
|
|
+ */
|
|
|
|
+ public class PublicRecordSerializer extends StdSerializer<PublicRecord> {
|
|
|
|
+
|
|
|
|
+ public PublicRecordSerializer() {
|
|
|
|
+ this(null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public PublicRecordSerializer(Class<PublicRecord> t) {
|
|
|
|
+ super(t);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 为了增加*Count的数值,定制了序列化方法。 TODO: 可能更简单的方法是使用POI-TL的Spring表达式能力
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void serialize(PublicRecord value, JsonGenerator jgen, SerializerProvider provider)
|
|
|
|
+ throws IOException, JsonProcessingException {
|
|
|
|
+
|
|
|
|
+ jgen.writeStartObject();
|
|
|
|
+
|
|
|
|
+ jgen.writeArrayFieldStart("dishonestPersons");
|
|
|
|
+ writeRawArray(
|
|
|
|
+ value.getDishonestPersonsList().stream().map(o -> (MessageOrBuilder) o).collect(Collectors.toList()),
|
|
|
|
+ jgen);
|
|
|
|
+ jgen.writeEndArray();
|
|
|
|
+ jgen.writeNumberField("dishonestPersonsCount", value.getDishonestPersonsCount());
|
|
|
|
+
|
|
|
|
+ jgen.writeArrayFieldStart("businessAbnormals");
|
|
|
|
+ writeRawArray(
|
|
|
|
+ value.getBusinessAbnormalsList().stream().map(o -> (MessageOrBuilder) o).collect(Collectors.toList()),
|
|
|
|
+ jgen);
|
|
|
|
+ jgen.writeEndArray();
|
|
|
|
+ jgen.writeNumberField("businessAbnormalsCount", value.getBusinessAbnormalsCount());
|
|
|
|
+
|
|
|
|
+ jgen.writeArrayFieldStart("penaltyRecords");
|
|
|
|
+ writeRawArray(
|
|
|
|
+ value.getPenaltyRecordsList().stream().map(o -> (MessageOrBuilder) o).collect(Collectors.toList()),
|
|
|
|
+ jgen);
|
|
|
|
+ jgen.writeEndArray();
|
|
|
|
+ jgen.writeNumberField("penaltyRecordsCount", value.getPenaltyRecordsCount());
|
|
|
|
+
|
|
|
|
+ jgen.writeArrayFieldStart("taxPenalties");
|
|
|
|
+ writeRawArray(
|
|
|
|
+ value.getTaxPenaltiesList().stream().map(o -> (MessageOrBuilder) o).collect(Collectors.toList()), jgen);
|
|
|
|
+ jgen.writeEndArray();
|
|
|
|
+ jgen.writeNumberField("taxPenaltiesCount", value.getTaxPenaltiesCount());
|
|
|
|
+
|
|
|
|
+ jgen.writeArrayFieldStart("severeViolations");
|
|
|
|
+ writeRawArray(
|
|
|
|
+ value.getSevereViolationsList().stream().map(o -> (MessageOrBuilder) o).collect(Collectors.toList()),
|
|
|
|
+ jgen);
|
|
|
|
+ jgen.writeEndArray();
|
|
|
|
+ jgen.writeNumberField("severeViolationsCount", value.getSevereViolationsCount());
|
|
|
|
+
|
|
|
|
+ jgen.writeRaw(", \"publicRecordSummary\": " + JsonFormat.printer().print(value.getPublicRecordSummary()));
|
|
|
|
+
|
|
|
|
+ jgen.writeEndObject();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void writeRawArray(List<MessageOrBuilder> values, JsonGenerator jgen)
|
|
|
|
+ throws IOException, InvalidProtocolBufferException {
|
|
|
|
+ String comma = null;
|
|
|
|
+ for (MessageOrBuilder item : values) {
|
|
|
|
+ if (comma != null) {
|
|
|
|
+ jgen.writeRaw(comma);
|
|
|
|
+ } else {
|
|
|
|
+ comma = ",";
|
|
|
|
+ }
|
|
|
|
+ jgen.writeRaw(JsonFormat.printer().print(item));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|