大概思路
前端传过来的搜索日期为 yyyy-MM-dd 的形式,我们后台为这个日期拼接上小时,每两个小时为一个时间段。直接到数据库做时间条件查询,这样得到的数据就是两个两个小时的数据
具体代码如下:
List<HashMap<String, Long>> result = new ArrayList<>();
String[] hoursArray = {"00", "02", "04", "06", "08", "10", "12", "14", "16", "18", "20", "22", "00"};
List<String> hours = Arrays.asList(hoursArray);
if (status == 0) {
// 按小时统计商品页分享次数(每两个小时一个阶段)
List<HashMap<String, String>> shareHour = new ArrayList<>();
if (startTime == null || ("").equals(startTime) || endTime == null || ("").equals(endTime)) {
Date date = new Date();
String format = DateUtil.format(date, "yyyy-MM-dd");
for (int i = 1; i < hours.size(); i++) {
HashMap<String, String> mm = new HashMap<>();
startTime = format + " " + hours.get(i - 1);
endTime = format + " " + hours.get(i);
Integer num = productManagerMapper.productShareHour(startTime, endTime);
if (num == null) {
mm.put("num", "0");
mm.put("hour",format + " "+ hours.get(i - 1) + "-" + hours.get(i));
shareHour.add(mm);
} else {
mm.put("num", num + "");
mm.put("hour",format + " "+ hours.get(i - 1) + "-" + hours.get(i));
shareHour.add(mm);
}
}
return new ResponseResult(ResponseMessage.OK, shareHour);
} else {
for (int i = 1; i < hours.size(); i++) {
HashMap<String, String> mm = new HashMap<>();
String start = startTime + " " + hours.get(i - 1);
String end = endTime + " " + hours.get(i);
Integer num = productManagerMapper.productShareHour(start, end);
if (num == null) {
mm.put("num", "0");
mm.put("hour", startTime + " " + hours.get(i - 1) + "-" + hours.get(i));
shareHour.add(mm);
} else {
mm.put("num", num + "");
mm.put("hour", startTime + " " +hours.get(i - 1) + "-" + hours.get(i));
shareHour.add(mm);
}
}
return new ResponseResult(ResponseMessage.OK, shareHour);
}
}
postMan访问效果: