Ver Fonte

更换接口

yangxiaokun há 2 anos atrás
pai
commit
8a0e42eb43

+ 68 - 4
src/main/java/com/zy/omp/controller/wx/DeviceController.java

@@ -1,9 +1,16 @@
 package com.zy.omp.controller.wx;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.zy.omp.common.ServerResponse;
 import com.zy.omp.common.controller.BaseController;
+import com.zy.omp.model.Device;
+import com.zy.omp.model.Relation;
 import com.zy.omp.service.DeviceInfoStateService;
+import com.zy.omp.service.DeviceService;
+import com.zy.omp.service.RelationService;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -21,12 +28,69 @@ public class DeviceController extends BaseController {
 
     @Resource
     private DeviceInfoStateService deviceInfoStateService;
+    @Resource
+    private RelationService relationService;
+    @Resource
+    private DeviceService deviceService;
+
+    /**
+     * 查询用户设备的最新数据
+     */
+    @GetMapping("getLatestState.do")
+    public ServerResponse getLatestState() {
+        return ServerResponse.createBySuccess(deviceInfoStateService.getLatestState(getOpenId()));
+    }
+
+    /**
+     * 设置默认设备
+     */
+    @PostMapping("setDefault.do")
+    public ServerResponse setDefault(String openNum) {
+        relationService.update(new UpdateWrapper<Relation>()
+                .set("isDefault", 1)
+                .eq("openId", getOpenId())
+                .eq("openNum", openNum));
+        return ServerResponse.createBySuccess();
+    }
 
     /**
-     * 查询用户的所有设备
+     * 更新设备名称
      */
-    @GetMapping("getListByOpenId.do")
-    public ServerResponse listByUserId() {
-        return ServerResponse.createBySuccess(deviceInfoStateService.listByUserId(getOpenId()));
+    @PostMapping("updateName.do")
+    public ServerResponse updateName(Relation relation) {
+        relationService.update(new UpdateWrapper<Relation>()
+                .set("deviceName", relation.getDeviceName())
+                .eq("openId", getOpenId())
+                .eq("openNum", relation.getOpenNum()));
+        return ServerResponse.createBySuccess();
     }
+
+    /**
+     * 添加绑定关系
+     */
+    @PostMapping("bind.do")
+    public ServerResponse bind(String openNum) {
+        //校验设备码有效性
+        if (deviceService.getOne(new QueryWrapper<Device>().eq("openNum", openNum)) == null)
+            return ServerResponse.createByWarning("设备码无效!");
+        //校验是否有绑定关系
+        if (relationService.checkBind(openNum)) return ServerResponse.createByWarning("该设备已经有用户绑定!");
+        Relation relation = new Relation();
+        relation.setOpenNum(openNum);
+        relation.setOpenId(getOpenId());
+        relationService.save(relation);
+        return ServerResponse.createBySuccess();
+    }
+
+    /**
+     * 解除绑定关系
+     */
+    @PostMapping("unbind.do")
+    public ServerResponse unbind(String openNum) {
+        relationService.remove(new QueryWrapper<Relation>()
+                .eq("openNum", openNum)
+                .eq("openId", getOpenId()));
+        return ServerResponse.createBySuccess();
+    }
+
 }

+ 0 - 71
src/main/java/com/zy/omp/controller/wx/RelationController.java

@@ -1,71 +0,0 @@
-package com.zy.omp.controller.wx;
-
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
-import com.zy.omp.common.ServerResponse;
-import com.zy.omp.common.controller.BaseController;
-import com.zy.omp.model.Device;
-import com.zy.omp.model.Relation;
-import com.zy.omp.service.DeviceService;
-import com.zy.omp.service.RelationService;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import javax.annotation.Resource;
-
-/**
- * 用户设备绑定关系
- *
- * @author chen_yi
- * Create on 2020/8/1
- */
-@RestController
-@RequestMapping("omp/api/wx/relation")
-public class RelationController extends BaseController {
-
-    @Resource
-    private RelationService relationService;
-    @Resource
-    private DeviceService deviceService;
-
-    /**
-     * 更新设备名称
-     */
-    @PostMapping("updateName.do")
-    public ServerResponse updateName(Relation relation) {
-        relationService.update(new UpdateWrapper<Relation>()
-                .set("deviceName", relation.getDeviceName())
-                .eq("openId", getOpenId())
-                .eq("openNum", relation.getOpenNum()));
-        return ServerResponse.createBySuccess();
-    }
-
-    /**
-     * 添加绑定关系
-     */
-    @PostMapping("bind.do")
-    public ServerResponse bind(Relation relation) {
-        //校验设备码有效性
-        if (deviceService.getOne(new QueryWrapper<Device>().eq("openNum", relation.getOpenNum())) == null)
-            return ServerResponse.createByWarning("设备码无效!");
-        //校验是否有绑定关系
-        if (relationService.checkBind(relation.getOpenNum()))
-            return ServerResponse.createByWarning("该设备已经有用户绑定!");
-        relation.setOpenId(getOpenId());
-        relationService.save(relation);
-        return ServerResponse.createBySuccess();
-    }
-
-    /**
-     * 解除绑定关系
-     */
-    @PostMapping("unbind.do")
-    public ServerResponse unbind(String openNum) {
-        relationService.remove(new QueryWrapper<Relation>()
-                .eq("openNum", openNum)
-                .eq("openId", getOpenId()));
-        return ServerResponse.createBySuccess();
-    }
-
-}

+ 2 - 1
src/main/java/com/zy/omp/mapper/DeviceInfoStateMapper.java

@@ -2,6 +2,7 @@ package com.zy.omp.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.zy.omp.model.DeviceInfoState;
+import com.zy.omp.pojo.vo.WxLatestDeviceState;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
@@ -23,5 +24,5 @@ public interface DeviceInfoStateMapper extends BaseMapper<DeviceInfoState> {
      *
      * @param openId 用户openId
      */
-    List<DeviceInfoState> listByUserId(@Param("openId") String openId);
+    List<WxLatestDeviceState> listByUserId(@Param("openId") String openId);
 }

+ 6 - 0
src/main/java/com/zy/omp/model/Relation.java

@@ -3,6 +3,8 @@ package com.zy.omp.model;
 import com.baomidou.mybatisplus.annotation.TableId;
 import lombok.Data;
 
+import java.time.LocalDateTime;
+
 /**
  * 用户设备关联关系表
  *
@@ -33,4 +35,8 @@ public class Relation {
      * 默认设备
      */
     private Integer isDefault;
+
+    private LocalDateTime createTime;
+
+    private LocalDateTime updateTime;
 }

