Commit 85cbc010 authored by zhoushiguang's avatar zhoushiguang

修改为批量插入

parent 05a703a4
......@@ -9,9 +9,13 @@ import java.util.List;
import java.util.Map;
public interface AnalysisProblemAndStrategyDayMapper extends BaseMapper<AnalysisProblemAndStrategyDay> {
List<AnalysisProblemAndStrategyDay> selectCrossEvent();
List<AnalysisProblemAndStrategyDay> selectGreenEvent();
Integer insertProblemAndStrategy(AnalysisProblemAndStrategyDay analysisProblemAndStrategyDay);
List<AnalysisProblemAndStrategyDay> selectCrossEvent(@Param("dtStart") Integer dtStart,@Param("dtEnd") Integer dtEnd);
List<AnalysisProblemAndStrategyDay> selectGreenEvent(@Param("dtStart") Integer dtStart,@Param("dtEnd") Integer dtEnd);
Integer insertProblemAndStrategy(List<AnalysisProblemAndStrategyDay> list);
AnalysisProblemAndStrategyDay getGreenStrategy(@Param("greenID") String greenID, @Param("time") String time);
AnalysisProblemAndStrategyDay getCrossStrategy(@Param("crossID") String crossID, @Param("time") String time,@Param("eventSerialNumber") String eventSerialNumber);
}
package net.wanji.opt.servicev2.judgeanalysis;
import org.apache.ibatis.annotations.Param;
public interface AnalysisProblemAndStrategyDayService {
public void selectCountByType();
public void selectCountByTypeOfCross(Integer dtStart, Integer dtEnd);
public void selectCountByTypeOfGreen(Integer dtStart,Integer dtEnd);
}
......@@ -12,7 +12,11 @@ import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* <p>
......@@ -29,15 +33,20 @@ public class AnalysisProblemAndStrategyDayImpl extends ServiceImpl<AnalysisProbl
private AnalysisProblemAndStrategyDayMapper analysisProblemAndStrategyDayMapper;
public void selectCountByType() {
StopWatch stopWatch = new StopWatch("事件优化时长计算监控");
public void selectCountByTypeOfGreen(Integer dtStart,Integer dtEnd) {
StopWatch stopWatch = new StopWatch("干线事件优化时长计算监控");
stopWatch.start();
//查询路口事件数据
List<AnalysisProblemAndStrategyDay> crossEvenList = analysisProblemAndStrategyDayMapper.selectCrossEvent();
if (crossEvenList.size() > 0) {
//循环查找路口事件数据是否有策略下发
for (AnalysisProblemAndStrategyDay vo : crossEvenList) {
AnalysisProblemAndStrategyDay temp = analysisProblemAndStrategyDayMapper.getCrossStrategy(vo.getCrossId(), vo.getHappenStartTime(),vo.getEventSerialNumber());
//查询干线事件数据
List<AnalysisProblemAndStrategyDay> greenEvenList = analysisProblemAndStrategyDayMapper.selectGreenEvent(dtStart,dtEnd);
int size = greenEvenList.size();
if (greenEvenList.size() > 0) {
long st = System.currentTimeMillis();
List<AnalysisProblemAndStrategyDay> saveList = new ArrayList<>();// greenEvenList.stream().filter(o->o.getGreenId().equals("1")).collect(Collectors.toList());
long total = 0;
for (AnalysisProblemAndStrategyDay vo : greenEvenList) {
AnalysisProblemAndStrategyDay temp = analysisProblemAndStrategyDayMapper.getGreenStrategy(vo.getGreenId(), vo.getHappenStartTime());
if (temp == null) {
vo.setOptStatus(0);
} else {
......@@ -46,23 +55,49 @@ public class AnalysisProblemAndStrategyDayImpl extends ServiceImpl<AnalysisProbl
vo.setOptEndTime(temp.getOptEndTime());
vo.setOptDuration(temp.getOptDuration());
}
LocalDate previousDay = LocalDate.now().minusDays(1);
String formattedDate = previousDay.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
vo.setDt(formattedDate);
// LocalDate previousDay = LocalDate.now().minusDays(1);
// String formattedDate = previousDay.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
vo.setDt(vo.getDt());
vo.setInsertTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//路口数据设置干线ID为不存在的ID
vo.setGreenId("-1");
//写入数据--Error
analysisProblemAndStrategyDayMapper.insertProblemAndStrategy(vo);
//干线数据设置路口ID为不存在的null字符串
vo.setCrossId("null");
saveList.add(vo);
//写入数据
if (saveList.size() % 100 == 0) {
analysisProblemAndStrategyDayMapper.insertProblemAndStrategy(saveList);
long et = System.currentTimeMillis();
total += 100;
log.info("干线事件优化记录挂接-批量插入优化挂接记录耗时:{}ms,已入库数量:{},总记录数:{}", et - st,total,size);
st = et;
}
}
if (saveList.size() > 0) {
total += saveList.size();
analysisProblemAndStrategyDayMapper.insertProblemAndStrategy(saveList);
long et = System.currentTimeMillis();
log.info("干线事件优化记录挂接-批量插入优化挂接记录耗时:{}ms,已入库数量:{},总记录数:{}", et - st,total,size);
saveList.clear();
}
}
stopWatch.stop();
log.info("干线事件优化时长计算监控 耗时:{}ms", stopWatch.getTotalTimeMillis());
//查询干线事件数据
List<AnalysisProblemAndStrategyDay> greenEvenList = analysisProblemAndStrategyDayMapper.selectGreenEvent();
if (greenEvenList.size() > 0) {
for (AnalysisProblemAndStrategyDay vo : greenEvenList) {
AnalysisProblemAndStrategyDay temp = analysisProblemAndStrategyDayMapper.getGreenStrategy(vo.getGreenId(), vo.getHappenStartTime());
}
public void selectCountByTypeOfCross(Integer dtStart,Integer dtEnd) {
StopWatch stopWatch = new StopWatch("路口事件优化时长计算监控");
stopWatch.start();
//查询路口事件数据
List<AnalysisProblemAndStrategyDay> crossEvenList = analysisProblemAndStrategyDayMapper.selectCrossEvent(dtStart,dtEnd);
int size = crossEvenList.size();
if (crossEvenList.size() > 0) {
long st = System.currentTimeMillis();
long total = 0;
//循环查找路口事件数据是否有策略下发
List<AnalysisProblemAndStrategyDay> saveList = new ArrayList<>();
for (AnalysisProblemAndStrategyDay vo : crossEvenList) {
AnalysisProblemAndStrategyDay temp = analysisProblemAndStrategyDayMapper.getCrossStrategy(vo.getCrossId(), vo.getHappenStartTime(), vo.getEventSerialNumber());
if (temp == null) {
vo.setOptStatus(0);
} else {
......@@ -71,18 +106,34 @@ public class AnalysisProblemAndStrategyDayImpl extends ServiceImpl<AnalysisProbl
vo.setOptEndTime(temp.getOptEndTime());
vo.setOptDuration(temp.getOptDuration());
}
LocalDate previousDay = LocalDate.now().minusDays(1);
String formattedDate = previousDay.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
vo.setDt(formattedDate);
// LocalDate previousDay = LocalDate.now().minusDays(1);
// String formattedDate = previousDay.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
vo.setDt(vo.getDt());
vo.setInsertTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//干线数据设置路口ID为不存在的null字符串
vo.setCrossId("null");
//写入数据
analysisProblemAndStrategyDayMapper.insertProblemAndStrategy(vo);
//路口数据设置干线ID为不存在的ID
vo.setGreenId("-1");
saveList.add(vo);
//写入数据--Error
if (saveList.size() % 100 == 0) {
analysisProblemAndStrategyDayMapper.insertProblemAndStrategy(saveList);
long et = System.currentTimeMillis();
total += 100;
log.info("干线事件优化记录挂接-批量插入优化挂接记录耗时:{}ms,已入库数量:{},总记录数:{}", et - st,total,size);
st = et;
}
}
if (saveList.size() > 0) {
analysisProblemAndStrategyDayMapper.insertProblemAndStrategy(saveList);
long et = System.currentTimeMillis();
total += saveList.size();
log.info("干线事件优化记录挂接-批量插入优化挂接记录耗时:{}ms,已入库数量:{},总记录数:{}", et - st,total,size);
saveList.clear();
}
}
stopWatch.stop();
log.info("事件优化记录优化耗时:{}ms",stopWatch.getTotalTimeMillis());
log.info("路口事件优化时长计算监控总耗时:{}ms", stopWatch.getTotalTimeMillis());
}
......
......@@ -22,9 +22,21 @@ public class AnalysisProblemAndStrategyDayTask {
private AnalysisProblemAndStrategyDayService analysisProblemAndStrategyDayService;
@Scheduled(cron = "0 15 1 * * ?")
public void task() {
public void taskCross() {
try {
analysisProblemAndStrategyDayService.selectCountByType();
analysisProblemAndStrategyDayService.selectCountByTypeOfCross(null,null);
} catch (Exception e) {
log.error("获取事件优化记录异常", e);
}
}
@Scheduled(cron = "0 15 1 * * ?")
public void taskGreen() {
try {
analysisProblemAndStrategyDayService.selectCountByTypeOfGreen(null,null);
} catch (Exception e) {
log.error("获取事件优化记录异常", e);
}
......
......@@ -3,7 +3,8 @@
<mapper namespace="net.wanji.opt.dao.mapper.judgeanalysis.AnalysisProblemAndStrategyDayMapper">
<!-- 通用查询映射结果 -->
<resultMap id="AnalysisProblemAndStrategyDayMap" type="net.wanji.opt.entity.judgeanalysis.AnalysisProblemAndStrategyDay">
<resultMap id="AnalysisProblemAndStrategyDayMap"
type="net.wanji.opt.entity.judgeanalysis.AnalysisProblemAndStrategyDay">
<result column="crossId" property="crossId"/>
<result column="eventSerialNumber" property="eventSerialNumber"/>
<result column="eventCategory" property="eventCategory"/>
......@@ -22,88 +23,112 @@
</resultMap>
<select id="selectCrossEvent" resultMap="AnalysisProblemAndStrategyDayMap">
select a.event_serial_number as eventSerialNumber , a.cross_id as crossId ,a.green_id as greenId,
a.category as eventCategory,a.type as eventType ,a.start_time as happenStartTime ,a.end_time as happenEndTime,
TIMESTAMPDIFF(SECOND, a.start_time , a.end_time) as duration , a.dir
from t_event_info a where a.dt = DATE_FORMAT(DATE_sub(now(),INTERVAL 24 HOUR ),'%Y%m%d') and a.type in (701,702,703,707)
select a.event_serial_number as eventSerialNumber,
a.cross_id as crossId,
a.green_id as greenId,
a.category as eventCategory,
a.type as eventType,
a.start_time as happenStartTime,
a.end_time as happenEndTime,
TIMESTAMPDIFF(SECOND, a.start_time, a.end_time) as duration,
a.dir,
dt
from t_event_info a
where 1=1
<choose>
<when test="dtStart !=null and dtEnd!=null">
and a.dt >= #{dtStart} and a.dt &lt;= #{dtEnd}
</when>
<otherwise>
and a.dt = DATE_FORMAT(DATE_sub(now(), INTERVAL 24 HOUR ), '%Y%m%d')
</otherwise>
</choose>
and a.type in (701, 702, 703, 707)
</select>
<select id="selectGreenEvent" resultMap="AnalysisProblemAndStrategyDayMap">
select a.event_serial_number as eventSerialNumber , a.cross_id as crossId ,a.green_id as greenId,
a.category as eventCategory,a.type as eventType ,a.start_time as happenStartTime ,a.end_time as happenEndTime,
TIMESTAMPDIFF(SECOND, a.start_time , a.end_time) as duration , a.dir
from t_event_info a where a.dt = DATE_FORMAT(DATE_sub(now(),INTERVAL 24 HOUR ),'%Y%m%d') and a.type in (705,706)
TIMESTAMPDIFF(SECOND, a.start_time , a.end_time) as duration , a.dir,dt
from t_event_info a
where 1=1
<choose>
<when test="dtStart !=null and dtEnd!=null">
and a.dt >= #{dtStart} and a.dt &lt;= #{dtEnd}
</when>
<otherwise>
and a.dt = DATE_FORMAT(DATE_sub(now(), INTERVAL 24 HOUR ), '%Y%m%d')
</otherwise>
</choose>
and a.type in (705,706)
</select>
<select id="getGreenStrategy" parameterType="String" resultMap="AnalysisProblemAndStrategyDayMap">
select DISTINCT a.control_time as optStartTime,DATE_ADD(a.control_time,INTERVAL a.control_duration SECOND ) as optEndTime ,a.control_duration as optDuration from t_strategy_green_opt_hist a
where a.control_time = (select MAX(control_time) from t_strategy_green_opt_hist where green_id = #{greenID}
and control_time <![CDATA[ >= ]]> DATE_FORMAT(DATE_sub(#{time},INTERVAL control_duration SECOND),'%Y-%m-%d %H:%i:%s') and control_time <![CDATA[ <= ]]> DATE_FORMAT(#{time}, '%Y-%m-%d %H:%i:%s')
and response_code = 200 )
select DISTINCT a.control_time as optStartTime,
DATE_ADD(a.control_time, INTERVAL a.control_duration SECOND ) as optEndTime,
a.control_duration as optDuration
from t_strategy_green_opt_hist a
where a.control_time = (select MAX(control_time)
from t_strategy_green_opt_hist
where green_id = #{greenID}
and control_time <![CDATA[ >= ]]> DATE_FORMAT(DATE_sub(#{time}, INTERVAL control_duration SECOND), '%Y-%m-%d %H:%i:%s')
and control_time <![CDATA[ <= ]]> DATE_FORMAT(#{time}, '%Y-%m-%d %H:%i:%s')
and response_code = 200)
and a.green_id = #{greenID}
</select>
<select id="getCrossStrategy" parameterType="String" resultMap="AnalysisProblemAndStrategyDayMap">
select a.issue_time as optStartTime,DATE_ADD(a.issue_time,INTERVAL a.duration SECOND ) as optEndTime,a.duration as optDuration from t_strategy_cross_result a
select a.issue_time as optStartTime,
DATE_ADD(a.issue_time, INTERVAL a.duration SECOND ) as optEndTime,
a.duration as optDuration
from t_strategy_cross_result a
where a.event_id = #{eventSerialNumber}
and response_code = 200
and dt = DATE_FORMAT(#{time}, '%Y%m%d')
and a.cross_id = #{crossID}
</select>
<insert id="insertProblemAndStrategy" parameterType="net.wanji.opt.entity.judgeanalysis.AnalysisProblemAndStrategyDay">
insert into t_event_optimize_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="crossId != null">cross_id,</if>
<if test="eventSerialNumber != null">event_serial_number,</if>
<if test="eventCategory != null">event_category,</if>
<if test="eventType != null">event_type,</if>
<if test="happenStartTime != null">happen_start_time,</if>
<if test="happenEndTime != null">happen_end_time,</if>
<if test="duration != null">duration,</if>
<if test="optStatus != null">opt_status,</if>
<if test="dir != null">dir,</if>
<if test="optStartTime != null">opt_start_time,</if>
<if test="optEndTime != null">opt_end_time,</if>
<if test="optDuration != null">opt_duration,</if>
<if test="dt != null">dt,</if>
<if test="insertTime != null">insert_time,</if>
<if test="greenId != null and greenId != '' ">green_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="crossId != null">#{crossId},</if>
<if test="eventSerialNumber != null">#{eventSerialNumber},</if>
<if test="eventCategory != null">#{eventCategory},</if>
<if test="eventType != null">#{eventType},</if>
<if test="happenStartTime != null">DATE_FORMAT(#{happenStartTime},'%Y-%m-%d %H:%i:%s'),</if>
<if test="happenEndTime != null">DATE_FORMAT(#{happenEndTime},'%Y-%m-%d %H:%i:%s'),</if>
<if test="duration != null">#{duration},</if>
<if test="optStatus != null">#{optStatus},</if>
<if test="dir != null">#{dir},</if>
<if test="optStartTime != null">DATE_FORMAT(#{optStartTime},'%Y-%m-%d %H:%i:%s'),</if>
<if test="optEndTime != null">DATE_FORMAT(#{optEndTime},'%Y-%m-%d %H:%i:%s'),</if>
<if test="optDuration != null">#{optDuration},</if>
<if test="dt != null">#{dt},</if>
<if test="insertTime != null">DATE_FORMAT(#{insertTime},'%Y-%m-%d %H:%i:%s'),</if>
<if test="greenId != null and greenId != ''">#{greenId},</if>
</trim>
<insert id="insertProblemAndStrategy"
parameterType="net.wanji.opt.entity.judgeanalysis.AnalysisProblemAndStrategyDay">
insert into
t_event_optimize_info(cross_id,event_serial_number,event_category,event_type,happen_start_time,happen_end_time,duration,
opt_status,dir,opt_start_time,opt_end_time,opt_duration,dt,insert_time,green_id)
VALUES
<foreach collection="list" item="entity" separator=",">
(
#{entity.crossId},
#{entity.eventSerialNumber},
#{entity.eventCategory},
#{entity.eventType},
DATE_FORMAT(#{entity.happenStartTime},'%Y-%m-%d %H:%i:%s'),
DATE_FORMAT(#{entity.happenEndTime},'%Y-%m-%d %H:%i:%s'),
#{entity.duration},
#{entity.optStatus},
#{entity.dir},
DATE_FORMAT(#{entity.optStartTime},'%Y-%m-%d %H:%i:%s'),
DATE_FORMAT(#{entity.optEndTime},'%Y-%m-%d %H:%i:%s'),
#{entity.optDuration},
#{entity.dt},
DATE_FORMAT(#{entity.insertTime},'%Y-%m-%d %H:%i:%s'),
#{entity.greenId}
)
</foreach>
ON DUPLICATE KEY UPDATE
event_serial_number=VALUES(event_serial_number),
<if test="crossId != null">cross_id=VALUES(cross_id),</if>
<if test="eventCategory != null"> event_category=VALUES(event_category),</if>
<if test="eventType != null">event_type=VALUES(event_type),</if>
<if test="happenStartTime != null">happen_start_time=VALUES(happen_start_time),</if>
<if test="happenEndTime != null">happen_end_time=VALUES(happen_end_time),</if>
<if test="duration != null">duration=VALUES(duration),</if>
<if test="optStatus != null">opt_status=VALUES(opt_status),</if>
<if test="dir != null">dir=VALUES(dir),</if>
<if test="optStartTime != null">opt_start_time=VALUES(opt_start_time),</if>
<if test="optEndTime != null">opt_end_time=VALUES(opt_end_time),</if>
<if test="optDuration != null">opt_duration=VALUES(opt_duration),</if>
<if test="insertTime != null">insert_time=VALUES(insert_time),</if>
<if test="greenId != null and greenId != ''">green_id=VALUES(green_id),</if>
cross_id=VALUES(cross_id),
event_category=VALUES(event_category),
event_type=VALUES(event_type),
happen_start_time=VALUES(happen_start_time),
happen_end_time=VALUES(happen_end_time),
duration=VALUES(duration),
opt_status=VALUES(opt_status),
dir=VALUES(dir),
opt_start_time=VALUES(opt_start_time),
opt_end_time=VALUES(opt_end_time),
opt_duration=VALUES(opt_duration),
insert_time=VALUES(insert_time),
green_id=VALUES(green_id),
dt=VALUES(dt)
</insert>
</mapper>
\ No newline at end of file
/**
*
*/
import cn.hutool.core.date.DateUtil;
import net.wanji.opt.SignalOptimizeApplication;
import net.wanji.opt.dao.mapper.judgeanalysis.AnalysisProblemCrossDayMapper;
import net.wanji.opt.servicev2.judgeanalysis.AnalysisProblemAndStrategyDayService;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
/**
* @author fengyi
* @date
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SignalOptimizeApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EventOptimizeTaskTest {
@Autowired
private AnalysisProblemAndStrategyDayService analysisProblemAndStrategyDayService;
@Test
public void taskGreen() {
try {
//analysisProblemAndStrategyDayService.selectCountByTypeOfCross();
Integer dtStart = 20250423;// Integer.valueOf(DateUtil.beginOfMonth(new Date()).toString("yyyyMMdd"));
Integer dtEnd = 20250423;// Integer.valueOf(DateUtil.format(new Date(),"yyyyMMdd"));
analysisProblemAndStrategyDayService.selectCountByTypeOfGreen(dtStart,dtEnd);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void taskCross() {
try {
Integer dtStart = 20250423;// Integer.valueOf(DateUtil.beginOfMonth(new Date()).toString("yyyyMMdd"));
Integer dtEnd = 20250423; //Integer.valueOf(DateUtil.format(new Date(),"yyyyMMdd"));
analysisProblemAndStrategyDayService.selectCountByTypeOfCross(dtStart,dtEnd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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