| | |
| | | package com.ruoyi.quartz.task; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import org.slf4j.Logger; |
| | |
| | | logger.info("开始执行车辆里程统计定时任务 - 统计日期: {}", dateStr); |
| | | |
| | | try { |
| | | // 解析日期字符串 |
| | | String[] parts = dateStr.split("-"); |
| | | if (parts.length != 3) { |
| | | throw new IllegalArgumentException("日期格式错误,应为: yyyy-MM-dd"); |
| | | if (dateStr == null || dateStr.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("日期不能为空"); |
| | | } |
| | | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.set(Calendar.YEAR, Integer.parseInt(parts[0])); |
| | | calendar.set(Calendar.MONTH, Integer.parseInt(parts[1]) - 1); // 月份从0开始 |
| | | calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parts[2])); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | Date targetDate = calendar.getTime(); |
| | | // 使用SimpleDateFormat解析日期字符串 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | sdf.setLenient(false); // 严格解析日期 |
| | | Date targetDate = sdf.parse(dateStr.trim()); |
| | | |
| | | // 批量计算里程统计 |
| | | int successCount = vehicleMileageStatsService.batchCalculateMileageStats(targetDate); |
| | | |
| | | logger.info("车辆里程统计定时任务执行完成 - 日期: {}, 成功统计: {} 辆车", dateStr, successCount); |
| | | |
| | | } catch (ParseException e) { |
| | | logger.error("车辆里程统计定时任务执行失败 - 日期格式错误: {}", dateStr, e); |
| | | throw new RuntimeException("定时任务执行失败: 日期格式错误,应为 yyyy-MM-dd"); |
| | | } catch (Exception e) { |
| | | logger.error("车辆里程统计定时任务执行失败 - 日期: {}", dateStr, e); |
| | | throw new RuntimeException("定时任务执行失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从GPS分段里程汇总生成昨日统计数据 |
| | | * (推荐使用此方法,基于已计算的分段里程数据汇总,性能更好) |
| | | */ |
| | | public void aggregateYesterdayFromSegments() { |
| | | logger.info("开始执行从分段里程汇总任务 - 统计昨日数据"); |
| | | |
| | | try { |
| | | // 获取昨天的日期 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | Date yesterday = calendar.getTime(); |
| | | |
| | | // 从分段里程汇总生成统计 |
| | | int successCount = vehicleMileageStatsService.batchAggregateFromSegmentMileage(yesterday); |
| | | |
| | | logger.info("从分段里程汇总任务执行完成 - 成功统计: {} 辆车", successCount); |
| | | |
| | | } catch (Exception e) { |
| | | logger.error("从分段里程汇总任务执行失败", e); |
| | | throw new RuntimeException("汇总任务执行失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从GPS分段里程汇总生成指定日期的统计数据 |
| | | * |
| | | * @param dateStr 日期字符串,格式:yyyy-MM-dd |
| | | */ |
| | | public void aggregateFromSegmentsByDate(String dateStr) { |
| | | logger.info("开始执行从分段里程汇总任务 - 统计日期: {}", dateStr); |
| | | |
| | | try { |
| | | if (dateStr == null || dateStr.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("日期不能为空"); |
| | | } |
| | | |
| | | // 使用SimpleDateFormat解析日期字符串 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | sdf.setLenient(false); // 严格解析日期 |
| | | Date targetDate = sdf.parse(dateStr.trim()); |
| | | |
| | | // 从分段里程汇总生成统计 |
| | | int successCount = vehicleMileageStatsService.batchAggregateFromSegmentMileage(targetDate); |
| | | |
| | | logger.info("从分段里程汇总任务执行完成 - 日期: {}, 成功统计: {} 辆车", dateStr, successCount); |
| | | |
| | | } catch (ParseException e) { |
| | | logger.error("从分段里程汇总任务执行失败 - 日期格式错误: {}", dateStr, e); |
| | | throw new RuntimeException("汇总任务执行失败: 日期格式错误,应为 yyyy-MM-dd"); |
| | | } catch (Exception e) { |
| | | logger.error("从分段里程汇总任务执行失败 - 日期: {}", dateStr, e); |
| | | throw new RuntimeException("汇总任务执行失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | } |