|
|
@@ -27,6 +27,7 @@ import com.xunmei.system.api.dto.SensorStatusDto;
|
|
|
import com.xunmei.system.api.dto.protection.ReceiveErrorDto;
|
|
|
import com.xunmei.system.api.enums.ElectricityMeterAttributes;
|
|
|
import com.xunmei.system.api.util.LogUtils;
|
|
|
+import io.netty.util.internal.StringUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
@@ -67,6 +68,20 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateDelByOrgId(Long orgId) {
|
|
|
+ if (ObjectUtil.isNull(orgId)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ final List<IotSensor> sensors = baseMapper.selectList(new LambdaQueryWrapper<IotSensor>().eq(IotSensor::getSource, 1).eq(IotSensor::getOrgId, orgId));
|
|
|
+ final List<String> collect = sensors.stream().map(IotSensor::getDeviceCode).distinct().collect(Collectors.toList());
|
|
|
+ if (ObjectUtil.isNotEmpty(collect)) {
|
|
|
+ iotSensorLogService.remove(new LambdaQueryWrapper<IotSensorLog>().eq(IotSensorLog::getOrgId, orgId).in(IotSensorLog::getDeviceCode, collect));
|
|
|
+ }
|
|
|
+ baseMapper.updateDelByOrgId(orgId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public ReceiveErrorDto saveDirectHostStatus(DataPageDto<SensorStatusDto> request, String msgId) {
|
|
|
ReceiveErrorDto dto = CheckDataUtil.checkObjFieldIsNull(request);
|
|
|
|
|
|
@@ -78,11 +93,24 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void saveStatusAsync(DataPageDto<SensorStatusDto> request, String msgId){
|
|
|
+ public void saveStatusAsync(DataPageDto<SensorStatusDto> request, String msgId) {
|
|
|
String path = "/api/status/sensorCommon";
|
|
|
List<NorthError> errors = new ArrayList<>();
|
|
|
- Map<String, List<IotSensor>> productDeviceMap = getAllDevice();
|
|
|
- if (ObjectUtil.isEmpty(productDeviceMap)){
|
|
|
+ final long count = request.getData().stream().map(SensorStatusDto::getOrgCode).distinct().count();
|
|
|
+ if (count > 1) {
|
|
|
+ errors.add(new NorthError(msgId, path, StringUtil.EMPTY_STRING, "数据异常:完整数据内发现存在不同机构数据"));
|
|
|
+ northErrorService.saveErrorData(errors);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ final SensorStatusDto dto = request.getData().get(0);
|
|
|
+ final SysOrg org = remoteOrgService.findByCode(dto.getOrgCode(), SecurityConstants.INNER);
|
|
|
+ if (org == null) {
|
|
|
+ errors.add(new NorthError(msgId, path, dto, "参数非法:机构orgCode不存在:" + dto.getOrgCode()));
|
|
|
+ northErrorService.saveErrorData(errors);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, List<IotSensor>> productDeviceMap = getAllDevice(org.getId());
|
|
|
+ if (ObjectUtil.isEmpty(productDeviceMap)) {
|
|
|
LogUtils.DIRECT_HOST_SENSOR_STATUS.error("处理动环传感器状态时,未查询到传感器数据!");
|
|
|
return;
|
|
|
}
|
|
|
@@ -94,17 +122,14 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
, status, "参数非法:未传orgCode"));
|
|
|
continue;
|
|
|
}
|
|
|
- final SysOrg org = remoteOrgService.findByCode(status.getOrgCode(), SecurityConstants.INNER);
|
|
|
- if (org == null) {
|
|
|
- errors.add(new NorthError(msgId, path
|
|
|
- , status, "参数非法:机构orgCode不存在:" + status.getOrgCode()));
|
|
|
+ final List<IotSensor> iotSensors = productDeviceMap.get(String.valueOf(status.getDeviceCode()));
|
|
|
+ if (ObjectUtil.isEmpty(iotSensors)) {
|
|
|
+ errors.add(new NorthError(msgId, path, status, "对应设备不存在:" + status.getDeviceCode()));
|
|
|
continue;
|
|
|
}
|
|
|
- final List<IotSensor> iotSensors = productDeviceMap.get(String.valueOf(org.getId()));
|
|
|
final Optional<IotSensor> optional = iotSensors.stream().filter(sensor -> ObjectUtil.equal(sensor.getDeviceCode(), status.getDeviceCode())).findFirst();
|
|
|
if (!optional.isPresent()) {
|
|
|
- errors.add(new NorthError(msgId, path
|
|
|
- , status, "对应设备不存在:" + status.getDeviceCode()));
|
|
|
+ errors.add(new NorthError(msgId, path, status, "对应设备不存在:" + status.getDeviceCode()));
|
|
|
continue;
|
|
|
}
|
|
|
final String infoStr = JSON.toJSONString(status.getInfos());
|
|
|
@@ -161,8 +186,12 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
} else {
|
|
|
obj.put("unit", attributesEnum.getEnumText());
|
|
|
final Map extraMap = JSON.parseObject(attributesEnum.getEnumText(), Map.class);
|
|
|
- final String o = (String) extraMap.get(val);
|
|
|
- obj.put("res", o);
|
|
|
+ for (Object o : extraMap.keySet()) {
|
|
|
+ if (String.valueOf(o).equals(val)) {
|
|
|
+ obj.put("res", (String) extraMap.get(o));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
return JSON.toJSONString(jsonArray);
|
|
|
@@ -173,35 +202,43 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
public ReceiveErrorDto saveDirectHost(DataPageDto<SensorDto> request, String msgId) {
|
|
|
//验证基础分页信息
|
|
|
ReceiveErrorDto dto = CheckDataUtil.checkObjFieldIsNull(request);
|
|
|
-
|
|
|
-
|
|
|
if (dto.getSuccess()) {
|
|
|
//分页数据的存取
|
|
|
final List<SensorDto> data = RedisCheckRepeatDataUtil.isCompleted(request, SensorDto.class);
|
|
|
-
|
|
|
LogUtils.DIRECT_HOST_SENSOR.info("判断是否获取通用传感器全部信息分页数据:{}", data.size() > 0);
|
|
|
if (ObjectUtil.isEmpty(data)) {
|
|
|
return dto;
|
|
|
}
|
|
|
+ List<IotSensor> devices = convertToDomain(data, msgId);
|
|
|
+ if (ObjectUtil.isEmpty(devices)) {
|
|
|
|
|
|
- /* new Thread(() -> {
|
|
|
- //删除直连主机传感器一个月前的状态数据
|
|
|
- // productDeviceMapper.deleteDirectHostHistory(DateUtil.offset(new Date(), DateField.MONTH, -1));
|
|
|
- }).start();*/
|
|
|
-
|
|
|
- List<Long> deletedDeviceIds = new ArrayList<>();
|
|
|
- List<IotSensor> devices = convertToDomain(data, msgId, deletedDeviceIds);
|
|
|
- this.removeByIds(deletedDeviceIds);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+ final Long orgId = devices.get(0).getOrgId();
|
|
|
+ this.updateDelByOrgId(orgId);
|
|
|
this.saveOrUpdateBatch(devices);
|
|
|
}
|
|
|
return dto;
|
|
|
}
|
|
|
|
|
|
- private List<IotSensor> convertToDomain(List<SensorDto> sensorDtos, String msgId, List<Long> deletedDeviceIds) {
|
|
|
+ private List<IotSensor> convertToDomain(List<SensorDto> sensorDtos, String msgId) {
|
|
|
List<IotSensor> domains = new ArrayList<>();
|
|
|
List<NorthError> errors = new ArrayList<>();
|
|
|
String path = "/api/data/sensorCommonList";
|
|
|
- Map<String, List<IotSensor>> productDeviceMap = getAllDevice();
|
|
|
+
|
|
|
+ final SensorDto dto = sensorDtos.get(0);
|
|
|
+ final String orgCode = dto.getOrgCode();
|
|
|
+ SysOrg org = remoteOrgService.findByCode(orgCode, SecurityConstants.INNER);
|
|
|
+ if (org == null) {
|
|
|
+ errors.add(new NorthError(msgId, path, dto, "参数非法:机构orgCode不存在:" + orgCode));
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ final long count = sensorDtos.stream().map(SensorDto::getOrgCode).distinct().count();
|
|
|
+ if (count > 1) {
|
|
|
+ errors.add(new NorthError(msgId, path, dto, "数据异常:完整数据内发现存在不同机构数据:" + orgCode));
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ Map<String, List<IotSensor>> productDeviceMap = getAllDevice(org.getId());
|
|
|
Set<String> uniqueSet = new HashSet<>();
|
|
|
for (SensorDto sensorDto : sensorDtos) {
|
|
|
NorthError error = null;
|
|
|
@@ -214,14 +251,7 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- SysOrg org = remoteOrgService.findByCode(sensorDto.getOrgCode(), SecurityConstants.INNER);
|
|
|
- if (org == null) {
|
|
|
- error = new NorthError(msgId, path
|
|
|
- , sensorDto, "参数非法:机构orgCode不存在:" + sensorDto.getOrgCode());
|
|
|
- errors.add(error);
|
|
|
- continue;
|
|
|
- }
|
|
|
- String key = org.getId() + sensorDto.getDeviceCode();
|
|
|
+ String key = sensorDto.getDeviceCode();
|
|
|
if (uniqueSet.contains(key)) {
|
|
|
errors.add(new NorthError(msgId, path
|
|
|
, sensorDto, "数据来源中,同机构中设备编号重复。"));
|
|
|
@@ -239,7 +269,6 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
domain.setDeviceType(sensorDto.getDeviceType());
|
|
|
domain.setDeleted(0);
|
|
|
domain.setCreateTime(LocalDateTime.now());
|
|
|
-
|
|
|
if (ObjectUtil.isNotEmpty(exists)) {
|
|
|
if (exists.size() > 1) {
|
|
|
errors.add(new NorthError(msgId, path, sensorDto, "同机构下设备编号重复,将取第一条"));
|
|
|
@@ -256,24 +285,21 @@ public class IotSensorServiceImpl extends ServiceImpl<IotSensorMapper, IotSensor
|
|
|
domain.setSource(1);
|
|
|
domains.add(domain);
|
|
|
}
|
|
|
-
|
|
|
northErrorService.saveErrorData(errors);
|
|
|
-
|
|
|
-
|
|
|
- productDeviceMap.keySet().stream().filter(k -> !uniqueSet.contains(k)).forEach(k -> productDeviceMap.get(k).forEach(d -> deletedDeviceIds.add(d.getId())));
|
|
|
-
|
|
|
return domains;
|
|
|
}
|
|
|
|
|
|
- private Map<String, List<IotSensor>> getAllDevice() {
|
|
|
+ private Map<String, List<IotSensor>> getAllDevice(Long orgId) {
|
|
|
return baseMapper.selectList(new LambdaQueryWrapper<IotSensor>()
|
|
|
+ .eq(ObjectUtil.isNotNull(orgId), IotSensor::getOrgId, orgId)
|
|
|
+ .eq(IotSensor::getDeleted, 0)
|
|
|
.eq(IotSensor::getSource, 1))
|
|
|
.stream()
|
|
|
- .collect(Collectors.groupingBy(c -> String.valueOf(c.getOrgId())));
|
|
|
+ .collect(Collectors.groupingBy(IotSensor::getDeviceCode));
|
|
|
}
|
|
|
|
|
|
private String convertDeviceType(String deviceType) {
|
|
|
- if (ObjectUtil.isEmpty(deviceType)){
|
|
|
+ if (ObjectUtil.isEmpty(deviceType)) {
|
|
|
return null;
|
|
|
}
|
|
|
return DictUtils.getDictLabel(DictConstants.SENSOR_DEVICE_TYPE, Integer.parseInt(deviceType));
|