AntiBriberyRender.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.deepoove.poi.config.Configure;
  8. import com.deepoove.poi.config.ConfigureBuilder;
  9. import com.deepoove.poi.policy.RenderPolicy;
  10. import com.fasterxml.jackson.core.type.TypeReference;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. import com.yaoyicloud.config.FilerepoProperties;
  13. import lombok.extern.slf4j.Slf4j;
  14. /**
  15. * AntiBribery渲染器
  16. *
  17. */
  18. @Slf4j
  19. public final class AntiBriberyRender extends AbstractRender {
  20. private final FilerepoProperties filerepoProperties;
  21. // 注入父类所需的 cwd 参数
  22. public AntiBriberyRender(String cwd, FilerepoProperties filerepoProperties) {
  23. super(cwd);
  24. this.filerepoProperties = filerepoProperties;
  25. }
  26. @Override
  27. protected String getBasicPath() throws IOException {
  28. return filerepoProperties.getBasePath();
  29. }
  30. @Override
  31. protected String getReportImagePath() {
  32. return filerepoProperties.getReportImagePath();
  33. }
  34. /**
  35. * Docx 渲染
  36. *
  37. * @param info 数据
  38. * @param addtionalMap 额外数据
  39. * @param templateFileContent 模板内容
  40. * @param relationId 关联ID
  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. RenderPolicy indicatorsRenderPolicy = this.indicatorsRenderPolicy();
  49. builder.bind("questionnaireItems", indicatorsRenderPolicy);
  50. // 解析数据
  51. ObjectMapper objectMapper = new ObjectMapper();
  52. Map<String, Object> data = objectMapper.readValue(info, new TypeReference<Map<String, Object>>() {});
  53. if (addtionalMap != null) {
  54. data.putAll(addtionalMap);
  55. }
  56. // 填充默认值
  57. fillDefaultValues(data);
  58. try {
  59. // 渲染文档
  60. String resultPath = this.renderDocx(data, templateFileContent, builder, relationId);
  61. log.info("防贿赂报告渲染成功,文件路径: {}", resultPath);
  62. return resultPath;
  63. } catch (Exception e) {
  64. log.error("防贿赂报告渲染失败,relationId: {}", relationId, e);
  65. throw new IOException("文档渲染失败", e);
  66. }
  67. }
  68. /**
  69. * 填充默认值,确保所有必要字段都存在
  70. */
  71. private void fillDefaultValues(Map<String, Object> data) {
  72. // 处理 questionnaireItems 列表
  73. if (!data.containsKey("questionnaireItems") || data.get("questionnaireItems") == null) {
  74. List<Map<String, Object>> defaultItems = new ArrayList<>();
  75. Map<String, Object> defaultItem = new HashMap<>();
  76. defaultItem.put("id", 1);
  77. defaultItem.put("question", "-");
  78. defaultItem.put("answer", "-");
  79. defaultItems.add(defaultItem);
  80. data.put("questionnaireItems", defaultItems);
  81. } else {
  82. // 确保列表中的每个项目都有所有字段
  83. @SuppressWarnings("unchecked")
  84. List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("questionnaireItems");
  85. for (Map<String, Object> item : items) {
  86. item.putIfAbsent("id", 1);
  87. item.putIfAbsent("question", "-");
  88. item.putIfAbsent("answer", "-");
  89. }
  90. }
  91. // 处理 antiBriberySummary 对象
  92. if (!data.containsKey("antiBriberySummary") || data.get("antiBriberySummary") == null) {
  93. Map<String, Object> defaultSummary = new HashMap<>();
  94. defaultSummary.put("score", "-");
  95. defaultSummary.put("riskSummary", "-");
  96. defaultSummary.put("suggestion", "-");
  97. data.put("antiBriberySummary", defaultSummary);
  98. } else {
  99. @SuppressWarnings("unchecked")
  100. Map<String, Object> summary = (Map<String, Object>) data.get("antiBriberySummary");
  101. summary.putIfAbsent("score", "-");
  102. summary.putIfAbsent("riskSummary", "-");
  103. summary.putIfAbsent("suggestion", "-");
  104. }
  105. }
  106. }