Commit ecd30b21 by tyfanchz

添加转移数据定时任务

1 parent cb79fc1e
......@@ -18,9 +18,13 @@ import org.apache.ibatis.annotations.Param;
public interface DeviceActionDetailMapper {
List<DeviceActionDetail> selectBeforeTime(@Param("time") Date time);
int insertInto(@Param("month") String month, @Param("record") DeviceActionDetail record);
int batchInsertInto(@Param("month") String month,
@Param("list") List<DeviceActionDetail> list);
int deleteBeforeTime(@Param("time") Date time);
int deleteByIds(List<Integer> idList);
//List<String> selectDeviceType();
......
......@@ -18,9 +18,13 @@ import org.apache.ibatis.annotations.Param;
public interface DeviceNetworkDetailMapper {
List<DeviceNetworkDetail> selectBeforeTime(@Param("time") Date time);
int insertInto(@Param("month") String month, @Param("record") DeviceNetworkDetail record);
int batchInsertInto(@Param("month") String month,
@Param("list") List<DeviceNetworkDetail> list);
int deleteBeforeTime(@Param("time") Date time);
int deleteByIds(List<Integer> idList);
......
......@@ -2,7 +2,6 @@ package cn.com.glsx.openplatform.task;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import cn.com.glsx.openplatform.mapper.DeviceActionDetailMapper;
import cn.com.glsx.openplatform.model.DeviceActionDetail;
import cn.hutool.core.date.DateUtil;
......@@ -10,6 +9,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* <p>Description:
......@@ -18,7 +19,7 @@ import org.springframework.scheduling.annotation.Scheduled;
* @author wbh
* @date 2021-01-10
*/
//@Component
@Component
public class DeviceActionDetailTask {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
......@@ -26,18 +27,24 @@ public class DeviceActionDetailTask {
private DeviceActionDetailMapper deviceActionDetailMapper;
@Scheduled(fixedDelay = 10000)
@Transactional(rollbackFor = Exception.class)
public void moveDeviceActionDetail() {
this.logger.info("$$$$ moveDeviceActionDetail start...");
Date now = new Date();
List<DeviceActionDetail> actionDetailList = this.deviceActionDetailMapper
.selectBeforeTime(now);
this.logger.info("$$$$ Queried list size: {}", actionDetailList.size());
if (!actionDetailList.isEmpty()) {
this.deviceActionDetailMapper.batchInsertInto(DateUtil.format(now, "yyyyMM"),
actionDetailList);
List<Integer> idList = actionDetailList.stream().map(DeviceActionDetail::getId)
.collect(Collectors.toList());
this.deviceActionDetailMapper.deleteByIds(idList);
// 删除原始数据
this.deviceActionDetailMapper.deleteBeforeTime(now);
// 一次性插入大量数据容易出问题,因此循环插入
String month = DateUtil.format(now, "yyyyMM");
int index = 0;
int listSize = actionDetailList.size();
while (index < listSize) {
int endIndex = Math.min(index + 1000, listSize);
List<DeviceActionDetail> subList = actionDetailList.subList(index, endIndex);
this.deviceActionDetailMapper.batchInsertInto(month, subList);
index = endIndex;
}
this.logger.info("$$$$ moveDeviceActionDetail finished.");
}
......
......@@ -2,7 +2,6 @@ package cn.com.glsx.openplatform.task;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import cn.com.glsx.openplatform.mapper.DeviceNetworkDetailMapper;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
import cn.hutool.core.date.DateUtil;
......@@ -10,6 +9,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
/**
* <p>Description:
......@@ -26,18 +26,24 @@ public class DeviceNetworkDetailTask {
private DeviceNetworkDetailMapper deviceNetworkDetailMapper;
@Scheduled(fixedDelay = 10000)
@Transactional(rollbackFor = Exception.class)
public void moveDeviceNetworkDetail() {
this.logger.info("$$$$ moveDeviceNetworkDetail start...");
Date now = new Date();
List<DeviceNetworkDetail> networkDetailList = this.deviceNetworkDetailMapper
.selectBeforeTime(now);
this.logger.info("$$$$ Queried list size: {}", networkDetailList.size());
if (!networkDetailList.isEmpty()) {
this.deviceNetworkDetailMapper.batchInsertInto(DateUtil.format(now, "yyyyMM"),
networkDetailList);
List<Integer> idList = networkDetailList.stream().map(DeviceNetworkDetail::getId)
.collect(Collectors.toList());
this.deviceNetworkDetailMapper.deleteByIds(idList);
// 删除原始数据
this.deviceNetworkDetailMapper.deleteBeforeTime(now);
// 一次性插入大量数据容易导致问题,所以分批插入
String month = DateUtil.format(now, "yyyyMM");
int index = 0;
int listSize = networkDetailList.size();
while (index < listSize) {
int endIndex = Math.min(index + 1000, networkDetailList.size());
List<DeviceNetworkDetail> subList = networkDetailList.subList(index, endIndex);
this.deviceNetworkDetailMapper.batchInsertInto(month, subList);
index = endIndex;
}
this.logger.info("$$$$ moveDeviceNetworkDetail finished.");
}
......
......@@ -20,8 +20,25 @@
select id, device_sn, type, create_time
from t_device_action_detail
where create_time &lt; #{time,jdbcType=TIMESTAMP}
order by id
</select>
<insert id="insertInto">
insert into
<choose>
<when test="month != null and month != ''">
t_device_action_detail_${month}
</when>
<otherwise>
t_device_action_detail
</otherwise>
</choose>
(id, device_sn, `type`, create_time)
values
(#{record.id,jdbcType=INTEGER}, #{record.deviceSn,jdbcType=VARCHAR},
#{record.type,jdbcType=VARCHAR}, #{record.createTime,jdbcType=TIMESTAMP})
</insert>
<insert id="batchInsertInto">
insert into
<choose>
......@@ -32,14 +49,19 @@
t_device_action_detail
</otherwise>
</choose>
(device_sn, `type`, create_time)
(id, device_sn, `type`, create_time)
values
<foreach collection="list" item="item" separator=",">
(#{item.deviceSn,jdbcType=VARCHAR}, #{item.type,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP})
(#{item.id,jdbcType=INTEGER}, #{item.deviceSn,jdbcType=VARCHAR},
#{item.type,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<delete id="deleteBeforeTime">
delete from t_device_action_detail
where create_time &lt; #{time,jdbcType=TIMESTAMP}
</delete>
<delete id="deleteByIds">
delete from t_device_action_detail
where id in
......
......@@ -19,8 +19,25 @@
select id, device_sn, create_time
from t_device_network_detail
where create_time &lt; #{time,jdbcType=TIMESTAMP}
order by id
</select>
<insert id="insertInto">
insert into
<choose>
<when test="month != null and month != ''">
t_device_network_detail_${month}
</when>
<otherwise>
t_device_network_detail
</otherwise>
</choose>
(id, device_sn, create_time)
values
(#{record.id,jdbcType=INTEGER}, #{record.deviceSn,jdbcType=VARCHAR},
#{record.createTime,jdbcType=TIMESTAMP})
</insert>
<insert id="batchInsertInto">
insert into
<choose>
......@@ -31,13 +48,19 @@
t_device_network_detail
</otherwise>
</choose>
(device_sn, create_time)
(id, device_sn, create_time)
values
<foreach collection="list" item="item" separator=",">
(#{item.deviceSn,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP})
(#{item.id,jdbcType=INTEGER}, #{item.deviceSn,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<delete id="deleteBeforeTime">
delete from t_device_network_detail
where create_time &lt; #{time,jdbcType=TIMESTAMP}
</delete>
<delete id="deleteByIds">
delete from t_device_network_detail
where id in
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!