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 cb79fc1e
authored
Jan 11, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
修改数据生成类
1 parent
2ddd2a21
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
999 additions
and
835 deletions
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/Main.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/CustomerActionStatisticsMapper.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/CustomerNetworkStatisticsMapper.java
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/CustomerActionStatisticsMapper.xml
rpc-openplatform-container/src/main/resources/META-INF/orm/CustomerNetworkStatisticsMapper.xml
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/test/java/cn/com/glsx/openplatform/GenerateCustomerActionStatistic.java
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceActionDetail.java
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceActionDetailByCustomer.java
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceActionDetailByCustomer2.java
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceNetworkDetailByCustomer.java
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GeneratePlatformDevice.java
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/generate-device-action-detail-scheduler.sql
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/generate-device-network-detail-scheduler.sql
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/Main.java
View file @
cb79fc1
...
...
@@ -4,6 +4,7 @@ import org.oreframework.boot.main.AbsOreBootStarter;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.web.servlet.ServletComponentScan
;
import
org.springframework.scheduling.annotation.EnableScheduling
;
/**
* @author deployer name
...
...
@@ -15,6 +16,7 @@ import org.springframework.boot.web.servlet.ServletComponentScan;
*/
@SpringBootApplication
@ServletComponentScan
@EnableScheduling
public
class
Main
extends
AbsOreBootStarter
{
public
static
void
main
(
String
[]
args
)
{
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/CustomerActionStatisticsMapper.java
View file @
cb79fc1
...
...
@@ -2,6 +2,7 @@ package cn.com.glsx.openplatform.mapper;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
cn.com.glsx.openplatform.model.CustomerActionStatistics
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
import
cn.com.glsx.openplatform.model.PlatformDevice
;
...
...
@@ -20,6 +21,13 @@ import org.apache.ibatis.annotations.Param;
public
interface
CustomerActionStatisticsMapper
{
List
<
String
>
selectMonthList
();
List
<
Map
<
String
,
Integer
>>
selectTypeCountMap
(
@Param
(
"statistics"
)
CustomerActionStatistics
statistics
);
int
countPlatformDevice
(
@Param
(
"statistics"
)
CustomerActionStatistics
statistics
,
@Param
(
"endDate"
)
Date
endDate
);
List
<
PlatformDevice
>
selectPlatformDevice
(
@Param
(
"statistics"
)
CustomerActionStatistics
statistics
,
@Param
(
"endDate"
)
Date
endDate
);
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/CustomerNetworkStatisticsMapper.java
View file @
cb79fc1
...
...
@@ -25,14 +25,14 @@ public interface CustomerNetworkStatisticsMapper {
@Param
(
"statistics"
)
CustomerNetworkStatistics
statistics
,
@Param
(
"endDate"
)
Date
endDate
);
List
<
CustomerNetworkStatistics
>
selectByDate
(
@Param
(
"startDate"
)
String
startDate
,
@Param
(
"endDate"
)
String
endDate
);
//
List<CustomerNetworkStatistics> selectByDate(@Param("startDate") String startDate,
//
@Param("endDate") String endDate);
//List<CustomerNetworkStatistics> selectFromDeviceNetworkByDate(
// @Param("startDate") Date startDate,
// @Param("endDate") Date endDate);
List
<
CustomerNetworkStatistics
>
selectFromCustomerDeviceStatistics
();
//
List<CustomerNetworkStatistics> selectFromCustomerDeviceStatistics();
int
deleteByPrimaryKey
(
Integer
id
);
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/DeviceActionDetailMapper.java
View file @
cb79fc1
...
...
@@ -3,7 +3,6 @@ package cn.com.glsx.openplatform.mapper;
import
java.util.Date
;
import
java.util.List
;
import
cn.com.glsx.openplatform.model.DeviceActionDetail
;
import
cn.com.glsx.openplatform.model.PlatformDevice
;
import
org.apache.ibatis.annotations.Mapper
;
import
org.apache.ibatis.annotations.Param
;
...
...
@@ -17,14 +16,21 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public
interface
DeviceActionDetailMapper
{
List
<
String
>
selectDeviceType
(
);
List
<
DeviceActionDetail
>
selectBeforeTime
(
@Param
(
"time"
)
Date
time
);
int
countByDate
(
@Param
(
"startDate"
)
Date
startDate
,
@Param
(
"endDate"
)
Date
endDate
,
@Param
(
"
type"
)
String
type
);
int
batchInsertInto
(
@Param
(
"month"
)
String
month
,
@Param
(
"
list"
)
List
<
DeviceActionDetail
>
list
);
List
<
PlatformDevice
>
selectByDate
(
@Param
(
"startDate"
)
Date
startDate
,
@Param
(
"endDate"
)
Date
endDate
,
@Param
(
"count"
)
Integer
count
,
@Param
(
"type"
)
String
type
);
int
deleteByIds
(
List
<
Integer
>
idList
);
//List<String> selectDeviceType();
//int countByDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate,
// @Param("type") String type);
//List<PlatformDevice> selectByDate(@Param("startDate") Date startDate,
// @Param("endDate") Date endDate, @Param("count") Integer count,
// @Param("type") String type);
int
deleteByPrimaryKey
(
Integer
id
);
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/DeviceNetworkDetailMapper.java
View file @
cb79fc1
package
cn
.
com
.
glsx
.
openplatform
.
mapper
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
import
java.util.Date
;
import
java.util.List
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
import
org.apache.ibatis.annotations.Mapper
;
import
org.apache.ibatis.annotations.Param
;
/**
* <p>Description:
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
...
...
@@ -15,6 +16,15 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public
interface
DeviceNetworkDetailMapper
{
List
<
DeviceNetworkDetail
>
selectBeforeTime
(
@Param
(
"time"
)
Date
time
);
int
batchInsertInto
(
@Param
(
"month"
)
String
month
,
@Param
(
"list"
)
List
<
DeviceNetworkDetail
>
list
);
int
deleteByIds
(
List
<
Integer
>
idList
);
int
deleteByPrimaryKey
(
Integer
id
);
int
insert
(
DeviceNetworkDetail
record
);
...
...
@@ -36,4 +46,4 @@ public interface DeviceNetworkDetailMapper {
int
updateBatchSelective
(
List
<
DeviceNetworkDetail
>
list
);
int
batchInsert
(
@Param
(
"list"
)
List
<
DeviceNetworkDetail
>
list
);
}
\ No newline at end of file
}
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/task/DeviceActionDetailTask.java
0 → 100644
View file @
cb79fc1
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
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-10
*/
//@Component
public
class
DeviceActionDetailTask
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
@Autowired
private
DeviceActionDetailMapper
deviceActionDetailMapper
;
@Scheduled
(
fixedDelay
=
10000
)
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
.
logger
.
info
(
"$$$$ moveDeviceActionDetail finished."
);
}
}
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/task/DeviceNetworkDetailTask.java
0 → 100644
View file @
cb79fc1
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
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-10
*/
//@Component
public
class
DeviceNetworkDetailTask
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
@Autowired
private
DeviceNetworkDetailMapper
deviceNetworkDetailMapper
;
@Scheduled
(
fixedDelay
=
10000
)
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
.
logger
.
info
(
"$$$$ moveDeviceNetworkDetail finished."
);
}
}
rpc-openplatform-container/src/main/resources/META-INF/orm/CustomerActionStatisticsMapper.xml
View file @
cb79fc1
...
...
@@ -23,6 +23,40 @@
<select
id=
"selectMonthList"
resultType=
"java.lang.String"
>
select distinct statistics_time
from t_customer_action_statistics
where statistics_time >= '2020-07'
order by statistics_time
</select>
<select
id=
"selectTypeCountMap"
resultType=
"java.util.Map"
>
select type, number
from t_customer_action_statistics
<where>
<if
test=
"statistics.customerId != null"
>
and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if>
<if
test=
"statistics.deviceType != null and statistics.deviceType != ''"
>
and device_type = #{statistics.deviceType,jdbcType=VARCHAR}
</if>
<if
test=
"statistics.statisticsTime != null and statistics.statisticsTime != ''"
>
and statistics_time = #{statistics.statisticsTime,jdbcType=VARCHAR}
</if>
</where>
</select>
<select
id=
"countPlatformDevice"
resultType=
"int"
>
select count(1)
from t_platform_device
<where>
<if
test=
"statistics.customerId != null"
>
and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if>
<if
test=
"statistics.deviceType != null and statistics.deviceType != ''"
>
and device_type = #{statistics.deviceType,jdbcType=VARCHAR}
</if>
<if
test=
"endDate != null"
>
and activat_time
<
#{endDate,jdbcType=TIMESTAMP}
</if>
</where>
</select>
<select
id=
"selectPlatformDevice"
resultType=
"cn.com.glsx.openplatform.model.PlatformDevice"
>
...
...
@@ -33,6 +67,9 @@
<if
test=
"statistics.customerId != null"
>
and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if>
<if
test=
"statistics.deviceType != null and statistics.deviceType != ''"
>
and device_type = #{statistics.deviceType,jdbcType=VARCHAR}
</if>
<if
test=
"endDate != null"
>
and activat_time
<
#{endDate,jdbcType=TIMESTAMP}
</if>
...
...
@@ -65,6 +102,7 @@
and statistics_time = #{month,jdbcType=VARCHAR}
</if>
</where>
order by customer_id
</select>
<select
id=
"selectOnlineNumber"
resultType=
"java.lang.Integer"
>
...
...
rpc-openplatform-container/src/main/resources/META-INF/orm/CustomerNetworkStatisticsMapper.xml
View file @
cb79fc1
...
...
@@ -23,6 +23,8 @@
<select
id=
"selectMonthList"
resultType=
"java.lang.String"
>
select distinct statistics_time
from t_customer_network_statistics
where statistics_time >= '2020-07'
order by statistics_time
</select>
<select
id=
"selectByMonth"
resultMap=
"BaseResultMap"
>
...
...
@@ -39,13 +41,16 @@
<select
id=
"selectPlatformDevice"
resultType=
"cn.com.glsx.openplatform.model.PlatformDevice"
>
select id, device_sn deviceSn, device_type deviceType, version version,
customer_id customerId, user_id userId, activat_time activatTime,
create_time createTime
customer_id customerId, user_id userId, activat_time activatTime,
create_time createTime
from t_platform_device
<where>
<if
test=
"statistics.customerId != null"
>
and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if>
<if
test=
"statistics.deviceType != null and statistics.deviceType != ''"
>
and device_type = #{statistics.deviceType,jdbcType=VARCHAR}
</if>
<if
test=
"endDate != null"
>
and activat_time
<
#{endDate,jdbcType=TIMESTAMP}
</if>
...
...
@@ -54,45 +59,27 @@
limit #{statistics.onlineNumber,jdbcType=INTEGER}
</select>
<select
id=
"selectByDate"
resultMap=
"BaseResultMap"
>
select id, customer_id, customer_name, device_type, statistics_time, online_number,
online_count
from t_customer_network_statistics cns
<where>
<if
test=
"startDate != null"
>
and statistics_time >= #{startDate,jdbcType=VARCHAR}
</if>
<if
test=
"endDate != null"
>
and statistics_time
<
#{endDate,jdbcType=VARCHAR}
</if>
</where>
</select>
<!--<select id="selectFromDeviceNetworkByDate" resultMap="BaseResultMap">-->
<!-- select pd.customer_id customerId, pc.customer_name customerName,-->
<!-- date_format(dnd.create_time, '%Y-%m') statisticsTime,-->
<!-- count(pd.device_sn) onlineNumber-->
<!-- from t_device_network_detail dnd-->
<!-- left join t_platform_device pd on pd.device_sn = dnd.device_sn-->
<!-- left join t_platform_customer pc on pc.id = pd.customer_id-->
<!--<select id="selectByDate" resultMap="BaseResultMap">-->
<!-- select id, customer_id, customer_name, device_type, statistics_time, online_number,-->
<!-- online_count-->
<!-- from t_customer_network_statistics cns-->
<!-- <where>-->
<!-- <if test="startDate != null">-->
<!-- and
dnd.create_time >= #{startDate,jdbcType=TIMESTAMP
}-->
<!-- and
statistics_time >= #{startDate,jdbcType=VARCHAR
}-->
<!-- </if>-->
<!-- <if test="endDate != null">-->
<!-- and
dnd.create_time < #{endDate,jdbcType=TIMESTAMP
}-->
<!-- and
statistics_time < #{endDate,jdbcType=VARCHAR
}-->
<!-- </if>-->
<!-- </where>-->
<!-- group by pd.customer_id, statisticsTime-->
<!--</select>-->
<
select
id=
"selectFromCustomerDeviceStatistics"
resultType=
"cn.com.glsx.openplatform.model.CustomerNetworkStatistics"
>
select customer_id customerId, customer_name customerName, device_type deviceType,
statistics_time statisticsTime, sum(online_number) onlineNumber,
sum(online_device_num) onlineDeviceNum
from t_customer_device_statistics
group by customer_id, statistics_time
<
/select
>
<
!--<select id="selectFromCustomerDeviceStatistics" resultType="cn.com.glsx.openplatform.model.CustomerNetworkStatistics">--
>
<!-- select customer_id customerId, customer_name customerName, device_type deviceType,-->
<!-- statistics_time statisticsTime, sum(online_number) onlineNumber,-->
<!-- sum(online_device_num) onlineDeviceNum-->
<!-- from t_customer_device_statistics-->
<!-- group by customer_id, statistics_time-->
<
!--</select>--
>
...
...
rpc-openplatform-container/src/main/resources/META-INF/orm/DeviceActionDetailMapper.xml
View file @
cb79fc1
...
...
@@ -16,49 +16,81 @@
id, device_sn, `type`, create_time
</sql>
<select
id=
"select
DeviceType"
resultType=
"java.lang.String
"
>
select
distinct device_typ
e
from t_
platform_device
where
device_type != '通信模组'
<select
id=
"select
BeforeTime"
resultMap=
"BaseResultMap
"
>
select
id, device_sn, type, create_tim
e
from t_
device_action_detail
where
create_time
<
#{time,jdbcType=TIMESTAMP}
</select>
<select
id=
"countByDate"
resultType=
"int"
>
select count(1)
from t_platform_device
<where>
<if
test=
"startDate != null"
>
and activat_time >= #{startDate,jdbcType=TIMESTAMP}
</if>
<if
test=
"endDate != null"
>
and activat_time
<
#{endDate,jdbcType=TIMESTAMP}
</if>
<if
test=
"type != null and type != ''"
>
and device_type = #{type,jdbcType=VARCHAR}
</if>
</where>
</select>
<insert
id=
"batchInsertInto"
>
insert into
<choose>
<when
test=
"month != null and month != ''"
>
t_device_action_detail_${month}
</when>
<otherwise>
t_device_action_detail
</otherwise>
</choose>
(device_sn, `type`, create_time)
values
<foreach
collection=
"list"
item=
"item"
separator=
","
>
(#{item.deviceSn,jdbcType=VARCHAR}, #{item.type,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<select
id=
"selectByDate"
resultType=
"cn.com.glsx.openplatform.model.PlatformDevice"
>
select id, device_sn deviceSn, device_type deviceType, customer_id customerId,
user_id userId, activat_time activatTime, status, create_time createTime,
update_time updateTime, create_by createBy, update_by updateBy, remark
from t_platform_device
<where>
<if
test=
"startDate != null"
>
and activat_time >= #{startDate,jdbcType=TIMESTAMP}
</if>
<if
test=
"endDate != null"
>
and activat_time
<
#{endDate,jdbcType=TIMESTAMP}
</if>
<if
test=
"type != null and type != ''"
>
and device_type = #{type,jdbcType=VARCHAR}
</if>
</where>
order by rand()
<if
test=
"count != null"
>
limit #{count,jdbcType=INTEGER}
</if>
</select>
<delete
id=
"deleteByIds"
>
delete from t_device_action_detail
where id in
<foreach
collection=
"list"
item=
"item"
open=
"("
separator=
","
close=
")"
>
#{item,jdbcType=INTEGER}
</foreach>
</delete>
<!--<select id="selectDeviceType" resultType="java.lang.String">-->
<!-- select distinct device_type-->
<!-- from t_platform_device-->
<!-- where device_type != '通信模组'-->
<!--</select>-->
<!--<select id="countByDate" resultType="int">-->
<!-- select count(1)-->
<!-- from t_platform_device-->
<!-- <where>-->
<!-- <if test="startDate != null">-->
<!-- and activat_time >= #{startDate,jdbcType=TIMESTAMP}-->
<!-- </if>-->
<!-- <if test="endDate != null">-->
<!-- and activat_time < #{endDate,jdbcType=TIMESTAMP}-->
<!-- </if>-->
<!-- <if test="type != null and type != ''">-->
<!-- and device_type = #{type,jdbcType=VARCHAR}-->
<!-- </if>-->
<!-- </where>-->
<!--</select>-->
<!--<select id="selectByDate" resultType="cn.com.glsx.openplatform.model.PlatformDevice">-->
<!-- select id, device_sn deviceSn, device_type deviceType, customer_id customerId,-->
<!-- user_id userId, activat_time activatTime, status, create_time createTime,-->
<!-- update_time updateTime, create_by createBy, update_by updateBy, remark-->
<!-- from t_platform_device-->
<!-- <where>-->
<!-- <if test="startDate != null">-->
<!-- and activat_time >= #{startDate,jdbcType=TIMESTAMP}-->
<!-- </if>-->
<!-- <if test="endDate != null">-->
<!-- and activat_time < #{endDate,jdbcType=TIMESTAMP}-->
<!-- </if>-->
<!-- <if test="type != null and type != ''">-->
<!-- and device_type = #{type,jdbcType=VARCHAR}-->
<!-- </if>-->
<!-- </where>-->
<!-- order by rand()-->
<!-- <if test="count != null">-->
<!-- limit #{count,jdbcType=INTEGER}-->
<!-- </if>-->
<!--</select>-->
<select
id=
"selectByPrimaryKey"
parameterType=
"java.lang.Integer"
resultMap=
"BaseResultMap"
>
<!--@mbg.generated-->
...
...
rpc-openplatform-container/src/main/resources/META-INF/orm/DeviceNetworkDetailMapper.xml
View file @
cb79fc1
...
...
@@ -15,6 +15,39 @@
id, device_sn, create_time
</sql>
<select
id=
"selectBeforeTime"
resultMap=
"BaseResultMap"
>
select id, device_sn, create_time
from t_device_network_detail
where create_time
<
#{time,jdbcType=TIMESTAMP}
</select>
<insert
id=
"batchInsertInto"
>
insert into
<choose>
<when
test=
"month != null and month != ''"
>
t_device_network_detail_${month}
</when>
<otherwise>
t_device_network_detail
</otherwise>
</choose>
(device_sn, create_time)
values
<foreach
collection=
"list"
item=
"item"
separator=
","
>
(#{item.deviceSn,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<delete
id=
"deleteByIds"
>
delete from t_device_network_detail
where id in
<foreach
collection=
"list"
item=
"id"
open=
"("
separator=
","
close=
")"
>
#{id,jdbcType=INTEGER}
</foreach>
</delete>
<select
id=
"selectByPrimaryKey"
parameterType=
"java.lang.Integer"
resultMap=
"BaseResultMap"
>
<!--@mbg.generated-->
select
...
...
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateCustomerActionStatistic.java
View file @
cb79fc1
package
cn
.
com
.
glsx
.
openplatform
;
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.List
;
import
cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper
;
import
cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper
;
import
cn.com.glsx.openplatform.model.CustomerNetworkStatistics
;
import
cn.hutool.core.util.RandomUtil
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-02
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
Main
.
class
)
public
class
GenerateCustomerActionStatistic
{
@Autowired
private
CustomerActionStatisticsMapper
customerActionStatisticsMapper
;
@Autowired
private
CustomerNetworkStatisticsMapper
customerNetworkStatisticsMapper
;
private
BufferedWriter
customerActionStatisticsWriter
;
@Test
public
void
generateData
()
{
this
.
initCustomerActionStatisticWriter
();
List
<
CustomerNetworkStatistics
>
networkStatisticsList
=
this
.
customerNetworkStatisticsMapper
.
selectByDate
(
null
,
null
);
for
(
CustomerNetworkStatistics
networkStatistics
:
networkStatisticsList
)
{
Integer
onlineNumber
=
networkStatistics
.
getOnlineNumber
();
this
.
addCustomerActionStatisticsRecord
(
networkStatistics
,
"声控调用"
,
this
.
getRandomDouble
(
onlineNumber
,
5
,
9
));
this
.
addCustomerActionStatisticsRecord
(
networkStatistics
,
"收音机使用"
,
this
.
getRandomDouble
(
onlineNumber
,
28
,
33
));
this
.
addCustomerActionStatisticsRecord
(
networkStatistics
,
"导航调用"
,
this
.
getRandomDouble
(
onlineNumber
,
3
,
6
));
this
.
addCustomerActionStatisticsRecord
(
networkStatistics
,
"音乐使用"
,
this
.
getRandomDouble
(
onlineNumber
,
13
,
18
));
this
.
addCustomerActionStatisticsRecord
(
networkStatistics
,
"联网应用使用"
,
this
.
getRandomDouble
(
onlineNumber
,
2
,
4
));
}
this
.
closeCustomerActionStatisticsWriter
();
}
private
void
initCustomerActionStatisticWriter
()
{
try
{
this
.
customerActionStatisticsWriter
=
Files
.
newBufferedWriter
(
Paths
.
get
(
"D:/Users/wbh/Desktop/customer-action-statistics.sql"
));
this
.
customerActionStatisticsWriter
.
write
(
"insert into t_customer_action_statistics "
+
"(customer_id, customer_name, device_type, type, statistics_time, number) values"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
closeCustomerActionStatisticsWriter
()
{
try
{
this
.
customerActionStatisticsWriter
.
flush
();
this
.
customerActionStatisticsWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
int
getRandomDouble
(
int
base
,
int
min
,
int
max
)
{
return
(
int
)
(((
double
)
base
)
*
RandomUtil
.
randomDouble
(
min
,
max
));
}
private
void
addCustomerActionStatisticsRecord
(
CustomerNetworkStatistics
networkStatistics
,
String
type
,
int
count
)
{
try
{
String
s
=
String
.
format
(
",\n(%d, '%s', '%s', '%s', '%s', %d)"
,
networkStatistics
.
getCustomerId
(),
networkStatistics
.
getCustomerName
(),
networkStatistics
.
getDeviceType
(),
type
,
networkStatistics
.
getStatisticsTime
(),
count
);
this
.
customerActionStatisticsWriter
.
write
(
s
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
//
package cn.com.glsx.openplatform;
//
//
import java.io.BufferedWriter;
//
import java.io.IOException;
//
import java.nio.file.Files;
//
import java.nio.file.Paths;
//
import java.util.List;
//
import cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper;
//
import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
//
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
//
import cn.hutool.core.util.RandomUtil;
//
import org.junit.Test;
//
import org.junit.runner.RunWith;
//
import org.springframework.beans.factory.annotation.Autowired;
//
import org.springframework.boot.test.context.SpringBootTest;
//
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//
/
//
**
//
* <p>Description:
//
* <p>Project: rpc-openplatform-container
//
*
//
* @author wbh
//
* @date 2021-01-02
//
*/
//
@RunWith(SpringJUnit4ClassRunner.class)
//
@SpringBootTest(classes = Main.class)
//
public class GenerateCustomerActionStatistic {
//
@Autowired
//
private CustomerActionStatisticsMapper customerActionStatisticsMapper;
//
//
@Autowired
//
private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
//
//
private BufferedWriter customerActionStatisticsWriter;
//
//
@Test
//
public void generateData() {
//
this.initCustomerActionStatisticWriter();
//
//
List<CustomerNetworkStatistics> networkStatisticsList = this.customerNetworkStatisticsMapper
//
.selectByDate(null, null);
//
for (CustomerNetworkStatistics networkStatistics : networkStatisticsList) {
//
Integer onlineNumber = networkStatistics.getOnlineNumber();
//
this.addCustomerActionStatisticsRecord(networkStatistics, "声控调用",
//
this.getRandomDouble(onlineNumber, 5, 9));
//
this.addCustomerActionStatisticsRecord(networkStatistics, "收音机使用",
//
this.getRandomDouble(onlineNumber, 28, 33));
//
this.addCustomerActionStatisticsRecord(networkStatistics, "导航调用",
//
this.getRandomDouble(onlineNumber, 3, 6));
//
this.addCustomerActionStatisticsRecord(networkStatistics, "音乐使用",
//
this.getRandomDouble(onlineNumber, 13, 18));
//
this.addCustomerActionStatisticsRecord(networkStatistics, "联网应用使用",
//
this.getRandomDouble(onlineNumber, 2, 4));
//
}
//
//
this.closeCustomerActionStatisticsWriter();
//
}
//
//
private void initCustomerActionStatisticWriter() {
//
try {
//
this.customerActionStatisticsWriter = Files.newBufferedWriter(
//
Paths.get("D:/Users/wbh/Desktop/customer-action-statistics.sql"));
//
this.customerActionStatisticsWriter.write(
//
"insert into t_customer_action_statistics " +
//
"(customer_id, customer_name, device_type, type, statistics_time, number) values");
//
} catch (IOException e) {
//
e.printStackTrace();
//
}
//
}
//
//
private void closeCustomerActionStatisticsWriter() {
//
try {
//
this.customerActionStatisticsWriter.flush();
//
this.customerActionStatisticsWriter.close();
//
} catch (IOException e) {
//
e.printStackTrace();
//
}
//
}
//
//
private int getRandomDouble(int base, int min, int max) {
//
return (int) (((double) base) * RandomUtil.randomDouble(min, max));
//
}
//
//
private void addCustomerActionStatisticsRecord(CustomerNetworkStatistics networkStatistics,
//
String type, int count) {
//
try {
//
String s = String.format(",\n(%d, '%s', '%s', '%s', '%s', %d)",
//
networkStatistics.getCustomerId(), networkStatistics.getCustomerName(),
//
networkStatistics.getDeviceType(),
//
type, networkStatistics.getStatisticsTime(), count);
//
this.customerActionStatisticsWriter.write(s);
//
} catch (IOException e) {
//
e.printStackTrace();
//
}
//
}
//
}
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceActionDetail.java
View file @
cb79fc1
package
cn
.
com
.
glsx
.
openplatform
;
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.List
;
import
cn.com.glsx.openplatform.mapper.DeviceActionDetailMapper
;
import
cn.com.glsx.openplatform.model.DeviceActionDetail
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
import
cn.com.glsx.openplatform.model.PlatformDevice
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.util.RandomUtil
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-02
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
Main
.
class
)
public
class
GenerateDeviceActionDetail
{
@Autowired
private
DeviceActionDetailMapper
deviceActionDetailMapper
;
//@Autowired
//private DeviceNetworkDetailMapper deviceNetworkDetailMapper;
private
BufferedWriter
deviceActionDataWriter
;
private
BufferedWriter
deviceNetworkDataWriter
;
@Test
public
void
generateData
()
throws
IOException
{
this
.
initDeviceActionDataWriter
();
//this.initDeviceNetworkDataWriter();
Date
startDate
=
DateUtil
.
parseDate
(
"2019-01-01"
);
Date
endDate
=
DateUtil
.
parseDate
(
"2021-01-01"
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
Date
currentStartDate
=
calendar
.
getTime
();
calendar
.
add
(
Calendar
.
MONTH
,
1
);
Date
currentEndDate
=
calendar
.
getTime
();
while
(
currentStartDate
.
before
(
endDate
))
{
// 获取所有类型的设备,按照设备类型进行生成
List
<
String
>
typeList
=
this
.
deviceActionDetailMapper
.
selectDeviceType
();
for
(
String
type
:
typeList
)
{
List
<
PlatformDevice
>
deviceList
=
this
.
getNeededDeviceList
(
currentStartDate
,
currentEndDate
,
type
);
this
.
generateDeviceActionData
(
currentStartDate
,
endDate
,
deviceList
);
//this.generateDeviceNetworkData(currentStartDate, deviceList);
}
currentStartDate
=
currentEndDate
;
calendar
.
add
(
Calendar
.
MONTH
,
1
);
currentEndDate
=
calendar
.
getTime
();
}
this
.
closeDeviceActionDataWriter
();
//this.closeDeviceNetworkDataWriter();
}
private
void
initDeviceNetworkDataWriter
()
throws
IOException
{
this
.
deviceNetworkDataWriter
=
Files
.
newBufferedWriter
(
Paths
.
get
(
"D:/Users/wbh/Desktop/device-network-data.sql"
));
this
.
deviceNetworkDataWriter
.
write
(
"insert into t_device_network_detail (device_sn, create_time) values"
);
}
private
void
closeDeviceNetworkDataWriter
()
throws
IOException
{
this
.
deviceNetworkDataWriter
.
flush
();
this
.
deviceNetworkDataWriter
.
close
();
}
private
void
initDeviceActionDataWriter
()
throws
IOException
{
this
.
deviceActionDataWriter
=
Files
.
newBufferedWriter
(
Paths
.
get
(
"D:/Users/wbh/Desktop/device-action-data.sql"
));
this
.
deviceActionDataWriter
.
write
(
"insert into t_device_action_detail (device_sn, type, create_time) values"
);
}
private
void
closeDeviceActionDataWriter
()
throws
IOException
{
this
.
deviceActionDataWriter
.
flush
();
this
.
deviceActionDataWriter
.
close
();
}
private
List
<
PlatformDevice
>
getNeededDeviceList
(
Date
startDate
,
Date
endDate
,
String
type
)
{
int
count
=
this
.
deviceActionDetailMapper
.
countByDate
(
startDate
,
endDate
,
type
);
// 取所有数量的 30% 左右
int
neededCount
=
this
.
getNumPercent
(
count
,
RandomUtil
.
randomInt
(
25
,
35
));
List
<
PlatformDevice
>
deviceList
=
this
.
deviceActionDetailMapper
.
selectByDate
(
startDate
,
endDate
,
neededCount
,
type
);
return
deviceList
;
}
private
void
generateDeviceActionData
(
Date
startDate
,
Date
endDate
,
List
<
PlatformDevice
>
deviceList
)
{
this
.
addDeviceActionSoundControlData
(
startDate
,
endDate
,
deviceList
);
this
.
addDeviceActionFmData
(
startDate
,
endDate
,
deviceList
);
this
.
addDeviceActionNavigationData
(
startDate
,
endDate
,
deviceList
);
this
.
addDeviceActionMusicData
(
startDate
,
endDate
,
deviceList
);
this
.
addDeviceActionInternetData
(
startDate
,
endDate
,
deviceList
);
}
private
void
generateDeviceNetworkData
(
Date
startDate
,
List
<
PlatformDevice
>
deviceList
)
{
this
.
addDeviceNetworkData
(
startDate
,
deviceList
);
}
private
int
getNumPercent
(
int
num
,
int
percent
)
{
return
num
*
percent
/
100
;
}
/**
* 生成声控调用数据
*
* @param startDate 开始日期
* @param endDate 结束日期
* @param deviceList 设备数据
*/
private
void
addDeviceActionSoundControlData
(
Date
startDate
,
Date
endDate
,
List
<
PlatformDevice
>
deviceList
)
{
int
neededCount
=
deviceList
.
size
();
// 取总数的 40% 左右,每天生成两条数据
int
twiceCount
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
35
,
45
));
List
<
PlatformDevice
>
twiceDeviceList
=
deviceList
.
subList
(
0
,
twiceCount
);
// 取剩余量的 60% 左右,每天生成一条数据
int
onceCount
=
this
.
getNumPercent
(
neededCount
-
twiceCount
,
RandomUtil
.
randomInt
(
55
,
65
));
List
<
PlatformDevice
>
onceDeviceList
=
deviceList
.
subList
(
twiceCount
,
twiceCount
+
onceCount
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
Date
currentDate
=
startDate
;
while
(
currentDate
.
before
(
endDate
))
{
// 每天需要生成两条数据的
for
(
PlatformDevice
device
:
twiceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"声控调用"
,
2
,
currentDate
);
}
// 每天生成一条数据的
for
(
PlatformDevice
device
:
onceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"声控调用"
,
1
,
currentDate
);
}
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
currentDate
=
calendar
.
getTime
();
}
}
/**
* 生成声收音机使用数据
*
* @param startDate 开始日期
* @param endDate 结束日期
* @param deviceList 设备数据
*/
private
void
addDeviceActionFmData
(
Date
startDate
,
Date
endDate
,
List
<
PlatformDevice
>
deviceList
)
{
int
neededCount
=
deviceList
.
size
();
// 取总数的 70% 左右,每天生成一条数据
int
onceCount
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
65
,
75
));
List
<
PlatformDevice
>
onceDeviceList
=
deviceList
.
subList
(
0
,
onceCount
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
Date
currentDate
=
startDate
;
while
(
currentDate
.
before
(
endDate
))
{
// 每天生成一条数据的
for
(
PlatformDevice
device
:
onceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"收音机使用"
,
1
,
currentDate
);
}
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
currentDate
=
calendar
.
getTime
();
}
}
/**
* 生成导航调用数据
*
* @param startDate 开始日期
* @param endDate 结束日期
* @param deviceList 设备数据
*/
private
void
addDeviceActionNavigationData
(
Date
startDate
,
Date
endDate
,
List
<
PlatformDevice
>
deviceList
)
{
int
neededCount
=
deviceList
.
size
();
// 取总数的 50% 左右,每天生成两条数据
int
twiceCount
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
45
,
55
));
List
<
PlatformDevice
>
twiceDeviceList
=
deviceList
.
subList
(
0
,
twiceCount
);
// 取剩余量的 50% 左右,每天生成一条数据
int
onceCount
=
this
.
getNumPercent
(
neededCount
-
twiceCount
,
RandomUtil
.
randomInt
(
45
,
55
));
List
<
PlatformDevice
>
onceDeviceList
=
deviceList
.
subList
(
twiceCount
,
twiceCount
+
onceCount
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
Date
currentDate
=
startDate
;
while
(
currentDate
.
before
(
endDate
))
{
// 每天需要生成两条数据的
for
(
PlatformDevice
device
:
twiceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"导航调用"
,
2
,
currentDate
);
}
// 每天生成一条数据的
for
(
PlatformDevice
device
:
onceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"导航调用"
,
1
,
currentDate
);
}
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
currentDate
=
calendar
.
getTime
();
}
}
/**
* 生成音乐使用数据
*
* @param startDate 开始日期
* @param endDate 结束日期
* @param deviceList 设备数据
*/
private
void
addDeviceActionMusicData
(
Date
startDate
,
Date
endDate
,
List
<
PlatformDevice
>
deviceList
)
{
int
neededCount
=
deviceList
.
size
();
// 取总数的 70% 左右,每天生成一条数据
int
onceCount
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
65
,
75
));
List
<
PlatformDevice
>
onceDeviceList
=
deviceList
.
subList
(
0
,
onceCount
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
Date
currentDate
=
startDate
;
while
(
currentDate
.
before
(
endDate
))
{
// 每天生成一条数据的
for
(
PlatformDevice
device
:
onceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"音乐使用"
,
1
,
currentDate
);
}
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
currentDate
=
calendar
.
getTime
();
}
}
/**
* 生成联网应用数据
*
* @param startDate 开始日期
* @param endDate 结束日期
* @param deviceList 设备数据
*/
private
void
addDeviceActionInternetData
(
Date
startDate
,
Date
endDate
,
List
<
PlatformDevice
>
deviceList
)
{
int
neededCount
=
deviceList
.
size
();
// 取总数的 40% 左右,每天生成一条数据
int
onceCount
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
35
,
45
));
List
<
PlatformDevice
>
onceDeviceList
=
deviceList
.
subList
(
0
,
onceCount
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
Date
currentDate
=
startDate
;
while
(
currentDate
.
before
(
endDate
))
{
// 每天生成一条数据的
for
(
PlatformDevice
device
:
onceDeviceList
)
{
this
.
addDeviceActionDetailRecord
(
device
,
"联网应用使用"
,
1
,
currentDate
);
}
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
currentDate
=
calendar
.
getTime
();
}
}
/**
* 按照条件生成设备行为数据
*
* @param device 设备数据
* @param type 设备行为类型
* @param count 生成数量
* @param currentDate 当前日期
*/
private
void
addDeviceActionDetailRecord
(
PlatformDevice
device
,
String
type
,
int
count
,
Date
currentDate
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
// 生成每日随机时间,范围为 06:00:00 ~ 22:59:59
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
RandomUtil
.
randomInt
(
6
,
23
));
calendar
.
set
(
Calendar
.
MINUTE
,
RandomUtil
.
randomInt
(
0
,
60
));
calendar
.
set
(
Calendar
.
SECOND
,
RandomUtil
.
randomInt
(
0
,
60
));
DeviceActionDetail
deviceActionDetail
=
new
DeviceActionDetail
();
deviceActionDetail
.
setDeviceSn
(
device
.
getDeviceSn
());
deviceActionDetail
.
setType
(
type
);
deviceActionDetail
.
setCreateTime
(
calendar
.
getTime
());
try
{
String
s
=
String
.
format
(
",\n('%s', '%s', '%s')"
,
deviceActionDetail
.
getDeviceSn
(),
deviceActionDetail
.
getType
(),
DateUtil
.
formatDateTime
(
deviceActionDetail
.
getCreateTime
()));
this
.
deviceActionDataWriter
.
write
(
s
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//this.deviceActionDetailMapper.insertSelective(deviceActionDetail);
}
}
private
void
addDeviceNetworkData
(
Date
startDate
,
List
<
PlatformDevice
>
deviceList
)
{
int
neededCount
=
deviceList
.
size
();
// 取总量的 20% 左右,生成小于 10 条数据
int
lt10Count
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
18
,
25
));
List
<
PlatformDevice
>
lt20DeviceList
=
deviceList
.
subList
(
0
,
lt10Count
);
for
(
PlatformDevice
device
:
lt20DeviceList
)
{
this
.
addDeviceNetworkDetailRecord
(
device
,
RandomUtil
.
randomInt
(
5
,
10
),
startDate
);
}
// 取总量的 60% 左右,生成 20 条数据
int
from10To20Count
=
this
.
getNumPercent
(
neededCount
,
RandomUtil
.
randomInt
(
55
,
65
));
List
<
PlatformDevice
>
from10To20DeviceList
=
deviceList
.
subList
(
lt10Count
,
lt10Count
+
from10To20Count
);
for
(
PlatformDevice
device
:
from10To20DeviceList
)
{
this
.
addDeviceNetworkDetailRecord
(
device
,
RandomUtil
.
randomInt
(
12
,
21
),
startDate
);
}
// 取总量的剩余,生成大于 20 条数据
List
<
PlatformDevice
>
gt20DeviceList
=
deviceList
.
subList
(
lt10Count
+
from10To20Count
,
neededCount
);
for
(
PlatformDevice
device
:
gt20DeviceList
)
{
this
.
addDeviceNetworkDetailRecord
(
device
,
RandomUtil
.
randomInt
(
21
,
31
),
startDate
);
}
}
/**
* 按照条件生成设备联网数据
*
* @param device 设备数据
* @param count 生成数量
* @param currentDate 当前日期
*/
private
void
addDeviceNetworkDetailRecord
(
PlatformDevice
device
,
int
count
,
Date
currentDate
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
int
totalDayNum
=
calendar
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
);
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
// 生成随机时间,范围为 06:00:00 ~ 22:59:59
calendar
.
set
(
Calendar
.
DAY_OF_MONTH
,
RandomUtil
.
randomInt
(
1
,
totalDayNum
+
1
));
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
RandomUtil
.
randomInt
(
6
,
23
));
calendar
.
set
(
Calendar
.
MINUTE
,
RandomUtil
.
randomInt
(
0
,
60
));
calendar
.
set
(
Calendar
.
SECOND
,
RandomUtil
.
randomInt
(
0
,
60
));
DeviceNetworkDetail
deviceNetworkDetail
=
new
DeviceNetworkDetail
();
deviceNetworkDetail
.
setDeviceSn
(
device
.
getDeviceSn
());
deviceNetworkDetail
.
setCreateTime
(
calendar
.
getTime
());
try
{
String
s
=
String
.
format
(
",\n('%s', '%s')"
,
deviceNetworkDetail
.
getDeviceSn
(),
DateUtil
.
formatDateTime
(
deviceNetworkDetail
.
getCreateTime
()));
deviceNetworkDataWriter
.
write
(
s
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//this.deviceNetworkDetailMapper.insertSelective(deviceNetworkDetail);
}
}
}
//
package cn.com.glsx.openplatform;
//
//
import java.io.BufferedWriter;
//
import java.io.IOException;
//
import java.nio.file.Files;
//
import java.nio.file.Paths;
//
import java.util.Calendar;
//
import java.util.Date;
//
import java.util.List;
//
import cn.com.glsx.openplatform.mapper.DeviceActionDetailMapper;
//
import cn.com.glsx.openplatform.model.DeviceActionDetail;
//
import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
//
import cn.com.glsx.openplatform.model.PlatformDevice;
//
import cn.hutool.core.date.DateUtil;
//
import cn.hutool.core.util.RandomUtil;
//
import org.junit.Test;
//
import org.junit.runner.RunWith;
//
import org.springframework.beans.factory.annotation.Autowired;
//
import org.springframework.boot.test.context.SpringBootTest;
//
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//
/
//
**
//
* <p>Description:
//
* <p>Project: rpc-openplatform-container
//
*
//
* @author wbh
//
* @date 2021-01-02
//
*/
//
@RunWith(SpringJUnit4ClassRunner.class)
//
@SpringBootTest(classes = Main.class)
//
public class GenerateDeviceActionDetail {
//
@Autowired
//
private DeviceActionDetailMapper deviceActionDetailMapper;
//
//@Autowired
//
//private DeviceNetworkDetailMapper deviceNetworkDetailMapper;
//
//
private BufferedWriter deviceActionDataWriter;
//
private BufferedWriter deviceNetworkDataWriter;
//
//
@Test
//
public void generateData() throws IOException {
//
this.initDeviceActionDataWriter();
//
//this.initDeviceNetworkDataWriter();
//
//
Date startDate = DateUtil.parseDate("2019-01-01");
//
Date endDate = DateUtil.parseDate("2021-01-01");
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(startDate);
//
Date currentStartDate = calendar.getTime();
//
calendar.add(Calendar.MONTH, 1);
//
Date currentEndDate = calendar.getTime();
//
while (currentStartDate.before(endDate)) {
//
// 获取所有类型的设备,按照设备类型进行生成
//
List<String> typeList = this.deviceActionDetailMapper.selectDeviceType();
//
for (String type : typeList) {
//
List<PlatformDevice> deviceList = this.getNeededDeviceList(
//
currentStartDate, currentEndDate, type);
//
this.generateDeviceActionData(currentStartDate, endDate, deviceList);
//
//this.generateDeviceNetworkData(currentStartDate, deviceList);
//
}
//
currentStartDate = currentEndDate;
//
calendar.add(Calendar.MONTH, 1);
//
currentEndDate = calendar.getTime();
//
}
//
//
this.closeDeviceActionDataWriter();
//
//this.closeDeviceNetworkDataWriter();
//
}
//
//
private void initDeviceNetworkDataWriter() throws IOException {
//
this.deviceNetworkDataWriter = Files.newBufferedWriter(Paths.get(
//
"D:/Users/wbh/Desktop/device-network-data.sql"));
//
this.deviceNetworkDataWriter.write(
//
"insert into t_device_network_detail (device_sn, create_time) values");
//
}
//
//
private void closeDeviceNetworkDataWriter() throws IOException {
//
this.deviceNetworkDataWriter.flush();
//
this.deviceNetworkDataWriter.close();
//
}
//
//
private void initDeviceActionDataWriter() throws IOException {
//
this.deviceActionDataWriter = Files.newBufferedWriter(Paths.get(
//
"D:/Users/wbh/Desktop/device-action-data.sql"));
//
this.deviceActionDataWriter.write(
//
"insert into t_device_action_detail (device_sn, type, create_time) values");
//
}
//
//
private void closeDeviceActionDataWriter() throws IOException {
//
this.deviceActionDataWriter.flush();
//
this.deviceActionDataWriter.close();
//
}
//
//
private List<PlatformDevice> getNeededDeviceList(Date startDate, Date endDate, String type) {
//
int count = this.deviceActionDetailMapper.countByDate(startDate, endDate, type);
//
// 取所有数量的 30% 左右
//
int neededCount = this.getNumPercent(count, RandomUtil.randomInt(25, 35));
//
List<PlatformDevice> deviceList = this.deviceActionDetailMapper.selectByDate(
//
startDate, endDate, neededCount, type);
//
return deviceList;
//
}
//
//
private void generateDeviceActionData(Date startDate, Date endDate,
//
List<PlatformDevice> deviceList) {
//
this.addDeviceActionSoundControlData(startDate, endDate, deviceList);
//
this.addDeviceActionFmData(startDate, endDate, deviceList);
//
this.addDeviceActionNavigationData(startDate, endDate, deviceList);
//
this.addDeviceActionMusicData(startDate, endDate, deviceList);
//
this.addDeviceActionInternetData(startDate, endDate, deviceList);
//
}
//
//
private void generateDeviceNetworkData(Date startDate,
//
List<PlatformDevice> deviceList) {
//
this.addDeviceNetworkData(startDate, deviceList);
//
}
//
//
private int getNumPercent(int num, int percent) {
//
return num * percent / 100;
//
}
//
//
/**
//
* 生成声控调用数据
//
*
//
* @param startDate 开始日期
//
* @param endDate 结束日期
//
* @param deviceList 设备数据
//
*/
//
private void addDeviceActionSoundControlData(Date startDate, Date endDate,
//
List<PlatformDevice> deviceList) {
//
int neededCount = deviceList.size();
//
// 取总数的 40% 左右,每天生成两条数据
//
int twiceCount = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(35, 45));
//
List<PlatformDevice> twiceDeviceList = deviceList.subList(0, twiceCount);
//
// 取剩余量的 60% 左右,每天生成一条数据
//
int onceCount = this.getNumPercent(neededCount - twiceCount,
//
RandomUtil.randomInt(55, 65));
//
List<PlatformDevice> onceDeviceList = deviceList.subList(twiceCount,
//
twiceCount + onceCount);
//
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(startDate);
//
Date currentDate = startDate;
//
while (currentDate.before(endDate)) {
//
// 每天需要生成两条数据的
//
for (PlatformDevice device : twiceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "声控调用", 2, currentDate);
//
}
//
//
// 每天生成一条数据的
//
for (PlatformDevice device : onceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "声控调用", 1, currentDate);
//
}
//
//
calendar.add(Calendar.DAY_OF_MONTH, 1);
//
currentDate = calendar.getTime();
//
}
//
}
//
//
/**
//
* 生成声收音机使用数据
//
*
//
* @param startDate 开始日期
//
* @param endDate 结束日期
//
* @param deviceList 设备数据
//
*/
//
private void addDeviceActionFmData(Date startDate, Date endDate,
//
List<PlatformDevice> deviceList) {
//
int neededCount = deviceList.size();
//
// 取总数的 70% 左右,每天生成一条数据
//
int onceCount = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(65, 75));
//
List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount);
//
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(startDate);
//
Date currentDate = startDate;
//
while (currentDate.before(endDate)) {
//
// 每天生成一条数据的
//
for (PlatformDevice device : onceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "收音机使用", 1, currentDate);
//
}
//
//
calendar.add(Calendar.DAY_OF_MONTH, 1);
//
currentDate = calendar.getTime();
//
}
//
}
//
//
/**
//
* 生成导航调用数据
//
*
//
* @param startDate 开始日期
//
* @param endDate 结束日期
//
* @param deviceList 设备数据
//
*/
//
private void addDeviceActionNavigationData(Date startDate, Date endDate,
//
List<PlatformDevice> deviceList) {
//
int neededCount = deviceList.size();
//
// 取总数的 50% 左右,每天生成两条数据
//
int twiceCount = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(45, 55));
//
List<PlatformDevice> twiceDeviceList = deviceList.subList(0, twiceCount);
//
// 取剩余量的 50% 左右,每天生成一条数据
//
int onceCount = this.getNumPercent(neededCount - twiceCount,
//
RandomUtil.randomInt(45, 55));
//
List<PlatformDevice> onceDeviceList = deviceList.subList(twiceCount,
//
twiceCount + onceCount);
//
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(startDate);
//
Date currentDate = startDate;
//
while (currentDate.before(endDate)) {
//
// 每天需要生成两条数据的
//
for (PlatformDevice device : twiceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "导航调用", 2, currentDate);
//
}
//
//
// 每天生成一条数据的
//
for (PlatformDevice device : onceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "导航调用", 1, currentDate);
//
}
//
//
calendar.add(Calendar.DAY_OF_MONTH, 1);
//
currentDate = calendar.getTime();
//
}
//
}
//
//
/**
//
* 生成音乐使用数据
//
*
//
* @param startDate 开始日期
//
* @param endDate 结束日期
//
* @param deviceList 设备数据
//
*/
//
private void addDeviceActionMusicData(Date startDate, Date endDate,
//
List<PlatformDevice> deviceList) {
//
int neededCount = deviceList.size();
//
// 取总数的 70% 左右,每天生成一条数据
//
int onceCount = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(65, 75));
//
List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount);
//
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(startDate);
//
Date currentDate = startDate;
//
while (currentDate.before(endDate)) {
//
// 每天生成一条数据的
//
for (PlatformDevice device : onceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "音乐使用", 1, currentDate);
//
}
//
//
calendar.add(Calendar.DAY_OF_MONTH, 1);
//
currentDate = calendar.getTime();
//
}
//
}
//
//
/**
//
* 生成联网应用数据
//
*
//
* @param startDate 开始日期
//
* @param endDate 结束日期
//
* @param deviceList 设备数据
//
*/
//
private void addDeviceActionInternetData(Date startDate, Date endDate,
//
List<PlatformDevice> deviceList) {
//
int neededCount = deviceList.size();
//
// 取总数的 40% 左右,每天生成一条数据
//
int onceCount = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(35, 45));
//
List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount);
//
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(startDate);
//
Date currentDate = startDate;
//
while (currentDate.before(endDate)) {
//
// 每天生成一条数据的
//
for (PlatformDevice device : onceDeviceList) {
//
this.addDeviceActionDetailRecord(device, "联网应用使用", 1, currentDate);
//
}
//
//
calendar.add(Calendar.DAY_OF_MONTH, 1);
//
currentDate = calendar.getTime();
//
}
//
}
//
//
/**
//
* 按照条件生成设备行为数据
//
*
//
* @param device 设备数据
//
* @param type 设备行为类型
//
* @param count 生成数量
//
* @param currentDate 当前日期
//
*/
//
private void addDeviceActionDetailRecord(PlatformDevice device, String type, int count,
//
Date currentDate) {
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(currentDate);
//
for (int i = 0; i < count; i++) {
//
// 生成每日随机时间,范围为 06:00:00 ~ 22:59:59
//
calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 23));
//
calendar.set(Calendar.MINUTE, RandomUtil.randomInt(0, 60));
//
calendar.set(Calendar.SECOND, RandomUtil.randomInt(0, 60));
//
DeviceActionDetail deviceActionDetail = new DeviceActionDetail();
//
deviceActionDetail.setDeviceSn(device.getDeviceSn());
//
deviceActionDetail.setType(type);
//
deviceActionDetail.setCreateTime(calendar.getTime());
//
try {
//
String s = String.format(",\n('%s', '%s', '%s')", deviceActionDetail.getDeviceSn(),
//
deviceActionDetail.getType(),
//
DateUtil.formatDateTime(deviceActionDetail.getCreateTime()));
//
this.deviceActionDataWriter.write(s);
//
} catch (IOException e) {
//
e.printStackTrace();
//
}
//
//this.deviceActionDetailMapper.insertSelective(deviceActionDetail);
//
}
//
}
//
//
private void addDeviceNetworkData(Date startDate, List<PlatformDevice> deviceList) {
//
int neededCount = deviceList.size();
//
// 取总量的 20% 左右,生成小于 10 条数据
//
int lt10Count = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(18, 25));
//
List<PlatformDevice> lt20DeviceList = deviceList.subList(0, lt10Count);
//
for (PlatformDevice device : lt20DeviceList) {
//
this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(5, 10), startDate);
//
}
//
//
// 取总量的 60% 左右,生成 20 条数据
//
int from10To20Count = this.getNumPercent(neededCount,
//
RandomUtil.randomInt(55, 65));
//
List<PlatformDevice> from10To20DeviceList = deviceList.subList(lt10Count,
//
lt10Count + from10To20Count);
//
for (PlatformDevice device : from10To20DeviceList) {
//
this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(12, 21), startDate);
//
}
//
//
// 取总量的剩余,生成大于 20 条数据
//
List<PlatformDevice> gt20DeviceList = deviceList.subList(lt10Count + from10To20Count,
//
neededCount);
//
for (PlatformDevice device : gt20DeviceList) {
//
this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(21, 31), startDate);
//
}
//
}
//
//
/**
//
* 按照条件生成设备联网数据
//
*
//
* @param device 设备数据
//
* @param count 生成数量
//
* @param currentDate 当前日期
//
*/
//
private void addDeviceNetworkDetailRecord(PlatformDevice device, int count, Date currentDate) {
//
Calendar calendar = Calendar.getInstance();
//
calendar.setTime(currentDate);
//
int totalDayNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//
for (int i = 0; i < count; i++) {
//
// 生成随机时间,范围为 06:00:00 ~ 22:59:59
//
calendar.set(Calendar.DAY_OF_MONTH, RandomUtil.randomInt(1, totalDayNum + 1));
//
calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 23));
//
calendar.set(Calendar.MINUTE, RandomUtil.randomInt(0, 60));
//
calendar.set(Calendar.SECOND, RandomUtil.randomInt(0, 60));
//
DeviceNetworkDetail deviceNetworkDetail = new DeviceNetworkDetail();
//
deviceNetworkDetail.setDeviceSn(device.getDeviceSn());
//
deviceNetworkDetail.setCreateTime(calendar.getTime());
//
try {
//
String s = String.format(",\n('%s', '%s')", deviceNetworkDetail.getDeviceSn(),
//
DateUtil.formatDateTime(deviceNetworkDetail.getCreateTime()));
//
deviceNetworkDataWriter.write(s);
//
} catch (IOException e) {
//
e.printStackTrace();
//
}
//
//this.deviceNetworkDetailMapper.insertSelective(deviceNetworkDetail);
//
}
//
}
//
}
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceActionDetailByCustomer.java
View file @
cb79fc1
...
...
@@ -3,11 +3,12 @@ package cn.com.glsx.openplatform;
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.
Calendar
;
import
java.util.
ArrayList
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper
;
import
cn.com.glsx.openplatform.model.CustomerActionStatistics
;
import
cn.com.glsx.openplatform.model.DeviceActionDetail
;
...
...
@@ -15,6 +16,7 @@ import cn.com.glsx.openplatform.model.PlatformDevice;
import
cn.hutool.core.date.DateField
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.lang.WeightRandom
;
import
cn.hutool.core.util.RandomUtil
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -37,29 +39,46 @@ public class GenerateDeviceActionDetailByCustomer {
private
BufferedWriter
deviceActionDetailWriter
;
private
static
final
Map
<
String
,
Integer
[]>
RAND_RANGE_MAP
=
new
HashMap
<>();
private
static
final
WeightRandom
<
Integer
>
HOUR_RANDOM
=
new
WeightRandom
<>();
private
static
final
Map
<
String
,
Integer
>
PERCENTAGE_MAP
=
new
HashMap
<>();
static
{
RAND_RANGE_MAP
.
put
(
"声控调用"
,
new
Integer
[]{
6
,
9
});
RAND_RANGE_MAP
.
put
(
"收音机使用"
,
new
Integer
[]{
28
,
32
});
RAND_RANGE_MAP
.
put
(
"导航调用"
,
new
Integer
[]{
3
,
6
});
RAND_RANGE_MAP
.
put
(
"音乐使用"
,
new
Integer
[]{
14
,
17
});
RAND_RANGE_MAP
.
put
(
"联网应用使用"
,
new
Integer
[]{
2
,
5
});
private
static
final
List
<
Integer
>
HOUR_LIST
=
new
ArrayList
<>();
static
{
PERCENTAGE_MAP
.
put
(
"声控调用"
,
40
);
PERCENTAGE_MAP
.
put
(
"收音机使用"
,
70
);
PERCENTAGE_MAP
.
put
(
"导航调用"
,
50
);
PERCENTAGE_MAP
.
put
(
"音乐使用"
,
70
);
PERCENTAGE_MAP
.
put
(
"联网应用使用"
,
40
);
int
[]
hourCounts
=
{
56
,
56
,
12
,
5
,
5
,
7
,
11
,
36
,
68
,
60
,
54
,
55
,
56
,
52
,
55
,
53
,
58
,
70
,
71
,
53
,
41
,
31
,
22
,
13
};
for
(
int
hour
=
0
;
hour
<
24
;
hour
++)
{
for
(
int
i
=
0
;
i
<
hourCounts
[
hour
];
i
++)
{
HOUR_LIST
.
add
(
hour
);
}
}
HOUR_RANDOM
.
add
(
0
,
0.0562
).
add
(
1
,
0.0556
)
.
add
(
2
,
0.0124
).
add
(
3
,
0.0049
)
.
add
(
4
,
0.0048
).
add
(
5
,
0.0070
)
.
add
(
6
,
0.0107
).
add
(
7
,
0.0361
)
.
add
(
8
,
0.0675
).
add
(
9
,
0.0602
)
.
add
(
10
,
0.0543
).
add
(
11
,
0.0551
)
.
add
(
12
,
0.0564
).
add
(
13
,
0.0516
)
.
add
(
14
,
0.0549
).
add
(
15
,
0.0533
)
.
add
(
16
,
0.0579
).
add
(
17
,
0.0703
)
.
add
(
18
,
0.0705
).
add
(
19
,
0.0537
)
.
add
(
20
,
0.0407
).
add
(
21
,
0.0314
)
.
add
(
22
,
0.0215
).
add
(
23
,
0.0131
);
}
/**
* 根据客户设备行为统计表 t_customer_action_statistics 生成设备行为明细表 t_device_action_detail 数据
*/
@Test
public
void
generateDeviceActionDetail
()
{
public
void
generateDeviceActionDetail
ByCustomer
()
{
List
<
String
>
monthList
=
this
.
customerActionStatisticsMapper
.
selectMonthList
();
for
(
String
month
:
monthList
)
{
this
.
initDeviceActionDetailWriter
(
month
);
...
...
@@ -68,42 +87,44 @@ public class GenerateDeviceActionDetailByCustomer {
for
(
CustomerActionStatistics
actionStatistics
:
actionStatisticsList
)
{
String
type
=
actionStatistics
.
getType
();
Integer
number
=
actionStatistics
.
getNumber
();
Integer
onlineNumber
=
actionStatistics
.
getOnlineNumber
();
Date
startDate
=
DateUtil
.
parse
(
actionStatistics
.
getStatisticsTime
(),
"yyyy-MM"
);
Date
endDate
=
DateUtil
.
offsetMonth
(
startDate
,
1
);
List
<
PlatformDevice
>
deviceList
=
this
.
customerActionStatisticsMapper
.
selectPlatformDevice
(
actionStatistics
,
endDate
);
if
(!
deviceList
.
isEmpty
())
{
int
percentage
=
PERCENTAGE_MAP
.
get
(
type
);
// 取出对应比例的设备生成数据
int
subCount
=
onlineNumber
*
percentage
/
100
;
List
<
PlatformDevice
>
subDeviceList
=
deviceList
.
subList
(
0
,
subCount
);
this
.
addDeviceActionDetail
(
subDeviceList
.
isEmpty
()
?
deviceList
:
subDeviceList
,
type
,
startDate
,
number
);
}
int
percentage
=
PERCENTAGE_MAP
.
get
(
type
);
// 取出对应比例的设备生成数据
int
subCount
=
Math
.
max
(
deviceList
.
size
()
*
percentage
/
100
,
deviceList
.
size
());
List
<
PlatformDevice
>
subDeviceList
=
deviceList
.
subList
(
0
,
subCount
);
this
.
addDeviceActionDetail
(
subDeviceList
,
type
,
startDate
,
number
);
}
this
.
closeDeviceActionDetailWriter
();
}
}
private
void
addDeviceActionDetail
(
List
<
PlatformDevice
>
deviceList
,
String
type
,
Date
startDate
,
int
count
)
{
private
void
addDeviceActionDetail
(
List
<
PlatformDevice
>
deviceList
,
String
type
,
Date
startDate
,
int
count
)
{
int
dayCountOfMonth
=
DateUtil
.
endOfMonth
(
startDate
).
dayOfMonth
();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
PlatformDevice
device
=
deviceList
.
get
(
i
%
deviceList
.
size
());
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
int
day
=
RandomUtil
.
randomInt
(
DateUtil
.
dayOfMonth
(
startDate
),
calendar
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
int
hour
=
RandomUtil
.
randomInt
(
6
,
24
);
int
minute
=
RandomUtil
.
randomInt
(
60
);
int
second
=
RandomUtil
.
randomInt
(
60
);
Date
createTime
=
DateUtil
.
dateNew
(
startDate
)
.
setField
(
DateField
.
DAY_OF_MONTH
,
day
)
.
setField
(
DateField
.
HOUR_OF_DAY
,
hour
)
.
setField
(
DateField
.
MINUTE
,
minute
)
.
setField
(
DateField
.
SECOND
,
second
);
Date
activatTime
=
device
.
getActivatTime
();
Date
createTime
;
if
(
DateUtil
.
isSameMonth
(
startDate
,
activatTime
))
{
long
startMs
=
activatTime
.
getTime
();
long
endMs
=
DateUtil
.
dateNew
(
startDate
)
.
offset
(
DateField
.
MONTH
,
1
).
getTime
();
createTime
=
new
Date
(
RandomUtil
.
randomLong
(
startMs
,
endMs
));
}
else
{
int
day
=
RandomUtil
.
randomInt
(
DateUtil
.
dayOfMonth
(
startDate
),
dayCountOfMonth
);
int
hour
=
HOUR_RANDOM
.
next
();
int
minute
=
RandomUtil
.
randomInt
(
60
);
int
second
=
RandomUtil
.
randomInt
(
60
);
createTime
=
DateUtil
.
dateNew
(
startDate
)
.
setField
(
DateField
.
DAY_OF_MONTH
,
day
)
.
setField
(
DateField
.
HOUR_OF_DAY
,
hour
)
.
setField
(
DateField
.
MINUTE
,
minute
)
.
setField
(
DateField
.
SECOND
,
second
);
}
DeviceActionDetail
actionDetail
=
new
DeviceActionDetail
();
actionDetail
.
setDeviceSn
(
device
.
getDeviceSn
());
actionDetail
.
setType
(
type
);
...
...
@@ -142,4 +163,19 @@ public class GenerateDeviceActionDetailByCustomer {
e
.
printStackTrace
();
}
}
Map
<
String
,
Integer
>
convertToTypeCountMap
(
List
<
Map
<
String
,
Integer
>>
list
)
{
Map
<
String
,
Integer
>
typeCountMap
=
new
HashMap
<>();
for
(
Map
<
String
,
Integer
>
map
:
list
)
{
typeCountMap
.
put
(
String
.
valueOf
(
map
.
get
(
"type"
)),
map
.
get
(
"number"
));
}
return
typeCountMap
;
}
private
int
getRandomHour
(
int
minHour
,
int
maxHour
)
{
List
<
Integer
>
filteredHourList
=
HOUR_LIST
.
stream
()
.
filter
(
hour
->
hour
>=
minHour
&&
hour
<
maxHour
)
.
collect
(
Collectors
.
toList
());
return
filteredHourList
.
get
(
RandomUtil
.
randomInt
(
0
,
filteredHourList
.
size
()));
}
}
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceActionDetailByCustomer2.java
deleted
100644 → 0
View file @
2ddd2a2
package
cn
.
com
.
glsx
.
openplatform
;
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper
;
import
cn.com.glsx.openplatform.model.CustomerActionStatistics
;
import
cn.com.glsx.openplatform.model.DeviceActionDetail
;
import
cn.com.glsx.openplatform.model.PlatformDevice
;
import
cn.hutool.core.date.DateTime
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.util.RandomUtil
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-03
*/
@Deprecated
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
Main
.
class
)
public
class
GenerateDeviceActionDetailByCustomer2
{
@Autowired
private
CustomerActionStatisticsMapper
customerActionStatisticsMapper
;
private
BufferedWriter
deviceActionDetailWriter
;
private
static
final
Map
<
String
,
Integer
[]>
RAND_RANGE_MAP
=
new
HashMap
<>();
static
{
RAND_RANGE_MAP
.
put
(
"声控调用"
,
new
Integer
[]{
6
,
9
});
RAND_RANGE_MAP
.
put
(
"收音机使用"
,
new
Integer
[]{
28
,
32
});
RAND_RANGE_MAP
.
put
(
"导航调用"
,
new
Integer
[]{
3
,
6
});
RAND_RANGE_MAP
.
put
(
"音乐使用"
,
new
Integer
[]{
14
,
17
});
RAND_RANGE_MAP
.
put
(
"联网应用使用"
,
new
Integer
[]{
2
,
5
});
}
//@Test
//public void generateDeviceActionDetail() {
// this.initDeviceActionDetailWriter(null);
//
// List<CustomerActionStatistics> actionStatisticsList =
// this.customerActionStatisticsMapper.selectByType(null);
// for (CustomerActionStatistics actionStatistics : actionStatisticsList) {
// this.batchAddDeviceActionDetail(actionStatistics, null);
// }
//
// this.closeDeviceActionDetailWriter();
//}
//
//public void generateDeviceActionDetail2() {
// Date startDate = DateUtil.parse("2018-12", "yyyy-MM");
// Date endDate = DateUtil.parse("2021-01", "yyyy-MM");
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(startDate);
// Date currentStartDate = startDate;
// while (currentStartDate.before(endDate)) {
// this.initDeviceActionDetailWriter(startDate);
// calendar.add(Calendar.MONTH, 1);
// Date currentEndDate = calendar.getTime();
// List<CustomerActionStatistics> actionStatisticsList =
// this.customerActionStatisticsMapper.selectByDate(
// currentStartDate, currentEndDate);
// for (CustomerActionStatistics actionStatistics : actionStatisticsList) {
// this.batchAddDeviceActionDetail(actionStatistics, currentStartDate);
// }
// currentStartDate = currentEndDate;
// this.closeDeviceActionDetailWriter();
// }
//}
@Test
public
void
generateDeviceActionDetail
()
{
List
<
String
>
monthList
=
this
.
customerActionStatisticsMapper
.
selectMonthList
();
for
(
String
month
:
monthList
)
{
this
.
initDeviceActionDetailWriter
(
month
);
List
<
CustomerActionStatistics
>
actionStatisticsList
=
this
.
customerActionStatisticsMapper
.
selectByMonth
(
month
);
for
(
CustomerActionStatistics
actionStatistics
:
actionStatisticsList
)
{
Integer
deviceCount
=
this
.
customerActionStatisticsMapper
.
selectOnlineNumber
(
actionStatistics
);
this
.
batchAddDeviceActionDetail
(
actionStatistics
,
deviceCount
);
}
this
.
closeDeviceActionDetailWriter
();
}
}
private
void
batchAddDeviceActionDetail
(
CustomerActionStatistics
actionStatistics
,
int
deviceCount
)
{
Date
statisticsDate
=
DateUtil
.
parse
(
actionStatistics
.
getStatisticsTime
(),
"yyyy-MM"
);
DateTime
endDate
=
DateUtil
.
offsetMonth
(
statisticsDate
,
1
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
statisticsDate
);
// 设备行为类型
String
type
=
actionStatistics
.
getType
();
// 该月需要生成数据的数量
Integer
neededNumber
=
actionStatistics
.
getNumber
();
List
<
PlatformDevice
>
deviceList
=
this
.
customerActionStatisticsMapper
.
selectDeviceList
(
actionStatistics
,
endDate
,
deviceCount
);
if
(!
deviceList
.
isEmpty
())
{
int
index
=
0
;
while
(
neededNumber
>
0
)
{
// 循环获取设备
PlatformDevice
device
=
deviceList
.
get
(
index
%
deviceList
.
size
());
index
++;
// 获取每种行为需要生成的随机数量
Integer
[]
randRange
=
RAND_RANGE_MAP
.
get
(
type
);
int
num
=
RandomUtil
.
randomInt
(
randRange
[
0
],
randRange
[
1
]);
int
generatedNumber
=
num
>
neededNumber
?
neededNumber
:
num
;
while
(
generatedNumber
>
0
)
{
this
.
addDeviceActionDetail
(
device
,
type
,
statisticsDate
);
// 减去已经生成的数据数量
generatedNumber
--;
neededNumber
--;
}
}
}
//for (PlatformDevice device : deviceList) {
// // 获取每种行为需要生成的随机数量
// Integer[] randRange = RAND_RANGE_MAP.get(type);
// int num = RandomUtil.randomInt(randRange[0], randRange[1]);
// int generatedNumber = num > neededNumber ? neededNumber : num;
// while (generatedNumber > 0) {
// this.addDeviceActionDetail(device, type, statisticsDate);
// // 减去已经生成的数据数量
// generatedNumber--;
// neededNumber--;
// }
// if (neededNumber <= 0) {
// break;
// }
//}
//
//// 如果还没达到需要生成的数据量,则继续生成
//while (neededNumber > 0) {
// // 随机取一个设备生成行为数据
// PlatformDevice device = deviceList.get(RandomUtil.randomInt(deviceList.size()));
// this.addDeviceActionDetail(device, type, statisticsDate);
// neededNumber--;
//}
}
private
void
addDeviceActionDetail
(
PlatformDevice
device
,
String
type
,
Date
startDate
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
int
maxDays
=
calendar
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
);
calendar
.
set
(
Calendar
.
DAY_OF_MONTH
,
RandomUtil
.
randomInt
(
maxDays
)
+
1
);
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
RandomUtil
.
randomInt
(
6
,
24
));
calendar
.
set
(
Calendar
.
MINUTE
,
RandomUtil
.
randomInt
(
60
));
calendar
.
set
(
Calendar
.
SECOND
,
RandomUtil
.
randomInt
(
60
));
DeviceActionDetail
actionDetail
=
new
DeviceActionDetail
();
actionDetail
.
setDeviceSn
(
device
.
getDeviceSn
());
actionDetail
.
setCreateTime
(
calendar
.
getTime
());
actionDetail
.
setType
(
type
);
try
{
String
s
=
String
.
format
(
",\n('%s', '%s', '%s')"
,
actionDetail
.
getDeviceSn
(),
actionDetail
.
getType
(),
DateUtil
.
formatDateTime
(
actionDetail
.
getCreateTime
()));
this
.
deviceActionDetailWriter
.
write
(
s
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
//private void addDeviceActionDetail2(CustomerActionStatistics actionStatistics) {
// Date statisticsDate = DateUtil.parse(actionStatistics.getStatisticsTime(),
// "yyyy-MM");
// DateTime endDate = DateUtil.offsetMonth(statisticsDate, 1);
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(statisticsDate);
// int maxDays = calendar.get(Calendar.DAY_OF_MONTH);
// String type = actionStatistics.getType();
// int count = actionStatistics.getNumber() / DIVISION_MAP.get(type);
// // 查询在统计月底之前出货的设备
// List<PlatformDevice> deviceList = this.customerActionStatisticsMapper
// .selectDeviceList(actionStatistics, endDate, count);
// for (PlatformDevice device : deviceList) {
// calendar.set(Calendar.DAY_OF_MONTH, RandomUtil.randomInt(maxDays) + 1);
// calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 24));
// calendar.set(Calendar.MINUTE, RandomUtil.randomInt(60));
// calendar.set(Calendar.SECOND, RandomUtil.randomInt(60));
// DeviceActionDetail actionDetail = new DeviceActionDetail();
// actionDetail.setDeviceSn(device.getDeviceSn());
// actionDetail.setCreateTime(calendar.getTime());
// actionDetail.setType(type);
// try {
// String s = String.format(",\n('%s', '%s', '%s')",
// actionDetail.getDeviceSn(), actionDetail.getType(),
// DateUtil.formatDateTime(actionDetail.getCreateTime()));
// this.deviceActionDetailWriter.write(s);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//}
private
void
initDeviceActionDetailWriter
(
String
month
)
{
try
{
String
monthStr
=
month
.
replace
(
"-"
,
""
);
String
filename
=
String
.
format
(
"D:/data/device-action-data-%s.sql"
,
monthStr
);
this
.
deviceActionDetailWriter
=
FileUtil
.
getWriter
(
filename
,
StandardCharsets
.
UTF_8
,
false
);
//String headLine = String.format(
// "insert into t_device_action_detail_%s (device_sn, type, create_time) values",
// monthStr);
String
headLine
=
"insert into t_device_action_detail (device_sn, type, create_time) values"
;
this
.
deviceActionDetailWriter
.
write
(
headLine
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
closeDeviceActionDetailWriter
()
{
try
{
this
.
deviceActionDetailWriter
.
flush
();
this
.
deviceActionDetailWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GenerateDeviceNetworkDetailByCustomer.java
View file @
cb79fc1
...
...
@@ -3,9 +3,10 @@ package cn.com.glsx.openplatform;
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.
Calendar
;
import
java.util.
ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper
;
import
cn.com.glsx.openplatform.model.CustomerNetworkStatistics
;
import
cn.com.glsx.openplatform.model.DeviceNetworkDetail
;
...
...
@@ -13,6 +14,7 @@ import cn.com.glsx.openplatform.model.PlatformDevice;
import
cn.hutool.core.date.DateField
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.lang.WeightRandom
;
import
cn.hutool.core.util.RandomUtil
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -35,6 +37,33 @@ public class GenerateDeviceNetworkDetailByCustomer {
private
BufferedWriter
deviceNetworkDetailWriter
;
private
static
final
WeightRandom
<
Integer
>
HOUR_RANDOM
=
new
WeightRandom
<>();
private
static
final
List
<
Integer
>
HOUR_LIST
=
new
ArrayList
<>();
static
{
int
[]
hourCounts
=
{
56
,
56
,
12
,
5
,
5
,
7
,
11
,
36
,
68
,
60
,
54
,
55
,
56
,
52
,
55
,
53
,
58
,
70
,
71
,
53
,
41
,
31
,
22
,
13
};
for
(
int
hour
=
0
;
hour
<
24
;
hour
++)
{
for
(
int
i
=
0
;
i
<
hourCounts
[
hour
];
i
++)
{
HOUR_LIST
.
add
(
hour
);
}
}
HOUR_RANDOM
.
add
(
0
,
0.0562
).
add
(
1
,
0.0556
)
.
add
(
2
,
0.0124
).
add
(
3
,
0.0049
)
.
add
(
4
,
0.0048
).
add
(
5
,
0.0070
)
.
add
(
6
,
0.0107
).
add
(
7
,
0.0361
)
.
add
(
8
,
0.0675
).
add
(
9
,
0.0602
)
.
add
(
10
,
0.0543
).
add
(
11
,
0.0551
)
.
add
(
12
,
0.0564
).
add
(
13
,
0.0516
)
.
add
(
14
,
0.0549
).
add
(
15
,
0.0533
)
.
add
(
16
,
0.0579
).
add
(
17
,
0.0703
)
.
add
(
18
,
0.0705
).
add
(
19
,
0.0537
)
.
add
(
20
,
0.0407
).
add
(
21
,
0.0314
)
.
add
(
22
,
0.0215
).
add
(
23
,
0.0131
);
}
/**
* 根据客户联网统计表 t_customer_network_statistics 生成设备联网明细表 t_device_network_detail 数据
*/
...
...
@@ -86,23 +115,25 @@ public class GenerateDeviceNetworkDetailByCustomer {
}
private
void
addDeviceNetworkDetail
(
List
<
PlatformDevice
>
deviceList
,
Date
startDate
,
int
count
)
{
int
dayCountOfMonth
=
DateUtil
.
endOfMonth
(
startDate
).
dayOfMonth
();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
PlatformDevice
device
=
deviceList
.
get
(
i
%
deviceList
.
size
());
Date
activatTime
=
device
.
getActivatTime
();
Date
createTime
;
int
day
;
int
hour
;
int
minute
;
int
second
;
if
(
DateUtil
.
isSameMonth
(
startDate
,
activatTime
))
{
long
startMs
=
activatTime
.
getTime
();
long
endMs
=
DateUtil
.
dateNew
(
startDate
)
.
offset
(
DateField
.
MONTH
,
1
).
getTime
();
createTime
=
new
Date
(
RandomUtil
.
randomLong
(
startMs
,
endMs
));
}
else
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
startDate
);
int
day
=
RandomUtil
.
randomInt
(
DateUtil
.
dayOfMonth
(
startDate
),
calendar
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
int
hour
=
RandomUtil
.
randomInt
(
24
);
int
minute
=
RandomUtil
.
randomInt
(
60
);
int
second
=
RandomUtil
.
randomInt
(
60
);
day
=
RandomUtil
.
randomInt
(
DateUtil
.
dayOfMonth
(
startDate
),
dayCountOfMonth
);
hour
=
HOUR_RANDOM
.
next
();
minute
=
RandomUtil
.
randomInt
(
60
);
second
=
RandomUtil
.
randomInt
(
60
);
createTime
=
DateUtil
.
dateNew
(
startDate
).
setField
(
DateField
.
DAY_OF_MONTH
,
day
)
.
setField
(
DateField
.
HOUR_OF_DAY
,
hour
)
.
setField
(
DateField
.
MINUTE
,
minute
)
...
...
@@ -121,4 +152,11 @@ public class GenerateDeviceNetworkDetailByCustomer {
}
}
}
private
int
getRandomHour
(
int
minHour
,
int
maxHour
)
{
List
<
Integer
>
filteredHourList
=
HOUR_LIST
.
stream
()
.
filter
(
hour
->
hour
>=
minHour
&&
hour
<
maxHour
)
.
collect
(
Collectors
.
toList
());
return
filteredHourList
.
get
(
RandomUtil
.
randomInt
(
0
,
filteredHourList
.
size
()));
}
}
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/GeneratePlatformDevice.java
View file @
cb79fc1
...
...
@@ -53,7 +53,7 @@ public class GeneratePlatformDevice {
TYPE_MAP
.
put
((
byte
)
3
,
"PCBA"
);
DEVICE_TYPE_MAP
.
put
(
"通信模组"
,
10
);
DEVICE_TYPE_MAP
.
put
(
"核心板"
,
1
0
);
DEVICE_TYPE_MAP
.
put
(
"核心板"
,
2
0
);
DEVICE_TYPE_MAP
.
put
(
"PCBA-4G"
,
31
);
DEVICE_TYPE_MAP
.
put
(
"PCBA-360"
,
32
);
DEVICE_TYPE_MAP
.
put
(
"PCBA-AR"
,
33
);
...
...
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/generate-device-action-detail-scheduler.sql
0 → 100644
View file @
cb79fc1
drop
procedure
if
exists
generateDeviceActionDetail
;
delimiter
//
create
procedure
generateDeviceActionDetail
()
begin
-- 当前时间
declare
_current_time
datetime
default
now
();
-- 数据表列
declare
_id
int
(
11
);
declare
_device_sn
varchar
(
32
);
declare
_type
varchar
(
32
);
declare
_create_time
datetime
;
declare
_finished
boolean
default
false
;
declare
_row
cursor
for
select
id
,
device_sn
,
type
,
create_time
from
t_device_action_detail
where
create_time
<
_current_time
order
by
create_time
;
declare
continue
handler
for
not
found
set
_finished
=
true
;
-- 开始遍历结果集
open
_row
;
fetch
_row
into
_id
,
_device_sn
,
_type
,
_create_time
;
while
not
_finished
do
-- 插入查出的设备联网数据
insert
into
t_device_action_detail_202101
(
id
,
device_sn
,
type
,
create_time
)
values
(
_id
,
_device_sn
,
_type
,
_create_time
);
-- 删除已插入的数据
delete
from
t_device_action_detail
where
id
=
_id
;
fetch
_row
into
_id
,
_device_sn
,
_type
,
_create_time
;
end
while
;
close
_row
;
-- -- 更新客户联网统计表
-- -- 先把表中当月的统计数据删除
-- delete from t_customer_action_statistics
-- where statistics_time = date_format(_current_time, '%Y-%m');
-- -- 再插入当月的统计数据
-- insert into t_customer_action_statistics (customer_id, customer_name, device_type, type,
-- statistics_time, number)
-- select pd.customer_id, pc.customer_name, pd.device_type, dad.type,
-- date_format(dad.create_time, '%Y-%m') statistics_time,
-- count(dad.device_sn) number
-- from t_device_action_detail_202101 dad
-- left join t_platform_device pd on pd.device_sn = dad.device_sn
-- left join t_platform_customer pc on pc.id = pd.customer_id
-- group by pc.id, pd.device_type, dad.type, statistics_time;
end
//
delimiter
;
-- 打开任务调度器
set
global
event_scheduler
=
on
;
drop
event
if
exists
deviceActionScheduler
;
create
event
deviceActionScheduler
on
schedule
every
10
second
on
completion
preserve
comment
'定时更新 t_device_action_detail 表和 t_customer_action_statistics 表'
do
begin
call
generateDeviceActionDetail
();
end
;
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/generate-device-network-detail-scheduler.sql
View file @
cb79fc1
drop
procedure
if
exists
generateDeviceNetworkDetail
;
delimiter
//
create
procedure
generateDeviceNetworkDetail
()
begin
-- 当前时间
declare
_current_time
datetime
default
now
();
-- 数据表列
declare
_id
int
(
11
);
declare
_device_sn
varchar
(
32
);
declare
_create_time
datetime
;
declare
_finished
boolean
default
false
;
declare
_row
cursor
for
select
id
,
device_sn
,
create_time
from
t_device_network_detail
where
create_time
<
_current_time
order
by
create_time
;
declare
continue
handler
for
not
found
set
_finished
=
true
;
-- 开始遍历结果集
open
_row
;
fetch
_row
into
_id
,
_device_sn
,
_create_time
;
while
not
_finished
do
-- 插入查出的设备联网数据
insert
into
t_device_network_detail_202101
(
id
,
device_sn
,
create_time
)
values
(
_id
,
_device_sn
,
_create_time
);
-- 删除已插入的数据
delete
from
t_device_network_detail
where
id
=
_id
;
fetch
_row
into
_id
,
_device_sn
,
_create_time
;
end
while
;
close
_row
;
-- -- 更新客户联网统计表
-- -- 先把表中当月的统计数据删除
-- delete from t_customer_network_statistics
-- where statistics_time = date_format(_current_time, '%Y-%m');
-- -- 再插入当月的统计数据
-- insert into t_customer_network_statistics (customer_id, customer_name, device_type, type,
-- statistics_time, online_number)
-- select pd.customer_id, pc.customer_name, pd.device_type, '月',
-- date_format(dnd.create_time, '%Y-%m') statistics_time,
-- count(distinct dnd.device_sn) online_number
-- from t_device_network_detail_202101 dnd
-- left join t_platform_device pd on pd.device_sn = dnd.device_sn
-- left join t_platform_customer pc on pc.id = pd.customer_id
-- group by pc.id, pd.device_type, statistics_time;
end
//
delimiter
;
-- 打开任务调度器
set
global
event_scheduler
=
on
;
drop
event
if
exists
deviceNetworkScheduler
;
create
event
deviceNetworkScheduler
on
schedule
every
10
second
on
completion
preserve
comment
'定时更新 t_device_network_detail 表和 t_customer_network_statistics 表'
do
begin
call
generateDeviceNetworkDetail
();
end
;
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