大屏接口速率优化提升 #60

Merged
root merged 1 commits from zzw-one into master 2024-08-29 17:23:13 +08:00
2 changed files with 62 additions and 16 deletions

View File

@ -24,4 +24,7 @@ public interface CheckTicketRepository extends MongoRepository<CheckTicket,Strin
// @Query("{'checkticketdate':{'$gte': ?0, '$lt': ?1} , 'checktickettime':{'$gte': ?2, '$lt': ?3}}") // @Query("{'checkticketdate':{'$gte': ?0, '$lt': ?1} , 'checktickettime':{'$gte': ?2, '$lt': ?3}}")
Long countByCheckticketdateBetweenAndChecktickettimeBetween(String startTime, String endTime,String beingTime, String resultTime); Long countByCheckticketdateBetweenAndChecktickettimeBetween(String startTime, String endTime,String beingTime, String resultTime);
@Query(value = "{'checkticketdate':{'$gte': ?0, '$lt': ?1}}", fields = "{ 'checktickettime': 1 }")
List<CheckTicket> findAllByCheckticketdateBetween(String startTime, String endTime);
} }

View File

@ -10,10 +10,7 @@ import javax.annotation.Resource;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** /**
@ -31,27 +28,60 @@ public class CheckTicketServiceImpl implements CheckTicketService {
@Override @Override
public List<Map<String, String>> findbytimetemp(String starTime, String endTime) { public List<Map<String, String>> findbytimetemp(String starTime, String endTime) {
// LocalDateTime startTime = LocalDateTime.now();
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// List<Map<String, String>> mapList = new ArrayList<>();
//
// for (int i = 0; i < 24; i++) {
// Map<String,String>map =new LinkedHashMap<>();
// LocalDateTime hourStartTime = startTime.withHour(i).withMinute(0).withSecond(0).withNano(0);
// LocalDateTime hourEndTime = hourStartTime.plusHours(1);
// String begintime = formatter.format(hourStartTime);
// String resulttime = formatter.format(hourEndTime);
// Long checkTickets;
// if (starTime != null && endTime != null && starTime.equals(endTime)){
// checkTickets = checkTicketRepository.countByCheckticketdateAndChecktickettimeBetween(starTime,begintime,resulttime);
// }else {
// checkTickets = checkTicketRepository.countByCheckticketdateBetweenAndChecktickettimeBetween(starTime,endTime,begintime,resulttime);
// }
// map.put("date",begintime+"-"+resulttime);
// map.put("count", String.valueOf(checkTickets));
// mapList.add(map);
// }
//最终数据源
List<Map<String, String>> mapList = new ArrayList<>();
//临时数据载体
HashMap<String, Integer> countMap = new HashMap<>();
for (int i = 0; i < 24; i++) {
countMap.put("map"+String.format("%02d", Integer.parseInt(i+"")),0);
}
//查询该时间区域的所有数据
List<CheckTicket> allByCheckticketdateBetween = checkTicketRepository.findAllByCheckticketdateBetween(starTime, this.publicMethod(endTime));
//分析数据
for (CheckTicket checkTicket : allByCheckticketdateBetween) {
String checktickettime = checkTicket.getChecktickettime();
String result = checktickettime.substring(0, 2);
Integer numberCount = countMap.get("map" + result);
countMap.put("map" + result, numberCount+1);
}
//构造数据结构
LocalDateTime startTime = LocalDateTime.now(); LocalDateTime startTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
List<Map<String, String>> mapList = new ArrayList<>();
for (int i = 0; i < 24; i++) { for (int i = 0; i < 24; i++) {
Map<String,String>map =new LinkedHashMap<>(); Map<String,String> mapName = new LinkedHashMap<>();
LocalDateTime hourStartTime = startTime.withHour(i).withMinute(0).withSecond(0).withNano(0); LocalDateTime hourStartTime = startTime.withHour(i).withMinute(0).withSecond(0).withNano(0);
LocalDateTime hourEndTime = hourStartTime.plusHours(1); LocalDateTime hourEndTime = hourStartTime.plusHours(1);
String begintime = formatter.format(hourStartTime); String begintime = formatter.format(hourStartTime);
String resulttime = formatter.format(hourEndTime); String resulttime = formatter.format(hourEndTime);
Long checkTickets; mapName.put("date",begintime+"-"+resulttime);
if (starTime != null && endTime != null && starTime.equals(endTime)){ mapName.put("count",countMap.get("map"+begintime.substring(0,2)).toString());
checkTickets = checkTicketRepository.countByCheckticketdateAndChecktickettimeBetween(starTime,begintime,resulttime); mapList.add(mapName);
}else {
checkTickets = checkTicketRepository.countByCheckticketdateBetweenAndChecktickettimeBetween(starTime,endTime,begintime,resulttime);
}
map.put("date",begintime+"-"+resulttime);
map.put("count", String.valueOf(checkTickets));
mapList.add(map);
} }
return mapList; return mapList;
} }
@ -69,4 +99,17 @@ public class CheckTicketServiceImpl implements CheckTicketService {
return checkTicketRepository.countByCheckticketdateBetween(starTime,formattedNextDay).size(); return checkTicketRepository.countByCheckticketdateBetween(starTime,formattedNextDay).size();
} }
public String publicMethod(String endTime){
// 创建 DateTimeFormatter 实例以解析指定日期的格式
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// 解析指定日期字符串为 LocalDate 对象
LocalDate specifiedDate = LocalDate.parse(endTime, inputFormatter);
// 获取指定日期的下一天
LocalDate nextDay = specifiedDate.plusDays(1);
// 格式化指定日期和下一天
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedNextDay = nextDay.format(outputFormatter);
return formattedNextDay;
}
} }