FoundationBasicInfoRender.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.yaoyicloud.render.foundation;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Map;
  5. import java.util.Objects;
  6. import com.deepoove.poi.config.Configure;
  7. import com.deepoove.poi.config.ConfigureBuilder;
  8. import com.deepoove.poi.policy.RenderPolicy;
  9. import com.fasterxml.jackson.core.JsonProcessingException;
  10. import com.fasterxml.jackson.core.type.TypeReference;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. import com.google.protobuf.InvalidProtocolBufferException;
  13. import com.google.protobuf.util.JsonFormat;
  14. import com.yaoyicloud.config.CommonDataCache;
  15. import com.yaoyicloud.config.FilerepoProperties;
  16. import com.yaoyicloud.message.FxyProtos;
  17. import com.yaoyicloud.render.AbstractRender;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.collections4.MapUtils;
  20. /**
  21. * BasicInfo渲染器
  22. *
  23. */
  24. @Slf4j
  25. public final class FoundationBasicInfoRender extends AbstractRender {
  26. private final FilerepoProperties filerepoProperties;
  27. private final CommonDataCache commonDataCache;
  28. public FoundationBasicInfoRender(String cwd, FilerepoProperties filerepoProperties,
  29. CommonDataCache commonDataCache) {
  30. super(cwd);
  31. this.filerepoProperties = filerepoProperties;
  32. this.commonDataCache = commonDataCache;
  33. }
  34. @Override
  35. protected String getBasicPath() throws IOException {
  36. return filerepoProperties.getBasePath();
  37. }
  38. @Override
  39. protected String getReportImagePath() {
  40. return filerepoProperties.getReportImagePath();
  41. }
  42. /**
  43. * Docx 渲染
  44. *
  45. * @param info 数据
  46. * @param inputStream 模板内容
  47. * @return 本地文件目录
  48. * @throws IOException
  49. */
  50. public String renderDocx(String info, InputStream inputStream,
  51. String relationId) throws IOException {
  52. log.info("开始渲染基金会基础信息报告模块,relationId: {}", relationId);
  53. // 配置POI-TL渲染器
  54. ConfigureBuilder builder = Configure.builder();
  55. RenderPolicy indicatorsRenderPolicyToProtobuf = this.indicatorsRenderPolicyToProtobuf();
  56. builder.bind("basicInfoChecks", indicatorsRenderPolicyToProtobuf).bind("superiorAuthority",
  57. indicatorsRenderPolicyToProtobuf);
  58. builder.addPlugin('^', this.pictureRenderPolicy());
  59. builder.useSpringEL();
  60. // 通过默认protobuf实例来填充不存在的key
  61. FxyProtos.BasicInfo.Builder basicInfoBuilder = FxyProtos.BasicInfo.newBuilder();
  62. JsonFormat.parser().merge(info, basicInfoBuilder);
  63. FxyProtos.BasicInfo defaultInstance = FxyProtos.BasicInfo.getDefaultInstance();
  64. FxyProtos.BasicInfo mergedProto = defaultInstance.toBuilder()
  65. .mergeFrom(basicInfoBuilder.build())
  66. .build();
  67. String completeJson = JsonFormat.printer()
  68. .includingDefaultValueFields()
  69. .print(mergedProto);
  70. ObjectMapper objectMapper = new ObjectMapper();
  71. Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
  72. data.replaceAll((k, v) -> v.equals("") ? "-" : v);
  73. Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
  74. if (MapUtils.isNotEmpty(commonDataCacheData)) {
  75. data.putAll(commonDataCacheData);
  76. }
  77. fillBasicDefaultValues(data);
  78. try {
  79. // 渲染文档
  80. String resultPath = this.renderDocx(data, inputStream, builder, relationId, "basicInfo");
  81. log.info("渲染基金会基础信息报告模块成功,文件路径: {}", resultPath);
  82. return resultPath;
  83. } catch (Exception e) {
  84. log.error("渲染基金会基础信息报告模块失败,relationId: {}", relationId, e);
  85. throw new IOException("文档渲染失败", e);
  86. }
  87. }
  88. /**
  89. * 填充默认值,确保所有必要字段都存在
  90. */
  91. private void fillBasicDefaultValues(Map<String, Object> data)
  92. throws InvalidProtocolBufferException, JsonProcessingException {
  93. Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("basicInfoSummary");
  94. basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);
  95. Object netAsset = data.get("netAsset");
  96. data.put("netAsset", netAsset + "元人民币");
  97. Map<String, Object> foundationExt = (Map<String, Object>) data.get("foundationExt");
  98. if (foundationExt == null) {
  99. ObjectMapper objectMapper = new ObjectMapper();
  100. FxyProtos.FoundationBasicInfoExt foundationBasicInfoExt =
  101. FxyProtos.FoundationBasicInfoExt.getDefaultInstance();
  102. String print = JsonFormat.printer()
  103. .includingDefaultValueFields()
  104. .print(foundationBasicInfoExt);
  105. foundationExt = objectMapper.readValue(print, new TypeReference<Map<String, Object>>() {});
  106. foundationExt.replaceAll((k, v) -> v.equals("") ? "-" : v);
  107. data.put("foundationExt", foundationExt);
  108. }
  109. foundationExt.replaceAll((k, v) -> v.equals("") ? "-" : v);
  110. Object lastYearSponsorshipDonationRatio = foundationExt.get("lastYearDonationIncome");
  111. foundationExt.put("lastYearDonationIncome", lastYearSponsorshipDonationRatio + "万元人民币");
  112. Object initialFund = foundationExt.get("initialFund");
  113. foundationExt.put("initialFund", initialFund + "万人民币");
  114. Object numberOfDirectors = foundationExt.get("numberOfDirectors");
  115. if (numberOfDirectors.equals("-")) {
  116. foundationExt.put("numberOfDirectors", 0);
  117. }
  118. Object certReceived = data.get("certReceived");
  119. if (Objects.equals(certReceived, "-")) {
  120. data.put("certReceived", "否");
  121. }
  122. Object bankLicense = data.get("bankLicense");
  123. if (Objects.equals(bankLicense, "-")) {
  124. data.put("bankLicense", "否");
  125. }
  126. Object annualInspectionResult = data.get("annualInspectionResult");
  127. if (Objects.equals(annualInspectionResult, "-")) {
  128. data.put("annualInspectionResult", "无");
  129. }
  130. }
  131. }