TRX 1 سال پیش
والد
کامیت
c0573a2a05

+ 2 - 2
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/iotCenter/DeviceSyncController.java

@@ -39,8 +39,8 @@ public class DeviceSyncController {
 
     @Operation(summary = "物联网同步设备最近上线时间", hidden = true)
     @RequestMapping(value = "lastDeviceOnLineTime", method = {RequestMethod.POST})
-    public ResultContent lastOnLineTime(@RequestBody DevicePingInfoParam param) {
-        return deviceSyncFromIotService.syncDeviceOnLineState(param);
+    public ResultContent lastDeviceOnLineTime(@RequestBody DevicePingInfoParam param) {
+        return deviceSyncFromIotService.syncDeviceOnLineTime(param);
     }
 
     @Operation(summary = "物联网同步设备在线状态", hidden = true)

+ 3 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/school/DeviceBind.java

@@ -49,6 +49,9 @@ public class DeviceBind extends SuperMain {
     @Schema(description = "是否在线")
     private OnLineState onLineState = OnLineState.OnLine;
 
+    @Schema(description = "最上线时间")
+    private Long lastOnlineTime;
+
     @Schema(description = "学校信息")
     @DBRef(lazy = true)
     private Organization schoolInfo;

+ 32 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/base/CommonService.java

@@ -44,4 +44,36 @@ public class CommonService {
         return updateResult.getUpsertedId();
     }
 
+    public Object updateData(String id, Map<String, Object> standardData, String collectionName) {
+        collectionName = CommonUtil.getCollectionName(collectionName);
+        Query query = new Query(Criteria.where("_id").is(id));
+        Update update = new Update();
+        standardData.forEach((key, value) -> {
+            if (!"id".equals(key)) {
+                update.set(key, value);
+            }
+        });
+        UpdateResult updateResult = mongoTemplate.upsert(query, update, collectionName);
+        return updateResult.getUpsertedId();
+    }
+
+    public Object updateData(Map<String, Object> where, Map<String, Object> standardData, String collectionName) {
+        collectionName = CommonUtil.getCollectionName(collectionName);
+        Criteria criteria = new Criteria();
+        if (where != null) {
+            where.forEach((key, value) -> {
+                criteria.and(key).is(value);
+            });
+        }
+        Query query = new Query(criteria);
+        Update update = new Update();
+        standardData.forEach((key, value) -> {
+            if (!"id".equals(key)) {
+                update.set(key, value);
+            }
+        });
+        UpdateResult updateResult = mongoTemplate.upsert(query, update, collectionName);
+        return updateResult.getUpsertedId();
+    }
+
 }

+ 24 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/sync/DeviceSyncFromIotService.java

@@ -8,6 +8,9 @@ import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.client.service.school.DeviceInfoService;
 import com.zhongshu.card.server.core.dao.org.OrganizationDao;
 import com.zhongshu.card.server.core.dao.org.OrganizationRelationDao;
+import com.zhongshu.card.server.core.domain.org.DeviceInfo;
+import com.zhongshu.card.server.core.domain.school.DeviceBind;
+import com.zhongshu.card.server.core.service.base.CommonService;
 import com.zhongshu.card.server.core.service.org.GateWayInfoServiceImpl;
 import com.zhongshu.card.server.core.service.user.OperationLogsService;
 import lombok.extern.slf4j.Slf4j;
@@ -15,6 +18,7 @@ import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.HashMap;
 import java.util.List;
 
 /**
@@ -40,6 +44,9 @@ public class DeviceSyncFromIotService {
     @Autowired
     GateWayInfoServiceImpl gateWayService;
 
+    @Autowired
+    CommonService commonService;
+
     /**
      * 物联网同步设备
      *
@@ -69,10 +76,27 @@ public class DeviceSyncFromIotService {
      * @return
      */
     public ResultContent syncDeviceOnLineState(DevicePingInfoParam param) {
+        HashMap<String, Object> where = new HashMap<>();
+        where.put("deviceId", param.getDeviceId());
 
+        HashMap<String, Object> map = new HashMap<>();
+        map.put("lastOnlineTime", param.getLastOnlineTime());
+        map.put("onLineState", param.getOnLineState());
+        commonService.updateData(where, map, DeviceInfo.class.getSimpleName());
+        commonService.updateData(where, map, DeviceBind.class.getSimpleName());
         return ResultContent.buildSuccess();
     }
 
+    public ResultContent syncDeviceOnLineTime(DevicePingInfoParam param) {
+        HashMap<String, Object> where = new HashMap<>();
+        where.put("deviceId", param.getDeviceId());
+
+        HashMap<String, Object> map = new HashMap<>();
+        map.put("lastOnlineTime", param.getLastOnlineTime());
+        commonService.updateData(where, map, DeviceInfo.class.getSimpleName());
+        commonService.updateData(where, map, DeviceBind.class.getSimpleName());
+        return ResultContent.buildSuccess();
+    }
 
     /**
      * 同步网关列表