123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package com.yaoyicloud.render;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.deepoove.poi.config.Configure;
- import com.deepoove.poi.config.ConfigureBuilder;
- import com.deepoove.poi.policy.RenderPolicy;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yaoyicloud.config.FilerepoProperties;
- import lombok.extern.slf4j.Slf4j;
- /**
- * AntiBribery渲染器
- *
- */
- @Slf4j
- public final class AntiBriberyRender extends AbstractRender {
- private final FilerepoProperties filerepoProperties;
- // 注入父类所需的 cwd 参数
- public AntiBriberyRender(String cwd, FilerepoProperties filerepoProperties) {
- super(cwd);
- this.filerepoProperties = filerepoProperties;
- }
- @Override
- protected String getBasicPath() throws IOException {
- return filerepoProperties.getBasePath();
- }
- @Override
- protected String getReportImagePath() {
- return filerepoProperties.getReportImagePath();
- }
- /**
- * Docx 渲染
- *
- * @param info 数据
- * @param addtionalMap 额外数据
- * @param templateFileContent 模板内容
- * @param relationId 关联ID
- * @return 本地文件目录
- * @throws IOException
- */
- public String renderDocx(String info, Map<String, Object> addtionalMap, byte[] templateFileContent, String relationId) throws IOException {
- log.info("开始渲染防贿赂报告,relationId: {}", relationId);
- // 配置POI-TL渲染器
- ConfigureBuilder builder = Configure.builder();
- RenderPolicy indicatorsRenderPolicy = this.indicatorsRenderPolicy();
- builder.bind("questionnaireItems", indicatorsRenderPolicy);
- // 解析数据
- ObjectMapper objectMapper = new ObjectMapper();
- Map<String, Object> data = objectMapper.readValue(info, new TypeReference<Map<String, Object>>() {});
- if (addtionalMap != null) {
- data.putAll(addtionalMap);
- }
- // 填充默认值
- fillDefaultValues(data);
- try {
- // 渲染文档
- String resultPath = this.renderDocx(data, templateFileContent, builder, relationId);
- log.info("防贿赂报告渲染成功,文件路径: {}", resultPath);
- return resultPath;
- } catch (Exception e) {
- log.error("防贿赂报告渲染失败,relationId: {}", relationId, e);
- throw new IOException("文档渲染失败", e);
- }
- }
- /**
- * 填充默认值,确保所有必要字段都存在
- */
- private void fillDefaultValues(Map<String, Object> data) {
- // 处理 questionnaireItems 列表
- if (!data.containsKey("questionnaireItems") || data.get("questionnaireItems") == null) {
- List<Map<String, Object>> defaultItems = new ArrayList<>();
- Map<String, Object> defaultItem = new HashMap<>();
- defaultItem.put("id", 1);
- defaultItem.put("question", "-");
- defaultItem.put("answer", "-");
- defaultItems.add(defaultItem);
- data.put("questionnaireItems", defaultItems);
- } else {
- // 确保列表中的每个项目都有所有字段
- @SuppressWarnings("unchecked")
- List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("questionnaireItems");
- for (Map<String, Object> item : items) {
- item.putIfAbsent("id", 1);
- item.putIfAbsent("question", "-");
- item.putIfAbsent("answer", "-");
- }
- }
- // 处理 antiBriberySummary 对象
- if (!data.containsKey("antiBriberySummary") || data.get("antiBriberySummary") == null) {
- Map<String, Object> defaultSummary = new HashMap<>();
- defaultSummary.put("score", "-");
- defaultSummary.put("riskSummary", "-");
- defaultSummary.put("suggestion", "-");
- data.put("antiBriberySummary", defaultSummary);
- } else {
- @SuppressWarnings("unchecked")
- Map<String, Object> summary = (Map<String, Object>) data.get("antiBriberySummary");
- summary.putIfAbsent("score", "-");
- summary.putIfAbsent("riskSummary", "-");
- summary.putIfAbsent("suggestion", "-");
- }
- }
- }
|