ProjectInfoRender.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.yaoyicloud.render;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import com.deepoove.poi.config.Configure;
  7. import com.deepoove.poi.config.ConfigureBuilder;
  8. import com.fasterxml.jackson.core.type.TypeReference;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.google.protobuf.Descriptors;
  11. import com.google.protobuf.util.JsonFormat;
  12. import com.yaoyicloud.config.CommonDataCache;
  13. import com.yaoyicloud.config.FilerepoProperties;
  14. import com.yaoyicloud.message.FxyProtos;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.collections4.MapUtils;
  17. /**
  18. * BasicInfo渲染器
  19. *
  20. */
  21. @Slf4j
  22. public final class ProjectInfoRender extends AbstractRender {
  23. private final FilerepoProperties filerepoProperties;
  24. private final CommonDataCache commonDataCache;
  25. public ProjectInfoRender(String cwd, FilerepoProperties filerepoProperties, CommonDataCache commonDataCache) {
  26. super(cwd);
  27. this.filerepoProperties = filerepoProperties;
  28. this.commonDataCache = commonDataCache;
  29. }
  30. @Override
  31. protected String getBasicPath() throws IOException {
  32. return filerepoProperties.getBasePath();
  33. }
  34. @Override
  35. protected String getReportImagePath() {
  36. return filerepoProperties.getReportImagePath();
  37. }
  38. /**
  39. * Docx 渲染
  40. *
  41. * @param info 数据
  42. * @param templateFileContent 模板内容
  43. * @return 本地文件目录
  44. * @throws IOException
  45. */
  46. public String renderDocx(String info, byte[] templateFileContent,
  47. String relationId) throws IOException {
  48. log.info("开始渲染项目信息报告模块,relationId: {}", relationId);
  49. // 配置POI-TL渲染器
  50. ConfigureBuilder builder = Configure.builder();
  51. builder.addPlugin('^', this.pictureRenderPolicy());
  52. // 通过默认protobuf实例来填充不存在的key
  53. FxyProtos.ProjectInfo.Builder basicInfoBuilder = FxyProtos.ProjectInfo.newBuilder();
  54. JsonFormat.parser().merge(info, basicInfoBuilder);
  55. FxyProtos.ProjectInfo defaultInstance = FxyProtos.ProjectInfo.getDefaultInstance();
  56. FxyProtos.ProjectInfo mergedProto = defaultInstance.toBuilder()
  57. .mergeFrom(basicInfoBuilder.build())
  58. .build();
  59. Map<String, Object> newAddtionMap = new HashMap<>();
  60. Descriptors.Descriptor descriptor = mergedProto.getDescriptorForType();
  61. for (Descriptors.FieldDescriptor field : descriptor.getFields()) {
  62. if (field.isRepeated()) {
  63. List<?> li = (List<?>) mergedProto.getField(field);
  64. if (li.size() == 0) {
  65. newAddtionMap.put(field.getName() + "AltText", "未上传");
  66. }
  67. }
  68. }
  69. String completeJson = JsonFormat.printer().includingDefaultValueFields().print(mergedProto);
  70. ObjectMapper objectMapper = new ObjectMapper();
  71. Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
  72. if (newAddtionMap != null) {
  73. data.putAll(newAddtionMap);
  74. }
  75. data.replaceAll((k, v) -> v.equals("") ? "-" : v);
  76. Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
  77. if (MapUtils.isNotEmpty(commonDataCacheData)) {
  78. data.putAll(commonDataCacheData);
  79. }
  80. fillDefaultValues(data);
  81. try {
  82. // 渲染文档
  83. String resultPath = this.renderDocx(data, templateFileContent, builder, relationId, "projectInfo");
  84. log.info("渲染项目信息报告模块成功,文件路径: {}", resultPath);
  85. return resultPath;
  86. } catch (Exception e) {
  87. log.error("渲染项目信息报告模块失败,relationId: {}", relationId, e);
  88. throw new IOException("文档渲染失败", e);
  89. }
  90. }
  91. /**
  92. * 填充默认值,确保所有必要字段都存在
  93. */
  94. private void fillDefaultValues(Map<String, Object> data) {
  95. Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("projectInfoSummary");
  96. basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);
  97. }
  98. }