Commit cb79fc1e by tyfanchz

修改数据生成类

1 parent 2ddd2a21
Showing 19 changed files with 986 additions and 822 deletions
...@@ -4,6 +4,7 @@ import org.oreframework.boot.main.AbsOreBootStarter; ...@@ -4,6 +4,7 @@ import org.oreframework.boot.main.AbsOreBootStarter;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* @author deployer name * @author deployer name
...@@ -15,6 +16,7 @@ import org.springframework.boot.web.servlet.ServletComponentScan; ...@@ -15,6 +16,7 @@ import org.springframework.boot.web.servlet.ServletComponentScan;
*/ */
@SpringBootApplication @SpringBootApplication
@ServletComponentScan @ServletComponentScan
@EnableScheduling
public class Main extends AbsOreBootStarter { public class Main extends AbsOreBootStarter {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -2,6 +2,7 @@ package cn.com.glsx.openplatform.mapper; ...@@ -2,6 +2,7 @@ package cn.com.glsx.openplatform.mapper;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import cn.com.glsx.openplatform.model.CustomerActionStatistics; import cn.com.glsx.openplatform.model.CustomerActionStatistics;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail; import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
import cn.com.glsx.openplatform.model.PlatformDevice; import cn.com.glsx.openplatform.model.PlatformDevice;
...@@ -20,6 +21,13 @@ import org.apache.ibatis.annotations.Param; ...@@ -20,6 +21,13 @@ import org.apache.ibatis.annotations.Param;
public interface CustomerActionStatisticsMapper { public interface CustomerActionStatisticsMapper {
List<String> selectMonthList(); 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( List<PlatformDevice> selectPlatformDevice(
@Param("statistics") CustomerActionStatistics statistics, @Param("statistics") CustomerActionStatistics statistics,
@Param("endDate") Date endDate); @Param("endDate") Date endDate);
......
...@@ -25,14 +25,14 @@ public interface CustomerNetworkStatisticsMapper { ...@@ -25,14 +25,14 @@ public interface CustomerNetworkStatisticsMapper {
@Param("statistics") CustomerNetworkStatistics statistics, @Param("statistics") CustomerNetworkStatistics statistics,
@Param("endDate") Date endDate); @Param("endDate") Date endDate);
List<CustomerNetworkStatistics> selectByDate(@Param("startDate") String startDate, //List<CustomerNetworkStatistics> selectByDate(@Param("startDate") String startDate,
@Param("endDate") String endDate); // @Param("endDate") String endDate);
//List<CustomerNetworkStatistics> selectFromDeviceNetworkByDate( //List<CustomerNetworkStatistics> selectFromDeviceNetworkByDate(
// @Param("startDate") Date startDate, // @Param("startDate") Date startDate,
// @Param("endDate") Date endDate); // @Param("endDate") Date endDate);
List<CustomerNetworkStatistics> selectFromCustomerDeviceStatistics(); //List<CustomerNetworkStatistics> selectFromCustomerDeviceStatistics();
int deleteByPrimaryKey(Integer id); int deleteByPrimaryKey(Integer id);
......
...@@ -3,7 +3,6 @@ package cn.com.glsx.openplatform.mapper; ...@@ -3,7 +3,6 @@ package cn.com.glsx.openplatform.mapper;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import cn.com.glsx.openplatform.model.DeviceActionDetail; 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.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
...@@ -17,14 +16,21 @@ import org.apache.ibatis.annotations.Param; ...@@ -17,14 +16,21 @@ import org.apache.ibatis.annotations.Param;
@Mapper @Mapper
public interface DeviceActionDetailMapper { public interface DeviceActionDetailMapper {
List<String> selectDeviceType(); List<DeviceActionDetail> selectBeforeTime(@Param("time") Date time);
int countByDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate, int batchInsertInto(@Param("month") String month,
@Param("type") String type); @Param("list") List<DeviceActionDetail> list);
List<PlatformDevice> selectByDate(@Param("startDate") Date startDate, int deleteByIds(List<Integer> idList);
@Param("endDate") Date endDate, @Param("count") Integer count,
@Param("type") String type); //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); int deleteByPrimaryKey(Integer id);
......
package cn.com.glsx.openplatform.mapper; package cn.com.glsx.openplatform.mapper;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail; import java.util.Date;
import java.util.List; import java.util.List;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
...@@ -15,6 +16,15 @@ import org.apache.ibatis.annotations.Param; ...@@ -15,6 +16,15 @@ import org.apache.ibatis.annotations.Param;
@Mapper @Mapper
public interface DeviceNetworkDetailMapper { 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 deleteByPrimaryKey(Integer id);
int insert(DeviceNetworkDetail record); int insert(DeviceNetworkDetail record);
......
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.");
}
}
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.");
}
}
...@@ -23,6 +23,40 @@ ...@@ -23,6 +23,40 @@
<select id="selectMonthList" resultType="java.lang.String"> <select id="selectMonthList" resultType="java.lang.String">
select distinct statistics_time select distinct statistics_time
from t_customer_action_statistics 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 &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
</where>
</select> </select>
<select id="selectPlatformDevice" resultType="cn.com.glsx.openplatform.model.PlatformDevice"> <select id="selectPlatformDevice" resultType="cn.com.glsx.openplatform.model.PlatformDevice">
...@@ -33,6 +67,9 @@ ...@@ -33,6 +67,9 @@
<if test="statistics.customerId != null"> <if test="statistics.customerId != null">
and customer_id = #{statistics.customerId,jdbcType=INTEGER} and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if> </if>
<if test="statistics.deviceType != null and statistics.deviceType != ''">
and device_type = #{statistics.deviceType,jdbcType=VARCHAR}
</if>
<if test="endDate != null"> <if test="endDate != null">
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP} and activat_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if> </if>
...@@ -65,6 +102,7 @@ ...@@ -65,6 +102,7 @@
and statistics_time = #{month,jdbcType=VARCHAR} and statistics_time = #{month,jdbcType=VARCHAR}
</if> </if>
</where> </where>
order by customer_id
</select> </select>
<select id="selectOnlineNumber" resultType="java.lang.Integer"> <select id="selectOnlineNumber" resultType="java.lang.Integer">
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
<select id="selectMonthList" resultType="java.lang.String"> <select id="selectMonthList" resultType="java.lang.String">
select distinct statistics_time select distinct statistics_time
from t_customer_network_statistics from t_customer_network_statistics
where statistics_time >= '2020-07'
order by statistics_time
</select> </select>
<select id="selectByMonth" resultMap="BaseResultMap"> <select id="selectByMonth" resultMap="BaseResultMap">
...@@ -46,6 +48,9 @@ ...@@ -46,6 +48,9 @@
<if test="statistics.customerId != null"> <if test="statistics.customerId != null">
and customer_id = #{statistics.customerId,jdbcType=INTEGER} and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if> </if>
<if test="statistics.deviceType != null and statistics.deviceType != ''">
and device_type = #{statistics.deviceType,jdbcType=VARCHAR}
</if>
<if test="endDate != null"> <if test="endDate != null">
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP} and activat_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if> </if>
...@@ -54,45 +59,27 @@ ...@@ -54,45 +59,27 @@
limit #{statistics.onlineNumber,jdbcType=INTEGER} limit #{statistics.onlineNumber,jdbcType=INTEGER}
</select> </select>
<select id="selectByDate" resultMap="BaseResultMap"> <!--<select id="selectByDate" resultMap="BaseResultMap">-->
select id, customer_id, customer_name, device_type, statistics_time, online_number, <!-- select id, customer_id, customer_name, device_type, statistics_time, online_number,-->
online_count <!-- online_count-->
from t_customer_network_statistics cns <!-- 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 &lt; #{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-->
<!-- <where>--> <!-- <where>-->
<!-- <if test="startDate != null">--> <!-- <if test="startDate != null">-->
<!-- and dnd.create_time >= #{startDate,jdbcType=TIMESTAMP}--> <!-- and statistics_time >= #{startDate,jdbcType=VARCHAR}-->
<!-- </if>--> <!-- </if>-->
<!-- <if test="endDate != null">--> <!-- <if test="endDate != null">-->
<!-- and dnd.create_time &lt; #{endDate,jdbcType=TIMESTAMP}--> <!-- and statistics_time &lt; #{endDate,jdbcType=VARCHAR}-->
<!-- </if>--> <!-- </if>-->
<!-- </where>--> <!-- </where>-->
<!-- group by pd.customer_id, statisticsTime-->
<!--</select>--> <!--</select>-->
<select id="selectFromCustomerDeviceStatistics" resultType="cn.com.glsx.openplatform.model.CustomerNetworkStatistics"> <!--<select id="selectFromCustomerDeviceStatistics" resultType="cn.com.glsx.openplatform.model.CustomerNetworkStatistics">-->
select customer_id customerId, customer_name customerName, device_type deviceType, <!-- select customer_id customerId, customer_name customerName, device_type deviceType,-->
statistics_time statisticsTime, sum(online_number) onlineNumber, <!-- statistics_time statisticsTime, sum(online_number) onlineNumber,-->
sum(online_device_num) onlineDeviceNum <!-- sum(online_device_num) onlineDeviceNum-->
from t_customer_device_statistics <!-- from t_customer_device_statistics-->
group by customer_id, statistics_time <!-- group by customer_id, statistics_time-->
</select> <!--</select>-->
......
...@@ -16,49 +16,81 @@ ...@@ -16,49 +16,81 @@
id, device_sn, `type`, create_time id, device_sn, `type`, create_time
</sql> </sql>
<select id="selectDeviceType" resultType="java.lang.String"> <select id="selectBeforeTime" resultMap="BaseResultMap">
select distinct device_type select id, device_sn, type, create_time
from t_platform_device from t_device_action_detail
where device_type != '通信模组' where create_time &lt; #{time,jdbcType=TIMESTAMP}
</select> </select>
<select id="countByDate" resultType="int"> <insert id="batchInsertInto">
select count(1) insert into
from t_platform_device <choose>
<where> <when test="month != null and month != ''">
<if test="startDate != null"> t_device_action_detail_${month}
and activat_time >= #{startDate,jdbcType=TIMESTAMP} </when>
</if> <otherwise>
<if test="endDate != null"> t_device_action_detail
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP} </otherwise>
</if> </choose>
<if test="type != null and type != ''"> (device_sn, `type`, create_time)
and device_type = #{type,jdbcType=VARCHAR} values
</if> <foreach collection="list" item="item" separator=",">
</where> (#{item.deviceSn,jdbcType=VARCHAR}, #{item.type,jdbcType=VARCHAR},
</select> #{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<select id="selectByDate" resultType="cn.com.glsx.openplatform.model.PlatformDevice"> <delete id="deleteByIds">
select id, device_sn deviceSn, device_type deviceType, customer_id customerId, delete from t_device_action_detail
user_id userId, activat_time activatTime, status, create_time createTime, where id in
update_time updateTime, create_by createBy, update_by updateBy, remark <foreach collection="list" item="item" open="(" separator="," close=")">
from t_platform_device #{item,jdbcType=INTEGER}
<where> </foreach>
<if test="startDate != null"> </delete>
and activat_time >= #{startDate,jdbcType=TIMESTAMP}
</if> <!--<select id="selectDeviceType" resultType="java.lang.String">-->
<if test="endDate != null"> <!-- select distinct device_type-->
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP} <!-- from t_platform_device-->
</if> <!-- where device_type != '通信模组'-->
<if test="type != null and type != ''"> <!--</select>-->
and device_type = #{type,jdbcType=VARCHAR}
</if> <!--<select id="countByDate" resultType="int">-->
</where> <!-- select count(1)-->
order by rand() <!-- from t_platform_device-->
<if test="count != null"> <!-- <where>-->
limit #{count,jdbcType=INTEGER} <!-- <if test="startDate != null">-->
</if> <!-- and activat_time >= #{startDate,jdbcType=TIMESTAMP}-->
</select> <!-- </if>-->
<!-- <if test="endDate != null">-->
<!-- and activat_time &lt; #{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 &lt; #{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"> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated--> <!--@mbg.generated-->
......
...@@ -15,6 +15,39 @@ ...@@ -15,6 +15,39 @@
id, device_sn, create_time id, device_sn, create_time
</sql> </sql>
<select id="selectBeforeTime" resultMap="BaseResultMap">
select id, device_sn, create_time
from t_device_network_detail
where create_time &lt; #{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"> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated--> <!--@mbg.generated-->
select select
......
package cn.com.glsx.openplatform; //package cn.com.glsx.openplatform;
//
import java.io.BufferedWriter; //import java.io.BufferedWriter;
import java.io.IOException; //import java.io.IOException;
import java.nio.file.Files; //import java.nio.file.Files;
import java.nio.file.Paths; //import java.nio.file.Paths;
import java.util.List; //import java.util.List;
import cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper; //import cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper;
import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper; //import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics; //import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.hutool.core.util.RandomUtil; //import cn.hutool.core.util.RandomUtil;
import org.junit.Test; //import org.junit.Test;
import org.junit.runner.RunWith; //import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; //import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//
/** ///**
* <p>Description: // * <p>Description:
* <p>Project: rpc-openplatform-container // * <p>Project: rpc-openplatform-container
* // *
* @author wbh // * @author wbh
* @date 2021-01-02 // * @date 2021-01-02
*/ // */
@RunWith(SpringJUnit4ClassRunner.class) //@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class) //@SpringBootTest(classes = Main.class)
public class GenerateCustomerActionStatistic { //public class GenerateCustomerActionStatistic {
@Autowired // @Autowired
private CustomerActionStatisticsMapper customerActionStatisticsMapper; // private CustomerActionStatisticsMapper customerActionStatisticsMapper;
//
@Autowired // @Autowired
private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper; // private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
//
private BufferedWriter customerActionStatisticsWriter; // private BufferedWriter customerActionStatisticsWriter;
//
@Test // @Test
public void generateData() { // public void generateData() {
this.initCustomerActionStatisticWriter(); // this.initCustomerActionStatisticWriter();
//
List<CustomerNetworkStatistics> networkStatisticsList = this.customerNetworkStatisticsMapper // List<CustomerNetworkStatistics> networkStatisticsList = this.customerNetworkStatisticsMapper
.selectByDate(null, null); // .selectByDate(null, null);
for (CustomerNetworkStatistics networkStatistics : networkStatisticsList) { // for (CustomerNetworkStatistics networkStatistics : networkStatisticsList) {
Integer onlineNumber = networkStatistics.getOnlineNumber(); // Integer onlineNumber = networkStatistics.getOnlineNumber();
this.addCustomerActionStatisticsRecord(networkStatistics, "声控调用", // this.addCustomerActionStatisticsRecord(networkStatistics, "声控调用",
this.getRandomDouble(onlineNumber, 5, 9)); // this.getRandomDouble(onlineNumber, 5, 9));
this.addCustomerActionStatisticsRecord(networkStatistics, "收音机使用", // this.addCustomerActionStatisticsRecord(networkStatistics, "收音机使用",
this.getRandomDouble(onlineNumber, 28, 33)); // this.getRandomDouble(onlineNumber, 28, 33));
this.addCustomerActionStatisticsRecord(networkStatistics, "导航调用", // this.addCustomerActionStatisticsRecord(networkStatistics, "导航调用",
this.getRandomDouble(onlineNumber, 3, 6)); // this.getRandomDouble(onlineNumber, 3, 6));
this.addCustomerActionStatisticsRecord(networkStatistics, "音乐使用", // this.addCustomerActionStatisticsRecord(networkStatistics, "音乐使用",
this.getRandomDouble(onlineNumber, 13, 18)); // this.getRandomDouble(onlineNumber, 13, 18));
this.addCustomerActionStatisticsRecord(networkStatistics, "联网应用使用", // this.addCustomerActionStatisticsRecord(networkStatistics, "联网应用使用",
this.getRandomDouble(onlineNumber, 2, 4)); // this.getRandomDouble(onlineNumber, 2, 4));
} // }
//
this.closeCustomerActionStatisticsWriter(); // this.closeCustomerActionStatisticsWriter();
} // }
//
private void initCustomerActionStatisticWriter() { // private void initCustomerActionStatisticWriter() {
try { // try {
this.customerActionStatisticsWriter = Files.newBufferedWriter( // this.customerActionStatisticsWriter = Files.newBufferedWriter(
Paths.get("D:/Users/wbh/Desktop/customer-action-statistics.sql")); // Paths.get("D:/Users/wbh/Desktop/customer-action-statistics.sql"));
this.customerActionStatisticsWriter.write( // this.customerActionStatisticsWriter.write(
"insert into t_customer_action_statistics " + // "insert into t_customer_action_statistics " +
"(customer_id, customer_name, device_type, type, statistics_time, number) values"); // "(customer_id, customer_name, device_type, type, statistics_time, number) values");
} catch (IOException e) { // } catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
private void closeCustomerActionStatisticsWriter() { // private void closeCustomerActionStatisticsWriter() {
try { // try {
this.customerActionStatisticsWriter.flush(); // this.customerActionStatisticsWriter.flush();
this.customerActionStatisticsWriter.close(); // this.customerActionStatisticsWriter.close();
} catch (IOException e) { // } catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
private int getRandomDouble(int base, int min, int max) { // private int getRandomDouble(int base, int min, int max) {
return (int) (((double) base) * RandomUtil.randomDouble(min, max)); // return (int) (((double) base) * RandomUtil.randomDouble(min, max));
} // }
//
private void addCustomerActionStatisticsRecord(CustomerNetworkStatistics networkStatistics, // private void addCustomerActionStatisticsRecord(CustomerNetworkStatistics networkStatistics,
String type, int count) { // String type, int count) {
try { // try {
String s = String.format(",\n(%d, '%s', '%s', '%s', '%s', %d)", // String s = String.format(",\n(%d, '%s', '%s', '%s', '%s', %d)",
networkStatistics.getCustomerId(), networkStatistics.getCustomerName(), // networkStatistics.getCustomerId(), networkStatistics.getCustomerName(),
networkStatistics.getDeviceType(), // networkStatistics.getDeviceType(),
type, networkStatistics.getStatisticsTime(), count); // type, networkStatistics.getStatisticsTime(), count);
this.customerActionStatisticsWriter.write(s); // this.customerActionStatisticsWriter.write(s);
} catch (IOException e) { // } catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
} //}
package cn.com.glsx.openplatform; //package cn.com.glsx.openplatform;
//
import java.io.BufferedWriter; //import java.io.BufferedWriter;
import java.io.IOException; //import java.io.IOException;
import java.nio.file.Files; //import java.nio.file.Files;
import java.nio.file.Paths; //import java.nio.file.Paths;
import java.util.Calendar; //import java.util.Calendar;
import java.util.Date; //import java.util.Date;
import java.util.List; //import java.util.List;
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.com.glsx.openplatform.model.DeviceNetworkDetail; //import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
import cn.com.glsx.openplatform.model.PlatformDevice; //import cn.com.glsx.openplatform.model.PlatformDevice;
import cn.hutool.core.date.DateUtil; //import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil; //import cn.hutool.core.util.RandomUtil;
import org.junit.Test; //import org.junit.Test;
import org.junit.runner.RunWith; //import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; //import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//
/** ///**
* <p>Description: // * <p>Description:
* <p>Project: rpc-openplatform-container // * <p>Project: rpc-openplatform-container
* // *
* @author wbh // * @author wbh
* @date 2021-01-02 // * @date 2021-01-02
*/ // */
@RunWith(SpringJUnit4ClassRunner.class) //@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class) //@SpringBootTest(classes = Main.class)
public class GenerateDeviceActionDetail { //public class GenerateDeviceActionDetail {
@Autowired // @Autowired
private DeviceActionDetailMapper deviceActionDetailMapper; // private DeviceActionDetailMapper deviceActionDetailMapper;
//@Autowired // //@Autowired
//private DeviceNetworkDetailMapper deviceNetworkDetailMapper; // //private DeviceNetworkDetailMapper deviceNetworkDetailMapper;
//
private BufferedWriter deviceActionDataWriter; // private BufferedWriter deviceActionDataWriter;
private BufferedWriter deviceNetworkDataWriter; // private BufferedWriter deviceNetworkDataWriter;
//
@Test // @Test
public void generateData() throws IOException { // public void generateData() throws IOException {
this.initDeviceActionDataWriter(); // this.initDeviceActionDataWriter();
//this.initDeviceNetworkDataWriter(); // //this.initDeviceNetworkDataWriter();
//
Date startDate = DateUtil.parseDate("2019-01-01"); // Date startDate = DateUtil.parseDate("2019-01-01");
Date endDate = DateUtil.parseDate("2021-01-01"); // Date endDate = DateUtil.parseDate("2021-01-01");
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // calendar.setTime(startDate);
Date currentStartDate = calendar.getTime(); // Date currentStartDate = calendar.getTime();
calendar.add(Calendar.MONTH, 1); // calendar.add(Calendar.MONTH, 1);
Date currentEndDate = calendar.getTime(); // Date currentEndDate = calendar.getTime();
while (currentStartDate.before(endDate)) { // while (currentStartDate.before(endDate)) {
// 获取所有类型的设备,按照设备类型进行生成 // // 获取所有类型的设备,按照设备类型进行生成
List<String> typeList = this.deviceActionDetailMapper.selectDeviceType(); // List<String> typeList = this.deviceActionDetailMapper.selectDeviceType();
for (String type : typeList) { // for (String type : typeList) {
List<PlatformDevice> deviceList = this.getNeededDeviceList( // List<PlatformDevice> deviceList = this.getNeededDeviceList(
currentStartDate, currentEndDate, type); // currentStartDate, currentEndDate, type);
this.generateDeviceActionData(currentStartDate, endDate, deviceList); // this.generateDeviceActionData(currentStartDate, endDate, deviceList);
//this.generateDeviceNetworkData(currentStartDate, deviceList); // //this.generateDeviceNetworkData(currentStartDate, deviceList);
} // }
currentStartDate = currentEndDate; // currentStartDate = currentEndDate;
calendar.add(Calendar.MONTH, 1); // calendar.add(Calendar.MONTH, 1);
currentEndDate = calendar.getTime(); // currentEndDate = calendar.getTime();
} // }
//
this.closeDeviceActionDataWriter(); // this.closeDeviceActionDataWriter();
//this.closeDeviceNetworkDataWriter(); // //this.closeDeviceNetworkDataWriter();
} // }
//
private void initDeviceNetworkDataWriter() throws IOException { // private void initDeviceNetworkDataWriter() throws IOException {
this.deviceNetworkDataWriter = Files.newBufferedWriter(Paths.get( // this.deviceNetworkDataWriter = Files.newBufferedWriter(Paths.get(
"D:/Users/wbh/Desktop/device-network-data.sql")); // "D:/Users/wbh/Desktop/device-network-data.sql"));
this.deviceNetworkDataWriter.write( // this.deviceNetworkDataWriter.write(
"insert into t_device_network_detail (device_sn, create_time) values"); // "insert into t_device_network_detail (device_sn, create_time) values");
} // }
//
private void closeDeviceNetworkDataWriter() throws IOException { // private void closeDeviceNetworkDataWriter() throws IOException {
this.deviceNetworkDataWriter.flush(); // this.deviceNetworkDataWriter.flush();
this.deviceNetworkDataWriter.close(); // this.deviceNetworkDataWriter.close();
} // }
//
private void initDeviceActionDataWriter() throws IOException { // private void initDeviceActionDataWriter() throws IOException {
this.deviceActionDataWriter = Files.newBufferedWriter(Paths.get( // this.deviceActionDataWriter = Files.newBufferedWriter(Paths.get(
"D:/Users/wbh/Desktop/device-action-data.sql")); // "D:/Users/wbh/Desktop/device-action-data.sql"));
this.deviceActionDataWriter.write( // this.deviceActionDataWriter.write(
"insert into t_device_action_detail (device_sn, type, create_time) values"); // "insert into t_device_action_detail (device_sn, type, create_time) values");
} // }
//
private void closeDeviceActionDataWriter() throws IOException { // private void closeDeviceActionDataWriter() throws IOException {
this.deviceActionDataWriter.flush(); // this.deviceActionDataWriter.flush();
this.deviceActionDataWriter.close(); // this.deviceActionDataWriter.close();
} // }
//
private List<PlatformDevice> getNeededDeviceList(Date startDate, Date endDate, String type) { // private List<PlatformDevice> getNeededDeviceList(Date startDate, Date endDate, String type) {
int count = this.deviceActionDetailMapper.countByDate(startDate, endDate, type); // int count = this.deviceActionDetailMapper.countByDate(startDate, endDate, type);
// 取所有数量的 30% 左右 // // 取所有数量的 30% 左右
int neededCount = this.getNumPercent(count, RandomUtil.randomInt(25, 35)); // int neededCount = this.getNumPercent(count, RandomUtil.randomInt(25, 35));
List<PlatformDevice> deviceList = this.deviceActionDetailMapper.selectByDate( // List<PlatformDevice> deviceList = this.deviceActionDetailMapper.selectByDate(
startDate, endDate, neededCount, type); // startDate, endDate, neededCount, type);
return deviceList; // return deviceList;
} // }
//
private void generateDeviceActionData(Date startDate, Date endDate, // private void generateDeviceActionData(Date startDate, Date endDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
this.addDeviceActionSoundControlData(startDate, endDate, deviceList); // this.addDeviceActionSoundControlData(startDate, endDate, deviceList);
this.addDeviceActionFmData(startDate, endDate, deviceList); // this.addDeviceActionFmData(startDate, endDate, deviceList);
this.addDeviceActionNavigationData(startDate, endDate, deviceList); // this.addDeviceActionNavigationData(startDate, endDate, deviceList);
this.addDeviceActionMusicData(startDate, endDate, deviceList); // this.addDeviceActionMusicData(startDate, endDate, deviceList);
this.addDeviceActionInternetData(startDate, endDate, deviceList); // this.addDeviceActionInternetData(startDate, endDate, deviceList);
} // }
//
private void generateDeviceNetworkData(Date startDate, // private void generateDeviceNetworkData(Date startDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
this.addDeviceNetworkData(startDate, deviceList); // this.addDeviceNetworkData(startDate, deviceList);
} // }
//
private int getNumPercent(int num, int percent) { // private int getNumPercent(int num, int percent) {
return num * percent / 100; // return num * percent / 100;
} // }
//
/** // /**
* 生成声控调用数据 // * 生成声控调用数据
* // *
* @param startDate 开始日期 // * @param startDate 开始日期
* @param endDate 结束日期 // * @param endDate 结束日期
* @param deviceList 设备数据 // * @param deviceList 设备数据
*/ // */
private void addDeviceActionSoundControlData(Date startDate, Date endDate, // private void addDeviceActionSoundControlData(Date startDate, Date endDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
int neededCount = deviceList.size(); // int neededCount = deviceList.size();
// 取总数的 40% 左右,每天生成两条数据 // // 取总数的 40% 左右,每天生成两条数据
int twiceCount = this.getNumPercent(neededCount, // int twiceCount = this.getNumPercent(neededCount,
RandomUtil.randomInt(35, 45)); // RandomUtil.randomInt(35, 45));
List<PlatformDevice> twiceDeviceList = deviceList.subList(0, twiceCount); // List<PlatformDevice> twiceDeviceList = deviceList.subList(0, twiceCount);
// 取剩余量的 60% 左右,每天生成一条数据 // // 取剩余量的 60% 左右,每天生成一条数据
int onceCount = this.getNumPercent(neededCount - twiceCount, // int onceCount = this.getNumPercent(neededCount - twiceCount,
RandomUtil.randomInt(55, 65)); // RandomUtil.randomInt(55, 65));
List<PlatformDevice> onceDeviceList = deviceList.subList(twiceCount, // List<PlatformDevice> onceDeviceList = deviceList.subList(twiceCount,
twiceCount + onceCount); // twiceCount + onceCount);
//
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // calendar.setTime(startDate);
Date currentDate = startDate; // Date currentDate = startDate;
while (currentDate.before(endDate)) { // while (currentDate.before(endDate)) {
// 每天需要生成两条数据的 // // 每天需要生成两条数据的
for (PlatformDevice device : twiceDeviceList) { // for (PlatformDevice device : twiceDeviceList) {
this.addDeviceActionDetailRecord(device, "声控调用", 2, currentDate); // this.addDeviceActionDetailRecord(device, "声控调用", 2, currentDate);
} // }
//
// 每天生成一条数据的 // // 每天生成一条数据的
for (PlatformDevice device : onceDeviceList) { // for (PlatformDevice device : onceDeviceList) {
this.addDeviceActionDetailRecord(device, "声控调用", 1, currentDate); // this.addDeviceActionDetailRecord(device, "声控调用", 1, currentDate);
} // }
//
calendar.add(Calendar.DAY_OF_MONTH, 1); // calendar.add(Calendar.DAY_OF_MONTH, 1);
currentDate = calendar.getTime(); // currentDate = calendar.getTime();
} // }
} // }
//
/** // /**
* 生成声收音机使用数据 // * 生成声收音机使用数据
* // *
* @param startDate 开始日期 // * @param startDate 开始日期
* @param endDate 结束日期 // * @param endDate 结束日期
* @param deviceList 设备数据 // * @param deviceList 设备数据
*/ // */
private void addDeviceActionFmData(Date startDate, Date endDate, // private void addDeviceActionFmData(Date startDate, Date endDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
int neededCount = deviceList.size(); // int neededCount = deviceList.size();
// 取总数的 70% 左右,每天生成一条数据 // // 取总数的 70% 左右,每天生成一条数据
int onceCount = this.getNumPercent(neededCount, // int onceCount = this.getNumPercent(neededCount,
RandomUtil.randomInt(65, 75)); // RandomUtil.randomInt(65, 75));
List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount); // List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount);
//
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // calendar.setTime(startDate);
Date currentDate = startDate; // Date currentDate = startDate;
while (currentDate.before(endDate)) { // while (currentDate.before(endDate)) {
// 每天生成一条数据的 // // 每天生成一条数据的
for (PlatformDevice device : onceDeviceList) { // for (PlatformDevice device : onceDeviceList) {
this.addDeviceActionDetailRecord(device, "收音机使用", 1, currentDate); // this.addDeviceActionDetailRecord(device, "收音机使用", 1, currentDate);
} // }
//
calendar.add(Calendar.DAY_OF_MONTH, 1); // calendar.add(Calendar.DAY_OF_MONTH, 1);
currentDate = calendar.getTime(); // currentDate = calendar.getTime();
} // }
} // }
//
/** // /**
* 生成导航调用数据 // * 生成导航调用数据
* // *
* @param startDate 开始日期 // * @param startDate 开始日期
* @param endDate 结束日期 // * @param endDate 结束日期
* @param deviceList 设备数据 // * @param deviceList 设备数据
*/ // */
private void addDeviceActionNavigationData(Date startDate, Date endDate, // private void addDeviceActionNavigationData(Date startDate, Date endDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
int neededCount = deviceList.size(); // int neededCount = deviceList.size();
// 取总数的 50% 左右,每天生成两条数据 // // 取总数的 50% 左右,每天生成两条数据
int twiceCount = this.getNumPercent(neededCount, // int twiceCount = this.getNumPercent(neededCount,
RandomUtil.randomInt(45, 55)); // RandomUtil.randomInt(45, 55));
List<PlatformDevice> twiceDeviceList = deviceList.subList(0, twiceCount); // List<PlatformDevice> twiceDeviceList = deviceList.subList(0, twiceCount);
// 取剩余量的 50% 左右,每天生成一条数据 // // 取剩余量的 50% 左右,每天生成一条数据
int onceCount = this.getNumPercent(neededCount - twiceCount, // int onceCount = this.getNumPercent(neededCount - twiceCount,
RandomUtil.randomInt(45, 55)); // RandomUtil.randomInt(45, 55));
List<PlatformDevice> onceDeviceList = deviceList.subList(twiceCount, // List<PlatformDevice> onceDeviceList = deviceList.subList(twiceCount,
twiceCount + onceCount); // twiceCount + onceCount);
//
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // calendar.setTime(startDate);
Date currentDate = startDate; // Date currentDate = startDate;
while (currentDate.before(endDate)) { // while (currentDate.before(endDate)) {
// 每天需要生成两条数据的 // // 每天需要生成两条数据的
for (PlatformDevice device : twiceDeviceList) { // for (PlatformDevice device : twiceDeviceList) {
this.addDeviceActionDetailRecord(device, "导航调用", 2, currentDate); // this.addDeviceActionDetailRecord(device, "导航调用", 2, currentDate);
} // }
//
// 每天生成一条数据的 // // 每天生成一条数据的
for (PlatformDevice device : onceDeviceList) { // for (PlatformDevice device : onceDeviceList) {
this.addDeviceActionDetailRecord(device, "导航调用", 1, currentDate); // this.addDeviceActionDetailRecord(device, "导航调用", 1, currentDate);
} // }
//
calendar.add(Calendar.DAY_OF_MONTH, 1); // calendar.add(Calendar.DAY_OF_MONTH, 1);
currentDate = calendar.getTime(); // currentDate = calendar.getTime();
} // }
} // }
//
/** // /**
* 生成音乐使用数据 // * 生成音乐使用数据
* // *
* @param startDate 开始日期 // * @param startDate 开始日期
* @param endDate 结束日期 // * @param endDate 结束日期
* @param deviceList 设备数据 // * @param deviceList 设备数据
*/ // */
private void addDeviceActionMusicData(Date startDate, Date endDate, // private void addDeviceActionMusicData(Date startDate, Date endDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
int neededCount = deviceList.size(); // int neededCount = deviceList.size();
// 取总数的 70% 左右,每天生成一条数据 // // 取总数的 70% 左右,每天生成一条数据
int onceCount = this.getNumPercent(neededCount, // int onceCount = this.getNumPercent(neededCount,
RandomUtil.randomInt(65, 75)); // RandomUtil.randomInt(65, 75));
List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount); // List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount);
//
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // calendar.setTime(startDate);
Date currentDate = startDate; // Date currentDate = startDate;
while (currentDate.before(endDate)) { // while (currentDate.before(endDate)) {
// 每天生成一条数据的 // // 每天生成一条数据的
for (PlatformDevice device : onceDeviceList) { // for (PlatformDevice device : onceDeviceList) {
this.addDeviceActionDetailRecord(device, "音乐使用", 1, currentDate); // this.addDeviceActionDetailRecord(device, "音乐使用", 1, currentDate);
} // }
//
calendar.add(Calendar.DAY_OF_MONTH, 1); // calendar.add(Calendar.DAY_OF_MONTH, 1);
currentDate = calendar.getTime(); // currentDate = calendar.getTime();
} // }
} // }
//
/** // /**
* 生成联网应用数据 // * 生成联网应用数据
* // *
* @param startDate 开始日期 // * @param startDate 开始日期
* @param endDate 结束日期 // * @param endDate 结束日期
* @param deviceList 设备数据 // * @param deviceList 设备数据
*/ // */
private void addDeviceActionInternetData(Date startDate, Date endDate, // private void addDeviceActionInternetData(Date startDate, Date endDate,
List<PlatformDevice> deviceList) { // List<PlatformDevice> deviceList) {
int neededCount = deviceList.size(); // int neededCount = deviceList.size();
// 取总数的 40% 左右,每天生成一条数据 // // 取总数的 40% 左右,每天生成一条数据
int onceCount = this.getNumPercent(neededCount, // int onceCount = this.getNumPercent(neededCount,
RandomUtil.randomInt(35, 45)); // RandomUtil.randomInt(35, 45));
List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount); // List<PlatformDevice> onceDeviceList = deviceList.subList(0, onceCount);
//
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // calendar.setTime(startDate);
Date currentDate = startDate; // Date currentDate = startDate;
while (currentDate.before(endDate)) { // while (currentDate.before(endDate)) {
// 每天生成一条数据的 // // 每天生成一条数据的
for (PlatformDevice device : onceDeviceList) { // for (PlatformDevice device : onceDeviceList) {
this.addDeviceActionDetailRecord(device, "联网应用使用", 1, currentDate); // this.addDeviceActionDetailRecord(device, "联网应用使用", 1, currentDate);
} // }
//
calendar.add(Calendar.DAY_OF_MONTH, 1); // calendar.add(Calendar.DAY_OF_MONTH, 1);
currentDate = calendar.getTime(); // currentDate = calendar.getTime();
} // }
} // }
//
/** // /**
* 按照条件生成设备行为数据 // * 按照条件生成设备行为数据
* // *
* @param device 设备数据 // * @param device 设备数据
* @param type 设备行为类型 // * @param type 设备行为类型
* @param count 生成数量 // * @param count 生成数量
* @param currentDate 当前日期 // * @param currentDate 当前日期
*/ // */
private void addDeviceActionDetailRecord(PlatformDevice device, String type, int count, // private void addDeviceActionDetailRecord(PlatformDevice device, String type, int count,
Date currentDate) { // Date currentDate) {
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate); // calendar.setTime(currentDate);
for (int i = 0; i < count; i++) { // for (int i = 0; i < count; i++) {
// 生成每日随机时间,范围为 06:00:00 ~ 22:59:59 // // 生成每日随机时间,范围为 06:00:00 ~ 22:59:59
calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 23)); // calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 23));
calendar.set(Calendar.MINUTE, RandomUtil.randomInt(0, 60)); // calendar.set(Calendar.MINUTE, RandomUtil.randomInt(0, 60));
calendar.set(Calendar.SECOND, RandomUtil.randomInt(0, 60)); // calendar.set(Calendar.SECOND, RandomUtil.randomInt(0, 60));
DeviceActionDetail deviceActionDetail = new DeviceActionDetail(); // DeviceActionDetail deviceActionDetail = new DeviceActionDetail();
deviceActionDetail.setDeviceSn(device.getDeviceSn()); // deviceActionDetail.setDeviceSn(device.getDeviceSn());
deviceActionDetail.setType(type); // deviceActionDetail.setType(type);
deviceActionDetail.setCreateTime(calendar.getTime()); // deviceActionDetail.setCreateTime(calendar.getTime());
try { // try {
String s = String.format(",\n('%s', '%s', '%s')", deviceActionDetail.getDeviceSn(), // String s = String.format(",\n('%s', '%s', '%s')", deviceActionDetail.getDeviceSn(),
deviceActionDetail.getType(), // deviceActionDetail.getType(),
DateUtil.formatDateTime(deviceActionDetail.getCreateTime())); // DateUtil.formatDateTime(deviceActionDetail.getCreateTime()));
this.deviceActionDataWriter.write(s); // this.deviceActionDataWriter.write(s);
} catch (IOException e) { // } catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//this.deviceActionDetailMapper.insertSelective(deviceActionDetail); // //this.deviceActionDetailMapper.insertSelective(deviceActionDetail);
} // }
} // }
//
private void addDeviceNetworkData(Date startDate, List<PlatformDevice> deviceList) { // private void addDeviceNetworkData(Date startDate, List<PlatformDevice> deviceList) {
int neededCount = deviceList.size(); // int neededCount = deviceList.size();
// 取总量的 20% 左右,生成小于 10 条数据 // // 取总量的 20% 左右,生成小于 10 条数据
int lt10Count = this.getNumPercent(neededCount, // int lt10Count = this.getNumPercent(neededCount,
RandomUtil.randomInt(18, 25)); // RandomUtil.randomInt(18, 25));
List<PlatformDevice> lt20DeviceList = deviceList.subList(0, lt10Count); // List<PlatformDevice> lt20DeviceList = deviceList.subList(0, lt10Count);
for (PlatformDevice device : lt20DeviceList) { // for (PlatformDevice device : lt20DeviceList) {
this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(5, 10), startDate); // this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(5, 10), startDate);
} // }
//
// 取总量的 60% 左右,生成 20 条数据 // // 取总量的 60% 左右,生成 20 条数据
int from10To20Count = this.getNumPercent(neededCount, // int from10To20Count = this.getNumPercent(neededCount,
RandomUtil.randomInt(55, 65)); // RandomUtil.randomInt(55, 65));
List<PlatformDevice> from10To20DeviceList = deviceList.subList(lt10Count, // List<PlatformDevice> from10To20DeviceList = deviceList.subList(lt10Count,
lt10Count + from10To20Count); // lt10Count + from10To20Count);
for (PlatformDevice device : from10To20DeviceList) { // for (PlatformDevice device : from10To20DeviceList) {
this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(12, 21), startDate); // this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(12, 21), startDate);
} // }
//
// 取总量的剩余,生成大于 20 条数据 // // 取总量的剩余,生成大于 20 条数据
List<PlatformDevice> gt20DeviceList = deviceList.subList(lt10Count + from10To20Count, // List<PlatformDevice> gt20DeviceList = deviceList.subList(lt10Count + from10To20Count,
neededCount); // neededCount);
for (PlatformDevice device : gt20DeviceList) { // for (PlatformDevice device : gt20DeviceList) {
this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(21, 31), startDate); // this.addDeviceNetworkDetailRecord(device, RandomUtil.randomInt(21, 31), startDate);
} // }
} // }
//
/** // /**
* 按照条件生成设备联网数据 // * 按照条件生成设备联网数据
* // *
* @param device 设备数据 // * @param device 设备数据
* @param count 生成数量 // * @param count 生成数量
* @param currentDate 当前日期 // * @param currentDate 当前日期
*/ // */
private void addDeviceNetworkDetailRecord(PlatformDevice device, int count, Date currentDate) { // private void addDeviceNetworkDetailRecord(PlatformDevice device, int count, Date currentDate) {
Calendar calendar = Calendar.getInstance(); // Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate); // calendar.setTime(currentDate);
int totalDayNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // int totalDayNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 0; i < count; i++) { // for (int i = 0; i < count; i++) {
// 生成随机时间,范围为 06:00:00 ~ 22:59:59 // // 生成随机时间,范围为 06:00:00 ~ 22:59:59
calendar.set(Calendar.DAY_OF_MONTH, RandomUtil.randomInt(1, totalDayNum + 1)); // calendar.set(Calendar.DAY_OF_MONTH, RandomUtil.randomInt(1, totalDayNum + 1));
calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 23)); // calendar.set(Calendar.HOUR_OF_DAY, RandomUtil.randomInt(6, 23));
calendar.set(Calendar.MINUTE, RandomUtil.randomInt(0, 60)); // calendar.set(Calendar.MINUTE, RandomUtil.randomInt(0, 60));
calendar.set(Calendar.SECOND, RandomUtil.randomInt(0, 60)); // calendar.set(Calendar.SECOND, RandomUtil.randomInt(0, 60));
DeviceNetworkDetail deviceNetworkDetail = new DeviceNetworkDetail(); // DeviceNetworkDetail deviceNetworkDetail = new DeviceNetworkDetail();
deviceNetworkDetail.setDeviceSn(device.getDeviceSn()); // deviceNetworkDetail.setDeviceSn(device.getDeviceSn());
deviceNetworkDetail.setCreateTime(calendar.getTime()); // deviceNetworkDetail.setCreateTime(calendar.getTime());
try { // try {
String s = String.format(",\n('%s', '%s')", deviceNetworkDetail.getDeviceSn(), // String s = String.format(",\n('%s', '%s')", deviceNetworkDetail.getDeviceSn(),
DateUtil.formatDateTime(deviceNetworkDetail.getCreateTime())); // DateUtil.formatDateTime(deviceNetworkDetail.getCreateTime()));
deviceNetworkDataWriter.write(s); // deviceNetworkDataWriter.write(s);
} catch (IOException e) { // } catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//this.deviceNetworkDetailMapper.insertSelective(deviceNetworkDetail); // //this.deviceNetworkDetailMapper.insertSelective(deviceNetworkDetail);
} // }
} // }
} //}
...@@ -3,11 +3,12 @@ package cn.com.glsx.openplatform; ...@@ -3,11 +3,12 @@ package cn.com.glsx.openplatform;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Calendar; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper; import cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerActionStatistics; import cn.com.glsx.openplatform.model.CustomerActionStatistics;
import cn.com.glsx.openplatform.model.DeviceActionDetail; import cn.com.glsx.openplatform.model.DeviceActionDetail;
...@@ -15,6 +16,7 @@ import cn.com.glsx.openplatform.model.PlatformDevice; ...@@ -15,6 +16,7 @@ import cn.com.glsx.openplatform.model.PlatformDevice;
import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -37,29 +39,46 @@ public class GenerateDeviceActionDetailByCustomer { ...@@ -37,29 +39,46 @@ public class GenerateDeviceActionDetailByCustomer {
private BufferedWriter deviceActionDetailWriter; 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<>(); private static final Map<String, Integer> PERCENTAGE_MAP = new HashMap<>();
static { private static final List<Integer> HOUR_LIST = new ArrayList<>();
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});
static {
PERCENTAGE_MAP.put("声控调用", 40); PERCENTAGE_MAP.put("声控调用", 40);
PERCENTAGE_MAP.put("收音机使用", 70); PERCENTAGE_MAP.put("收音机使用", 70);
PERCENTAGE_MAP.put("导航调用", 50); PERCENTAGE_MAP.put("导航调用", 50);
PERCENTAGE_MAP.put("音乐使用", 70); PERCENTAGE_MAP.put("音乐使用", 70);
PERCENTAGE_MAP.put("联网应用使用", 40); 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 数据 * 根据客户设备行为统计表 t_customer_action_statistics 生成设备行为明细表 t_device_action_detail 数据
*/ */
@Test @Test
public void generateDeviceActionDetail() { public void generateDeviceActionDetailByCustomer() {
List<String> monthList = this.customerActionStatisticsMapper.selectMonthList(); List<String> monthList = this.customerActionStatisticsMapper.selectMonthList();
for (String month : monthList) { for (String month : monthList) {
this.initDeviceActionDetailWriter(month); this.initDeviceActionDetailWriter(month);
...@@ -68,42 +87,44 @@ public class GenerateDeviceActionDetailByCustomer { ...@@ -68,42 +87,44 @@ public class GenerateDeviceActionDetailByCustomer {
for (CustomerActionStatistics actionStatistics : actionStatisticsList) { for (CustomerActionStatistics actionStatistics : actionStatisticsList) {
String type = actionStatistics.getType(); String type = actionStatistics.getType();
Integer number = actionStatistics.getNumber(); Integer number = actionStatistics.getNumber();
Integer onlineNumber = actionStatistics.getOnlineNumber();
Date startDate = DateUtil.parse(actionStatistics.getStatisticsTime(), Date startDate = DateUtil.parse(actionStatistics.getStatisticsTime(),
"yyyy-MM"); "yyyy-MM");
Date endDate = DateUtil.offsetMonth(startDate, 1); Date endDate = DateUtil.offsetMonth(startDate, 1);
List<PlatformDevice> deviceList = this.customerActionStatisticsMapper List<PlatformDevice> deviceList = this.customerActionStatisticsMapper
.selectPlatformDevice(actionStatistics, endDate); .selectPlatformDevice(actionStatistics, endDate);
if (!deviceList.isEmpty()) {
int percentage = PERCENTAGE_MAP.get(type); int percentage = PERCENTAGE_MAP.get(type);
// 取出对应比例的设备生成数据 // 取出对应比例的设备生成数据
int subCount = onlineNumber * percentage / 100; int subCount = Math.max(deviceList.size() * percentage / 100, deviceList.size());
List<PlatformDevice> subDeviceList = deviceList.subList(0, subCount); List<PlatformDevice> subDeviceList = deviceList.subList(0, subCount);
this.addDeviceActionDetail( this.addDeviceActionDetail(subDeviceList, type, startDate, number);
subDeviceList.isEmpty() ? deviceList : subDeviceList,
type, startDate, number);
}
} }
this.closeDeviceActionDetailWriter(); this.closeDeviceActionDetailWriter();
} }
} }
private void addDeviceActionDetail(List<PlatformDevice> deviceList, private void addDeviceActionDetail(List<PlatformDevice> deviceList, String type,
String type, Date startDate, int count) { Date startDate, int count) {
int dayCountOfMonth = DateUtil.endOfMonth(startDate).dayOfMonth();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
PlatformDevice device = deviceList.get(i % deviceList.size()); PlatformDevice device = deviceList.get(i % deviceList.size());
Calendar calendar = Calendar.getInstance(); Date activatTime = device.getActivatTime();
calendar.setTime(startDate); Date createTime;
int day = RandomUtil.randomInt(DateUtil.dayOfMonth(startDate), if (DateUtil.isSameMonth(startDate, activatTime)) {
calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); long startMs = activatTime.getTime();
int hour = RandomUtil.randomInt(6, 24); 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 minute = RandomUtil.randomInt(60);
int second = RandomUtil.randomInt(60); int second = RandomUtil.randomInt(60);
Date createTime = DateUtil.dateNew(startDate) createTime = DateUtil.dateNew(startDate)
.setField(DateField.DAY_OF_MONTH, day) .setField(DateField.DAY_OF_MONTH, day)
.setField(DateField.HOUR_OF_DAY, hour) .setField(DateField.HOUR_OF_DAY, hour)
.setField(DateField.MINUTE, minute) .setField(DateField.MINUTE, minute)
.setField(DateField.SECOND, second); .setField(DateField.SECOND, second);
}
DeviceActionDetail actionDetail = new DeviceActionDetail(); DeviceActionDetail actionDetail = new DeviceActionDetail();
actionDetail.setDeviceSn(device.getDeviceSn()); actionDetail.setDeviceSn(device.getDeviceSn());
actionDetail.setType(type); actionDetail.setType(type);
...@@ -142,4 +163,19 @@ public class GenerateDeviceActionDetailByCustomer { ...@@ -142,4 +163,19 @@ public class GenerateDeviceActionDetailByCustomer {
e.printStackTrace(); 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()));
}
} }
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();
}
}
}
...@@ -3,9 +3,10 @@ package cn.com.glsx.openplatform; ...@@ -3,9 +3,10 @@ package cn.com.glsx.openplatform;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Calendar; import java.util.ArrayList;
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.CustomerNetworkStatisticsMapper; import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics; import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail; import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
...@@ -13,6 +14,7 @@ import cn.com.glsx.openplatform.model.PlatformDevice; ...@@ -13,6 +14,7 @@ import cn.com.glsx.openplatform.model.PlatformDevice;
import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -35,6 +37,33 @@ public class GenerateDeviceNetworkDetailByCustomer { ...@@ -35,6 +37,33 @@ public class GenerateDeviceNetworkDetailByCustomer {
private BufferedWriter deviceNetworkDetailWriter; 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 数据 * 根据客户联网统计表 t_customer_network_statistics 生成设备联网明细表 t_device_network_detail 数据
*/ */
...@@ -86,23 +115,25 @@ public class GenerateDeviceNetworkDetailByCustomer { ...@@ -86,23 +115,25 @@ public class GenerateDeviceNetworkDetailByCustomer {
} }
private void addDeviceNetworkDetail(List<PlatformDevice> deviceList, Date startDate, int count) { private void addDeviceNetworkDetail(List<PlatformDevice> deviceList, Date startDate, int count) {
int dayCountOfMonth = DateUtil.endOfMonth(startDate).dayOfMonth();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
PlatformDevice device = deviceList.get(i % deviceList.size()); PlatformDevice device = deviceList.get(i % deviceList.size());
Date activatTime = device.getActivatTime(); Date activatTime = device.getActivatTime();
Date createTime; Date createTime;
int day;
int hour;
int minute;
int second;
if (DateUtil.isSameMonth(startDate, activatTime)) { if (DateUtil.isSameMonth(startDate, activatTime)) {
long startMs = activatTime.getTime(); long startMs = activatTime.getTime();
long endMs = DateUtil.dateNew(startDate) long endMs = DateUtil.dateNew(startDate)
.offset(DateField.MONTH, 1).getTime(); .offset(DateField.MONTH, 1).getTime();
createTime = new Date(RandomUtil.randomLong(startMs, endMs)); createTime = new Date(RandomUtil.randomLong(startMs, endMs));
} else { } else {
Calendar calendar = Calendar.getInstance(); day = RandomUtil.randomInt(DateUtil.dayOfMonth(startDate), dayCountOfMonth);
calendar.setTime(startDate); hour = HOUR_RANDOM.next();
int day = RandomUtil.randomInt(DateUtil.dayOfMonth(startDate), minute = RandomUtil.randomInt(60);
calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); second = RandomUtil.randomInt(60);
int hour = RandomUtil.randomInt(24);
int minute = RandomUtil.randomInt(60);
int second = RandomUtil.randomInt(60);
createTime = DateUtil.dateNew(startDate).setField(DateField.DAY_OF_MONTH, day) createTime = DateUtil.dateNew(startDate).setField(DateField.DAY_OF_MONTH, day)
.setField(DateField.HOUR_OF_DAY, hour) .setField(DateField.HOUR_OF_DAY, hour)
.setField(DateField.MINUTE, minute) .setField(DateField.MINUTE, minute)
...@@ -121,4 +152,11 @@ public class GenerateDeviceNetworkDetailByCustomer { ...@@ -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()));
}
} }
...@@ -53,7 +53,7 @@ public class GeneratePlatformDevice { ...@@ -53,7 +53,7 @@ public class GeneratePlatformDevice {
TYPE_MAP.put((byte) 3, "PCBA"); TYPE_MAP.put((byte) 3, "PCBA");
DEVICE_TYPE_MAP.put("通信模组", 10); DEVICE_TYPE_MAP.put("通信模组", 10);
DEVICE_TYPE_MAP.put("核心板", 10); DEVICE_TYPE_MAP.put("核心板", 20);
DEVICE_TYPE_MAP.put("PCBA-4G", 31); DEVICE_TYPE_MAP.put("PCBA-4G", 31);
DEVICE_TYPE_MAP.put("PCBA-360", 32); DEVICE_TYPE_MAP.put("PCBA-360", 32);
DEVICE_TYPE_MAP.put("PCBA-AR", 33); DEVICE_TYPE_MAP.put("PCBA-AR", 33);
......
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;
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;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!