|
@@ -0,0 +1,124 @@
|
|
|
+package vip.xiaonuo.coldchain.modular.app.param.mock;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import vip.xiaonuo.coldchain.modular.app.param.AppDevice;
|
|
|
+import vip.xiaonuo.coldchain.modular.app.param.AppDeviceData;
|
|
|
+import vip.xiaonuo.coldchain.modular.app.param.AppDevicePageParam;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+public class AppDeviceTestDataGenerator {
|
|
|
+
|
|
|
+ // 模拟生成设备数据
|
|
|
+ public static Page<AppDevice> getPagedDeviceData(AppDevicePageParam pageParam) {
|
|
|
+ // 获取所有设备数据
|
|
|
+ List<AppDevice> allDevices = generateAllAppDeviceData(1000); // 假设总数据量是1000条
|
|
|
+
|
|
|
+ // 排序逻辑(如有需要)
|
|
|
+ if (pageParam.getSortField() != null && pageParam.getSortOrder() != null) {
|
|
|
+ String sortField = pageParam.getSortField();
|
|
|
+ String sortOrder = pageParam.getSortOrder();
|
|
|
+
|
|
|
+ allDevices = allDevices.stream()
|
|
|
+ .sorted((device1, device2) -> {
|
|
|
+ int result = 0;
|
|
|
+ switch (sortField) {
|
|
|
+ case "deviceName":
|
|
|
+ result = device1.getDeviceName().compareTo(device2.getDeviceName());
|
|
|
+ break;
|
|
|
+ case "deviceCode":
|
|
|
+ result = device1.getDeviceCode().compareTo(device2.getDeviceCode());
|
|
|
+ break;
|
|
|
+ case "status":
|
|
|
+ result = Integer.compare(device1.getStatus(), device2.getStatus());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return "ASCEND".equals(sortOrder) ? result : -result;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算分页的起始和结束索引
|
|
|
+ int fromIndex = (pageParam.getCurrent() - 1) * pageParam.getSize(); // 当前页开始索引
|
|
|
+ int toIndex = Math.min(fromIndex + pageParam.getSize(), allDevices.size()); // 结束索引
|
|
|
+
|
|
|
+ // 创建 MyBatis-Plus 的 Page 对象
|
|
|
+ Page<AppDevice> pageResult = new Page<>();
|
|
|
+ pageResult.setCurrent(pageParam.getCurrent()); // 设置当前页码
|
|
|
+ pageResult.setSize(pageParam.getSize()); // 设置每页条数
|
|
|
+ pageResult.setTotal(allDevices.size()); // 设置总记录数
|
|
|
+ pageResult.setPages((int) Math.ceil((double) allDevices.size() / pageParam.getSize())); // 设置总页数
|
|
|
+
|
|
|
+ // 设置分页结果
|
|
|
+ pageResult.setRecords(allDevices.subList(fromIndex, toIndex)); // 设置当前页的数据
|
|
|
+
|
|
|
+ return pageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模拟生成设备数据
|
|
|
+ *
|
|
|
+ * @param totalDevices 总设备数量
|
|
|
+ * @return 设备列表
|
|
|
+ */
|
|
|
+ private static List<AppDevice> generateAllAppDeviceData(int totalDevices) {
|
|
|
+ List<AppDevice> devices = new ArrayList<>();
|
|
|
+
|
|
|
+ for (int i = 1; i <= totalDevices; i++) {
|
|
|
+ String deviceName = "冷链设备-" + i;
|
|
|
+ String deviceCode = "设备编号" + String.format("%03d", i);
|
|
|
+ int status = ThreadLocalRandom.current().nextInt(1, 3); // 随机状态 1 或 2
|
|
|
+ double batteryPercentage = 20 + (80 * ThreadLocalRandom.current().nextDouble()); // 电池电量在 20% 到 100% 之间
|
|
|
+ LocalDateTime lastUpdated = LocalDateTime.now().minusDays(ThreadLocalRandom.current().nextInt(10)); // 最近更新时间:最近 10 天内
|
|
|
+ boolean plugIn = ThreadLocalRandom.current().nextBoolean(); // 插电状态
|
|
|
+ LocalDateTime lastLoginTime = LocalDateTime.now().minusDays(ThreadLocalRandom.current().nextInt(5)); // 上次登录时间:最近 5 天内
|
|
|
+
|
|
|
+ AppDevice device = new AppDevice(
|
|
|
+ deviceName,
|
|
|
+ deviceCode,
|
|
|
+ status,
|
|
|
+ batteryPercentage,
|
|
|
+ lastUpdated,
|
|
|
+ plugIn,
|
|
|
+ lastLoginTime,
|
|
|
+ generateDeviceDataList(i)
|
|
|
+ );
|
|
|
+
|
|
|
+ devices.add(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ return devices;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成每个设备的子设备数据
|
|
|
+ *
|
|
|
+ * @param deviceIndex 设备索引,用于生成数据
|
|
|
+ * @return 子设备数据列表
|
|
|
+ */
|
|
|
+ private static List<AppDeviceData> generateDeviceDataList(int deviceIndex) {
|
|
|
+ List<AppDeviceData> deviceDataList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (int i = 1; i <= 5; i++) {
|
|
|
+ AppDeviceData data = new AppDeviceData(
|
|
|
+ "设备编号" + deviceIndex + "-数据" + i,
|
|
|
+ LocalDateTime.now().minusMinutes(ThreadLocalRandom.current().nextInt(60)), // 随机时间戳:最近 1 小时内
|
|
|
+ 15 + ThreadLocalRandom.current().nextDouble() * 10, // 温度 15℃ 到 25℃
|
|
|
+ 30 + ThreadLocalRandom.current().nextDouble() * 40, // 湿度 30% 到 70%
|
|
|
+ 350 + ThreadLocalRandom.current().nextDouble() * 150, // CO2浓度 350ppm 到 500ppm
|
|
|
+ 20 + (80 * ThreadLocalRandom.current().nextDouble()), // 电池电量 20% 到 100%
|
|
|
+ "子设备-" + deviceIndex + "-传感器" + i
|
|
|
+ );
|
|
|
+ deviceDataList.add(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ return deviceDataList;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|