浏览代码

feat: 分页参数对象

jackzhou 6 月之前
父节点
当前提交
ddf082561b

+ 70 - 0
snowy-plugin/snowy-plugin-coldchain/src/main/java/vip/xiaonuo/coldchain/modular/app/param/AppDevicePageParam.java

@@ -0,0 +1,70 @@
+/*
+ * Copyright [2022] [https://www.xiaonuo.vip]
+ *
+ * Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
+ *
+ * 1.请不要删除和修改根目录下的LICENSE文件。
+ * 2.请不要删除和修改Snowy源码头部的版权声明。
+ * 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
+ * 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
+ * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
+ * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
+ */
+package vip.xiaonuo.coldchain.modular.app.param;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 用户查询参数
+ *
+ * @author xuyuxiang
+ * @date 2022/7/26 16:00
+ **/
+@Getter
+@Setter
+public class AppDevicePageParam {
+
+    /**
+     * 当前页
+     */
+    @Schema(description = "当前页码")
+    private Integer current=1;
+
+    /**
+     * 每页条数
+     */
+    @Schema(description = "每页条数")
+    private Integer size = 20;
+
+    /**
+     * 排序字段
+     */
+    @Schema(description = "排序字段,字段驼峰名称,如:userName")
+    private String sortField;
+
+    /**
+     * 排序方式
+     */
+    @Schema(description = "排序方式,升序:ASCEND;降序:DESCEND")
+    private String sortOrder;
+
+    /**
+     * 账号、姓名关键词
+     */
+    @Schema(description = "账号、姓名关键词")
+    private String searchKey;
+
+    /**
+     * 用户状态
+     */
+    @Schema(description = "用户状态")
+    private String userStatus;
+
+    /**
+     * 所属组织
+     */
+    @Schema(description = "所属组织")
+    private String orgId;
+}

+ 29 - 0
snowy-plugin/snowy-plugin-coldchain/src/main/java/vip/xiaonuo/coldchain/modular/app/param/DeviceStatus.java

@@ -0,0 +1,29 @@
+package vip.xiaonuo.coldchain.modular.app.param;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author jackzhou
+ * @version 1.0
+ * @project jfcloud-coldchain
+ * @description
+ * @date 2024/11/21 16:29:37
+ */
+@Data
+public class DeviceStatus {
+    @Schema(description = "设备总数")
+    private int total = 0;
+    @Schema(description = "在线设备总数")
+    private int online = 0;
+    @Schema(description = "离线设备总数")
+    private int offline = 0;
+
+    // 构造函数
+    public DeviceStatus(int totalDevices, int onlineDevices, int offlineDevices) {
+        this.total = totalDevices;
+        this.online = onlineDevices;
+        this.offline = offlineDevices;
+    }
+
+}

+ 124 - 0
snowy-plugin/snowy-plugin-coldchain/src/main/java/vip/xiaonuo/coldchain/modular/app/param/mock/AppDeviceTestDataGenerator.java

@@ -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;
+    }
+
+}