Browse Source

添加交通断面数据缓存恢复功能

develop
xiepufeng 11 months ago
parent
commit
971ca5045b
  1. 131
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficSectionDataServiceImpl.java
  2. 5
      zc-business/src/main/java/com/zc/business/statistics/cache/AbstractTrafficStatisticsCache.java
  3. 14
      zc-business/src/main/java/com/zc/business/statistics/cache/DailyTrafficStatisticsCache.java
  4. 14
      zc-business/src/main/java/com/zc/business/statistics/cache/MonthlyTrafficStatisticsCache.java
  5. 14
      zc-business/src/main/java/com/zc/business/statistics/cache/QuarterlyTrafficStatisticsCache.java
  6. 14
      zc-business/src/main/java/com/zc/business/statistics/cache/YearlyTrafficStatisticsCache.java
  7. 2
      zc-business/src/main/resources/mapper/business/DcTrafficSectionDataMapper.xml

131
zc-business/src/main/java/com/zc/business/service/impl/DcTrafficSectionDataServiceImpl.java

@ -1,9 +1,9 @@
package com.zc.business.service.impl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.zc.business.domain.DcTrafficSectionData;
import com.zc.business.enums.TrafficDataPeriodTypeEnum;
import com.zc.business.statistics.cache.*;
@ -13,12 +13,16 @@ import com.zc.business.statistics.handler.RealtimeTrafficStatistics;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
/**
* 通断面数据服务实现类负责处理实时设备消息缓存数据定时任务以及数据保存等功能
* 交通断面数据服务实现类负责处理实时设备消息缓存数据定时任务以及数据保存等功能
*
* @author xiepufeng
*/
@Service
@ -29,6 +33,60 @@ public class DcTrafficSectionDataServiceImpl
@Resource
private DcTrafficSectionDataMapper dcTrafficSectionDataMapper;
/**
* 初始化方法用于在对象创建后恢复各种周期的交通数据缓存
* 该方法标注了@PostConstruct注解确保在依赖注入完成后调用
*/
@PostConstruct
public void init() {
// TODO 恢复每天交通数据缓存(es获取数据)
recoveryMonthlyCache(); // 恢复每月交通数据缓存
recoveryQuarterlyCache(); // 恢复每季度交通数据缓存
recoveryYearlyCache(); // 恢复每年交通数据缓存
}
/**
* 恢复每月交通数据缓存的方法
* 通过查询当前月份至今的每日交通数据并将其添加到每月交通统计缓存中
*/
private void recoveryMonthlyCache() {
// 构建查询条件,查询当前月份至今的每日交通数据
LambdaQueryWrapper<DcTrafficSectionData> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DcTrafficSectionData::getPeriodType, TrafficDataPeriodTypeEnum.DAY);
queryWrapper.between(DcTrafficSectionData::getStatisticalDate, DateUtil.beginOfMonth(new Date()), new Date());
List<DcTrafficSectionData> dcTrafficSectionDataList = this.list(queryWrapper);
// 遍历查询结果,将每日数据添加到每月交通统计缓存
dcTrafficSectionDataList.forEach(MonthlyTrafficStatisticsCache::addCacheData);
}
/**
* 恢复每季度交通数据缓存的方法
* 通过查询当前季度至今的每月交通数据并将其添加到每季度交通统计缓存中
*/
private void recoveryQuarterlyCache() {
// 构建查询条件,查询当前季度至今的每月交通数据
LambdaQueryWrapper<DcTrafficSectionData> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DcTrafficSectionData::getPeriodType, TrafficDataPeriodTypeEnum.MONTH);
queryWrapper.between(DcTrafficSectionData::getStatisticalDate, DateUtil.beginOfQuarter(new Date()), new Date());
List<DcTrafficSectionData> dcTrafficSectionDataList = this.list(queryWrapper);
// 遍历查询结果,将每月数据添加到每季度交通统计缓存
dcTrafficSectionDataList.forEach(QuarterlyTrafficStatisticsCache::addCacheData);
}
/**
* 恢复每年交通数据缓存的方法
* 通过查询当前年份至今的每季度交通数据并将其添加到每年交通统计缓存中
*/
private void recoveryYearlyCache() {
// 构建查询条件,查询当前年份至今的每季度交通数据
LambdaQueryWrapper<DcTrafficSectionData> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DcTrafficSectionData::getPeriodType, TrafficDataPeriodTypeEnum.QUARTER);
queryWrapper.between(DcTrafficSectionData::getStatisticalDate, DateUtil.beginOfYear(new Date()), new Date());
List<DcTrafficSectionData> dcTrafficSectionDataList = this.list(queryWrapper);
// 遍历查询结果,将每季度数据添加到每年交通统计缓存
dcTrafficSectionDataList.forEach(YearlyTrafficStatisticsCache::addCacheData);
}
/**
* 处理实时接收到的设备消息并将其转换为交通断面统计数据对象并缓存
*
@ -79,7 +137,7 @@ public class DcTrafficSectionDataServiceImpl
// 添加年交通断面数据到缓存中
persistAggregatedData(QuarterlyTrafficStatisticsCache.getCache(), TrafficDataPeriodTypeEnum.QUARTER, YearlyTrafficStatisticsCache::addCacheData);
// 将缓存中的数据按年统计后保存至数据库
persistAggregatedData(YearlyTrafficStatisticsCache.getCache(), TrafficDataPeriodTypeEnum.YEAR, (a) -> {});
persistAggregatedData(YearlyTrafficStatisticsCache.getCache(), TrafficDataPeriodTypeEnum.YEAR, (a) -> {});
}
/**
@ -105,69 +163,4 @@ public class DcTrafficSectionDataServiceImpl
}
}
/**
* 将计算给定列表中所有交通断面数据的统计结果保存至数据库
*/
public boolean persistData(DcTrafficSectionData aggregatedData) {
if (aggregatedData == null) {
return false;
}
// 创建更新条件封装器
LambdaUpdateWrapper<DcTrafficSectionData> updateWrapper = new LambdaUpdateWrapper<>();
// 设置更新条件:根据设备ID、方向、时段类型、桩号、统计时间查找记录
updateWrapper.eq(DcTrafficSectionData::getDeviceId, aggregatedData.getDeviceId());
// 方向
updateWrapper.eq(DcTrafficSectionData::getDirection, aggregatedData.getDirection());
// 时段类型
updateWrapper.eq(DcTrafficSectionData::getPeriodType, aggregatedData.getPeriodType());
// 桩号
updateWrapper.eq(DcTrafficSectionData::getStakeMark, aggregatedData.getStakeMark());
// 统计时间
updateWrapper.eq(DcTrafficSectionData::getStatisticalDate, aggregatedData.getStatisticalDate());
if (this.update(aggregatedData, updateWrapper)) {
return true;
} else {
// 更新失败则尝试插入
aggregatedData.setCreateTime(DateUtils.getNowDate());
return this.save(aggregatedData);
}
}
/* public boolean persistData(DcTrafficSectionData aggregatedData) {
if (aggregatedData == null) {
return false;
}
// 创建更新条件封装器
LambdaUpdateWrapper<DcTrafficSectionData> updateWrapper = new LambdaUpdateWrapper<>();
// 设置更新条件:根据设备ID、方向、时段类型、桩号、统计时间查找记录
updateWrapper.eq(DcTrafficSectionData::getDeviceId, aggregatedData.getDeviceId());
// 方向
updateWrapper.eq(DcTrafficSectionData::getDirection, aggregatedData.getDirection());
// 时段类型
updateWrapper.eq(DcTrafficSectionData::getPeriodType, aggregatedData.getPeriodType());
// 桩号
updateWrapper.eq(DcTrafficSectionData::getStakeMark, aggregatedData.getStakeMark());
// 统计时间
updateWrapper.eq(DcTrafficSectionData::getStatisticalDate, aggregatedData.getStatisticalDate());
if (this.update(aggregatedData, updateWrapper)) {
return true;
} else {
// 更新失败则尝试插入
aggregatedData.setCreateTime(DateUtils.getNowDate());
return this.save(aggregatedData);
}
}*/
}

