本周
LocalDate localDate = LocalDate.now();
LocalDate startTime = localDate.with(DayOfWeek.MONDAY);
LocalDate endTime = localDate.with(DayOfWeek.SUNDAY);
List<String> xList = Lists.newArrayList();
List<Integer> yList = Lists.newArrayList();
Map<String, Object> valueMap = new HashMap<>();
List<DeathDetail> list = deathDetailDAO.betwenStartAndEndTime(startTime, endTime);
Map<LocalDate, List<DeathDetail>> map = list.stream().collect(Collectors.groupingBy(e -> e.getDeathTime()));
for (LocalDate d = startTime; d.isBefore(endTime.plusDays(1)); d = d.plusDays(1)) {
String weekName = d.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
xList.add(weekName);
if (!map.isEmpty()) {
List<DeathDetail> deathDetails = map.getOrDefault(d, new ArrayList<>());
yList.add(deathDetails.size());
}
}
valueMap.put("xValue", xList);
valueMap.put("yValue", yList);
return new ObjectResponse<>(valueMap);
本月
LocalDate localDate = LocalDate.now();
LocalDate startTime = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate endTime = localDate.with(TemporalAdjusters.lastDayOfMonth());
List<DeathDetail> list = deathDetailDAO.betwenStartAndEndTime(startTime, endTime);
List<Integer> xList = Lists.newArrayList();
List<Integer> yList = Lists.newArrayList();
Map<Integer, List<DeathDetail>> collect = list.stream().collect(Collectors.groupingBy(o -> o.getDeathTime().getDayOfMonth()));
for (LocalDate d = startTime; d.isBefore(endTime.plusDays(1)); d = d.plusDays(1)) {
int dayOfMonth = d.getDayOfMonth();
xList.add(dayOfMonth);
if (!collect.isEmpty()) {
List<DeathDetail> deathDetailList = collect.getOrDefault(d.getDayOfMonth(), new ArrayList<>());
yList.add(deathDetailList.size());
}
}
Map<String, Object> map = new HashMap<>();
map.put("xValue", xList);
map.put("yValue", yList);
return new ObjectResponse<>(map);
全年12个月
LocalDate localDate = LocalDate.now();
LocalDate startTime = localDate.with(TemporalAdjusters.firstDayOfYear());
LocalDate endTime = localDate.with(TemporalAdjusters.lastDayOfYear());
List<DeathDetail> list = deathDetailDAO.betwenStartAndEndTime(startTime, endTime);
Map<Integer, List<DeathDetail>> collect = list.stream().collect(Collectors.groupingBy(e -> e.getDeathTime().getMonthValue()));
List<String> xList = Lists.newArrayList();
List<Integer> yList = Lists.newArrayList();
int month = 12;
for (int i = 1; i <= month; i++) {
xList.add(i + "月");
if (!collect.isEmpty()) {
List<DeathDetail> details = collect.getOrDefault(i, new ArrayList<>());
yList.add(details.size());
}
}
Map<String, Object> map = new HashMap<>();
map.put("xValue", xList);
map.put("yValue", yList);
return new ObjectResponse<>(map);