8 changed files with 406 additions and 0 deletions
@ -0,0 +1,96 @@ |
|||
package com.zc.business.domain; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.Date; |
|||
|
|||
/** |
|||
* 门架牌识数据统计实体类 |
|||
* @author xiepufeng |
|||
*/ |
|||
@Data |
|||
public class DcGantryVehiclePlateStatistics implements Serializable { |
|||
|
|||
/** id */ |
|||
private Long id; |
|||
|
|||
/** 门架编号 */ |
|||
private String gantryid; |
|||
|
|||
/** 统计时间 */ |
|||
private Date statisticalDate; |
|||
|
|||
/** 车流量 */ |
|||
private Integer trafficVolume; |
|||
|
|||
/** 统计粒度 |
|||
1-年 |
|||
2-季 |
|||
3-月 |
|||
4-日 |
|||
5-时 */ |
|||
private String periodType; |
|||
|
|||
/** 创建时间 */ |
|||
private Date createTime; |
|||
|
|||
/** 修改事件 */ |
|||
private Date updateTime; |
|||
|
|||
public Long getId() { |
|||
return id; |
|||
} |
|||
|
|||
public void setId(Long id) { |
|||
this.id = id; |
|||
} |
|||
|
|||
public String getGantryid() { |
|||
return gantryid; |
|||
} |
|||
|
|||
public void setGantryid(String gantryid) { |
|||
this.gantryid = gantryid; |
|||
} |
|||
|
|||
public Date getStatisticalDate() { |
|||
return statisticalDate; |
|||
} |
|||
|
|||
public void setStatisticalDate(Date statisticalDate) { |
|||
this.statisticalDate = statisticalDate; |
|||
} |
|||
|
|||
public Integer getTrafficVolume() { |
|||
return trafficVolume; |
|||
} |
|||
|
|||
public void setTrafficVolume(Integer trafficVolume) { |
|||
this.trafficVolume = trafficVolume; |
|||
} |
|||
|
|||
public String getPeriodType() { |
|||
return periodType; |
|||
} |
|||
|
|||
public void setPeriodType(String periodType) { |
|||
this.periodType = periodType; |
|||
} |
|||
|
|||
public Date getCreateTime() { |
|||
return createTime; |
|||
} |
|||
|
|||
public void setCreateTime(Date createTime) { |
|||
this.createTime = createTime; |
|||
} |
|||
|
|||
public Date getUpdateTime() { |
|||
return updateTime; |
|||
} |
|||
|
|||
public void setUpdateTime(Date updateTime) { |
|||
this.updateTime = updateTime; |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
package com.zc.business.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.zc.business.domain.DcGantryStatisticsData; |
|||
import com.zc.business.domain.DcGantryVehiclePlateStatistics; |
|||
import com.zc.business.domain.TrafficFlowStatisticsMap; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
import org.apache.ibatis.annotations.Param; |
|||
|
|||
import java.util.Date; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 门架牌识统计数据Mapper |
|||
*/ |
|||
@Mapper |
|||
public interface DcGantryVehiclePlateStatisticsMapper { |
|||
|
|||
/** |
|||
* 批量插入或更新门架牌识数据统计。 |
|||
* |
|||
*/ |
|||
boolean batchInsertOrUpdate(List<DcGantryVehiclePlateStatistics> list); |
|||
|
|||
|
|||
DcGantryVehiclePlateStatistics selectStatisticsByGantryid(@Param("statisticalDate") Date statisticalDate, @Param("gantryid") String gantryid, @Param("periodType") String periodType); |
|||
} |
|||
|
|||
@ -0,0 +1,201 @@ |
|||
package com.zc.business.statistics.handler; |
|||
|
|||
import cn.hutool.core.date.DateTime; |
|||
import cn.hutool.core.date.DateUtil; |
|||
import com.zc.business.domain.DcGantryMetricsStatisticsData; |
|||
import com.zc.business.domain.DcGantryVehiclePlateStatistics; |
|||
import com.zc.business.domain.OdsTollViuData; |
|||
import com.zc.business.enums.TrafficDataPeriodTypeEnum; |
|||
import com.zc.business.enums.UniversalEnum; |
|||
import com.zc.business.mapper.DcGantryMetricsStatisticsDataMapper; |
|||
import com.zc.business.mapper.DcGantryVehiclePlateStatisticsMapper; |
|||
import com.zc.business.service.IDcGantryMetricsStatisticsDataService; |
|||
import com.zc.business.service.IOdsTollViuDataService; |
|||
import com.zc.business.statistics.cache.AbstractTrafficStatisticsCache; |
|||
import com.zc.business.statistics.cache.metrics.DailyGantryMetricsStatisticsCache; |
|||
import com.zc.business.statistics.cache.metrics.MonthlyGantryMetricsStatisticsCache; |
|||
import com.zc.business.statistics.cache.metrics.QuarterlyGantryMetricsStatisticsCache; |
|||
import com.zc.business.statistics.cache.metrics.YearlyGantryMetricsStatisticsCache; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.format.annotation.DateTimeFormat; |
|||
import org.springframework.scheduling.annotation.Scheduled; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.util.CollectionUtils; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.util.*; |
|||
import java.util.function.Consumer; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* TrafficGantryPlateStatistics类用于处理门架牌识的统计数据。 |
|||
* @author xiepufeng |
|||
*/ |
|||
@Component |
|||
@RestController |
|||
@RequestMapping("/statistics/trafficGantryVehiclePlateStatistics") |
|||
public class TrafficGantryVehiclePlateStatistics { |
|||
|
|||
// 日志记录器
|
|||
protected final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|||
|
|||
@Resource |
|||
private IOdsTollViuDataService odsTollViuDataService; |
|||
|
|||
@Resource |
|||
private DcGantryVehiclePlateStatisticsMapper dcGantryVehiclePlateStatisticsMapper; |
|||
|
|||
|
|||
/** |
|||
* 定义每小时第30分钟执行的任务,用于将中间库门架牌识流水 统计后保存至dc数据库。 |
|||
*/ |
|||
@Scheduled(cron = "0 30 * * * ?") // 每小时的30分整点执行该任务
|
|||
public void performHourlyCleanupAndPersist() { |
|||
|
|||
|
|||
// 计算一小时前的时间点
|
|||
DateTime lastHour = DateUtil.offsetHour(new Date(), UniversalEnum.MINUS_ONE.getNumber()); |
|||
Date startTime = DateUtil.beginOfHour(lastHour); |
|||
Date endTime = DateUtil.endOfHour(lastHour); |
|||
|
|||
//前一小时的牌识统计数据
|
|||
List<DcGantryVehiclePlateStatistics> lastHourStatisticsList = odsTollViuDataService.selectHourDataList(startTime,endTime); |
|||
|
|||
if (lastHourStatisticsList == null || lastHourStatisticsList.isEmpty()) { |
|||
return; |
|||
} |
|||
//补充时间 类型信息
|
|||
lastHourStatisticsList.forEach(item -> { |
|||
item.setStatisticalDate(startTime); |
|||
item.setCreateTime(new Date()); |
|||
item.setPeriodType("5"); |
|||
}); |
|||
|
|||
// 批量插入小时统计数据
|
|||
dcGantryVehiclePlateStatisticsMapper.batchInsertOrUpdate(lastHourStatisticsList); |
|||
|
|||
//年、季、月、日统计数据
|
|||
List<DcGantryVehiclePlateStatistics> statisticsList = new ArrayList<>(); |
|||
for (DcGantryVehiclePlateStatistics hourData : lastHourStatisticsList) { |
|||
//天
|
|||
Date dayStartTime = DateUtil.beginOfDay(startTime); |
|||
DcGantryVehiclePlateStatistics dayStatistics = new DcGantryVehiclePlateStatistics(); |
|||
dayStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
dayStatistics.setStatisticalDate(dayStartTime); |
|||
dayStatistics.setPeriodType("4"); |
|||
dayStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(dayStatistics); |
|||
|
|||
//月
|
|||
Date monthStartTime = DateUtil.beginOfMonth(startTime); |
|||
DcGantryVehiclePlateStatistics monthStatistics = new DcGantryVehiclePlateStatistics(); |
|||
monthStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
monthStatistics.setStatisticalDate(monthStartTime); |
|||
monthStatistics.setPeriodType("3"); |
|||
monthStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(monthStatistics); |
|||
|
|||
//季度
|
|||
Date quarterStartTime = DateUtil.beginOfQuarter(startTime); |
|||
DcGantryVehiclePlateStatistics quarterStatistics = new DcGantryVehiclePlateStatistics(); |
|||
quarterStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
quarterStatistics.setStatisticalDate(quarterStartTime); |
|||
quarterStatistics.setPeriodType("2"); |
|||
quarterStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(quarterStatistics); |
|||
|
|||
//年
|
|||
Date yearStartTime = DateUtil.beginOfYear(startTime); |
|||
DcGantryVehiclePlateStatistics yearStatistics = new DcGantryVehiclePlateStatistics(); |
|||
yearStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
yearStatistics.setStatisticalDate(yearStartTime); |
|||
yearStatistics.setPeriodType("1"); |
|||
yearStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(yearStatistics); |
|||
} |
|||
// 批量插入年季月日统计数据
|
|||
dcGantryVehiclePlateStatisticsMapper.batchInsertOrUpdate(statisticsList); |
|||
|
|||
} |
|||
|
|||
@GetMapping("/syncData") |
|||
public void syncData(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, |
|||
@RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime){ |
|||
Date hourStart = DateUtil.beginOfHour(startTime); |
|||
|
|||
syncHourStatistics(hourStart,endTime); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 时间范围统计门架牌识数据 |
|||
* 注: 会将该范围内的数据累加到年\季\月\日上,谨慎操作 |
|||
*/ |
|||
public void syncHourStatistics(Date hourStart,Date endTime){ |
|||
|
|||
//结束递归
|
|||
while (hourStart.getTime() <= endTime.getTime()){ |
|||
Date hourEnd = DateUtil.endOfHour(hourStart); |
|||
//前一小时的牌识统计数据
|
|||
List<DcGantryVehiclePlateStatistics> lastHourStatisticsList = odsTollViuDataService.selectHourDataList(hourStart,hourEnd); |
|||
if (lastHourStatisticsList == null || lastHourStatisticsList.isEmpty()) { |
|||
hourStart = DateUtil.offsetHour(hourStart, 1); |
|||
continue; //跳过当前小时,继续循环
|
|||
} |
|||
//补充时间 类型信息
|
|||
Date finalHourStart = hourStart; |
|||
lastHourStatisticsList.forEach(item -> { |
|||
item.setStatisticalDate(finalHourStart); |
|||
item.setCreateTime(new Date()); |
|||
item.setPeriodType("5"); |
|||
}); |
|||
// 批量插入小时统计数据
|
|||
dcGantryVehiclePlateStatisticsMapper.batchInsertOrUpdate(lastHourStatisticsList); |
|||
|
|||
//年、季、月、日统计数据
|
|||
List<DcGantryVehiclePlateStatistics> statisticsList = new ArrayList<>(); |
|||
for (DcGantryVehiclePlateStatistics hourData : lastHourStatisticsList) { |
|||
//天
|
|||
Date dayStartTime = DateUtil.beginOfDay(finalHourStart); |
|||
DcGantryVehiclePlateStatistics dayStatistics = new DcGantryVehiclePlateStatistics(); |
|||
dayStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
dayStatistics.setStatisticalDate(dayStartTime); |
|||
dayStatistics.setPeriodType("4"); |
|||
dayStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(dayStatistics); |
|||
//月
|
|||
Date monthStartTime = DateUtil.beginOfMonth(finalHourStart); |
|||
DcGantryVehiclePlateStatistics monthStatistics = new DcGantryVehiclePlateStatistics(); |
|||
monthStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
monthStatistics.setStatisticalDate(monthStartTime); |
|||
monthStatistics.setPeriodType("3"); |
|||
monthStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(monthStatistics); |
|||
//季度
|
|||
Date quarterStartTime = DateUtil.beginOfQuarter(finalHourStart); |
|||
DcGantryVehiclePlateStatistics quarterStatistics = new DcGantryVehiclePlateStatistics(); |
|||
quarterStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
quarterStatistics.setStatisticalDate(quarterStartTime); |
|||
quarterStatistics.setPeriodType("2"); |
|||
quarterStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(quarterStatistics); |
|||
//年
|
|||
Date yearStartTime = DateUtil.beginOfYear(finalHourStart); |
|||
DcGantryVehiclePlateStatistics yearStatistics = new DcGantryVehiclePlateStatistics(); |
|||
yearStatistics.setTrafficVolume(hourData.getTrafficVolume()); |
|||
yearStatistics.setStatisticalDate(yearStartTime); |
|||
yearStatistics.setPeriodType("1"); |
|||
yearStatistics.setGantryid(hourData.getGantryid()); |
|||
statisticsList.add(yearStatistics); |
|||
} |
|||
// 批量插入年季月日统计数据
|
|||
dcGantryVehiclePlateStatisticsMapper.batchInsertOrUpdate(statisticsList); |
|||
|
|||
|
|||
hourStart = DateUtil.offsetHour(hourStart, 1); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
<?xml version="1.0" encoding="UTF-8" ?> |
|||
<!DOCTYPE mapper |
|||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
<mapper namespace="com.zc.business.mapper.DcGantryVehiclePlateStatisticsMapper"> |
|||
|
|||
<insert id="batchInsertOrUpdate" parameterType="list"> |
|||
INSERT INTO dc_gantry_vehicle_plate_statistics |
|||
( |
|||
gantryid, |
|||
statistical_date, |
|||
traffic_volume, |
|||
period_type, |
|||
create_time |
|||
) |
|||
VALUES |
|||
<foreach collection="list" item="item" index="index" separator=","> |
|||
( |
|||
#{item.gantryid}, |
|||
#{item.statisticalDate}, |
|||
#{item.trafficVolume}, |
|||
#{item.periodType}, |
|||
NOW() |
|||
) |
|||
</foreach> |
|||
ON DUPLICATE KEY UPDATE |
|||
traffic_volume = traffic_volume + VALUES(traffic_volume), |
|||
update_time = NOW() |
|||
</insert> |
|||
<select id="selectStatisticsByGantryid" resultType="com.zc.business.domain.DcGantryVehiclePlateStatistics"> |
|||
select * from dc_gantry_vehicle_plate_statistics |
|||
where gantryid = #{gantryid} and statistical_date = #{statisticalDate} and period_type = #{periodType} |
|||
</select> |
|||
</mapper> |
|||
@ -0,0 +1,14 @@ |
|||
<?xml version="1.0" encoding="UTF-8" ?> |
|||
<!DOCTYPE mapper |
|||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
<mapper namespace="com.zc.business.mapper.OdsTollViuDataMapper"> |
|||
|
|||
|
|||
<select id="selectHourDataList" resultType="com.zc.business.domain.DcGantryVehiclePlateStatistics"> |
|||
SELECT gantryid,COUNT(DISTINCT vehicleplate_MD5) AS trafficVolume |
|||
FROM ods_toll_viu_data |
|||
WHERE pictime BETWEEN #{startTime} and #{endTime} |
|||
GROUP BY gantryid; |
|||
</select> |
|||
</mapper> |
|||
Loading…
Reference in new issue