123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package com.yaoyicloud.render;
- import java.io.IOException;
- 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.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.google.protobuf.Descriptors;
- import com.google.protobuf.util.JsonFormat;
- import com.yaoyicloud.config.CommonDataCache;
- import com.yaoyicloud.config.FilerepoProperties;
- import com.yaoyicloud.message.FxyProtos;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.collections4.MapUtils;
- /**
- * BasicInfo渲染器
- *
- */
- @Slf4j
- public final class ProjectInfoRender extends AbstractRender {
- private final FilerepoProperties filerepoProperties;
- private final CommonDataCache commonDataCache;
- public ProjectInfoRender(String cwd, FilerepoProperties filerepoProperties, CommonDataCache commonDataCache) {
- super(cwd);
- this.filerepoProperties = filerepoProperties;
- this.commonDataCache = commonDataCache;
- }
- @Override
- protected String getBasicPath() throws IOException {
- return filerepoProperties.getBasePath();
- }
- @Override
- protected String getReportImagePath() {
- return filerepoProperties.getReportImagePath();
- }
- /**
- * Docx 渲染
- *
- * @param info 数据
- * @param templateFileContent 模板内容
- * @return 本地文件目录
- * @throws IOException
- */
- public String renderDocx(String info, byte[] templateFileContent,
- String relationId) throws IOException {
- log.info("开始渲染项目信息报告模块,relationId: {}", relationId);
- // 配置POI-TL渲染器
- ConfigureBuilder builder = Configure.builder();
- builder.addPlugin('^', this.pictureRenderPolicy());
- // 通过默认protobuf实例来填充不存在的key
- FxyProtos.ProjectInfo.Builder basicInfoBuilder = FxyProtos.ProjectInfo.newBuilder();
- JsonFormat.parser().merge(info, basicInfoBuilder);
- FxyProtos.ProjectInfo defaultInstance = FxyProtos.ProjectInfo.getDefaultInstance();
- FxyProtos.ProjectInfo mergedProto = defaultInstance.toBuilder()
- .mergeFrom(basicInfoBuilder.build())
- .build();
- Map<String, Object> newAddtionMap = new HashMap<>();
- Descriptors.Descriptor descriptor = mergedProto.getDescriptorForType();
- for (Descriptors.FieldDescriptor field : descriptor.getFields()) {
- if (field.isRepeated()) {
- List<?> li = (List<?>) mergedProto.getField(field);
- if (li.size() == 0) {
- newAddtionMap.put(field.getName() + "AltText", "未上传");
- }
- }
- }
- String completeJson = JsonFormat.printer().includingDefaultValueFields().print(mergedProto);
- ObjectMapper objectMapper = new ObjectMapper();
- Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
- if (newAddtionMap != null) {
- data.putAll(newAddtionMap);
- }
- data.replaceAll((k, v) -> v.equals("") ? "-" : v);
- Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
- if (MapUtils.isNotEmpty(commonDataCacheData)) {
- data.putAll(commonDataCacheData);
- }
- fillDefaultValues(data);
- try {
- // 渲染文档
- String resultPath = this.renderDocx(data, templateFileContent, builder, relationId, "projectInfo");
- log.info("渲染项目信息报告模块成功,文件路径: {}", resultPath);
- return resultPath;
- } catch (Exception e) {
- log.error("渲染项目信息报告模块失败,relationId: {}", relationId, e);
- throw new IOException("文档渲染失败", e);
- }
- }
- /**
- * 填充默认值,确保所有必要字段都存在
- */
- private void fillDefaultValues(Map<String, Object> data) {
- Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("projectInfoSummary");
- basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);
- }
- }
|