mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 19:15:35 +08:00
update
This commit is contained in:
@@ -1281,82 +1281,98 @@ public class TempService {
|
||||
* @return
|
||||
*/
|
||||
public OrderCountByTimeVO queryOrderCountByTime(QueryOrderDTO dto) {
|
||||
// 处理时间默认值
|
||||
if (dto == null) {
|
||||
dto = new QueryOrderDTO();
|
||||
}
|
||||
|
||||
// 如果开始时间为空,则默认为当天开始时间
|
||||
if (dto.getStartTime() == null || dto.getStartTime().isEmpty()) {
|
||||
LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
|
||||
dto.setStartTime(todayStart.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
}
|
||||
|
||||
// 如果结束时间为空,则默认为当前时间
|
||||
if (dto.getEndTime() == null || dto.getEndTime().isEmpty()) {
|
||||
dto.setEndTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
}
|
||||
|
||||
//条件查询订单
|
||||
List<OrderListVO> orderListVOS = orderBasicInfoMapper.selectOrderBasicInfoList(dto);
|
||||
|
||||
//统计订单
|
||||
List<OrderCountByTimeVO.OrderCountByTimeListVO> result = new ArrayList<>();
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS);
|
||||
try {
|
||||
// 将开始时间和结束时间转换为LocalDateTime类型
|
||||
LocalDateTime queryStartTime = LocalDateTime.parse(dto.getStartTime(), dateTimeFormatter);
|
||||
LocalDateTime queryEndTime = LocalDateTime.parse(dto.getEndTime(), dateTimeFormatter);
|
||||
|
||||
// 开始时间默认:当天零点
|
||||
if (StringUtils.isEmpty(dto.getStartTime())) {
|
||||
LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
|
||||
dto.setStartTime(todayStart.format(dateTimeFormatter));
|
||||
}
|
||||
|
||||
// 结束时间默认:当前时间
|
||||
if (StringUtils.isEmpty(dto.getEndTime())) {
|
||||
dto.setEndTime(LocalDateTime.now().format(dateTimeFormatter));
|
||||
}
|
||||
|
||||
// ====== 查询订单数据 ======
|
||||
List<OrderListVO> orderList = orderBasicInfoMapper.selectOrderBasicInfoList(dto);
|
||||
|
||||
// ====== 统计逻辑 ======
|
||||
List<OrderCountByTimeVO.OrderCountByTimeListVO> result = new ArrayList<>();
|
||||
BigDecimal totalInsuranceAmount = BigDecimal.ZERO; // 总保险金额
|
||||
|
||||
try {
|
||||
// 转换查询时间
|
||||
LocalDateTime queryStart = LocalDateTime.parse(dto.getStartTime(), dateTimeFormatter);
|
||||
LocalDateTime queryEnd = LocalDateTime.parse(dto.getEndTime(), dateTimeFormatter);
|
||||
|
||||
// 按小时分组订单
|
||||
Map<LocalDateTime, List<OrderListVO>> ordersByHour = orderListVOS.stream()
|
||||
Map<LocalDateTime, List<OrderListVO>> ordersByHour = orderList.stream()
|
||||
.collect(Collectors.groupingBy(order -> {
|
||||
LocalDateTime createTime;
|
||||
// 如果订单创建时间为空,则默认为当前时间
|
||||
if (order.getCreateTime() != null) {
|
||||
createTime = LocalDateTime.parse((String) order.getCreateTime(), dateTimeFormatter);
|
||||
return LocalDateTime.parse(order.getCreateTime() , dateTimeFormatter)
|
||||
.withMinute(0).withSecond(0).withNano(0);
|
||||
} else {
|
||||
logger.warn("订单创建时间为空,订单号:{}", order.getOrderCode());
|
||||
return LocalDateTime.now();
|
||||
return LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
|
||||
}
|
||||
// 将订单创建时间按小时分组
|
||||
return createTime.withMinute(0).withSecond(0).withNano(0);
|
||||
}));
|
||||
|
||||
|
||||
// 从开始时间开始,按小时统计订单数量
|
||||
LocalDateTime currentHour = queryStartTime.withMinute(0).withSecond(0).withNano(0);
|
||||
while (currentHour.isBefore(queryEndTime)) {
|
||||
// 查询当前小时订单数量
|
||||
// 从开始时间到结束时间,逐小时统计
|
||||
LocalDateTime currentHour = queryStart.withMinute(0).withSecond(0).withNano(0);
|
||||
while (currentHour.isBefore(queryEnd)) {
|
||||
LocalDateTime nextHour = currentHour.plusHours(1);
|
||||
int count = ordersByHour.getOrDefault(currentHour, Collections.emptyList()).size();
|
||||
|
||||
// 当前小时的订单集合
|
||||
List<OrderListVO> ordersInHour = ordersByHour.getOrDefault(currentHour, Collections.emptyList());
|
||||
int count = ordersInHour.size();
|
||||
|
||||
// 统计该小时保险金额
|
||||
BigDecimal hourInsuranceAmount = ordersInHour.stream()
|
||||
.map(OrderListVO::getInsuranceAmount) // 假设 VO 里有该字段
|
||||
.filter(Objects::nonNull)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 封装结果
|
||||
OrderCountByTimeVO.OrderCountByTimeListVO vo = new OrderCountByTimeVO.OrderCountByTimeListVO();
|
||||
vo.setStartTime(currentHour.format(dateTimeFormatter));
|
||||
vo.setEndTime(nextHour.format(dateTimeFormatter));
|
||||
vo.setCount(count);
|
||||
vo.setOrderCount(count);
|
||||
vo.setInsuranceAmount(hourInsuranceAmount);
|
||||
|
||||
result.add(vo);
|
||||
|
||||
// 累加总保险金额
|
||||
totalInsuranceAmount = totalInsuranceAmount.add(hourInsuranceAmount);
|
||||
|
||||
currentHour = nextHour;
|
||||
}
|
||||
|
||||
// 按开始时间排序
|
||||
// 排序(确保结果有序)
|
||||
result.sort(Comparator.comparing(OrderCountByTimeVO.OrderCountByTimeListVO::getStartTime));
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("统计订单数量失败", e);
|
||||
}
|
||||
logger.info("查询订单数量结果:{}", JSONObject.toJSONString(result));
|
||||
|
||||
// ====== 封装返回结果 ======
|
||||
OrderCountByTimeVO orderCountByTimeVO = new OrderCountByTimeVO();
|
||||
orderCountByTimeVO.setOrderCountByTimeList(result);
|
||||
|
||||
//订单总数
|
||||
Integer totalCount = 0;
|
||||
// 计算总订单数
|
||||
int totalCount = 0;
|
||||
for (OrderCountByTimeVO.OrderCountByTimeListVO vo : result) {
|
||||
totalCount += vo.getCount();
|
||||
}
|
||||
orderCountByTimeVO.setTotalCount(totalCount);
|
||||
orderCountByTimeVO.setTotalInsuranceAmount(totalInsuranceAmount);
|
||||
|
||||
logger.info("查询订单数量结果:{}", JSONObject.toJSONString(orderCountByTimeVO));
|
||||
return orderCountByTimeVO;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user