| | |
| | | package com.ruoyi.quartz.task; |
| | | |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import com.ruoyi.system.service.IVehicleMileageStatsService; |
| | | |
| | | /** |
| | | * 车辆里程统计定时任务 |
| | | */ |
| | | @Component("vehicleMileageStatsTask") |
| | | public class VehicleMileageStatsTask { |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(VehicleMileageStatsTask.class); |
| | | |
| | | @Autowired |
| | | private IVehicleMileageStatsService vehicleMileageStatsService; |
| | | |
| | | /** |
| | | * 计算昨日所有车辆的里程统计 |
| | | */ |
| | | public void calculateYesterdayMileage() { |
| | | 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.batchCalculateMileageStats(yesterday); |
| | | |
| | | logger.info("车辆里程统计定时任务执行完成 - 成功统计: {} 辆车", successCount); |
| | | |
| | | } catch (Exception e) { |
| | | logger.error("车辆里程统计定时任务执行失败", e); |
| | | throw new RuntimeException("定时任务执行失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计算指定日期的里程统计(用于手动触发或补算历史数据) |
| | | * |
| | | * @param dateStr 日期字符串,格式:yyyy-MM-dd |
| | | */ |
| | | public void calculateMileageByDate(String dateStr) { |
| | | logger.info("开始执行车辆里程统计定时任务 - 统计日期: {}", dateStr); |
| | | |
| | | try { |
| | | // 解析日期字符串 |
| | | String[] parts = dateStr.split("-"); |
| | | if (parts.length != 3) { |
| | | throw new IllegalArgumentException("日期格式错误,应为: yyyy-MM-dd"); |
| | | } |
| | | |
| | | 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(); |
| | | |
| | | // 批量计算里程统计 |
| | | int successCount = vehicleMileageStatsService.batchCalculateMileageStats(targetDate); |
| | | |
| | | logger.info("车辆里程统计定时任务执行完成 - 日期: {}, 成功统计: {} 辆车", dateStr, successCount); |
| | | |
| | | } catch (Exception e) { |
| | | logger.error("车辆里程统计定时任务执行失败 - 日期: {}", dateStr, e); |
| | | throw new RuntimeException("定时任务执行失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | } |