| | |
| | | @Override |
| | | public int batchCalculateMileageStats(Date statDate) { |
| | | try { |
| | | // 计算查询开始时间(7天前) |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(statDate); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -7); |
| | | Date startTime = calendar.getTime(); |
| | | |
| | | // 查询所有活跃车辆 |
| | | List<Long> vehicleIds = vehicleGpsMapper.selectActiveVehicleIds(); |
| | | List<Long> vehicleIds = vehicleGpsMapper.selectActiveVehicleIds(startTime); |
| | | |
| | | if (vehicleIds == null || vehicleIds.isEmpty()) { |
| | | logger.info("没有找到活跃车辆"); |
| | |
| | | // 计算任务里程占比 |
| | | if (result.totalMileage.compareTo(BigDecimal.ZERO) > 0) { |
| | | result.taskRatio = result.taskMileage.divide(result.totalMileage, 4, RoundingMode.HALF_UP); |
| | | |
| | | // 数据校验:占比应在0-1之间 |
| | | if (result.taskRatio.compareTo(BigDecimal.ONE) > 0) { |
| | | logger.warn("任务里程占比异常: {} (任务里程:{}, 总里程:{}), 强制设为1.0", |
| | | result.taskRatio, result.taskMileage, result.totalMileage); |
| | | result.taskRatio = BigDecimal.ONE; |
| | | } else if (result.taskRatio.compareTo(BigDecimal.ZERO) < 0) { |
| | | logger.warn("任务里程占比为负: {}, 强制设为0", result.taskRatio); |
| | | result.taskRatio = BigDecimal.ZERO; |
| | | } |
| | | } |
| | | |
| | | // 保留两位小数 |
| | |
| | | BigDecimal taskRatio = BigDecimal.ZERO; |
| | | if (totalMileage.compareTo(BigDecimal.ZERO) > 0) { |
| | | taskRatio = taskMileage.divide(totalMileage, 4, RoundingMode.HALF_UP); |
| | | |
| | | // 数据校验:占比应在0-1之间,如果超出说明数据异常 |
| | | if (taskRatio.compareTo(BigDecimal.ONE) > 0) { |
| | | logger.warn("车辆ID: {} 日期: {} 任务里程占比异常: {} (任务里程:{}, 总里程:{}), 强制设为1.0", |
| | | vehicleId, statDate, taskRatio, taskMileage, totalMileage); |
| | | taskRatio = BigDecimal.ONE; |
| | | } else if (taskRatio.compareTo(BigDecimal.ZERO) < 0) { |
| | | logger.warn("车辆ID: {} 日期: {} 任务里程占比为负: {}, 强制设为0", |
| | | vehicleId, statDate, taskRatio); |
| | | taskRatio = BigDecimal.ZERO; |
| | | } |
| | | } |
| | | |
| | | // 5. 查询或创建统计记录 |
| | |
| | | @Override |
| | | public int batchAggregateFromSegmentMileage(Date statDate) { |
| | | try { |
| | | // 计算查询开始时间(7天前) |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(statDate); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -7); |
| | | Date startTime = calendar.getTime(); |
| | | |
| | | // 查询所有活跃车辆 |
| | | List<Long> vehicleIds = vehicleGpsMapper.selectActiveVehicleIds(); |
| | | List<Long> vehicleIds = vehicleGpsMapper.selectActiveVehicleIds(startTime); |
| | | |
| | | if (vehicleIds == null || vehicleIds.isEmpty()) { |
| | | logger.info("没有找到活跃车辆"); |