按小时范围提供给前端

develop
Angel 11 months ago
parent 4a0c535f79
commit 757fc496d5

@ -13,9 +13,11 @@ import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -31,25 +33,56 @@ public class PeopleVehicleHourFlowController extends BaseController {
@GetMapping @GetMapping
@ApiOperation("查询人车记录表") @ApiOperation("查询人车记录表")
public AjaxResult list(PeopleVehicleHourFlow peopleVehicleHFlow) { public AjaxResult list(@RequestParam String statisticTime, @RequestParam int statistictype) {
try { try {
// 创建一个 TreeMap 用于存储每天的数据量 // 解析前端传递的日期参数
Map<LocalDate, Long> dailyCountsMap = new TreeMap<>(); LocalDate date = LocalDate.parse(statisticTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
List<PeopleVehicleHourFlow> list = peopleVehicleHourFlowService.list(new QueryWrapper<>(peopleVehicleHFlow));
// 构建日期时间范围从当天的00:00到23:59:59
LocalDateTime startDateTime = date.atStartOfDay();
LocalDateTime endDateTime = date.plusDays(1).atStartOfDay().minusSeconds(1);
// 构建查询条件
PeopleVehicleHourFlow queryFlow = new PeopleVehicleHourFlow();
queryFlow.setStartTimeRange(startDateTime);
queryFlow.setEndTimeRange(endDateTime);
queryFlow.setStatisticType(statistictype);
// 查询数据库
List<PeopleVehicleHourFlow> list = peopleVehicleHourFlowService.list(new QueryWrapper<>(queryFlow));
// 处理查询结果
Map<LocalDateTime, Long> hourlyCountsMap = new TreeMap<>();
for (PeopleVehicleHourFlow flow : list) { for (PeopleVehicleHourFlow flow : list) {
String countString = flow.getStatisticCount(); LocalDateTime time = LocalDateTime.parse(flow.getStatisticTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
String timeString = flow.getStatisticTime(); Long count = Long.parseLong(flow.getStatisticCount());
// 将时间字符串转换为 LocalDate hourlyCountsMap.put(time, count);
LocalDate time = LocalDate.parse(timeString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
// 将计数字符串转换为 Long
Long count = Long.parseLong(countString);
dailyCountsMap.put(time, count);
} }
return AjaxResult.success(dailyCountsMap); return AjaxResult.success(hourlyCountsMap);
} catch (Exception e) { } catch (Exception e) {
logger.info(StringKit.toString(e)); logger.info(StringKit.toString(e));
return AjaxResult.error("请求失败,请联系管理员"); return AjaxResult.error("请求失败,请联系管理员");
} }
} }
// public AjaxResult list(PeopleVehicleHourFlow peopleVehicleHFlow) {
// try {
// // 创建一个 TreeMap 用于存储每天的数据量
// Map<LocalDate, Long> dailyCountsMap = new TreeMap<>();
// List<PeopleVehicleHourFlow> list = peopleVehicleHourFlowService.list(new QueryWrapper<>(peopleVehicleHFlow));
// for (PeopleVehicleHourFlow flow : list) {
// String countString = flow.getStatisticCount();
// String timeString = flow.getStatisticTime();
// // 将时间字符串转换为 LocalDate
// LocalDate time = LocalDate.parse(timeString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
//
// // 将计数字符串转换为 Long
// Long count = Long.parseLong(countString);
// dailyCountsMap.put(time, count);
// }
// return AjaxResult.success(dailyCountsMap);
// } catch (Exception e) {
// logger.info(StringKit.toString(e));
// return AjaxResult.error("请求失败,请联系管理员");
// }
// }
} }

@ -9,6 +9,8 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
@Data @Data
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
@TableName(value = "statistics_user_vehicle_hour_info") @TableName(value = "statistics_user_vehicle_hour_info")
@ -41,5 +43,10 @@ public class PeopleVehicleHourFlow {
@ApiModelProperty("日期") @ApiModelProperty("日期")
@TableField(value = "statistic_time") @TableField(value = "statistic_time")
private String statisticTime; private String statisticTime;
// 瞬时字段,不映射到数据库
@TableField(exist = false)
private LocalDateTime startTimeRange;
@TableField(exist = false)
private LocalDateTime endTimeRange;
} }

Loading…
Cancel
Save