|
|
@@ -1,6 +1,7 @@
|
|
|
package com.xunmei.mediator.api.service;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.xunmei.common.core.constant.SecurityConstants;
|
|
|
import com.xunmei.common.core.domain.iot.domain.IotDevice;
|
|
|
import com.xunmei.common.core.domain.mediator.domain.*;
|
|
|
@@ -12,6 +13,7 @@ import com.xunmei.system.api.domain.SysOrg;
|
|
|
import com.xunmei.system.api.domain.north.NorthError;
|
|
|
import com.xunmei.system.api.dto.device.DeviceDto;
|
|
|
import com.xunmei.system.api.enums.DataType;
|
|
|
+import com.xunmei.system.api.enums.DeviceOnlineStatus;
|
|
|
import com.xunmei.system.api.enums.DeviceType;
|
|
|
import com.xunmei.system.api.enums.ProductTypeEnum;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -73,31 +75,17 @@ public class EquipmentSyncService {
|
|
|
this.stringRedisTemplate.opsForValue().set(key, key);
|
|
|
this.stringRedisTemplate.expire(key, 10, TimeUnit.MINUTES);
|
|
|
|
|
|
- /**
|
|
|
- * 获取所有传递过来的数据
|
|
|
- */
|
|
|
- /**
|
|
|
- * 缓存机构数据,减少重复查询机构数据
|
|
|
- */
|
|
|
- Map<String, Map<String, Object>> orgs = new HashMap<>();
|
|
|
|
|
|
- Map<String, List<IotDevice>> rel = analysisDeviceList(pages, orgs, msgId);
|
|
|
- final List<IotDevice> olds = rel.get("olds");
|
|
|
- final List<IotDevice> list = rel.get("list");
|
|
|
- final List<Long> orgIdList = rel.values().stream().flatMap(Collection::stream).map(IotDevice::getOrgId).distinct().collect(Collectors.toList());
|
|
|
+ Map<String, Map<String, Object>> orgMap = new HashMap<>();
|
|
|
+ List<IotDevice> rel = analysisDeviceList(pages, orgMap, msgId);
|
|
|
+ final List<IotDevice> list = rel.stream().filter(device -> ObjectUtil.isNull(device.getId())).collect(Collectors.toList());
|
|
|
+ final List<IotDevice> olds = rel.stream().filter(device -> ObjectUtil.isNotNull(device.getId())).collect(Collectors.toList());
|
|
|
+ final List<Long> orgIdList = rel.stream().map(IotDevice::getOrgId).distinct().collect(Collectors.toList());
|
|
|
//第一步逻辑删除数据,逻辑删除
|
|
|
this.iIotDeviceService.updateDeviceDeleted(orgIdList);
|
|
|
-
|
|
|
-
|
|
|
//第二步查询数据,如果存在则更新,如果不存在,则新增
|
|
|
- //批量更新
|
|
|
- if (olds.size() > 0) {
|
|
|
- this.iIotDeviceService.batchUpdateDevice(olds);
|
|
|
- }
|
|
|
- //批量插入
|
|
|
- if (list.size() > 0) {
|
|
|
- this.iIotDeviceService.batchInsertDevice(list);
|
|
|
- }
|
|
|
+ this.iIotDeviceService.batchUpdateDevice(olds);
|
|
|
+ this.iIotDeviceService.batchInsertDevice(list);
|
|
|
this.northStatisticsSyncService.saveOrUpdateBaseCountByDataType(DataType.deviceCount.getIndex(), list.size() + olds.size(), true);
|
|
|
}
|
|
|
|
|
|
@@ -107,16 +95,13 @@ public class EquipmentSyncService {
|
|
|
* @param
|
|
|
* @return
|
|
|
*/
|
|
|
- private Map<String, List<IotDevice>> analysisDeviceList(List<DeviceDto> dtos, Map<String, Map<String, Object>> orgs, String msgId) {
|
|
|
- Map<String, List<IotDevice>> rel = new HashMap<>();
|
|
|
-
|
|
|
+ private List<IotDevice> analysisDeviceList(List<DeviceDto> dtoList, Map<String, Map<String, Object>> orgMap, String msgId) {
|
|
|
/**
|
|
|
* 验证数据正确性,有错误的数据,存储到错误数据中
|
|
|
*/
|
|
|
- Iterator var2 = dtos.iterator();
|
|
|
+ Iterator var2 = dtoList.iterator();
|
|
|
List<NorthError> errors = new ArrayList<>();
|
|
|
List<IotDevice> list = new ArrayList<>();
|
|
|
- List<IotDevice> olds = new ArrayList<>();
|
|
|
DeviceDto datum = null;
|
|
|
NorthError error = null;
|
|
|
|
|
|
@@ -157,15 +142,14 @@ public class EquipmentSyncService {
|
|
|
errors.add(error);
|
|
|
continue;
|
|
|
}
|
|
|
- Map<String, Object> obj = orgs.get(datum.getOrganizationGuid());
|
|
|
+ Map<String, Object> obj = orgMap.get(datum.getOrganizationGuid());
|
|
|
if (obj == null) {
|
|
|
SysOrg theOrg = orgService.findByCode(datum.getOrganizationGuid(), SecurityConstants.INNER);
|
|
|
Map<String, Object> org = new HashMap<>();
|
|
|
if (theOrg == null) {
|
|
|
org.put("isOk", "N");
|
|
|
org.put("msg", "organizationGuid:" + datum.getOrganizationGuid() + "不正确");
|
|
|
- error = new NorthError(msgId, "/data/equipmentList"
|
|
|
- , datum, "orgGUID:" + datum.getOrganizationGuid() + "不正确");
|
|
|
+ error = new NorthError(msgId, "/data/equipmentList", datum, "orgGUID:" + datum.getOrganizationGuid() + "不正确");
|
|
|
errors.add(error);
|
|
|
continue;
|
|
|
} else {
|
|
|
@@ -173,13 +157,12 @@ public class EquipmentSyncService {
|
|
|
org.put("org", theOrg);
|
|
|
datum.setOrg(theOrg);
|
|
|
}
|
|
|
- orgs.put(datum.getOrganizationGuid(), org);
|
|
|
+ orgMap.put(datum.getOrganizationGuid(), org);
|
|
|
} else {
|
|
|
String isOk = obj.get("isOk").toString();
|
|
|
if (isOk.equals("N")) {
|
|
|
String msg = obj.get("msg").toString();
|
|
|
- error = new NorthError(msgId, "/data/equipmentList"
|
|
|
- , datum, msg);
|
|
|
+ error = new NorthError(msgId, "/data/equipmentList", datum, msg);
|
|
|
errors.add(error);
|
|
|
continue;
|
|
|
}
|
|
|
@@ -188,39 +171,20 @@ public class EquipmentSyncService {
|
|
|
datum.setOrg(theOrg);
|
|
|
}
|
|
|
}
|
|
|
- Map<String, Object> map = resolveDevice(datum);
|
|
|
-
|
|
|
-
|
|
|
- IotDevice device = (IotDevice) map.get("device");
|
|
|
- Integer isOld = (Integer) map.get("isOld");
|
|
|
- if (isOld == 1) {
|
|
|
- olds.add(device);
|
|
|
- } else {
|
|
|
- list.add(device);
|
|
|
- }
|
|
|
+ IotDevice device = resolveDevice(datum);
|
|
|
+ list.add(device);
|
|
|
}
|
|
|
northErrorService.saveErrorData(errors);
|
|
|
- rel.put("olds", olds);
|
|
|
- rel.put("list", list);
|
|
|
-
|
|
|
- return rel;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 解析生成设备数据
|
|
|
- *
|
|
|
- * @param datum
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Map<String, Object> resolveDevice(DeviceDto datum) {
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- IotDevice device = new IotDevice();
|
|
|
- map.put("isOld", 0);
|
|
|
- IotDevice device1 = iIotDeviceService.findByHostCode(datum.getEquipmentCode(), datum.getOrg().getId());
|
|
|
- if (device1 != null) {
|
|
|
- map.put("isOld", 1);
|
|
|
- device = device1;
|
|
|
+ private IotDevice resolveDevice(DeviceDto datum) {
|
|
|
+ IotDevice device = iIotDeviceService.findByHostCode(datum.getEquipmentCode(), datum.getOrg().getId());
|
|
|
+ if (device == null) {
|
|
|
+ device = new IotDevice();
|
|
|
+ device.setCreateTime(LocalDateTime.now());
|
|
|
+ device.setNetStatus(DeviceOnlineStatus.UNKNOW.ordinal());
|
|
|
}
|
|
|
device.setEquipmentCode(datum.getEquipmentCode());
|
|
|
device.setEquipmentName(datum.getEquipmentName());
|
|
|
@@ -259,10 +223,8 @@ public class EquipmentSyncService {
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
- device.setCreateTime(LocalDateTime.now());
|
|
|
device.setUpdateTime(LocalDateTime.now());
|
|
|
- map.put("device", device);
|
|
|
- return map;
|
|
|
+ return device;
|
|
|
}
|
|
|
|
|
|
|