|
@@ -1,13 +1,111 @@
|
|
|
package com.zhiyun.project.item.service;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.zhiyun.project.item.domain.entity.Device;
|
|
|
+import com.zhiyun.project.item.domain.vo.CoordinateVo;
|
|
|
+import com.zhiyun.project.item.domain.vo.DeviceVo;
|
|
|
+import com.zhiyun.project.item.domain.vo.OptionsVo;
|
|
|
+import com.zhiyun.project.item.mapper.DeviceMapper;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Service
|
|
|
-public class DeviceService {
|
|
|
- public boolean getDeviceData(JSONObject jsonObject) {
|
|
|
+public class DeviceService extends ServiceImpl<DeviceMapper, Device> {
|
|
|
+
|
|
|
+ public DeviceVo getDeviceData(Integer id){
|
|
|
+
|
|
|
+ Device device = baseMapper.selectById(id);
|
|
|
+
|
|
|
+ if(device == null)
|
|
|
+ return null;
|
|
|
+
|
|
|
+ DeviceVo deviceVo = new DeviceVo();
|
|
|
+ BeanUtils.copyProperties(device, deviceVo);
|
|
|
+
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ if(Duration.between(device.getLatestTime(), now).toMinutes() < 1){
|
|
|
+ deviceVo.setStatue(1);
|
|
|
+ }else {
|
|
|
+ deviceVo.setStatue(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ return deviceVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<CoordinateVo> getCoordinateData(){
|
|
|
+ // 查询所有设备数据
|
|
|
+ List<Device> deviceList = baseMapper.selectList(null);
|
|
|
+
|
|
|
+ // 如果设备列表为空,直接返回空列表
|
|
|
+ if (deviceList == null || deviceList.isEmpty()) {
|
|
|
+ return null; // 返回空的列表
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用流式 API 转换 Device 列表为 CoordinateVo 列表
|
|
|
+ List<CoordinateVo> coordinateVoList = deviceList.stream().map(device -> {
|
|
|
+ // 创建 CoordinateVo 对象
|
|
|
+ CoordinateVo coordinateVo = new CoordinateVo();
|
|
|
+
|
|
|
+ // 设置 deviceid
|
|
|
+ coordinateVo.setDeviceid(device.getDeviceid());
|
|
|
+
|
|
|
+ // 设置 coordinate 数组,包含 x 和 y 坐标
|
|
|
+ coordinateVo.setCoordinate(new Double[]{device.getX(), device.getY()});
|
|
|
+
|
|
|
+ // 获取当前时间
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+
|
|
|
+ // 判断 latestTime 和当前时间的差值
|
|
|
+ if (Duration.between(device.getLatestTime(), now).toMinutes() < 1) {
|
|
|
+ coordinateVo.setStatue(1);
|
|
|
+ } else {
|
|
|
+ coordinateVo.setStatue(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回处理后的 CoordinateVo 对象
|
|
|
+ return coordinateVo;
|
|
|
+ }).collect(Collectors.toList()); // 收集成一个列表
|
|
|
+
|
|
|
+ // 返回 CoordinateVo 列表
|
|
|
+ return coordinateVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int insetDeviceData(JSONObject jsonObject) {
|
|
|
+ // 创建 Device 实体对象
|
|
|
+ Device device = new Device();
|
|
|
+
|
|
|
+ // 从jsonObject中获取值并设置到 Device 对象中
|
|
|
+ device.setDeviceid(jsonObject.getString("deviceid"));
|
|
|
+ device.setLat(jsonObject.getDouble("lat"));
|
|
|
+ device.setLon(jsonObject.getDouble("lon"));
|
|
|
+ device.setX(jsonObject.getDouble("x"));
|
|
|
+ device.setY(jsonObject.getDouble("y"));
|
|
|
+ device.setZ(jsonObject.getDouble("z"));
|
|
|
+
|
|
|
+ // 如果需要处理 latestTime 字段,可以设置为当前时间,或者从 JSON 中获取时间字段
|
|
|
+ // 这里暂时设置为当前时间
|
|
|
+ LocalDateTime now = LocalDateTime.now(); // 获取当前的 LocalDateTime
|
|
|
+ device.setLatestTime(now); // 设置到 Device 对象中
|
|
|
+
|
|
|
+ //数据库操作判断
|
|
|
+ Device judgeObject = baseMapper.getDeviceById(device.getDeviceid());
|
|
|
+ if (judgeObject == null) {
|
|
|
+ return baseMapper.insert(device);
|
|
|
+ } else {
|
|
|
+ device.setId(judgeObject.getId());
|
|
|
+ System.out.println(device);
|
|
|
+ return baseMapper.updateById(device);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- System.out.println(jsonObject);
|
|
|
- return true;
|
|
|
+ public List<OptionsVo> getDeviceOptions() {
|
|
|
+ return baseMapper.getDeviceOptions();
|
|
|
}
|
|
|
}
|