|
@@ -23,9 +23,11 @@ import space.anyi.BI.util.BeanCopyUtil;
|
|
import space.anyi.BI.util.ExcelUtils;
|
|
import space.anyi.BI.util.ExcelUtils;
|
|
import space.anyi.BI.util.SecurityUtils;
|
|
import space.anyi.BI.util.SecurityUtils;
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author 杨逸
|
|
* @author 杨逸
|
|
@@ -36,6 +38,78 @@ import java.util.List;
|
|
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
implements ChartService{
|
|
implements ChartService{
|
|
private final static Logger log = LoggerFactory.getLogger(ChartServiceImpl.class);
|
|
private final static Logger log = LoggerFactory.getLogger(ChartServiceImpl.class);
|
|
|
|
+ @Resource
|
|
|
|
+ private ThreadPoolExecutor threadPoolExecutor;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 异步使用AI生成图表
|
|
|
|
+ * @param chartDTO
|
|
|
|
+ * @param file
|
|
|
|
+ * @description:
|
|
|
|
+ * @author: 杨逸
|
|
|
|
+ * @data:2024/12/07 17:43:22
|
|
|
|
+ * @since 1.0.0
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void generateChartByAIAsyn(ChartDTO chartDTO, MultipartFile file) {
|
|
|
|
+
|
|
|
|
+ Chart chart = BeanCopyUtil.copyBean(chartDTO, Chart.class);
|
|
|
|
+ long chartId = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
|
+ chart.setId(chartId);
|
|
|
|
+ chart.setUserId(SecurityUtils.getUserId());
|
|
|
|
+ chart.setState("等待中");
|
|
|
|
+ save(chart);
|
|
|
|
+ //使用线程池优化生成图表的逻辑
|
|
|
|
+ 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("生成中");
|
|
|
|
+ updateById(chart);
|
|
|
|
+
|
|
|
|
+ StringBuilder message = new StringBuilder("原始数据:\n");
|
|
|
|
+ message.append(csvData);
|
|
|
|
+ message.append("分析目标:\n");
|
|
|
|
+ message.append(chartDTO.getAnalysisTarget());
|
|
|
|
+ message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
|
|
+ //配置prompt向AI发送请求
|
|
|
|
+ HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
|
|
+ HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
|
|
+ //解析AI返回的数据
|
|
|
|
+ String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
|
+ log.info("AI返回的数据为:{}", content);
|
|
|
|
+ int index = content.indexOf("```");
|
|
|
|
+ int endIndex = content.lastIndexOf("```");
|
|
|
|
+ if (index == -1 || endIndex == -1){
|
|
|
|
+ chart.setState("失败");
|
|
|
|
+ chart.setExecuteMessage("AI生成图表失败");
|
|
|
|
+ updateById(chart);
|
|
|
|
+ throw new SystemException(500, "AI生成图表失败");
|
|
|
|
+ }
|
|
|
|
+ //数据可视化,Echarts的option代码
|
|
|
|
+ chart.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
|
|
+ index = endIndex;
|
|
|
|
+ //分析结论
|
|
|
|
+ chart.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
|
+ //保存到数据库
|
|
|
|
+ chart.setState("成功");
|
|
|
|
+ chart.setExecuteMessage("AI生成图表成功");
|
|
|
|
+ updateById(chart);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public PageVO getChartPage(Integer pageNum, Integer pageSize, String name, Long userId) {
|
|
public PageVO getChartPage(Integer pageNum, Integer pageSize, String name, Long userId) {
|