| | |
| | | import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy; |
| | | import com.estate.expert.admin.service.UserFileService; |
| | | import com.estate.expert.admin.stat.report.render.CustomPictureRenderPolicy; |
| | | import com.estate.expert.admin.stat.report.render.DeviceGroupTablePolicy; |
| | | import com.estate.expert.admin.stat.report.render.DeviceTablePolicy; |
| | | import lombok.AllArgsConstructor; |
| | | import com.estate.expert.admin.stat.report.render.WordNumberingSanitizer; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | |
| | | import java.io.IOException; |
| | |
| | | /** |
| | | * 设备风险评估报告构建器 |
| | | */ |
| | | @AllArgsConstructor |
| | | public class DeviceReportBuilder implements ReportBuilderBase { |
| | | |
| | | private static final String DEFAULT_TEMPLATE_FILE = "设备风险评估报告.docx"; |
| | | |
| | | protected UserFileService userFileService; |
| | | private final String templateFileName; |
| | | |
| | | public DeviceReportBuilder(UserFileService userFileService) { |
| | | this(userFileService, DEFAULT_TEMPLATE_FILE); |
| | | } |
| | | |
| | | public DeviceReportBuilder(UserFileService userFileService, String templateFileName) { |
| | | this.userFileService = userFileService; |
| | | this.templateFileName = templateFileName; |
| | | } |
| | | |
| | | /** |
| | | * 构建设备风险评估报告 |
| | |
| | | */ |
| | | @Override |
| | | public XWPFTemplate buildReport(JSONObject data, Map<String, Integer> templateConfig) { |
| | | var res = new ClassPathResource("templates/设备风险评估报告.docx"); |
| | | var res = new ClassPathResource("templates/" + templateFileName); |
| | | |
| | | if (res.getPath().contains("new")) { |
| | | return buildNewReport(res, data); |
| | | } |
| | | return buildDefaultReport(res, data); |
| | | } |
| | | |
| | | private XWPFTemplate buildDefaultReport(ClassPathResource res, JSONObject data) { |
| | | // 将data.评估结果.分类评估.详情 复制 |
| | | // 避免渲染的时候的策略冲突 |
| | | if (data.containsKey("评估结果") && data.getJSONObject("评估结果").containsKey("分类评估")) { |
| | | var evalResult = data.getJSONObject("评估结果").getJSONObject("分类评估"); |
| | | var details = evalResult.getJSONArray("详情"); |
| | | evalResult.put("详情2", details); |
| | | } |
| | | copyCategoryDetails(data); |
| | | |
| | | ConfigureBuilder builder = Configure.builder(); |
| | | builder.bind("data.设备信息", new LoopRowTableRenderPolicy()); |
| | |
| | | builder.bind("data.评估结果.隐患等级分布汇总.分项", new LoopRowTableRenderPolicy()); |
| | | builder.bind("分项隐患", new LoopRowTableRenderPolicy()); |
| | | builder.bind("分项问题", new DeviceTablePolicy()); |
| | | // builder.bind("data.设备信息", new DeviceInfoTablePolicy()); |
| | | |
| | | builder.addPlugin('%', new CustomPictureRenderPolicy(userFileService)); |
| | | |
| | | return compileAndRender(res, builder, data); |
| | | } |
| | | |
| | | private XWPFTemplate buildNewReport(ClassPathResource res, JSONObject data) { |
| | | copyCategoryDetails(data); |
| | | |
| | | ConfigureBuilder builder = Configure.builder(); |
| | | builder.bind("data.设备信息", new LoopRowTableRenderPolicy()); |
| | | builder.bind("data.设备分组信息", new DeviceGroupTablePolicy()); |
| | | builder.bind("data.评估结果.设备得分情况", new LoopRowTableRenderPolicy()); |
| | | builder.bind("详情", new LoopRowTableRenderPolicy()); |
| | | builder.bind("data.评估结果.分类评估", new LoopRowTableRenderPolicy()); |
| | | builder.bind("data.评估结果.分类评估.详情2", new LoopRowTableRenderPolicy()); |
| | | builder.bind("data.评估结果.隐患等级分布汇总.分项", new LoopRowTableRenderPolicy()); |
| | | builder.bind("分项隐患", new LoopRowTableRenderPolicy()); |
| | | builder.bind("分项问题", new DeviceTablePolicy()); |
| | | builder.addPlugin('%', new CustomPictureRenderPolicy(userFileService)); |
| | | |
| | | return compileAndRender(res, builder, data); |
| | | } |
| | | |
| | | private void copyCategoryDetails(JSONObject data) { |
| | | if (data.containsKey("评估结果") && data.getJSONObject("评估结果").containsKey("分类评估")) { |
| | | var evalResult = data.getJSONObject("评估结果").getJSONObject("分类评估"); |
| | | var details = evalResult.getJSONArray("详情"); |
| | | evalResult.put("详情2", details); |
| | | } |
| | | } |
| | | |
| | | private XWPFTemplate compileAndRender(ClassPathResource res, ConfigureBuilder builder, JSONObject data) { |
| | | try { |
| | | return XWPFTemplate.compile(res.getInputStream(), builder.build()).render( |
| | | Map.of( "data", data |
| | | )); |
| | | return WordNumberingSanitizer.sanitize( |
| | | XWPFTemplate.compile(res.getInputStream(), builder.build()).render(Map.of("data", data)), |
| | | res.getPath().contains("new") |
| | | ); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |