业务场景
可以看到下面高亮部分的代码是依次查询了不同的mapper
当a查询结束才会执行b查询,以此类推,存在查询效率问题
/**
* 首页--基础数据统计
* @return
*/
@Override
public IndexVo getTodayInfo(String beginCreateTime,String endCreateTime) {
/*当天时间区间*/
LocalDate now = LocalDate.now();
LocalDateTime min = LocalDateTime.of(now, LocalTime.MIN);
LocalDateTime max = LocalDateTime.of(now, LocalTime.MAX);
//转换时间类型
String minTime = String.valueOf(min);
String maxTime = String.valueOf(max);
//使用工具类获取username
String username = SecurityUtils.getUsername();
//线索数量
Integer cluesNum = reportMpper.getCluesNum(minTime, maxTime, username);
//获取商机数量
Integer businessNum = reportMpper.getBusinessNum(minTime, maxTime, username);
//获取合同数量
Integer contractNum = reportMpper.getContractNum(minTime, maxTime, username);
//获取合同金额
Double salesAmount = reportMpper.getSalesAmount(minTime, maxTime, username);
//待跟进线索
Integer tofollowedCluesNum = reportMpper.getTofollowedCluesNum(beginCreateTime, endCreateTime, username);
IndexVo indexVo = new IndexVo();
indexVo.setTodayBusinessNum(cluesNum); //商机数量
indexVo.setTodayCluesNum(cluesNum);//线索数量
indexVo.setTodayContractNum(contractNum);//今日合同数目
indexVo.setTodaySalesAmount(salesAmount);//今日销售金额
indexVo.setTofollowedCluesNum(tofollowedCluesNum);//待跟进线索
return indexVo;
}
代码优化
使用CompletableFuture实现并发执行
这里有个细节:在下面代码中,我们省略了 return
关键字,并直接将 reportMpper.getCluesNum(minTime, maxTime, username)
作为返回值。这是合法的,并且与之前的代码是等效的。 请注意,省略 return
关键字只适用于Lambda表达式只有一行代码的情况。如果Lambda表达式有多行代码,则仍然需要使用 return
关键字来明确返回值。
/**
* 首页--基础数据统计
* @return
*/
@Override
public IndexVo getTodayInfo(String beginCreateTime,String endCreateTime) {
/*当天时间区间*/
LocalDate now = LocalDate.now();
LocalDateTime min = LocalDateTime.of(now, LocalTime.MIN);
LocalDateTime max = LocalDateTime.of(now, LocalTime.MAX);
//转换时间类型
String minTime = String.valueOf(min);
String maxTime = String.valueOf(max);
//使用工具类获取username
String username = SecurityUtils.getUsername();
IndexVo indexVo = null;
IndexBaseInfoVO result = new IndexBaseInfoVO();
try {
//线索数量
CompletableFuture<Integer> clueNums = CompletableFuture.supplyAsync(()->{
return reportMpper.getCluesNum(minTime, maxTime, username);
});
//获取商机数量
CompletableFuture<Integer> businessNum = CompletableFuture.supplyAsync(()->{
return reportMpper.getBusinessNum(minTime, maxTime, username);
});
//获取合同数量
CompletableFuture<Integer> contractNum = CompletableFuture.supplyAsync(()->{
return reportMpper.getContractNum(minTime, maxTime, username);
});
//获取合同金额
CompletableFuture<Double> salesAmount = CompletableFuture.supplyAsync(()->{
return reportMpper.getSalesAmount(minTime, maxTime, username);
});
//待跟进线索
Integer tofollowedCluesNum = reportMpper.getTofollowedCluesNum(beginCreateTime, endCreateTime, username);
//3 join等待所有线程全部执行完成
CompletableFuture.allOf(
clueNums,
businessNum,
contractNum,
salesAmount).join();
indexVo = new IndexVo();
indexVo.setTodayBusinessNum(businessNum.get()); //商机数量
indexVo.setTodayCluesNum(clueNums.get());//线索数量
indexVo.setTodayContractNum(contractNum.get());//今日合同数目
indexVo.setTodaySalesAmount(salesAmount.get());//今日销售金额
indexVo.setTofollowedCluesNum(tofollowedCluesNum);//待跟进线索
} catch (Exception e) {
e.printStackTrace();
return null;
}
//返回封装对象
return indexVo;
}