Commit 2ddd2a21 by tyfanchz

增加数据生成类

1 parent 3fe033e8
......@@ -13,11 +13,6 @@ package cn.com.glsx.openplatform.model;
*/
public class CustomerActionStatistics {
/**
* 在线设备数
*/
private Integer onlineNumber;
/**
* id
*/
private Integer id;
......@@ -52,13 +47,10 @@ public class CustomerActionStatistics {
*/
private Integer number;
public Integer getOnlineNumber() {
return onlineNumber;
}
public void setOnlineNumber(Integer onlineNumber) {
this.onlineNumber = onlineNumber;
}
/**
* 在线设备数
*/
private Integer onlineNumber;
public Integer getId() {
return id;
......@@ -116,17 +108,25 @@ public class CustomerActionStatistics {
this.number = number;
}
public Integer getOnlineNumber() {
return onlineNumber;
}
public void setOnlineNumber(Integer onlineNumber) {
this.onlineNumber = onlineNumber;
}
@Override
public String toString() {
return "CustomerActionStatistics{" +
"onlineNumber=" + onlineNumber +
", id=" + id +
"id=" + id +
", customerId=" + customerId +
", customerName='" + customerName + '\'' +
", deviceType='" + deviceType + '\'' +
", type='" + type + '\'' +
", statisticsTime='" + statisticsTime + '\'' +
", number=" + number +
", onlineNumber=" + onlineNumber +
'}';
}
}
......@@ -47,6 +47,11 @@ public class CustomerNetworkStatistics {
*/
private Integer onlineNumber;
/**
* 联网次数
*/
private Integer onlineCount;
public Integer getId() {
return id;
}
......@@ -103,6 +108,14 @@ public class CustomerNetworkStatistics {
this.onlineNumber = onlineNumber;
}
public Integer getOnlineCount() {
return onlineCount;
}
public void setOnlineCount(Integer onlineCount) {
this.onlineCount = onlineCount;
}
@Override
public String toString() {
return "CustomerNetworkStatistics{" +
......@@ -113,6 +126,7 @@ public class CustomerNetworkStatistics {
", type='" + type + '\'' +
", statisticsTime='" + statisticsTime + '\'' +
", onlineNumber=" + onlineNumber +
", onlineCount=" + onlineCount +
'}';
}
}
......@@ -3,7 +3,6 @@ package cn.com.glsx.openplatform.mapper;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.model.ActivationData;
import cn.com.glsx.openplatform.model.PlatformDevice;
import cn.com.glsx.openplatform.model.ShipmentData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
......@@ -17,7 +16,7 @@ import org.apache.ibatis.annotations.Param;
*/
@Mapper
public interface ActivationDataMapper {
List<ActivationData> getActivationList();
List<ActivationData> selectActivationList();
int countByDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
......@@ -28,8 +27,4 @@ public interface ActivationDataMapper {
List<String> selectDeviceTypeList();
List<ShipmentData> selectShipmentByType(@Param("deviceType") String deviceType);
int updatePlatformDeviceActivation(@Param("device") PlatformDevice device,
@Param("startTime") Date startTime, @Param("endTime") Date endTime,
@Param("count") int count);
}
......@@ -3,6 +3,7 @@ package cn.com.glsx.openplatform.mapper;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.model.CustomerActionStatistics;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
import cn.com.glsx.openplatform.model.PlatformDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
......@@ -19,12 +20,17 @@ import org.apache.ibatis.annotations.Param;
public interface CustomerActionStatisticsMapper {
List<String> selectMonthList();
List<PlatformDevice> selectPlatformDevice(
@Param("statistics") CustomerActionStatistics statistics,
@Param("endDate") Date endDate);
List<DeviceNetworkDetail> selectDeviceFromDeviceNetworkDetail(
@Param("customerId") Integer customerId, @Param("endDate") Date endDate);
List<CustomerActionStatistics> selectByMonth(@Param("month") String month);
Integer selectOnlineNumber(CustomerActionStatistics actionStatistics);
List<CustomerActionStatistics> selectByType(@Param("type") String type);
List<PlatformDevice> selectDeviceList(
@Param("actionStatistics") CustomerActionStatistics actionStatistics,
@Param("endDate") Date endDate, @Param("count") Integer count);
......
......@@ -3,6 +3,7 @@ package cn.com.glsx.openplatform.mapper;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.com.glsx.openplatform.model.PlatformDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
......@@ -16,11 +17,22 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public interface CustomerNetworkStatisticsMapper {
List<String> selectMonthList();
List<CustomerNetworkStatistics> selectByMonth(@Param("month") String month);
List<PlatformDevice> selectPlatformDevice(
@Param("statistics") CustomerNetworkStatistics statistics,
@Param("endDate") Date 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> selectFromDeviceNetworkByDate(
// @Param("startDate") Date startDate,
// @Param("endDate") Date endDate);
List<CustomerNetworkStatistics> selectFromCustomerDeviceStatistics();
int deleteByPrimaryKey(Integer id);
......
......@@ -13,7 +13,7 @@
<result column="type" jdbcType="TINYINT" property="type"/>
</resultMap>
<select id="getActivationList" resultType="cn.com.glsx.openplatform.model.ActivationData">
<select id="selectActivationList" resultType="cn.com.glsx.openplatform.model.ActivationData">
select id, month, customer_id customerId, customer, num, type
from t_activation_data
order by month;
......@@ -66,16 +66,4 @@
</if>
</where>
</select>
<update id="updatePlatformDeviceActivation">
update t_platform_device
set activat_time = from_unixtime(
#{startTime,jdbcType=TIMESTAMP}
+ floor(rand() * (#{endTime,jdbcType=TIMESTAMP} - #{startTime,jdbcType=TIMESTAMP}))
)
where customer_id = #{device.customerId,jdbcType=INTEGER}
and device_type = #{device.deviceType,jdbcType=VARCHAR}
order by rand()
limit #{count}
</update>
</mapper>
......@@ -12,11 +12,12 @@
<result column="type" jdbcType="VARCHAR" property="type"/>
<result column="statistics_time" jdbcType="VARCHAR" property="statisticsTime"/>
<result column="number" jdbcType="INTEGER" property="number"/>
<result column="online_number" jdbcType="INTEGER" property="onlineNumber"/>
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, customer_id, customer_name, device_type, `type`, statistics_time, number
id, customer_id, customer_name, device_type, `type`, statistics_time, number, online_number
</sql>
<select id="selectMonthList" resultType="java.lang.String">
......@@ -24,8 +25,40 @@
from t_customer_action_statistics
</select>
<select id="selectPlatformDevice" resultType="cn.com.glsx.openplatform.model.PlatformDevice">
select id, device_sn deviceSn, device_type deviceType, version, customer_id customerId,
user_id, activat_time activatTime, status, create_time createTime
from t_platform_device
<where>
<if test="statistics.customerId != null">
and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if>
<if test="endDate != null">
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
</where>
order by rand()
</select>
<select id="selectDeviceFromDeviceNetworkDetail"
resultType="cn.com.glsx.openplatform.model.DeviceNetworkDetail">
select dnd.device_sn deviceSn, dnd.create_time creatreTime
from t_device_network_detail dnd
left join t_platform_device pd on pd.device_sn = dnd.device_sn
<where>
<if test="customerId != null">
pd.customer_id = #{customerId,jdbcType=INTEGER}
</if>
<if test="endDate != null">
and dnd.create_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
</where>
order by rand()
</select>
<select id="selectByMonth" resultMap="BaseResultMap">
select id, customer_id, customer_name, device_type, type, statistics_time, number
select id, customer_id, customer_name, device_type, type, statistics_time, number,
online_number
from t_customer_action_statistics
<where>
<if test="month != null and month != ''">
......@@ -48,16 +81,6 @@
group by customer_id, statistics_time
</select>
<select id="selectByType" resultMap="BaseResultMap">
select id, customer_id, customer_name, device_type, type, statistics_time, number
from t_customer_action_statistics
<where>
<if test="type != null and type != ''">
and type = #{type,jdbcType=VARCHAR}
</if>
</where>
</select>
<select id="selectDeviceList"
resultType="cn.com.glsx.openplatform.model.PlatformDevice">
select id, device_sn deviceSn, device_type deviceType
......
......@@ -12,45 +12,90 @@
<result column="type" jdbcType="VARCHAR" property="type"/>
<result column="statistics_time" jdbcType="VARCHAR" property="statisticsTime"/>
<result column="online_number" jdbcType="INTEGER" property="onlineNumber"/>
<result column="online_count" jdbcType="INTEGER" property="onlineCount"/>
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, customer_id, customer_name, device_type, `type`, statistics_time, online_number
id, customer_id, customer_name, device_type, `type`, statistics_time, online_number, online_count
</sql>
<select id="selectByDate" resultMap="BaseResultMap">
select id, customer_id, customer_name, device_type, statistics_time, online_number
from t_customer_device_statistics_new_doing_copy2 cns
<select id="selectMonthList" resultType="java.lang.String">
select distinct statistics_time
from t_customer_network_statistics
</select>
<select id="selectByMonth" 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 test="month != null and month != ''">
and statistics_time = #{month,jdbcType=VARCHAR}
</if>
</where>
</select>
<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
from t_platform_device
<where>
<if test="statistics.customerId != null">
and customer_id = #{statistics.customerId,jdbcType=INTEGER}
</if>
<if test="endDate != null">
and statistics_time &lt; #{endDate,jdbcType=VARCHAR}
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
and online_number > 0
</where>
order by rand()
limit #{statistics.onlineNumber,jdbcType=INTEGER}
</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 &lt; #{endDate,jdbcType=TIMESTAMP}
and statistics_time &lt; #{endDate,jdbcType=VARCHAR}
</if>
</where>
group by pd.customer_id, statisticsTime
</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>-->
<!-- <if test="startDate != null">-->
<!-- and dnd.create_time >= #{startDate,jdbcType=TIMESTAMP}-->
<!-- </if>-->
<!-- <if test="endDate != null">-->
<!-- and dnd.create_time &lt; #{endDate,jdbcType=TIMESTAMP}-->
<!-- </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="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
......
package cn.com.glsx.openplatform;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.hutool.core.date.DateUtil;
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 GenerateCustomerNetworkStatistic {
@Autowired
private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
@Test
public void generateData() {
Date startDate = DateUtil.parse("2019-01-01", "yyyy-MM-dd");
Date endDate = DateUtil.parse("2021-01-01", "yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
calendar.add(Calendar.MONTH, 1);
Date currentStartDate = startDate;
Date currentEndDate = calendar.getTime();
while (currentStartDate.before(endDate)) {
List<CustomerNetworkStatistics> statisticsList = this.customerNetworkStatisticsMapper
.selectFromDeviceNetworkByDate(currentStartDate, currentEndDate);
if (!statisticsList.isEmpty()) {
this.customerNetworkStatisticsMapper.batchInsert(statisticsList);
}
currentStartDate = currentEndDate;
calendar.add(Calendar.MONTH, 1);
currentEndDate = calendar.getTime();
}
}
}
//package cn.com.glsx.openplatform;
//
//import java.util.Calendar;
//import java.util.Date;
//import java.util.List;
//import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
//import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
//import cn.hutool.core.date.DateUtil;
//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 GenerateCustomerNetworkStatistic {
// @Autowired
// private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
//
// @Test
// public void generateData() {
// Date startDate = DateUtil.parse("2019-01-01", "yyyy-MM-dd");
// Date endDate = DateUtil.parse("2021-01-01", "yyyy-MM-dd");
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(startDate);
// calendar.add(Calendar.MONTH, 1);
// Date currentStartDate = startDate;
// Date currentEndDate = calendar.getTime();
// while (currentStartDate.before(endDate)) {
// List<CustomerNetworkStatistics> statisticsList = this.customerNetworkStatisticsMapper
// .selectFromDeviceNetworkByDate(currentStartDate, currentEndDate);
// if (!statisticsList.isEmpty()) {
// this.customerNetworkStatisticsMapper.batchInsert(statisticsList);
// }
// currentStartDate = currentEndDate;
// calendar.add(Calendar.MONTH, 1);
// currentEndDate = calendar.getTime();
// }
// }
//}
......@@ -12,7 +12,7 @@ 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.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
......@@ -39,48 +39,25 @@ public class GenerateDeviceActionDetailByCustomer {
private static final Map<String, Integer[]> RAND_RANGE_MAP = new HashMap<>();
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});
}
//@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.selectByMonth(
// currentStartDate, currentEndDate);
// for (CustomerActionStatistics actionStatistics : actionStatisticsList) {
// this.batchAddDeviceActionDetail(actionStatistics, currentStartDate);
// }
// currentStartDate = currentEndDate;
// this.closeDeviceActionDetailWriter();
// }
//}
PERCENTAGE_MAP.put("声控调用", 40);
PERCENTAGE_MAP.put("收音机使用", 70);
PERCENTAGE_MAP.put("导航调用", 50);
PERCENTAGE_MAP.put("音乐使用", 70);
PERCENTAGE_MAP.put("联网应用使用", 40);
}
/**
* 根据客户设备行为统计表 t_customer_action_statistics 生成设备行为明细表 t_device_action_detail 数据
*/
@Test
public void generateDeviceActionDetail() {
List<String> monthList = this.customerActionStatisticsMapper.selectMonthList();
......@@ -89,134 +66,68 @@ public class GenerateDeviceActionDetailByCustomer {
List<CustomerActionStatistics> actionStatisticsList =
this.customerActionStatisticsMapper.selectByMonth(month);
for (CustomerActionStatistics actionStatistics : actionStatisticsList) {
Integer deviceCount = this.customerActionStatisticsMapper.selectOnlineNumber(
actionStatistics);
this.batchAddDeviceActionDetail(actionStatistics, deviceCount);
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);
}
}
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--;
}
private void addDeviceActionDetail(List<PlatformDevice> deviceList,
String type, Date startDate, int count) {
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);
DeviceActionDetail actionDetail = new DeviceActionDetail();
actionDetail.setDeviceSn(device.getDeviceSn());
actionDetail.setType(type);
actionDetail.setCreateTime(createTime);
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();
}
}
//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);
String filename = String.format("D:/data/device-action-detail-%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";
String headLine = String.format(
"insert into t_device_action_detail_%s (device_sn, type, create_time) values",
monthStr);
this.deviceActionDetailWriter.write(headLine);
} catch (IOException e) {
e.printStackTrace();
......
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();
}
}
}
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.List;
import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.com.glsx.openplatform.model.DeviceNetworkDetail;
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.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-06
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)
public class GenerateDeviceNetworkDetailByCustomer {
@Autowired
private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
private BufferedWriter deviceNetworkDetailWriter;
/**
* 根据客户联网统计表 t_customer_network_statistics 生成设备联网明细表 t_device_network_detail 数据
*/
@Test
public void generateDeviceNetworkDetail() {
// 取出所有月份,按月存储数据
List<String> monthList = this.customerNetworkStatisticsMapper.selectMonthList();
for (String month : monthList) {
this.initDeviceNetworkDetailWriter(month);
List<CustomerNetworkStatistics> networkStatisticsList =
this.customerNetworkStatisticsMapper.selectByMonth(month);
Date startDate = DateUtil.parse(month, "yyyy-MM");
Date endDate = DateUtil.offsetMonth(startDate, 1);
for (CustomerNetworkStatistics networkStatistics : networkStatisticsList) {
List<PlatformDevice> deviceList = this.customerNetworkStatisticsMapper
.selectPlatformDevice(networkStatistics, endDate);
Integer onlineCount = networkStatistics.getOnlineCount();
if (!deviceList.isEmpty()) {
this.addDeviceNetworkDetail(deviceList, startDate, onlineCount);
}
}
this.closeDeviceNetworkDetailWriter();
}
}
private void initDeviceNetworkDetailWriter(String month) {
try {
String monthStr = month.replace("-", "");
String filename = String.format("D:/data/device-network-detail-%s.sql",
monthStr);
this.deviceNetworkDetailWriter = FileUtil.getWriter(filename, StandardCharsets.UTF_8,
false);
String header = String.format(
"insert into t_device_network_detail_%s (device_sn, create_time) values",
monthStr);
this.deviceNetworkDetailWriter.write(header);
} catch (IOException e) {
e.printStackTrace();
}
}
private void closeDeviceNetworkDetailWriter() {
try {
this.deviceNetworkDetailWriter.flush();
this.deviceNetworkDetailWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void addDeviceNetworkDetail(List<PlatformDevice> deviceList, Date startDate, int count) {
for (int i = 0; i < count; i++) {
PlatformDevice device = deviceList.get(i % deviceList.size());
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 {
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);
createTime = DateUtil.dateNew(startDate).setField(DateField.DAY_OF_MONTH, day)
.setField(DateField.HOUR_OF_DAY, hour)
.setField(DateField.MINUTE, minute)
.setField(DateField.SECOND, second);
}
DeviceNetworkDetail networkDetail = new DeviceNetworkDetail();
networkDetail.setDeviceSn(device.getDeviceSn());
networkDetail.setCreateTime(createTime);
try {
String s = String.format(",\n('%s', '%s')",
networkDetail.getDeviceSn(),
DateUtil.formatDateTime(networkDetail.getCreateTime()));
this.deviceNetworkDetailWriter.write(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
......@@ -37,7 +37,7 @@ public class GeneratePlatformDevice {
private ActivationDataMapper activationDataMapper;
@Autowired
private PlatformDeviceMapper deviceMapper;
private PlatformDeviceMapper platformDeviceMapper;
private BufferedWriter platformDeviceWriter;
......@@ -45,6 +45,8 @@ public class GeneratePlatformDevice {
private static final Map<String, Integer> DEVICE_TYPE_MAP = new HashMap<>();
private int currentId = 874003;
static {
TYPE_MAP.put((byte) 1, "通信模组");
TYPE_MAP.put((byte) 2, "核心板");
......@@ -141,7 +143,9 @@ public class GeneratePlatformDevice {
serialNumMap.put(serialDateStr, 0);
}
// 获取当天已经出货的量,作为起始序列号
int startSerialNum = serialNumMap.get(serialDateStr) + 1;
//int startSerialNum = serialNumMap.get(serialDateStr) + 1;
int startSerialNum = this.activationDataMapper.countByDate(serialDate,
DateUtil.offsetDay(serialDate, 1)) + 1;
int endSerialNum;
int randCount = goodsNum > 5000 ? RandomUtil.randomInt(4000, 5000) : goodsNum;
// 计算终止序列号
......@@ -171,7 +175,7 @@ public class GeneratePlatformDevice {
private void initPlatformDeviceWriter() throws IOException {
this.platformDeviceWriter = Files.newBufferedWriter(
Paths.get("D:/data/platform-device.sql"));
this.platformDeviceWriter.write("insert into t_platform_device (device_sn, device_type, version, customer_id, status, create_time, update_time, create_by, update_by) values");
this.platformDeviceWriter.write("insert into t_platform_device (id, device_sn, device_type, version, customer_id, status, create_time, update_time, create_by, update_by) values");
}
private void closePlatformDeviceWriter() throws IOException {
......@@ -190,6 +194,7 @@ public class GeneratePlatformDevice {
DateUtil.dayOfMonth(serialDate),
serialNum);
PlatformDevice device = new PlatformDevice();
device.setId(this.currentId);
device.setDeviceSn(deviceSn);
device.setDeviceType(shipmentData.getDeviceType());
device.setVersion(shipmentData.getVersion());
......@@ -201,13 +206,16 @@ public class GeneratePlatformDevice {
device.setCreateBy(0);
device.setUpdateBy(0);
try {
String s = String.format(",\n('%s', '%s', '%s', %d, %d, '%s', '%s', %d, %d)",
String s = String.format(",\n(%d, '%s', '%s', '%s', %d, %d, '%s', '%s', %d, %d)",
this.currentId,
device.getDeviceSn(), device.getDeviceType(), device.getVersion(),
device.getCustomerId(), device.getStatus(),
DateUtil.formatDateTime(device.getCreateTime()),
DateUtil.formatDateTime(device.getUpdateTime()),
device.getCreateBy(), device.getUpdateBy());
this.platformDeviceMapper.insertSelective(device);
this.platformDeviceWriter.write(s);
this.currentId++;
} catch (IOException e) {
e.printStackTrace();
}
......@@ -217,7 +225,7 @@ public class GeneratePlatformDevice {
@Test
public void generateDeviceActivationData() throws Exception {
//查询数据库
List<ActivationData> activationList = activationDataMapper.getActivationList();
List<ActivationData> activationList = activationDataMapper.selectActivationList();
for (ActivationData activationData : activationList) {
activationData.setTypeName(TYPE_MAP.get(activationData.getType()));
String month = activationData.getMonth();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!