+ 78 - 0
src/main/java/com/zy/omp/pojo/vo/WxLatestDeviceState.java

@@ -0,0 +1,78 @@
+package com.zy.omp.pojo.vo;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+/**
+ * 设备最新状态信息
+ *
+ * @author yang xiao kun
+ * create on 2021/11/29
+ */
+@Data
+public class WxLatestDeviceState {
+
+    /**
+     * 设备ID
+     */
+    @TableId
+    private String openNum;
+
+    /**
+     * 0:无法定位,1:北斗定位,2:基站定位,3:GPS定位
+     */
+    private Integer mode;
+
+    /**
+     * 电量
+     */
+    private Integer batteryNum;
+
+    /**
+     * 信号强度
+     */
+    private Integer signalNum;
+
+    /**
+     * 经度
+     */
+    private String lon;
+
+    /**
+     * 纬度
+     */
+    private String lat;
+
+    /**
+     * 经度(gcj)
+     */
+    private String lonGcj;
+
+    /**
+     * 纬度(gcj)
+     */
+    private String latGcj;
+
+    /**
+     * 格式化的地理位置
+     */
+    private String site;
+
+    /**
+     * 卫星颗数
+     */
+    private Integer num;
+
+    /**
+     * 数据上传时间
+     */
+    private LocalDateTime uploadTime;
+
+    /**
+     * 设备名称
+     */
+    private String deviceName;
+
+}

+ 8 - 2
src/main/java/com/zy/omp/service/DeviceInfoStateService.java

@@ -3,6 +3,7 @@ package com.zy.omp.service;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.zy.omp.mapper.DeviceInfoStateMapper;
 import com.zy.omp.model.DeviceInfoState;
+import com.zy.omp.pojo.vo.WxLatestDeviceState;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
@@ -21,7 +22,12 @@ public class DeviceInfoStateService extends ServiceImpl<DeviceInfoStateMapper, D
      *
      * @param openId 用户openId
      */
-    public List<DeviceInfoState> listByUserId(String openId) {
-        return baseMapper.listByUserId(openId);
+    public WxLatestDeviceState getLatestState(String openId) {
+        //用户设备列表
+        List<WxLatestDeviceState> devices = baseMapper.listByUserId(openId);
+        if (devices != null && !devices.isEmpty()) {
+            return devices.get(0);
+        }
+        return null;
     }
 }

+ 5 - 3
src/main/resources/mapper/DeviceInfoStateMapping.xml

@@ -2,12 +2,14 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zy.omp.mapper.DeviceInfoStateMapper">
 
-    <select id="listByUserId" resultType="com.zy.omp.model.DeviceInfoState">
+    <select id="listByUserId" resultType="com.zy.omp.pojo.vo.WxLatestDeviceState">
         SELECT
+            t1.deviceName,
             t2.*
         FROM
-        relation t1
+            relation t1
         LEFT JOIN device_info_state t2 ON t1.openNum = t2.openNum
-        WHERE t1.openId = #{openId}
+            WHERE t1.openId = #{openId}
+        ORDER BY t1.isDefault,t1.updateTime DESC
     </select>
 </mapper>