|
|
|
@ -1,72 +1,61 @@
|
|
|
|
|
package com.dsideal.base.Tools;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dsideal.base.BaseApplication;
|
|
|
|
|
import com.dsideal.base.DataEase.Model.DataEaseModel;
|
|
|
|
|
import com.dsideal.base.Plugin.YamlProp;
|
|
|
|
|
import com.dsideal.base.Tools.Util.CallDeepSeek;
|
|
|
|
|
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
|
|
|
|
|
import com.dsideal.base.Tools.Util.PptAIKit;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
|
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class AiGenerate extends PptAIKit {
|
|
|
|
|
/**
|
|
|
|
|
* AI生成器主类 - 协调数据获取、Word生成和PPT生成
|
|
|
|
|
*/
|
|
|
|
|
public class AiGenerate {
|
|
|
|
|
public static DataEaseModel dm = new DataEaseModel();
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
//加载配置文件
|
|
|
|
|
try {
|
|
|
|
|
// 初始化配置
|
|
|
|
|
initializeConfiguration();
|
|
|
|
|
|
|
|
|
|
// 定义对比地区
|
|
|
|
|
String[] regions = {"文山州", "楚雄州"};
|
|
|
|
|
|
|
|
|
|
// 第一步:数据获取
|
|
|
|
|
System.out.println("=== 开始数据收集 ===");
|
|
|
|
|
String dataContent = DataCollector.collectEducationData(regions);
|
|
|
|
|
String analysisPrompt = DataCollector.createAnalysisPrompt(dataContent, regions);
|
|
|
|
|
|
|
|
|
|
// 第二步:AI分析并生成Word文档
|
|
|
|
|
System.out.println("=== 开始AI分析和Word文档生成 ===");
|
|
|
|
|
generateWordReport(analysisPrompt, regions);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("程序执行出错: " + e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化配置
|
|
|
|
|
*/
|
|
|
|
|
private static void initializeConfiguration() {
|
|
|
|
|
String configFile = "application.yaml";
|
|
|
|
|
BaseApplication.PropKit = new YamlProp(configFile);
|
|
|
|
|
LocalMysqlConnectUtil.Init();
|
|
|
|
|
|
|
|
|
|
// 查询教育资源配置发展预测相关表
|
|
|
|
|
String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dataease' AND TABLE_NAME LIKE 'excel_报告-教育资源配置发展预测%';";
|
|
|
|
|
List<Record> tableList = Db.find(sql);
|
|
|
|
|
|
|
|
|
|
String[] biJiao = new String[]{"文山州", "楚雄州"};
|
|
|
|
|
StringBuilder dataContent = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
// 收集两个州的数据
|
|
|
|
|
dataContent.append("教育资源配置发展预测数据对比分析\n\n");
|
|
|
|
|
dataContent.append("对比州市:").append(String.join(" vs ", biJiao)).append("\n\n");
|
|
|
|
|
|
|
|
|
|
for (Record record : tableList) {
|
|
|
|
|
String tableName = record.getStr("TABLE_NAME");
|
|
|
|
|
dataContent.append("数据表:").append(tableName).append("\n");
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < biJiao.length; j++) {
|
|
|
|
|
sql = "select * from `" + tableName + "` where `行政区划`=?";
|
|
|
|
|
List<Record> listContent = Db.use(DataEaseModel.DB_NAME).find(sql, biJiao[j]);
|
|
|
|
|
|
|
|
|
|
if (!listContent.isEmpty()) {
|
|
|
|
|
dataContent.append("\n").append(biJiao[j]).append("数据:\n");
|
|
|
|
|
for (Record dataRecord : listContent) {
|
|
|
|
|
dataContent.append(JSONUtil.toJsonPrettyStr(dataRecord.getColumns())).append("\n");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
dataContent.append("\n").append(biJiao[j]).append(":无相关数据\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dataContent.append("\n----------------------------------------\n\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建分析提示词
|
|
|
|
|
String analysisPrompt = createAnalysisPrompt(dataContent.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("配置初始化完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成Word报告
|
|
|
|
|
*/
|
|
|
|
|
private static void generateWordReport(String analysisPrompt, String[] regions) {
|
|
|
|
|
// 生成输出文件路径
|
|
|
|
|
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
|
|
|
|
String outputPath = "教育资源对比分析报告_" + timestamp + ".docx";
|
|
|
|
|
|
|
|
|
|
System.out.println("开始调用DeepSeek进行数据分析...");
|
|
|
|
|
|
|
|
|
|
// 调用DeepSeek进行分析
|
|
|
|
@ -85,14 +74,13 @@ public class AiGenerate extends PptAIKit {
|
|
|
|
|
System.out.println("\n\n=== Word文档分析完成 ===");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 使用Apache POI生成Word文档
|
|
|
|
|
generateWordDocument(fullResponse.toString(), outputPath, biJiao);
|
|
|
|
|
// 生成Word文档
|
|
|
|
|
WordGenerator.generateWordDocument(fullResponse.toString(), outputPath, regions);
|
|
|
|
|
System.out.println("Word分析报告已保存到: " + outputPath);
|
|
|
|
|
|
|
|
|
|
// 生成PPT
|
|
|
|
|
// 生成PPT
|
|
|
|
|
System.out.println("\n\n=== 开始生成PPT ===");
|
|
|
|
|
generatePptPresentation(analysisPrompt, biJiao);
|
|
|
|
|
// // 第三步:生成PPT
|
|
|
|
|
// System.out.println("\n\n=== 开始生成PPT ===");
|
|
|
|
|
// PptGenerator.generatePptPresentation(fullResponse.toString(), regions);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("保存文件时出错: " + e.getMessage());
|
|
|
|
@ -106,250 +94,4 @@ public class AiGenerate extends PptAIKit {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建PPT生成提示词
|
|
|
|
|
*/
|
|
|
|
|
private static String createPptPrompt(String dataContent, String[] cities) {
|
|
|
|
|
try {
|
|
|
|
|
// 构建给 DeepSeek 的精简提示词
|
|
|
|
|
String deepseekPrompt = "你是一位专业的教育数据分析专家,请将以下教育资源配置数据进行精简总结," +
|
|
|
|
|
"提取关键信息和对比要点,控制在300字以内,重点突出" + String.join("与", cities) + "的对比差异:\n\n" +
|
|
|
|
|
dataContent;
|
|
|
|
|
|
|
|
|
|
System.out.println("正在调用 DeepSeek 精简数据...");
|
|
|
|
|
|
|
|
|
|
// 调用 DeepSeek 获取精简内容
|
|
|
|
|
String simplifiedContent = CallDeepSeek.callDeepSeek(deepseekPrompt);
|
|
|
|
|
|
|
|
|
|
if (simplifiedContent != null && !simplifiedContent.trim().isEmpty()) {
|
|
|
|
|
System.out.println("DeepSeek 精简完成,精简后长度: " + simplifiedContent.length());
|
|
|
|
|
|
|
|
|
|
// 使用精简后的内容构建 PPT 提示词
|
|
|
|
|
return "请基于以下教育资源配置数据,为" + String.join("与", cities) +
|
|
|
|
|
"教育资源配置对比分析创建一个专业的PPT大纲。\n\n" +
|
|
|
|
|
"PPT应包含以下主要部分:\n" +
|
|
|
|
|
"1. 封面 - 标题和基本信息\n" +
|
|
|
|
|
"2. 目录 - 分析框架\n" +
|
|
|
|
|
"3. 数据概览 - 关键指标对比\n" +
|
|
|
|
|
"4. 详细分析 - 各维度深入对比\n" +
|
|
|
|
|
"5. 问题识别 - 存在的主要问题\n" +
|
|
|
|
|
"6. 建议方案 - 改进措施\n" +
|
|
|
|
|
"7. 总结 - 结论和展望\n\n" +
|
|
|
|
|
"请确保内容简洁明了,适合PPT展示。\n\n" +
|
|
|
|
|
"=== 精简后的关键数据 ===\n" + simplifiedContent;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("DeepSeek 调用失败,使用基础提示词");
|
|
|
|
|
return createBasicPptPrompt(cities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("调用 DeepSeek 精简数据时出错: " + e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
// 如果 DeepSeek 调用失败,返回基础提示词
|
|
|
|
|
return createBasicPptPrompt(cities);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建基础PPT提示词(作为备用方案)
|
|
|
|
|
*/
|
|
|
|
|
private static String createBasicPptPrompt(String[] cities) {
|
|
|
|
|
return "请为" + String.join("与", cities) +
|
|
|
|
|
"教育资源配置对比分析创建一个专业的PPT大纲。\n\n" +
|
|
|
|
|
"PPT应包含以下主要部分:\n" +
|
|
|
|
|
"1. 封面 - 标题和基本信息\n" +
|
|
|
|
|
"2. 目录 - 分析框架\n" +
|
|
|
|
|
"3. 数据概览 - 关键指标对比\n" +
|
|
|
|
|
"4. 详细分析 - 各维度深入对比\n" +
|
|
|
|
|
"5. 问题识别 - 存在的主要问题\n" +
|
|
|
|
|
"6. 建议方案 - 改进措施\n" +
|
|
|
|
|
"7. 总结 - 结论和展望\n\n" +
|
|
|
|
|
"请基于教育资源配置的一般分析框架,重点关注对比分析结果。";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成PPT演示文稿
|
|
|
|
|
*/
|
|
|
|
|
private static void generatePptPresentation(String analysisContent, String[] biJiao) {
|
|
|
|
|
try {
|
|
|
|
|
String uid = "test";
|
|
|
|
|
// 第一步:使用DeepSeek生成简化的PPT提示词
|
|
|
|
|
String simplifiedPptPrompt = createPptPrompt(analysisContent, biJiao);
|
|
|
|
|
|
|
|
|
|
// 创建 api token (有效期2小时,建议缓存到redis,同一个 uid 创建时之前的 token 会在10秒内失效)
|
|
|
|
|
String apiToken = PptAIKit.createApiToken(uid, null);
|
|
|
|
|
System.out.println("api token: " + apiToken);
|
|
|
|
|
|
|
|
|
|
// 生成大纲
|
|
|
|
|
System.out.println("\n\n========== 正在生成大纲 ==========");
|
|
|
|
|
System.out.println(simplifiedPptPrompt.length());
|
|
|
|
|
System.out.println(simplifiedPptPrompt);
|
|
|
|
|
String outline = PptAIKit.generateOutline(apiToken, null, null, simplifiedPptPrompt);
|
|
|
|
|
|
|
|
|
|
// 生成大纲内容
|
|
|
|
|
System.out.println("\n\n========== 正在生成大纲内容 ==========");
|
|
|
|
|
String markdown = PptAIKit.generateContent(apiToken, outline, null, simplifiedPptPrompt);
|
|
|
|
|
|
|
|
|
|
// 随机一个模板
|
|
|
|
|
System.out.println("\n\n========== 随机选择模板 ==========");
|
|
|
|
|
String templateId = PptAIKit.randomOneTemplateId(apiToken);
|
|
|
|
|
System.out.println(templateId);
|
|
|
|
|
|
|
|
|
|
// 生成PPT
|
|
|
|
|
System.out.println("\n\n========== 正在生成PPT ==========");
|
|
|
|
|
JSONObject pptInfo = PptAIKit.generatePptx(apiToken, templateId, markdown, false);
|
|
|
|
|
String pptId = pptInfo.getString("id");
|
|
|
|
|
System.out.println("pptId: " + pptId);
|
|
|
|
|
System.out.println("ppt主题:" + pptInfo.getString("subject"));
|
|
|
|
|
System.out.println("ppt封面:" + pptInfo.getString("coverUrl") + "?token=" + apiToken);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建分析提示词
|
|
|
|
|
*/
|
|
|
|
|
private static String createAnalysisPrompt(String dataContent) {
|
|
|
|
|
return "你是一位专业的教育数据分析专家,请基于以下数据对文山州和楚雄州的教育资源配置情况进行深入对比分析。\n\n" +
|
|
|
|
|
"重要要求:请生成结构化的纯文本格式报告,使用清晰的段落结构,不要使用markdown语法。\n\n" +
|
|
|
|
|
"请按照以下结构进行分析并生成专业的分析报告:\n\n" +
|
|
|
|
|
"1. 执行摘要\n" +
|
|
|
|
|
" 简要概述两州教育资源配置的整体情况和主要发现\n\n" +
|
|
|
|
|
"2. 数据概览\n" +
|
|
|
|
|
" 两州基本教育数据对比和关键指标汇总\n\n" +
|
|
|
|
|
"3. 详细对比分析\n" +
|
|
|
|
|
" 教育资源配置水平对比、发展趋势分析、优势与不足分析\n\n" +
|
|
|
|
|
"4. 问题识别\n" +
|
|
|
|
|
" 存在的主要问题和资源配置不均衡情况\n\n" +
|
|
|
|
|
"5. 建议与对策\n" +
|
|
|
|
|
" 针对性改进建议和资源优化配置方案\n\n" +
|
|
|
|
|
"6. 结论\n" +
|
|
|
|
|
" 总体评价和未来发展方向\n\n" +
|
|
|
|
|
"请确保分析客观、专业,数据引用准确,建议具有可操作性。\n\n" +
|
|
|
|
|
"=== 原始数据 ===\n" + dataContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用Apache POI生成Word文档
|
|
|
|
|
*/
|
|
|
|
|
private static void generateWordDocument(String analysisResult, String outputPath, String[] cities) throws Exception {
|
|
|
|
|
XWPFDocument document = new XWPFDocument();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 动态生成标题
|
|
|
|
|
String reportTitle;
|
|
|
|
|
if (cities != null && cities.length >= 2) {
|
|
|
|
|
reportTitle = String.join("与", cities) + "教育资源配置对比分析报告";
|
|
|
|
|
} else {
|
|
|
|
|
reportTitle = "教育资源配置对比分析报告"; // 默认标题
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加标题
|
|
|
|
|
XWPFParagraph titleParagraph = document.createParagraph();
|
|
|
|
|
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
|
|
|
|
|
XWPFRun titleRun = titleParagraph.createRun();
|
|
|
|
|
titleRun.setText(reportTitle);
|
|
|
|
|
titleRun.setBold(true);
|
|
|
|
|
titleRun.setFontSize(18);
|
|
|
|
|
titleRun.setFontFamily("宋体");
|
|
|
|
|
|
|
|
|
|
// 添加空行
|
|
|
|
|
document.createParagraph();
|
|
|
|
|
|
|
|
|
|
// 添加生成信息
|
|
|
|
|
XWPFParagraph infoParagraph = document.createParagraph();
|
|
|
|
|
XWPFRun infoRun = infoParagraph.createRun();
|
|
|
|
|
infoRun.setText("生成时间:" + new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()));
|
|
|
|
|
infoRun.setFontSize(12);
|
|
|
|
|
infoRun.setFontFamily("宋体");
|
|
|
|
|
|
|
|
|
|
XWPFRun toolRun = infoParagraph.createRun();
|
|
|
|
|
toolRun.addBreak();
|
|
|
|
|
toolRun.setText("分析工具:DeepSeek AI");
|
|
|
|
|
toolRun.setFontSize(12);
|
|
|
|
|
toolRun.setFontFamily("宋体");
|
|
|
|
|
|
|
|
|
|
// 添加分隔线
|
|
|
|
|
XWPFParagraph separatorParagraph = document.createParagraph();
|
|
|
|
|
XWPFRun separatorRun = separatorParagraph.createRun();
|
|
|
|
|
separatorRun.setText("=".repeat(50));
|
|
|
|
|
separatorRun.setFontSize(10);
|
|
|
|
|
|
|
|
|
|
// 添加空行
|
|
|
|
|
document.createParagraph();
|
|
|
|
|
|
|
|
|
|
// 处理分析内容
|
|
|
|
|
String[] lines = analysisResult.split("\n");
|
|
|
|
|
XWPFParagraph currentParagraph = null;
|
|
|
|
|
XWPFRun currentRun = null;
|
|
|
|
|
|
|
|
|
|
for (String line : lines) {
|
|
|
|
|
if (line.trim().isEmpty()) {
|
|
|
|
|
// 空行,创建新段落
|
|
|
|
|
document.createParagraph();
|
|
|
|
|
currentParagraph = null;
|
|
|
|
|
currentRun = null;
|
|
|
|
|
} else {
|
|
|
|
|
// 检查是否是标题行(数字开头或特殊格式)
|
|
|
|
|
boolean isTitle = line.matches("^\\d+\\..+") ||
|
|
|
|
|
line.matches("^[一二三四五六七八九十]+、.+") ||
|
|
|
|
|
line.trim().matches("^(执行摘要|数据概览|详细对比分析|问题识别|建议与对策|结论).*");
|
|
|
|
|
|
|
|
|
|
if (currentParagraph == null) {
|
|
|
|
|
currentParagraph = document.createParagraph();
|
|
|
|
|
currentRun = currentParagraph.createRun();
|
|
|
|
|
currentRun.setFontFamily("宋体");
|
|
|
|
|
currentRun.setFontSize(12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isTitle) {
|
|
|
|
|
// 如果当前段落已有内容,创建新段落
|
|
|
|
|
String currentText = currentRun.getText(0);
|
|
|
|
|
if (currentText != null && !currentText.isEmpty()) {
|
|
|
|
|
currentParagraph = document.createParagraph();
|
|
|
|
|
currentRun = currentParagraph.createRun();
|
|
|
|
|
currentRun.setFontFamily("宋体");
|
|
|
|
|
}
|
|
|
|
|
currentRun.setText(line);
|
|
|
|
|
currentRun.setBold(true);
|
|
|
|
|
currentRun.setFontSize(14);
|
|
|
|
|
currentParagraph = null; // 强制下一行创建新段落
|
|
|
|
|
currentRun = null;
|
|
|
|
|
} else {
|
|
|
|
|
// 普通内容
|
|
|
|
|
String currentText = currentRun.getText(0);
|
|
|
|
|
if (currentText != null && !currentText.isEmpty()) {
|
|
|
|
|
currentRun.addBreak();
|
|
|
|
|
}
|
|
|
|
|
currentRun.setText(line);
|
|
|
|
|
currentRun.setFontSize(12);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加结尾
|
|
|
|
|
XWPFParagraph endParagraph = document.createParagraph();
|
|
|
|
|
endParagraph.createRun().addBreak();
|
|
|
|
|
XWPFRun endSeparatorRun = endParagraph.createRun();
|
|
|
|
|
endSeparatorRun.setText("=".repeat(50));
|
|
|
|
|
endSeparatorRun.setFontSize(10);
|
|
|
|
|
|
|
|
|
|
XWPFRun endRun = endParagraph.createRun();
|
|
|
|
|
endRun.addBreak();
|
|
|
|
|
endRun.setText("报告结束");
|
|
|
|
|
endRun.setFontSize(12);
|
|
|
|
|
endRun.setFontFamily("宋体");
|
|
|
|
|
endRun.setBold(true);
|
|
|
|
|
|
|
|
|
|
// 保存文档
|
|
|
|
|
try (FileOutputStream out = new FileOutputStream(outputPath)) {
|
|
|
|
|
document.write(out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
document.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|