Compare commits

...

2 Commits

Author SHA1 Message Date
ffc330485f Merge pull request '大屏接口速率优化提升' (#60) from zzw-one into master
All checks were successful
continuous-integration/drone/tag Build is passing
Reviewed-on: #60
2024-08-29 17:23:12 +08:00
47051ef8a2 大屏接口速率优化提升 2024-08-29 16:55:47 +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}}")
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.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
@ -31,27 +28,60 @@ public class CheckTicketServiceImpl implements CheckTicketService {
@Override
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();
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<>();
Map<String,String> mapName = 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);
mapName.put("date",begintime+"-"+resulttime);
mapName.put("count",countMap.get("map"+begintime.substring(0,2)).toString());
mapList.add(mapName);
}
return mapList;
}
@ -69,4 +99,17 @@ public class CheckTicketServiceImpl implements CheckTicketService {
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;
}
}