package com.project.report; import com.project.analyzer.PostAnalyzer; import com.project.model.PostInfo; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Map; public class HTMLReportGenerator { private static final String OUTPUT_DIR = "d:\\java\\project\\reports"; public static void generateReport(PostAnalyzer analyzer) { try { Files.createDirectories(Paths.get(OUTPUT_DIR)); String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); String filename = "report_" + timestamp + ".html"; String filepath = OUTPUT_DIR + "/" + filename; try (BufferedWriter writer = new BufferedWriter( new FileWriter(filepath, StandardCharsets.UTF_8))) { writer.write(generateHTMLContent(analyzer)); } System.out.println("HTML报告已生成: " + filepath); } catch (IOException e) { System.err.println("生成HTML报告时出错: " + e.getMessage()); } } private static String generateHTMLContent(PostAnalyzer analyzer) { StringBuilder html = new StringBuilder(); html.append("\n"); html.append("\n"); html.append("\n"); html.append(" \n"); html.append(" \n"); html.append(" 图文帖子数据分析报告\n"); html.append(" \n"); html.append("\n"); html.append("\n"); html.append("
\n"); html.append("

图文帖子数据分析报告

\n"); html.append("

生成时间: ").append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append("

\n"); html.append(generateSummarySection(analyzer)); html.append(generateSentimentSection(analyzer)); html.append(generateEngagementSection(analyzer)); html.append(generateAuthorSection(analyzer)); html.append(generateChartsSection()); html.append("
\n"); html.append("\n"); html.append(""); return html.toString(); } private static String generateSummarySection(PostAnalyzer analyzer) { StringBuilder section = new StringBuilder(); int totalPosts = analyzer.getPosts().size(); double avgLikes = analyzer.getPosts().stream() .mapToInt(PostInfo::getLikeCount) .average() .orElse(0); section.append("
\n"); section.append("
\n"); section.append("

").append(totalPosts).append("

\n"); section.append("

帖子总数

\n"); section.append("
\n"); section.append("
\n"); section.append("

").append(String.format("%.1f", avgLikes)).append("

\n"); section.append("

平均点赞

\n"); section.append("
\n"); section.append("
\n"); section.append("
\n"); section.append("

分析摘要

\n"); section.append(" \n"); section.append("
\n"); return section.toString(); } private static String generateSentimentSection(PostAnalyzer analyzer) { StringBuilder section = new StringBuilder(); Map sentimentData = analyzer.getSentimentDistributionData(); section.append("
\n"); section.append("

情感倾向分布分析

\n"); section.append(" \n"); section.append(" \n"); long total = sentimentData.values().stream().mapToLong(Long::longValue).sum(); for (Map.Entry entry : sentimentData.entrySet()) { double percent = (entry.getValue() * 100.0) / total; section.append(" \n"); } section.append("
情感倾向帖子数量占比
").append(entry.getKey()) .append("").append(entry.getValue()) .append("").append(String.format("%.1f%%", percent)) .append("
\n"); section.append("
\n"); return section.toString(); } private static String generateEngagementSection(PostAnalyzer analyzer) { StringBuilder section = new StringBuilder(); Map engagementData = analyzer.getEngagementData(); section.append("
\n"); section.append("

互动指标分析

\n"); section.append(" \n"); section.append(" \n"); for (Map.Entry entry : engagementData.entrySet()) { section.append(" \n"); } section.append("
指标平均值
").append(entry.getKey()) .append("").append(String.format("%.1f", entry.getValue())) .append("
\n"); section.append("
\n"); return section.toString(); } private static String generateAuthorSection(PostAnalyzer analyzer) { StringBuilder section = new StringBuilder(); Map authorData = analyzer.getAuthorPostCount(); section.append("
\n"); section.append("

热门作者排行TOP10

\n"); section.append(" \n"); section.append(" \n"); int rank = 1; for (Map.Entry entry : authorData.entrySet()) { section.append(" \n"); } section.append("
排名作者帖子数量
").append(rank++) .append("").append(entry.getKey()) .append("").append(entry.getValue()) .append("
\n"); section.append("
\n"); return section.toString(); } private static String generateChartsSection() { StringBuilder section = new StringBuilder(); section.append("
\n"); section.append("

数据可视化图表

\n"); section.append("
\n"); section.append("

情感倾向分布

\n"); section.append(" \"情感倾向分布图\"\n"); section.append("
\n"); section.append("
\n"); section.append("

互动指标分析

\n"); section.append(" \"互动指标图\"\n"); section.append("
\n"); section.append("
\n"); section.append("

热门作者排行

\n"); section.append(" \"作者排行图\"\n"); section.append("
\n"); section.append("
\n"); return section.toString(); } }