EntAppendixRender.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.yaoyicloud.render.cso;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import org.apache.commons.collections4.MapUtils;
  5. import com.deepoove.poi.config.Configure;
  6. import com.deepoove.poi.config.ConfigureBuilder;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.yaoyicloud.config.CommonDataCache;
  10. import com.yaoyicloud.config.FilerepoProperties;
  11. import com.yaoyicloud.render.AbstractRender;
  12. import lombok.extern.slf4j.Slf4j;
  13. @Slf4j
  14. public final class EntAppendixRender extends AbstractRender {
  15. private final FilerepoProperties filerepoProperties;
  16. private final CommonDataCache commonDataCache;
  17. public EntAppendixRender(String cwd, FilerepoProperties filerepoProperties, CommonDataCache commonDataCache) {
  18. super(cwd);
  19. this.filerepoProperties = filerepoProperties;
  20. this.commonDataCache = commonDataCache;
  21. }
  22. @Override
  23. protected String getBasicPath() throws IOException {
  24. return filerepoProperties.getBasePath();
  25. }
  26. @Override
  27. protected String getReportImagePath() {
  28. return filerepoProperties.getReportImagePath();
  29. }
  30. /**
  31. * Docx 渲染
  32. * @param info 数据
  33. * @param templateFileContent 模板内容
  34. * @param relationId 用于查询缓存
  35. * @return
  36. * @throws IOException
  37. */
  38. public String renderDocx(String info, byte[] templateFileContent, String relationId) throws IOException {
  39. log.info("开始渲染CSO企业报告签章模块,relationId: {}", relationId);
  40. // 配置POI-TL渲染器
  41. ConfigureBuilder builder = Configure.builder();
  42. ObjectMapper objectMapper = new ObjectMapper();
  43. Map<String, Object> data = objectMapper.readValue(info, new TypeReference<Map<String, Object>>() {});
  44. Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
  45. if (MapUtils.isNotEmpty(commonDataCacheData)) {
  46. data.putAll(commonDataCacheData);
  47. }
  48. fillDefaultValues(data);
  49. try {
  50. // 渲染文档
  51. String resultPath = this.renderDocx(data, templateFileContent, builder, relationId, "Appendix");
  52. log.info("渲染CSO企业报告签章模块成功,文件路径: {}", resultPath);
  53. return resultPath;
  54. } catch (Exception e) {
  55. log.error("渲染CSO企业报告签章模块失败,relationId: {}", relationId, e);
  56. throw new IOException("文档渲染失败", e);
  57. }
  58. }
  59. /**
  60. * 填充默认值,确保所有必要字段都存在
  61. */
  62. private void fillDefaultValues(Map<String, Object> data) {
  63. }
  64. }