BasicInfoRender.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.yaoyicloud.render;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import com.deepoove.poi.config.Configure;
  5. import com.deepoove.poi.config.ConfigureBuilder;
  6. import com.deepoove.poi.policy.RenderPolicy;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.google.protobuf.util.JsonFormat;
  10. import com.yaoyicloud.config.FilerepoProperties;
  11. import com.yaoyicloud.message.FxyProtos;
  12. import lombok.extern.slf4j.Slf4j;
  13. /**
  14. * BasicInfo渲染器
  15. *
  16. */
  17. @Slf4j
  18. public final class BasicInfoRender extends AbstractRender {
  19. private final FilerepoProperties filerepoProperties;
  20. public BasicInfoRender(String cwd, FilerepoProperties filerepoProperties) {
  21. super(cwd);
  22. this.filerepoProperties = filerepoProperties;
  23. }
  24. @Override
  25. protected String getBasicPath() throws IOException {
  26. return filerepoProperties.getBasePath();
  27. }
  28. @Override
  29. protected String getReportImagePath() {
  30. return filerepoProperties.getReportImagePath();
  31. }
  32. /**
  33. * Docx 渲染
  34. *
  35. * @param info 数据
  36. * @param templateFileContent 模板内容
  37. * @return 本地文件目录
  38. * @throws IOException
  39. */
  40. public String renderDocx(String info, Map<String, Object> addtionalMap, byte[] templateFileContent, String relationId) throws IOException {
  41. log.info("开始渲染基础信息报告模块,relationId: {}", relationId);
  42. // 配置POI-TL渲染器
  43. ConfigureBuilder builder = Configure.builder();
  44. RenderPolicy indicatorsRenderPolicy = this.indicatorsRenderPolicy();
  45. builder.bind("basicInfoChecks", indicatorsRenderPolicy);
  46. FxyProtos.BasicInfo.Builder basicInfoBuilder = FxyProtos.BasicInfo.newBuilder();
  47. JsonFormat.parser().merge(info, basicInfoBuilder);
  48. FxyProtos.BasicInfo defaultInstance = FxyProtos.BasicInfo.getDefaultInstance();
  49. FxyProtos.BasicInfo mergedProto = defaultInstance.toBuilder()
  50. .mergeFrom(basicInfoBuilder.build())
  51. .build();
  52. String completeJson = JsonFormat.printer()
  53. .includingDefaultValueFields()
  54. .print(mergedProto);
  55. ObjectMapper objectMapper = new ObjectMapper();
  56. Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
  57. data.replaceAll((k, v) -> v.equals("") ? "-" : v);
  58. if (addtionalMap != null) {
  59. data.putAll(addtionalMap);
  60. }
  61. fillBasicDefaultValues(data);
  62. try {
  63. // 渲染文档
  64. String resultPath = this.renderDocx(data, templateFileContent, builder, relationId);
  65. log.info("渲染基础信息报告模块成功,文件路径: {}", resultPath);
  66. return resultPath;
  67. } catch (Exception e) {
  68. log.error("渲染基础信息报告模块失败,relationId: {}", relationId, e);
  69. throw new IOException("文档渲染失败", e);
  70. }
  71. }
  72. /**
  73. * 填充默认值,确保所有必要字段都存在
  74. */
  75. private void fillBasicDefaultValues(Map<String, Object> data) {
  76. Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("basicInfoSummary");
  77. basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);
  78. Map<String, Object> platformExt = (Map<String, Object>) data.get("platformExt");
  79. platformExt.replaceAll((k, v) -> v.equals("") ? "-" : v);
  80. Object certReceived = data.get("certReceived");
  81. if (certReceived.equals("-")) {
  82. data.put("certReceived", "否");
  83. }
  84. Object bankLicense = data.get("bankLicense");
  85. if (bankLicense.equals("-")) {
  86. data.put("bankLicense", "否");
  87. }
  88. }
  89. }