1、查询本月的数据
2、查询最近一个月数据
1、查询本月数据
Date startTime = DateUtil.getStartDayOfMonth();Date endTime = DateUtil.getEndDayOfMonth();//获取日期//[2024-07-01, 2024-07-02, 2024-07-03, 2024-07-04, 2024-07-05, 2024-07-06, 2024-07-07, 2024-07-08, 2024-07-09, 2024-07-10, 2024-07-11, 2024-07-12, 2024-07-13, 2024-07-14, 2024-07-15, 2024-07-16, 2024-07-17, 2024-07-18, 2024-07-19, 2024-07-20, 2024-07-21, 2024-07-22, 2024-07-23, 2024-07-24, 2024-07-25, 2024-07-26, 2024-07-27, 2024-07-28, 2024-07-29, 2024-07-30]List<String> dateList = dateList(startTime ,endTime );List<Map<String, Object>> maps = getMaps(startTime, endTime);
// 根据时间 将指取出计算每个时间的值,放到对应时间的list中。List<Integer> countList = getCountList(dateList, maps);//求和,得到就是30天的访问总数int logSum = getLogSum(countList);//将 datalist 和 countlist,返回出来就是折线图时间统计
public List<String> dateList(Date start,Date end) {List<String> list = new ArrayList<>();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDate startDate = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();LocalDate endDate = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);for (int i = 0; i < numOfDaysBetween; i++) {LocalDate currentDate = startDate.plusDays(i);list.add(currentDate.format(formatter));}return list;}
//将今天的也统计进去private List<Map<String, Object>> getMaps(Date startTime, Date endTime) {//获取每天数量QueryWrapper<LossCalculationLog> queryWrapper = new QueryWrapper<>();queryWrapper.select("count(id) as count,date_format(create_time,'%Y-%m-%d') as date").between("create_time", startTime, endTime).groupBy("date");//转成map集合形式maps = lossCalculationLogService.listMaps(queryWrapper);return maps;}
private List<Integer> getCountList(List<String> dateList, List<Map<String, Object>> maps) {//将一个月的日期初始化List<Integer> countList = new ArrayList<>();for (String date : dateList) {countList.add(0);}if (maps.size() > 0) {for (Map<String, Object> map : maps) {//将map集合中的日期和数量进行匹配int index = dateList.indexOf(map.get("date"));Long count =(Long) map.get("count");countList.set(index,count.intValue());}}return countList;}
private int getLogSum(List<Integer> countList) {if (CollectionUtils.isEmpty(countList)){return 0;}return countList.stream().mapToInt(Integer::intValue).sum();}
2、查询最近一个月数据
//获取现在时间 Date newEndTime = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());LocalDate localDate = now.minusDays(31);//获取一个月之前时间Date newStartTime = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());//获取日期 列表[2024-06-22, 2024-06-23, 2024-06-24, 2024-06-25, 2024-06-26, 2024-06-27, 2024-06-28, 2024-06-29, 2024-06-30, 2024-07-01, 2024-07-02, 2024-07-03, 2024-07-04, 2024-07-05, 2024-07-06, 2024-07-07, 2024-07-08, 2024-07-09, 2024-07-10, 2024-07-11, 2024-07-12, 2024-07-13, 2024-07-14, 2024-07-15, 2024-07-16, 2024-07-17, 2024-07-18, 2024-07-19, 2024-07-20, 2024-07-21, 2024-07-22]List<String> dateList = dateList(newStartTime, newEndTime);List<Map<String, Object>> maps = getMaps(newStartTime, newEndTime);// 根据时间 将指取出,放到list中。List<Integer> countList = getCountList(dateList, maps);//求和int logSum = getLogSum(countList);//将 datalist 和 countlist,返回出来就是折线图时间统计
//调用方法和第一种一样。
//今天不进行统计private List<Map<String, Object>> getMaps(Date startTime, Date endTime) {//获取当前的时间----2024-07-23,将时间格式进行转换 LocalDate localDate = LocalDate.now();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");String date = localDate.format(formatter);//获取每天数量QueryWrapper<LossCalculationLog> queryWrapper = new QueryWrapper<>();queryWrapper.select("count(id) as count,date_format(create_time,'%Y-%m-%d') as date").between("create_time", startTime, endTime).groupBy("date");//转成map集合形式List<Map<String, Object>> maps = lossCalculationLogService.listMaps(queryWrapper);//今天不进行统计maps.removeIf(map -> map.get("date").equals(date));return maps;}