AttachmentSectionRender.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.yaoyicloud.render;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.yaoyicloud.config.FilerepoProperties;
  10. import com.yaoyicloud.message.FxyProtos;
  11. import lombok.extern.slf4j.Slf4j;
  12. import com.deepoove.poi.config.Configure;
  13. import com.deepoove.poi.config.ConfigureBuilder;
  14. import com.google.protobuf.Descriptors;
  15. import com.google.protobuf.util.JsonFormat;
  16. import com.yaoyicloud.message.FxyProtos.AttachmentSection;
  17. /**
  18. * AttachmentSection渲染器
  19. *
  20. */
  21. @Slf4j
  22. public final class AttachmentSectionRender extends AbstractRender {
  23. private final FilerepoProperties filerepoProperties;
  24. public AttachmentSectionRender(String cwd, FilerepoProperties filerepoProperties) {
  25. super(cwd);
  26. this.filerepoProperties = filerepoProperties;
  27. }
  28. @Override
  29. protected String getBasicPath() throws IOException {
  30. return filerepoProperties.getBasePath();
  31. }
  32. @Override
  33. protected String getReportImagePath() {
  34. return filerepoProperties.getReportImagePath();
  35. }
  36. /**
  37. * Docx 渲染
  38. *
  39. * @param info 数据
  40. * @param templateFileContent 模板内容
  41. * @return 本地文件目录
  42. * @throws IOException
  43. */
  44. public String renderDocx(String info, Map<String, Object> addtionalMap, byte[] templateFileContent, String relationId) throws IOException {
  45. log.info("开始渲染附件模块,relationId: {}", relationId);
  46. // 配置POI-TL渲染器
  47. ConfigureBuilder builder = Configure.builder();
  48. builder.addPlugin('^', this.pictureRenderPolicy());
  49. builder.addPlugin('`', this.hyperlinkRenderPolicy());
  50. AttachmentSection.Builder attachmentSection = AttachmentSection.newBuilder();
  51. JsonFormat.parser().merge(info, attachmentSection);
  52. FxyProtos.AttachmentSection defaultInstance = FxyProtos.AttachmentSection.getDefaultInstance();
  53. FxyProtos.AttachmentSection mergedProto = defaultInstance.toBuilder()
  54. .mergeFrom(attachmentSection.build())
  55. .build();
  56. Map<String, Object> newAddtionMap = new HashMap<>();
  57. Descriptors.Descriptor descriptor = mergedProto.getDescriptorForType();
  58. for (Descriptors.FieldDescriptor field : descriptor.getFields()) {
  59. if (field.isRepeated()) {
  60. List<?> li = (List<?>) mergedProto.getField(field);
  61. if (li.size() == 0) {
  62. newAddtionMap.put(field.getName() + "AltText", "未上传");
  63. }
  64. }
  65. }
  66. String completeJson = JsonFormat.printer()
  67. .includingDefaultValueFields()
  68. .print(mergedProto);
  69. ObjectMapper objectMapper = new ObjectMapper();
  70. Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
  71. if (newAddtionMap != null) {
  72. data.putAll(newAddtionMap);
  73. }
  74. if (addtionalMap != null) {
  75. data.putAll(addtionalMap);
  76. }
  77. fillDefaultValues(data);
  78. try {
  79. // 渲染文档
  80. String resultPath = this.renderDocx(data, templateFileContent, builder, relationId, "attachmentSection");
  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 fillDefaultValues(Map<String, Object> data) {
  92. ArrayList<String> fillDeclLinks = (ArrayList) data.get("fillDeclLinks");
  93. if (fillDeclLinks != null) {
  94. data.put("fillDeclLinksExits", true);
  95. }
  96. }
  97. }