|
@@ -9,8 +9,10 @@ package vip.xiaonuo.coldchain.modular.app.service;
|
|
|
*/
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.github.jfcloud.influxdb.flux.AggregationWindow;
|
|
|
import com.google.common.collect.Lists;
|
|
@@ -26,10 +28,12 @@ import vip.xiaonuo.coldchain.modular.monitortarget.param.TargetStatus;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.service.MonitorTargetService;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortargetregion.entity.MonitorTargetRegion;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortargetregion.service.MonitorTargetRegionService;
|
|
|
+import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Supplier;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
@@ -136,6 +140,104 @@ public class AppDeviceService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 分页设备关键字查询
|
|
|
+ */
|
|
|
+ public Page<AppDevice> pageSearchDevices(AppDevicePageParam appDevicePageParam) {
|
|
|
+ //根据关键字搜索监控设备编码
|
|
|
+ List<MonitorTargetRegion> regionList = monitorTargetRegionService.list(new LambdaQueryWrapper<>(MonitorTargetRegion.class)
|
|
|
+ .like(MonitorTargetRegion::getSensorCode, appDevicePageParam.getSearchKey())
|
|
|
+ .eq(MonitorTargetRegion::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE));
|
|
|
+ Map<String, List<MonitorTargetRegion>> regionTargetMap = regionList.stream().collect(Collectors.groupingBy(MonitorTargetRegion::getMonitorTargetId));
|
|
|
+
|
|
|
+ //根据监控设备 或 关键字搜索监控对象
|
|
|
+ Page<MonitorTarget> targetPage = monitorTargetService.page(new Page<>(appDevicePageParam.getCurrent(), appDevicePageParam.getSize()),
|
|
|
+ new LambdaQueryWrapper<>(MonitorTarget.class)
|
|
|
+ .eq(StrUtil.isNotBlank(appDevicePageParam.getRoomId()), MonitorTarget::getRoomId, appDevicePageParam.getRoomId())
|
|
|
+ .eq(MonitorTarget::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE)
|
|
|
+ .and(x -> x.like(MonitorTarget::getName, appDevicePageParam.getSearchKey())
|
|
|
+ .or(CollUtil.isNotEmpty(regionTargetMap.keySet()), y -> y.in(MonitorTarget::getId, regionTargetMap.keySet())))
|
|
|
+ .orderByAsc(MonitorTarget::getStatus, MonitorTarget::getName));
|
|
|
+
|
|
|
+ // 用于存储最终返回的 AppDevice 列表
|
|
|
+ List<AppDevice> rlt = Lists.newArrayList();
|
|
|
+
|
|
|
+ // 遍历监控目标分页记录
|
|
|
+ targetPage.getRecords().forEach(monitorTarget -> {
|
|
|
+ // 创建新的 AppDevice 对象来存储转换后的设备信息
|
|
|
+ AppDevice appDevice = new AppDevice();
|
|
|
+ // 将监控目标信息复制到 AppDevice 中
|
|
|
+ BeanUtil.copyProperties(monitorTarget, appDevice);
|
|
|
+ appDevice.setMonitorTargetRegionList(null);
|
|
|
+ // 获取当前监控目标的 ID,用于查询子设备数据
|
|
|
+ String monitorTargetId = monitorTarget.getId();
|
|
|
+
|
|
|
+ // 如果是用监控设备编码查询到的,则只展示对应的监控设备;如果是用关键字查询到的,则展示该监控目标下的所有子设备
|
|
|
+ List<MonitorTargetRegion> monitorTargetRegions = regionTargetMap.getOrDefault(monitorTargetId,
|
|
|
+ monitorTargetRegionService.getRegionListByTargetId(monitorTargetId));
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(monitorTargetRegions)) {
|
|
|
+ rlt.add(appDevice);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历每个区域,将其转换为子设备数据,并添加到 AppDevice 的子设备列表中
|
|
|
+ for (MonitorTargetRegion monitorTargetRegion : monitorTargetRegions) {
|
|
|
+ // 获取 AppDevice 的子设备列表
|
|
|
+ List<AppDeviceData> children = appDevice.getChildren();
|
|
|
+ // 如果子设备列表为空,初始化一个空的列表
|
|
|
+ if (children == null) {
|
|
|
+ children = new ArrayList<>();
|
|
|
+ appDevice.setChildren(children);
|
|
|
+ }
|
|
|
+ // 创建新的 AppDeviceData 对象,将监控区域数据复制到该子设备对象中
|
|
|
+ AppDeviceData appDeviceData = new AppDeviceData();
|
|
|
+ children.add(appDeviceData);
|
|
|
+ BeanUtil.copyProperties(monitorTargetRegion, appDeviceData);
|
|
|
+ String deviceCode = appDeviceData.getDeviceCode();
|
|
|
+ // 传感器路数
|
|
|
+ Integer sensorRoute = appDeviceData.getSensorRoute();
|
|
|
+ if (!Objects.isNull(sensorRoute) && StrUtil.isNotBlank(deviceCode)) {
|
|
|
+ SensorData sensorData = monitorDeviceService.queryLatestDataByDeviceIdAndRoads(deviceCode, sensorRoute);
|
|
|
+ if (!Objects.isNull(sensorData)) {
|
|
|
+ appDeviceData.setBatteryPercentage(sensorData.getBattery());
|
|
|
+ if (sensorData.getTime() != null) {
|
|
|
+ appDeviceData.setTimestamp(Date.from(sensorData.getTime()));
|
|
|
+ }
|
|
|
+ appDeviceData.setHumidity(sensorData.getHumidity());
|
|
|
+ appDeviceData.setTemperature(sensorData.getTemperature());
|
|
|
+ appDeviceData.setCo2Level(sensorData.getCo2());
|
|
|
+ appDeviceData.setPlugInStatus(sensorData.getPlugInStatus());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 取第一个子设备的数据来填充主体设备的电池电量和插电状态等信息
|
|
|
+ if (!appDevice.getChildren().isEmpty()) {
|
|
|
+ AppDeviceData firstChild = appDevice.getChildren().get(0);
|
|
|
+ if (Objects.nonNull(firstChild)) {
|
|
|
+ appDevice.setPlugInStatus(firstChild.getPlugInStatus());
|
|
|
+ appDevice.setBatteryPercentage(firstChild.getBatteryPercentage());
|
|
|
+ appDevice.setUpdateTime(firstChild.getTimestamp());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将转换后的设备对象添加到最终的设备列表中
|
|
|
+ rlt.add(appDevice);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 创建分页返回对象,设置分页信息和记录数据
|
|
|
+ Page<AppDevice> pageResult = new Page<>();
|
|
|
+ pageResult.setCurrent(appDevicePageParam.getCurrent()); // 当前页
|
|
|
+ pageResult.setSize(appDevicePageParam.getSize()); // 每页记录数
|
|
|
+ pageResult.setTotal(targetPage.getTotal()); // 总记录数
|
|
|
+ pageResult.setPages(targetPage.getPages()); // 总页数
|
|
|
+ pageResult.setRecords(rlt); // 设置当前页的设备数据
|
|
|
+
|
|
|
+ // 返回分页结果
|
|
|
+ return pageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 获取设备总数
|
|
|
*
|