|
@@ -49,38 +49,41 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
* @author: 杨逸
|
|
* @author: 杨逸
|
|
* @data:2024/12/07 17:43:22
|
|
* @data:2024/12/07 17:43:22
|
|
* @since 1.0.0
|
|
* @since 1.0.0
|
|
|
|
+ * @return
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public void generateChartByAIAsyn(ChartDTO chartDTO, MultipartFile file) {
|
|
|
|
|
|
+ public long generateChartByAIAsyn(ChartDTO chartDTO, MultipartFile file) {
|
|
|
|
|
|
Chart chart = BeanCopyUtil.copyBean(chartDTO, Chart.class);
|
|
Chart chart = BeanCopyUtil.copyBean(chartDTO, Chart.class);
|
|
long chartId = IdUtil.getSnowflake(1, 1).nextId();
|
|
long chartId = IdUtil.getSnowflake(1, 1).nextId();
|
|
chart.setId(chartId);
|
|
chart.setId(chartId);
|
|
chart.setUserId(SecurityUtils.getUserId());
|
|
chart.setUserId(SecurityUtils.getUserId());
|
|
chart.setState("等待中");
|
|
chart.setState("等待中");
|
|
|
|
+ //读数据
|
|
|
|
+ String csvData = "";
|
|
|
|
+ try {
|
|
|
|
+ csvData = ExcelUtils.excel2csv(file.getInputStream());
|
|
|
|
+ log.info("上传的数据为:\n{}", csvData);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ if (csvData.length()>3000){
|
|
|
|
+ chart.setChartData("");
|
|
|
|
+ chart.setState("失败");
|
|
|
|
+ chart.setExecuteMessage("数据量过大,请上传小于3000行的数据");
|
|
|
|
+ save(chart);
|
|
|
|
+ throw new SystemException(500, "数据量过大,请上传小于3000行的数据");
|
|
|
|
+ }
|
|
|
|
+ chart.setChartData(csvData);
|
|
save(chart);
|
|
save(chart);
|
|
//使用线程池优化生成图表的逻辑
|
|
//使用线程池优化生成图表的逻辑
|
|
|
|
+ String finalCsvData = csvData;
|
|
threadPoolExecutor.execute(()->{
|
|
threadPoolExecutor.execute(()->{
|
|
- //读数据
|
|
|
|
- String csvData = "";
|
|
|
|
- try {
|
|
|
|
- csvData = ExcelUtils.excel2csv(file.getInputStream());
|
|
|
|
- log.info("上传的数据为:\n{}", csvData);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- if (csvData.length()>3000){
|
|
|
|
- chart.setState("失败");
|
|
|
|
- chart.setExecuteMessage("数据量过大,请上传小于3000行的数据");
|
|
|
|
- updateById(chart);
|
|
|
|
- throw new SystemException(500, "数据量过大,请上传小于3000行的数据");
|
|
|
|
- }
|
|
|
|
- chart.setChartData(csvData);
|
|
|
|
chart.setState("生成中");
|
|
chart.setState("生成中");
|
|
- updateById(chart);
|
|
|
|
|
|
+ updateById(chart);
|
|
|
|
|
|
StringBuilder message = new StringBuilder("原始数据:\n");
|
|
StringBuilder message = new StringBuilder("原始数据:\n");
|
|
- message.append(csvData);
|
|
|
|
|
|
+ message.append(finalCsvData);
|
|
message.append("分析目标:\n");
|
|
message.append("分析目标:\n");
|
|
message.append(chartDTO.getAnalysisTarget());
|
|
message.append(chartDTO.getAnalysisTarget());
|
|
message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
@@ -109,6 +112,7 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
updateById(chart);
|
|
updateById(chart);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ return chartId;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -135,6 +139,11 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
public ChartVO generateChartByAI(ChartDTO chartDTO, MultipartFile file) {
|
|
public ChartVO generateChartByAI(ChartDTO chartDTO, MultipartFile file) {
|
|
//读数据
|
|
//读数据
|
|
String csvData = "";
|
|
String csvData = "";
|
|
|
|
+ long charId = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
|
+ Chart chart = BeanCopyUtil.copyBean(chartDTO, Chart.class);
|
|
|
|
+ chart.setId(charId);
|
|
|
|
+ chart.setUserId(SecurityUtils.getUserId());
|
|
|
|
+ chart.setState("等待中");
|
|
try {
|
|
try {
|
|
csvData = ExcelUtils.excel2csv(file.getInputStream());
|
|
csvData = ExcelUtils.excel2csv(file.getInputStream());
|
|
log.info("上传的数据为:\n{}", csvData);
|
|
log.info("上传的数据为:\n{}", csvData);
|
|
@@ -142,37 +151,48 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
if (csvData.length()>3000){
|
|
if (csvData.length()>3000){
|
|
|
|
+ chart.setState("失败");
|
|
|
|
+ chart.setExecuteMessage("数据量过大,请上传小于3000行的数据");
|
|
|
|
+ chart.setChartData("");
|
|
|
|
+ save(chart);
|
|
throw new SystemException(500, "数据量过大,请上传小于3000行的数据");
|
|
throw new SystemException(500, "数据量过大,请上传小于3000行的数据");
|
|
}
|
|
}
|
|
|
|
+ chart.setChartData(csvData);
|
|
|
|
+ save(chart);
|
|
StringBuilder message = new StringBuilder("原始数据:\n");
|
|
StringBuilder message = new StringBuilder("原始数据:\n");
|
|
message.append(csvData);
|
|
message.append(csvData);
|
|
message.append("分析目标:\n");
|
|
message.append("分析目标:\n");
|
|
message.append(chartDTO.getAnalysisTarget());
|
|
message.append(chartDTO.getAnalysisTarget());
|
|
message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
|
|
+ chart.setState("生成中");
|
|
|
|
+ chart.setExecuteMessage("AI正在生成图表");
|
|
|
|
+ updateById(chart);
|
|
//配置prompt向AI发送请求
|
|
//配置prompt向AI发送请求
|
|
HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
//解析AI返回的数据
|
|
//解析AI返回的数据
|
|
- ChartVO chartVO = BeanCopyUtil.copyBean(chartDTO, ChartVO.class);
|
|
|
|
|
|
+ //ChartVO chartVO = BeanCopyUtil.copyBean(chartDTO, ChartVO.class);
|
|
String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
log.info("AI返回的数据为:{}", content);
|
|
log.info("AI返回的数据为:{}", content);
|
|
int index = content.indexOf("```");
|
|
int index = content.indexOf("```");
|
|
int endIndex = content.lastIndexOf("```");
|
|
int endIndex = content.lastIndexOf("```");
|
|
if (index == -1 || endIndex == -1){
|
|
if (index == -1 || endIndex == -1){
|
|
|
|
+ chart.setState("失败");
|
|
|
|
+ chart.setExecuteMessage("AI生成图表失败");
|
|
|
|
+ updateById(chart);
|
|
throw new SystemException(500, "AI生成图表失败");
|
|
throw new SystemException(500, "AI生成图表失败");
|
|
}
|
|
}
|
|
//数据可视化,Echarts的option代码
|
|
//数据可视化,Echarts的option代码
|
|
- chartVO.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
|
|
|
|
+ chart.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
index = endIndex;
|
|
index = endIndex;
|
|
//分析结论
|
|
//分析结论
|
|
- chartVO.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
|
|
|
+ chart.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
|
|
|
- Chart chart = BeanCopyUtil.copyBean(chartVO, Chart.class);
|
|
|
|
- chart.setUserId(SecurityUtils.getUserId());
|
|
|
|
- chart.setChartData(csvData);
|
|
|
|
- chart.setId(IdUtil.getSnowflake(1,1).nextId());
|
|
|
|
- //保存到数据库
|
|
|
|
- save(chart);
|
|
|
|
|
|
+ //更新数据库
|
|
|
|
+ chart.setState("成功");
|
|
|
|
+ chart.setExecuteMessage("图表生成成功");
|
|
|
|
+ updateById(chart);
|
|
|
|
+ ChartVO chartVO = BeanCopyUtil.copyBean(chart, ChartVO.class);
|
|
return chartVO;
|
|
return chartVO;
|
|
}
|
|
}
|
|
}
|
|
}
|