5
zc-business/src/main/java/com/zc/business/statistics/cache/AbstractTrafficStatisticsCache.java

@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
/**
* 交通断面数据缓存定义
@ -44,4 +45,8 @@ public abstract class AbstractTrafficStatisticsCache {
* 存储具体交通断面数据的列表
*/
private Collection<DcTrafficSectionData> data;
public AbstractTrafficStatisticsCache() {
this.data = new HashSet<>();
}
}

14
zc-business/src/main/java/com/zc/business/statistics/cache/DailyTrafficStatisticsCache.java

@ -32,8 +32,6 @@ public class DailyTrafficStatisticsCache extends AbstractTrafficStatisticsCache
// 私有构造函数,确保只能通过静态方法获取实例
private DailyTrafficStatisticsCache() {
super();
this.setData(new ArrayList<>());
}
/**
@ -83,11 +81,11 @@ public class DailyTrafficStatisticsCache extends AbstractTrafficStatisticsCache
}
/**
* 该方法用于生成一个基于DcTrafficSectionData对象属性的唯一键字符串
* 这个键由设备ID与统计日期两部分组成通常用于索引或标识特定设备在某一日期的交通流量数据
* 生成缓存键
* 该方法通过设备ID统计日期和道路方向生成一个唯一的缓存键用于存储或检索特定条件下的交通数据
*
* @param dcTrafficSectionData DcTrafficSectionData类型的对象包含设备ID和统计日期等信息
* @return 格式化后的字符串键格式为 "deviceId|formattedDate"
* @param dcTrafficSectionData 包含设备ID统计日期和道路方向的交通段数据对象
* @return 返回一个字符串形式的缓存键由设备ID格式化的统计日期和道路方向以"|"字符连接而成
*/
public static String generateCacheKey(DcTrafficSectionData dcTrafficSectionData) {
// 获取设备ID
@ -96,8 +94,8 @@ public class DailyTrafficStatisticsCache extends AbstractTrafficStatisticsCache
// 获取并格式化统计日期
String formattedDate = formatDate(dcTrafficSectionData.getStatisticalDate());
// 使用"|"字符连接设备ID和日期,形成唯一键
return deviceId + "|" + formattedDate;
// 使用"|"字符连接设备ID、格式化的日期和道路方向,形成唯一键
return deviceId + "|" + formattedDate + "|" + dcTrafficSectionData.getDirection();
}
/**

14
zc-business/src/main/java/com/zc/business/statistics/cache/MonthlyTrafficStatisticsCache.java

@ -32,8 +32,6 @@ public class MonthlyTrafficStatisticsCache extends AbstractTrafficStatisticsCach
// 私有构造函数,确保只能通过静态方法获取实例
private MonthlyTrafficStatisticsCache() {
super();
this.setData(new HashSet<>());
}
/**
@ -83,11 +81,11 @@ public class MonthlyTrafficStatisticsCache extends AbstractTrafficStatisticsCach
}
/**
* 该方法用于生成一个基于DcTrafficSectionData对象属性的唯一键字符串
* 这个键由设备ID与统计日期两部分组成通常用于索引或标识特定设备在某一日期的交通流量数据
* 生成缓存键
* 该方法通过设备ID统计日期和道路方向生成一个唯一的缓存键用于存储或检索特定条件下的交通数据
*
* @param dcTrafficSectionData DcTrafficSectionData类型的对象包含设备ID和统计日期等信息
* @return 格式化后的字符串键格式为 "deviceId|formattedDate"
* @param dcTrafficSectionData 包含设备ID统计日期和道路方向的交通段数据对象
* @return 返回一个字符串形式的缓存键由设备ID格式化的统计日期和道路方向以"|"字符连接而成
*/
public static String generateCacheKey(DcTrafficSectionData dcTrafficSectionData) {
// 获取设备ID
@ -96,8 +94,8 @@ public class MonthlyTrafficStatisticsCache extends AbstractTrafficStatisticsCach
// 获取并格式化统计日期
String formattedDate = formatDate(dcTrafficSectionData.getStatisticalDate());
// 使用"|"字符连接设备ID和日期,形成唯一键
return deviceId + "|" + formattedDate;
// 使用"|"字符连接设备ID、格式化的日期和道路方向,形成唯一键
return deviceId + "|" + formattedDate + "|" + dcTrafficSectionData.getDirection();
}
/**

14
zc-business/src/main/java/com/zc/business/statistics/cache/QuarterlyTrafficStatisticsCache.java

@ -35,8 +35,6 @@ public class QuarterlyTrafficStatisticsCache extends AbstractTrafficStatisticsCa
// 私有构造函数,确保只能通过静态方法获取实例
private QuarterlyTrafficStatisticsCache() {
super();
this.setData(new HashSet<>());
}
/**
@ -86,11 +84,11 @@ public class QuarterlyTrafficStatisticsCache extends AbstractTrafficStatisticsCa
}
/**
* 该方法用于生成一个基于DcTrafficSectionData对象属性的唯一键字符串
* 这个键由设备ID与统计日期两部分组成通常用于索引或标识特定设备在某一日期的交通流量数据
* 生成缓存键
* 该方法通过设备ID统计日期和道路方向生成一个唯一的缓存键用于存储或检索特定条件下的交通数据
*
* @param dcTrafficSectionData DcTrafficSectionData类型的对象包含设备ID和统计日期等信息
* @return 格式化后的字符串键格式为 "deviceId|formattedDate"
* @param dcTrafficSectionData 包含设备ID统计日期和道路方向的交通段数据对象
* @return 返回一个字符串形式的缓存键由设备ID格式化的统计日期和道路方向以"|"字符连接而成
*/
public static String generateCacheKey(DcTrafficSectionData dcTrafficSectionData) {
// 获取设备ID
@ -99,8 +97,8 @@ public class QuarterlyTrafficStatisticsCache extends AbstractTrafficStatisticsCa
// 获取并格式化统计日期
String formattedDate = formatDate(dcTrafficSectionData.getStatisticalDate());
// 使用"|"字符连接设备ID和日期,形成唯一键
return deviceId + "|" + formattedDate;
// 使用"|"字符连接设备ID、格式化的日期和道路方向,形成唯一键
return deviceId + "|" + formattedDate + "|" + dcTrafficSectionData.getDirection();
}
/**

14
zc-business/src/main/java/com/zc/business/statistics/cache/YearlyTrafficStatisticsCache.java

@ -35,8 +35,6 @@ public class YearlyTrafficStatisticsCache extends AbstractTrafficStatisticsCache
// 私有构造函数,确保只能通过静态方法获取实例
private YearlyTrafficStatisticsCache() {
super();
this.setData(new HashSet<>());
}
/**
@ -86,11 +84,11 @@ public class YearlyTrafficStatisticsCache extends AbstractTrafficStatisticsCache
}
/**
* 该方法用于生成一个基于DcTrafficSectionData对象属性的唯一键字符串
* 这个键由设备ID与统计日期两部分组成通常用于索引或标识特定设备在某一日期的交通流量数据
* 生成缓存键
* 该方法通过设备ID统计日期和道路方向生成一个唯一的缓存键用于存储或检索特定条件下的交通数据
*
* @param dcTrafficSectionData DcTrafficSectionData类型的对象包含设备ID和统计日期等信息
* @return 格式化后的字符串键格式为 "deviceId|formattedDate"
* @param dcTrafficSectionData 包含设备ID统计日期和道路方向的交通段数据对象
* @return 返回一个字符串形式的缓存键由设备ID格式化的统计日期和道路方向以"|"字符连接而成
*/
public static String generateCacheKey(DcTrafficSectionData dcTrafficSectionData) {
// 获取设备ID
@ -99,8 +97,8 @@ public class YearlyTrafficStatisticsCache extends AbstractTrafficStatisticsCache
// 获取并格式化统计日期
String formattedDate = formatDate(dcTrafficSectionData.getStatisticalDate());
// 使用"|"字符连接设备ID和日期,形成唯一键
return deviceId + "|" + formattedDate;
// 使用"|"字符连接设备ID、格式化的日期和道路方向,形成唯一键
return deviceId + "|" + formattedDate + "|" + dcTrafficSectionData.getDirection();
}
/**

2
zc-business/src/main/resources/mapper/business/DcTrafficSectionDataMapper.xml

@ -38,7 +38,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
direction = VALUES(direction),
period_type = VALUES(period_type),
stake_mark = VALUES(stake_mark),
update_time = VALUES(NOW())
update_time = NOW()
</insert>
</mapper>

Loading…
Cancel
Save