Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
rpc-openplatform
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ecd30b21
authored
Jan 13, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
添加转移数据定时任务
1 parent
cb79fc1e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
86 additions
and
20 deletions
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/DeviceActionDetailMapper.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/DeviceNetworkDetailMapper.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/task/DeviceActionDetailTask.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/task/DeviceNetworkDetailTask.java
rpc-openplatform-container/src/main/resources/META-INF/orm/DeviceActionDetailMapper.xml
rpc-openplatform-container/src/main/resources/META-INF/orm/DeviceNetworkDetailMapper.xml
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/DeviceActionDetailMapper.java
View file @
ecd30b2
...
@@ -18,9 +18,13 @@ import org.apache.ibatis.annotations.Param;
...
@@ -18,9 +18,13 @@ import org.apache.ibatis.annotations.Param;
public
interface
DeviceActionDetailMapper
{
public
interface
DeviceActionDetailMapper
{
List
<
DeviceActionDetail
>
selectBeforeTime
(
@Param
(
"time"
)
Date
time
);
List
<
DeviceActionDetail
>
selectBeforeTime
(
@Param
(
"time"
)
Date
time
);
int
insertInto
(
@Param
(
"month"
)
String
month
,
@Param
(
"record"
)
DeviceActionDetail
record
);
int
batchInsertInto
(
@Param
(
"month"
)
String
month
,
int
batchInsertInto
(
@Param
(
"month"
)
String
month
,
@Param
(
"list"
)
List
<
DeviceActionDetail
>
list
);
@Param
(
"list"
)
List
<
DeviceActionDetail
>
list
);
int
deleteBeforeTime
(
@Param
(
"time"
)
Date
time
);
int
deleteByIds
(
List
<
Integer
>
idList
);
int
deleteByIds
(
List
<
Integer
>
idList
);
//List<String> selectDeviceType();
//List<String> selectDeviceType();
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/DeviceNetworkDetailMapper.java
View file @
ecd30b2
...
@@ -18,9 +18,13 @@ import org.apache.ibatis.annotations.Param;
...
@@ -18,9 +18,13 @@ import org.apache.ibatis.annotations.Param;
public
interface
DeviceNetworkDetailMapper
{
public
interface
DeviceNetworkDetailMapper
{
List
<
DeviceNetworkDetail
>
selectBeforeTime
(
@Param
(
"time"
)
Date
time
);
List
<
DeviceNetworkDetail
>
selectBeforeTime
(
@Param
(
"time"
)
Date
time
);
int
insertInto
(
@Param
(
"month"
)
String
month
,
@Param
(
"record"
)
DeviceNetworkDetail
record
);
int
batchInsertInto
(
@Param
(
"month"
)
String
month
,
int
batchInsertInto
(
@Param
(
"month"
)
String
month
,
@Param
(
"list"
)
List
<
DeviceNetworkDetail
>
list
);
@Param
(
"list"
)
List
<
DeviceNetworkDetail
>
list
);
int
deleteBeforeTime
(
@Param
(
"time"
)
Date
time
);
int
deleteByIds
(
List
<
Integer
>
idList
);
int
deleteByIds
(
List
<
Integer
>
idList
);
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/task/DeviceActionDetailTask.java
View file @
ecd30b2
...
@@ -2,7 +2,6 @@ package cn.com.glsx.openplatform.task;
...
@@ -2,7 +2,6 @@ package cn.com.glsx.openplatform.task;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
cn.com.glsx.openplatform.mapper.DeviceActionDetailMapper
;
import
cn.com.glsx.openplatform.mapper.DeviceActionDetailMapper
;
import
cn.com.glsx.openplatform.model.DeviceActionDetail
;
import
cn.com.glsx.openplatform.model.DeviceActionDetail
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.date.DateUtil
;
...
@@ -10,6 +9,8 @@ import org.slf4j.Logger;
...
@@ -10,6 +9,8 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
org.springframework.transaction.annotation.Transactional
;
/**
/**
* <p>Description:
* <p>Description:
...
@@ -18,7 +19,7 @@ import org.springframework.scheduling.annotation.Scheduled;
...
@@ -18,7 +19,7 @@ import org.springframework.scheduling.annotation.Scheduled;
* @author wbh
* @author wbh
* @date 2021-01-10
* @date 2021-01-10
*/
*/
//
@Component
@Component
public
class
DeviceActionDetailTask
{
public
class
DeviceActionDetailTask
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
...
@@ -26,18 +27,24 @@ public class DeviceActionDetailTask {
...
@@ -26,18 +27,24 @@ public class DeviceActionDetailTask {
private
DeviceActionDetailMapper
deviceActionDetailMapper
;
private
DeviceActionDetailMapper
deviceActionDetailMapper
;
@Scheduled
(
fixedDelay
=
10000
)
@Scheduled
(
fixedDelay
=
10000
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
moveDeviceActionDetail
()
{
public
void
moveDeviceActionDetail
()
{
this
.
logger
.
info
(
"$$$$ moveDeviceActionDetail start..."
);
this
.
logger
.
info
(
"$$$$ moveDeviceActionDetail start..."
);
Date
now
=
new
Date
();
Date
now
=
new
Date
();
List
<
DeviceActionDetail
>
actionDetailList
=
this
.
deviceActionDetailMapper
List
<
DeviceActionDetail
>
actionDetailList
=
this
.
deviceActionDetailMapper
.
selectBeforeTime
(
now
);
.
selectBeforeTime
(
now
);
this
.
logger
.
info
(
"$$$$ Queried list size: {}"
,
actionDetailList
.
size
());
this
.
logger
.
info
(
"$$$$ Queried list size: {}"
,
actionDetailList
.
size
());
if
(!
actionDetailList
.
isEmpty
())
{
// 删除原始数据
this
.
deviceActionDetailMapper
.
batchInsertInto
(
DateUtil
.
format
(
now
,
"yyyyMM"
),
this
.
deviceActionDetailMapper
.
deleteBeforeTime
(
now
);
actionDetailList
);
// 一次性插入大量数据容易出问题,因此循环插入
List
<
Integer
>
idList
=
actionDetailList
.
stream
().
map
(
DeviceActionDetail:
:
getId
)
String
month
=
DateUtil
.
format
(
now
,
"yyyyMM"
);
.
collect
(
Collectors
.
toList
());
int
index
=
0
;
this
.
deviceActionDetailMapper
.
deleteByIds
(
idList
);
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."
);
this
.
logger
.
info
(
"$$$$ moveDeviceActionDetail finished."
);
}
}
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/task/DeviceNetworkDetailTask.java
View file @
ecd30b2
...
@@ -2,7 +2,6 @@ package cn.com.glsx.openplatform.task;
...
@@ -2,7 +2,6 @@ package cn.com.glsx.openplatform.task;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
cn.com.glsx.openplatform.mapper.DeviceNetworkDetailMapper
;
import
cn.com.glsx.openplatform.mapper.DeviceNetworkDetailMapper
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.date.DateUtil
;
...
@@ -10,6 +9,7 @@ import org.slf4j.Logger;
...
@@ -10,6 +9,7 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.transaction.annotation.Transactional
;
/**
/**
* <p>Description:
* <p>Description:
...
@@ -26,18 +26,24 @@ public class DeviceNetworkDetailTask {
...
@@ -26,18 +26,24 @@ public class DeviceNetworkDetailTask {
private
DeviceNetworkDetailMapper
deviceNetworkDetailMapper
;
private
DeviceNetworkDetailMapper
deviceNetworkDetailMapper
;
@Scheduled
(
fixedDelay
=
10000
)
@Scheduled
(
fixedDelay
=
10000
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
moveDeviceNetworkDetail
()
{
public
void
moveDeviceNetworkDetail
()
{
this
.
logger
.
info
(
"$$$$ moveDeviceNetworkDetail start..."
);
this
.
logger
.
info
(
"$$$$ moveDeviceNetworkDetail start..."
);
Date
now
=
new
Date
();
Date
now
=
new
Date
();
List
<
DeviceNetworkDetail
>
networkDetailList
=
this
.
deviceNetworkDetailMapper
List
<
DeviceNetworkDetail
>
networkDetailList
=
this
.
deviceNetworkDetailMapper
.
selectBeforeTime
(
now
);
.
selectBeforeTime
(
now
);
this
.
logger
.
info
(
"$$$$ Queried list size: {}"
,
networkDetailList
.
size
());
this
.
logger
.
info
(
"$$$$ Queried list size: {}"
,
networkDetailList
.
size
());
if
(!
networkDetailList
.
isEmpty
())
{
// 删除原始数据
this
.
deviceNetworkDetailMapper
.
batchInsertInto
(
DateUtil
.
format
(
now
,
"yyyyMM"
),
this
.
deviceNetworkDetailMapper
.
deleteBeforeTime
(
now
);
networkDetailList
);
// 一次性插入大量数据容易导致问题,所以分批插入
List
<
Integer
>
idList
=
networkDetailList
.
stream
().
map
(
DeviceNetworkDetail:
:
getId
)
String
month
=
DateUtil
.
format
(
now
,
"yyyyMM"
);
.
collect
(
Collectors
.
toList
());
int
index
=
0
;
this
.
deviceNetworkDetailMapper
.
deleteByIds
(
idList
);
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."
);
this
.
logger
.
info
(
"$$$$ moveDeviceNetworkDetail finished."
);
}
}
...
...
rpc-openplatform-container/src/main/resources/META-INF/orm/DeviceActionDetailMapper.xml
View file @
ecd30b2
...
@@ -20,8 +20,25 @@
...
@@ -20,8 +20,25 @@
select id, device_sn, type, create_time
select id, device_sn, type, create_time
from t_device_action_detail
from t_device_action_detail
where create_time
<
#{time,jdbcType=TIMESTAMP}
where create_time
<
#{time,jdbcType=TIMESTAMP}
order by id
</select>
</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
id=
"batchInsertInto"
>
insert into
insert into
<choose>
<choose>
...
@@ -32,14 +49,19 @@
...
@@ -32,14 +49,19 @@
t_device_action_detail
t_device_action_detail
</otherwise>
</otherwise>
</choose>
</choose>
(device_sn, `type`, create_time)
(
id,
device_sn, `type`, create_time)
values
values
<foreach
collection=
"list"
item=
"item"
separator=
","
>
<foreach
collection=
"list"
item=
"item"
separator=
","
>
(#{item.
deviceSn,jdbcType=VARCHAR}, #{item.type
,jdbcType=VARCHAR},
(#{item.
id,jdbcType=INTEGER}, #{item.deviceSn
,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP})
#{item.type,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</foreach>
</insert>
</insert>
<delete
id=
"deleteBeforeTime"
>
delete from t_device_action_detail
where create_time
<
#{time,jdbcType=TIMESTAMP}
</delete>
<delete
id=
"deleteByIds"
>
<delete
id=
"deleteByIds"
>
delete from t_device_action_detail
delete from t_device_action_detail
where id in
where id in
...
...
rpc-openplatform-container/src/main/resources/META-INF/orm/DeviceNetworkDetailMapper.xml
View file @
ecd30b2
...
@@ -19,8 +19,25 @@
...
@@ -19,8 +19,25 @@
select id, device_sn, create_time
select id, device_sn, create_time
from t_device_network_detail
from t_device_network_detail
where create_time
<
#{time,jdbcType=TIMESTAMP}
where create_time
<
#{time,jdbcType=TIMESTAMP}
order by id
</select>
</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
id=
"batchInsertInto"
>
insert into
insert into
<choose>
<choose>
...
@@ -31,13 +48,19 @@
...
@@ -31,13 +48,19 @@
t_device_network_detail
t_device_network_detail
</otherwise>
</otherwise>
</choose>
</choose>
(device_sn, create_time)
(
id,
device_sn, create_time)
values
values
<foreach
collection=
"list"
item=
"item"
separator=
","
>
<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>
</foreach>
</insert>
</insert>
<delete
id=
"deleteBeforeTime"
>
delete from t_device_network_detail
where create_time
<
#{time,jdbcType=TIMESTAMP}
</delete>
<delete
id=
"deleteByIds"
>
<delete
id=
"deleteByIds"
>
delete from t_device_network_detail
delete from t_device_network_detail
where id in
where id in
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment