Commit bf397310 authored by wangyecheng's avatar wangyecheng

研判路口溢出数据定时推送

parent bb362f24
package net.wanji.opt.dao.mapper.judgeanalysis;
import net.wanji.databus.po.CrossBaseLaneInfoPO;
import net.wanji.databus.po.CrossDirDataHistPO;
import net.wanji.databus.po.CrossTurnDataHistPO;
import net.wanji.opt.entity.judgeanalysis.AnalysisCrossHourOverFlowTask;
import net.wanji.opt.entity.judgeanalysis.AnalysisProblemCrossDirHour;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import net.wanji.opt.entity.judgeanalysis.CrossTurnInfo;
import net.wanji.opt.po.base.BaseRidInfo;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
......@@ -50,5 +61,22 @@ public interface AnalysisProblemCrossDirHourMapper extends BaseMapper<AnalysisPr
List<AnalysisProblemCrossDirHour> selectDirectByProblemTrend(Map<String, Object> Map);
List<AnalysisProblemCrossDirHour> selectOverFlowCrossIds(String formattedDate, String eventType);
List<BaseRidInfo> getNextCross(@Param("crossId") String crossId,@Param("dir") Integer dir);
List<CrossBaseLaneInfoPO> getNextCrossBaseLaneNum(@Param("nextCrossid") String nextCrossid,@Param("dir") Integer dir);
List<CrossDirDataHistPO> getNextCrossPass(@Param("nextCrossid") String nextCrossid,
@Param("startTimeAll") String startTimeAll,
@Param("endTimeALL") String endTimeALL);
List<CrossTurnInfo> getCrossTurnInfo(@Param("crossId") String crossId, @Param("dirCode") Integer dirCode);
List<CrossTurnDataHistPO> getCrossTurnDataHist(@Param("crossId") String crossId, @Param("startTimeAll") String startTimeAll, @Param("endTimeALL") String endTimeALL, @Param("crossTurnInfo") List<CrossTurnInfo> crossTurnInfo);
void insertCrossOverFlow(@Param("crossHourOverFlowList") List<AnalysisCrossHourOverFlowTask> crossHourOverFlowList);
}
package net.wanji.opt.entity.judgeanalysis;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
@Data
@EqualsAndHashCode(callSuper=false)
@TableName("t_analysis_cross_overflow_reason")
public class AnalysisCrossHourOverFlowTask {
private static final long serialVersionUID = 1L;
// 路口ID
private String crossId;
// 主要溢出方向
private int overflowDir;
// 出口道总流量
private Integer outFlowSum;
// 平均流量/5min
private Integer outFlowAvg;
// 出口道方向下一个路口进口车道数量
private Integer nextCrossLaneNum;
// 下个路口通行能力
private Integer nextCrossCapacity;
// 到下一个路口之间间距,单位米
private Integer lengthToNextCross;
// 主要溢出时段开始时间
private String overflowStartTime;
// 主要溢出时段截止时间
private String overflowEndTime;
// 主要溢出时段内溢出事件数量
private Integer overflowNum;
// 日期,格式yyyyMMdd
private int dt;
// 数据插入时间
private Date insertTime;
}
......@@ -84,4 +84,7 @@ public class AnalysisProblemCrossDirHour implements Serializable {
private String countNumber;
//按小时开始时间
private String hours_window_start_time;
//开始时间 年月日时分秒
private String winStartTime;
}
package net.wanji.opt.entity.judgeanalysis;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
@Data
@EqualsAndHashCode(callSuper=false)
@TableName("t_base_cross_turn_info")
public class CrossTurnInfo {
// 转向ID(路口ID_驶入方向_转向类型)
private String id;
// 转向类型:u掉头;l左转;s直行;r右转;
private String turnType;
// 驶入方向:1北;2东北;3东;4东南;5南;6西南;7西;8西北
private Integer inDir;
// 驶出方向:1北;2东北;3东;4东南;5南;6西南;7西;8西北
private Integer outDir;
// 路口ID
private String crossId;
// 创建时间
private Date gmtCreate;
// 修改时间
private Date gmtModified;
}
......@@ -56,4 +56,6 @@ public interface AnalysisProblemCrossDirHourService extends IService<AnalysisPro
Map<String, Object> directionByProblemTrend(String crossId, String eventType, Integer startTime, Integer endTime) throws ParseException;
Map<String, Object> crossingHappyByProblemTrend(String crossId,String eventType, Integer startTime, Integer endTime)throws Exception;
void insertCorssOverFlowData();
}
......@@ -2,21 +2,32 @@ package net.wanji.opt.servicev2.judgeanalysis.impl;
import com.alibaba.fastjson.JSONObject;
import net.wanji.common.utils.tool.DateUtil;
import net.wanji.databus.po.CrossBaseLaneInfoPO;
import net.wanji.databus.po.CrossDirDataHistPO;
import net.wanji.databus.po.CrossTurnDataHistPO;
import net.wanji.opt.common.EsDateIndexUtil;
import net.wanji.opt.common.enums.CrossDirEnum;
import net.wanji.opt.entity.judgeanalysis.AnalysisCrossHourOverFlowTask;
import net.wanji.opt.entity.judgeanalysis.AnalysisProblemCrossDay;
import net.wanji.opt.entity.judgeanalysis.AnalysisProblemCrossDirHour;
import net.wanji.opt.dao.mapper.judgeanalysis.AnalysisProblemCrossDirHourMapper;
import net.wanji.opt.entity.judgeanalysis.CrossTurnInfo;
import net.wanji.opt.po.base.BaseRidInfo;
import net.wanji.opt.servicev2.judgeanalysis.AnalysisProblemCrossDirHourService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
......@@ -197,4 +208,170 @@ import java.util.stream.Collectors;
jsonObject.put("timeList",allHourset);
return jsonObject;
}
/*
* 插入溢出路口概况 t_analysis_cross_overflow_reason
* */
@Override
@Transactional
public void insertCorssOverFlowData() {
try {
//最终组装的数据
ArrayList<AnalysisCrossHourOverFlowTask> crossHourOverFlowList=new ArrayList<>();
//获取上一天的日期
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取前一天的日期
LocalDate previousDay = today.minusDays(1);
// 定义日期格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// 格式化前一天的日期为字符串
String formattedDate = previousDay.format(formatter);
//溢出
String eventType="703";
// dt用 前一天没有时分秒
Integer startTime =Integer.parseInt(formattedDate);
Integer endTime =Integer.parseInt(formattedDate);
//溢出区分的时分秒
String startTimeAll="";
String endTimeALL="";
//溢出的数量
Integer eventNumber=0;
//方向
Integer dirCode=0;
// // 定义格式化器
DateTimeFormatter formatterall = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//查询前一天溢出的所有的路口ID
List<AnalysisProblemCrossDirHour> crossIdsList=analysisProblemCrossDirHourMapper.selectOverFlowCrossIds(formattedDate,eventType);
for (AnalysisProblemCrossDirHour item:crossIdsList) {
AnalysisCrossHourOverFlowTask crossHourOverFlow=new AnalysisCrossHourOverFlowTask();
//每个路口
String crossId = item.getCrossId();
//取出时间
List<AnalysisProblemCrossDirHour> listHourSql = analysisProblemCrossDirHourMapper.selectHappyByProblemTrend(getHappyParamMap(crossId, eventType, startTime, endTime));
//溢出时间 年月日 时分秒
LocalDateTime windowStartTime=null;
LocalDateTime windowEndTime=null;
// 定义时间格式
DateTimeFormatter formatterTask = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//取出时间
for (AnalysisProblemCrossDirHour list: listHourSql) {
//溢出时间 年月日 时分秒
String windowStartTime1 = list.getWinStartTime();
eventNumber = list.getEventNumber();
dirCode = list.getDir();
if(windowStartTime1!=null ) {
// 解析时间字符串为LocalTime对象
windowStartTime = LocalDateTime.parse(windowStartTime1, formatterTask);
// 对年月日时分秒的 时间加一个小时得到结束时间
windowEndTime = windowStartTime.plusHours(1);
if (windowStartTime!=null && windowEndTime!=null){
startTimeAll = windowStartTime.format(formatterall);
endTimeALL = windowEndTime.format(formatterall);
}
}
}
//到下一个路口之间间距
List<BaseRidInfo> BaseRidInfoList=analysisProblemCrossDirHourMapper.getNextCross(crossId,dirCode);
//下一个路口
String nextCrossid="";
//到下一个路口之间间距,单位米
Double nextCrossLength=0.0;
Integer nextCrossLengthint =0;
if(CollectionUtils.isNotEmpty(BaseRidInfoList)) {
for (BaseRidInfo ridInfo : BaseRidInfoList) {
nextCrossLength = ridInfo.getLength();
nextCrossLengthint = (int) Math.round(nextCrossLength);
nextCrossid = ridInfo.getEndCrossId();
}
}
//出口道方向下一个路口进口车道数量
List<CrossBaseLaneInfoPO>CrossBaseLaneInfo= analysisProblemCrossDirHourMapper.getNextCrossBaseLaneNum(nextCrossid,dirCode);
//统计数据条数(出口道方向下一个路口进口车道数量)
long BaseLaneInfoCount = CrossBaseLaneInfo.stream().count();
//下个路口通行能力
List<CrossDirDataHistPO> CrossDirDataHistList =analysisProblemCrossDirHourMapper.getNextCrossPass(nextCrossid,startTimeAll,endTimeALL);
//下个路口通行能力
Integer nextCrossPassFlow=0;
if(CollectionUtils.isNotEmpty(CrossDirDataHistList)) {
for (CrossDirDataHistPO CrossDirDataHist : CrossDirDataHistList) {
if (CrossDirDataHist!=null) {
nextCrossPassFlow = CrossDirDataHist.getFlow();
}
}
}
//查询出口道总流量跟平均流量net.wanji.opt.synthesis.pojo.CrossTurnInfoEntity
//查询t_base_cross_turn_info表中的in_dir,turn_type
List<CrossTurnInfo>CrossTurnInfo= analysisProblemCrossDirHourMapper.getCrossTurnInfo(crossId,dirCode);
//查询t_cross_turn_data_hist表
List<CrossTurnDataHistPO> CrossTurnDataHistList =analysisProblemCrossDirHourMapper.getCrossTurnDataHist(crossId,startTimeAll,endTimeALL,CrossTurnInfo);
Integer sumFlow=0;
Integer avgFlow=0;
if(CollectionUtils.isNotEmpty(CrossTurnDataHistList)) {
// 计算 sum 总流量
sumFlow = CrossTurnDataHistList.stream()
.mapToInt(CrossTurnDataHistPO::getFlow)
.sum();
// 计算 avg 平均流量
OptionalDouble avg1 = CrossTurnDataHistList.stream()
.mapToInt(CrossTurnDataHistPO::getFlow)
.average();
// 将 avg 转换为 Integer 类型
avgFlow = avg1.isPresent()
? (int) Math.round(avg1.getAsDouble()) // 四舍五入
: 0; // 如果流为空,返回默认值 0
}
//路口id
crossHourOverFlow.setCrossId(crossId);
//溢出方向
crossHourOverFlow.setOverflowDir(dirCode);
//总流量
crossHourOverFlow.setOutFlowSum(sumFlow);
//平均流量/5min
crossHourOverFlow.setOutFlowAvg(avgFlow);
//出口道方向下一个路口进口车道数量
crossHourOverFlow.setNextCrossLaneNum((int) BaseLaneInfoCount);
//下个路口通行能力
crossHourOverFlow.setNextCrossCapacity(nextCrossPassFlow);
//到下一个路口之间间距,单位米
crossHourOverFlow.setLengthToNextCross(nextCrossLengthint);
//主要溢出时段开始时间
crossHourOverFlow.setOverflowStartTime(startTimeAll);
//主要溢出时段截止时间
crossHourOverFlow.setOverflowEndTime(endTimeALL);
//主要溢出时段内溢出事件数量
crossHourOverFlow.setOverflowNum(eventNumber);
//日期dt
crossHourOverFlow.setDt(startTime);
crossHourOverFlowList.add(crossHourOverFlow);
}
//System.out.println("crossHourOverFlowList"+crossHourOverFlowList);
analysisProblemCrossDirHourMapper.insertCrossOverFlow(crossHourOverFlowList);
}catch (Exception e){
e.printStackTrace();
log.error("定时任务--路口溢出情况更新--任务执行错误" + e);
}
}
}
package net.wanji.opt.task;
import lombok.extern.slf4j.Slf4j;
import net.wanji.opt.servicev2.judgeanalysis.AnalysisProblemCrossDirHourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling
@Slf4j
public class AnalysisProblemCrossDirHourTask implements ApplicationRunner {
@Autowired
private AnalysisProblemCrossDirHourService analysisProblemCrossDirHourService;
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("服务启动时--周总体拥堵概况更新--任务执行时间:" + LocalDateTime.now());
// 周总体拥堵概况更新
try {
// executeCrossOverFlowTask();
}catch (Exception e){
log.error("服务启动时--周总体拥堵概况更新--任务执行错误" + e);
}
log.info("服务启动时--周总体拥堵概况更新--任务执行结束时间:" + LocalDateTime.now());
}
/**
* 每天凌晨执行路口溢出查询的任务
*/
@Scheduled(cron = "0 1 0 ? * *")
// @Scheduled(cron = "*/3 * * ? * *")
public void executeCrossOverFlowTask() {
//路口溢出情况更新
log.info("定时任务--路口溢出情况更新--执行开始时间:" + LocalDateTime.now());
System.out.println("路口溢出情况更新:" + new java.util.Date());
try {
analysisProblemCrossDirHourService.insertCorssOverFlowData();
}catch (Exception e){
log.error("定时任务--路口溢出情况更新--任务执行错误" + e);
}
log.info("定时任务--路口溢出情况更新--执行结束时间:" + LocalDateTime.now());
}
}
......@@ -196,13 +196,14 @@
GROUP BY dt, t1.dir
ORDER BY dt
</select>
<select id="selectHappyByProblemTrend" parameterType="map" resultMap="AnalysisProblemCrossDirHourMap">
<select id="selectHappyByProblemTrend" parameterType="map" resultType="net.wanji.opt.entity.judgeanalysis.AnalysisProblemCrossDirHour">
SELECT
dt,
dir,
t1.cross_id,
sum(event_number)AS event_number ,
DATE_FORMAT(t1.window_start_time,'%H:%i') AS hours_window_start_time ,
DATE_FORMAT(t1.window_start_time,'%H:%i') AS hours_window_start_time ,
DATE_FORMAT(t1.window_start_time, '%Y-%m-%d %H:%i:%s') AS winStartTime,
t1.event_type,
t2.label AS typeName
FROM t_analysis_problem_cross_dir_hour t1
......@@ -221,7 +222,7 @@
GROUP BY
DATE_FORMAT(t1.window_start_time,'%H:%i')
ORDER BY event_number desc
limit 2
limit 1
</select>
<select id="selectDirectByProblemTrend" parameterType="map" resultMap="AnalysisProblemCrossDirHourMap">
......@@ -230,7 +231,7 @@
dir,
t1.cross_id,
sum(event_number)AS event_number ,
DATE_FORMAT(t1.window_start_time,'%H:%i') ,
DATE_FORMAT(t1.window_start_time,'%H:%i') AS hours_window_start_time,
t1.event_type,
t2.label AS typeName
FROM t_analysis_problem_cross_dir_hour t1
......@@ -249,7 +250,99 @@
GROUP BY
dir
ORDER BY event_number desc
limit 2
limit 1
</select>
<select id="selectOverFlowCrossIds" parameterType="map" resultMap="AnalysisProblemCrossDirHourMap">
SELECT
DISTINCT
cross_id
FROM t_analysis_problem_cross_dir_hour t1
WHERE
1=1
<if test="formattedDate != null and formattedDate != ''">
and t1.dt = #{formattedDate}
</if>
<if test="eventType != null and eventType != ''">
and t1.event_type = #{eventType}
</if>
</select>
<select id="getNextCross" parameterType="map" resultType="BaseRidInfo">
SELECT start_cross_id,end_cross_id,length,out_dir
from t_base_rid_info t1
where
1=1
<if test="crossId != null and crossId != ''">
and t1.start_cross_id = #{crossId}
</if>
<if test="dir != null and dir != ''">
and t1.out_dir = #{dir}
</if>
</select>
<select id="getNextCrossBaseLaneNum" parameterType="map" resultType="net.wanji.databus.po.CrossBaseLaneInfoPO">
SELECT dir,type,cross_id
FROM
t_base_lane_info t1
WHERE
1=1
and type=2
<if test="nextCrossid != null and nextCrossid != ''">
and t1.cross_id = #{nextCrossid}
</if>
<if test="dir != null and dir != ''">
and t1.dir = #{dir}
</if>
</select>
<select id="getNextCrossPass" parameterType="map" resultType="net.wanji.databus.po.CrossDirDataHistPO">
SELECT cross_id,max(flow)as flow,start_time
FROM t_cross_dir_data_hist t1
WHERE
1=1
<if test="nextCrossid != null and nextCrossid != ''">
and t1.cross_id = #{nextCrossid}
</if>
<if test="startTimeAll != null and endTimeALL !=null ">
and start_time >= #{startTimeAll} AND start_time &lt;= #{endTimeALL}
</if>
</select>
<select id="getCrossTurnInfo" parameterType="map" resultType="CrossTurnInfo">
SELECT in_dir,out_dir,cross_id,
case when turn_type='l' then 1 when turn_type='s' then 2 when turn_type='r' then 3 end as turn_type
from t_base_cross_turn_info t1
where
1=1
<if test="crossId != null and crossId != ''">
and t1.cross_id = #{crossId}
</if>
<if test="dirCode != null and dirCode != ''">
and t1.out_dir = #{dirCode}
</if>
</select>
<select id="getCrossTurnDataHist" parameterType="map" resultType="net.wanji.databus.po.CrossTurnDataHistPO">
SELECT id,turn_type,out_dir,in_dir,flow,start_time
FROM t_cross_turn_data_hist b
WHERE
1=1
<if test="crossId != null and crossId != ''">
and b.cross_id = #{crossId}
</if>
<if test="startTimeAll!=null and endTimeALL!=null ">
AND b.start_time >= #{startTimeAll} and b.start_time &lt;= #{endTimeALL}
</if>
AND (in_dir,turn_type) in (
<foreach collection="crossTurnInfo" item="pair" open="(" separator="),(" close=")">
#{pair.inDir}, #{pair.turnType}
</foreach>
)
</select>
<select id="insertCrossOverFlow" parameterType="java.util.List">
INSERT INTO
t_analysis_cross_overflow_reason (cross_id,overflow_dir,out_flow_sum,out_flow_avg,next_cross_lane_num,next_cross_capacity,
length_to_next_cross,overflow_start_time,overflow_end_time,overflow_num,dt)
VALUES
<foreach collection="crossHourOverFlowList" item="item" separator=",">
(#{item.crossId},#{item.overflowDir},#{item.outFlowSum},#{item.outFlowAvg},#{item.nextCrossLaneNum},#{item.nextCrossCapacity},
#{item.lengthToNextCross}, #{item.overflowStartTime}, #{item.overflowEndTime}, #{item.overflowNum}, #{item.dt})
</foreach>
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment