From b80960204ea58400b33e8e9ed58ae4cb40c8ce9f Mon Sep 17 00:00:00 2001 From: lizijie <lzjiiie@163.com> Date: Fri, 25 Jun 2021 10:31:13 +0800 Subject: [PATCH] 刷新redis缓存判断参数是否为空 --- screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java | 102 ++++++++++++++++++++++++-------------------------- 1 files changed, 49 insertions(+), 53 deletions(-) diff --git a/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java b/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java index 4af7aef..fcf29d3 100644 --- a/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java +++ b/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java @@ -34,12 +34,10 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; @@ -92,38 +90,21 @@ * ���redis������������������ * */ private Map<String, Object> getDeviceInfoFromRedis(String mac) { - return (Map<String, Object>) redisTemplate.opsForValue().get(getDeviceKey(mac)); + return (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DEVICE + mac); } /* * ������������������redis */ private void setDeviceInfoToRedis(String mac, Map<String, Object> deviceInfo) { - redisTemplate.opsForValue().set(getDeviceKey(mac), deviceInfo); + redisTemplate.opsForValue().set(RedisConstants.DEVICE + mac, deviceInfo); } /* * ���redis������������������ */ private void delDeviceInfoFromRedis(String mac) { - redisTemplate.delete(getDeviceKey(mac)); - } - - /* - * ���������������������redis������key - */ - private String getDeviceKey(String mac) { - return keysConnect(RedisConstants.DEVICE, mac); - } - - //redis key������ - private String keysConnect(String... keys) { - StringBuilder key = new StringBuilder(keys[0]); - for (int i = 1; i < keys.length; i++) { - key.append("_"); - key.append(keys[i]); - } - return key.toString().toLowerCase(); + redisTemplate.delete(RedisConstants.DEVICE + mac); } @Override @@ -134,7 +115,7 @@ deviceMapper.insert(device); Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId()); //��������������������������� - insertOrganizationUnitAlarm(orgId,device.getDeviceVersionId()); + insertOrganizationUnitAlarm(orgId, device.getDeviceVersionId()); //������������������������redis String mac = device.getMac(); //���redis��������������������� @@ -163,7 +144,7 @@ //��������������������������� Integer versionId = device.getDeviceVersionId(); Integer orgId = device.getOrganizationId(); - deleteOrganizationUnitAlarm(orgId,versionId); + deleteOrganizationUnitAlarm(orgId, versionId); //������deviceInfo������ CacheUtils.flushDeviceAlarmInfo(); //������������������ @@ -179,7 +160,7 @@ Integer deviceId = device.getId(); Device oldDevice = deviceMapper.selectById(deviceId); //���������������������������������������������������������������������������������id������������ - if(!ObjectUtils.isEmpty(device.getMonitorPointId())){ + if (!ObjectUtils.isEmpty(device.getMonitorPointId())) { MonitorPoint monitorPoint = monitorPointMapper.selectById(device.getMonitorPointId()); device.setOrganizationId(monitorPoint.getOrganizationId()); } @@ -187,14 +168,14 @@ Device updateDevice = deviceMapper.selectById(deviceId); String mac = updateDevice.getMac(); //��������������������������� - Integer oldOrgId = oldDevice.getOrganizationId(); - Integer newOrgId = updateDevice.getOrganizationId(); - Integer oldVersionId = oldDevice.getDeviceVersionId(); - Integer newVersionId = updateDevice.getDeviceVersionId(); - if(!oldOrgId.equals(newOrgId)||!oldVersionId.equals(newVersionId)){ - deleteOrganizationUnitAlarm(oldOrgId,oldVersionId); - insertOrganizationUnitAlarm(newOrgId,newVersionId); - } + Integer oldOrgId = oldDevice.getOrganizationId(); + Integer newOrgId = updateDevice.getOrganizationId(); + Integer oldVersionId = oldDevice.getDeviceVersionId(); + Integer newVersionId = updateDevice.getDeviceVersionId(); + if (!oldOrgId.equals(newOrgId) || !oldVersionId.equals(newVersionId)) { + deleteOrganizationUnitAlarm(oldOrgId, oldVersionId); + insertOrganizationUnitAlarm(newOrgId, newVersionId); + } //���redis��������������������� delDeviceInfoFromRedis(mac); Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId); @@ -438,7 +419,22 @@ @Override public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData) { - return adjustDataUtils.adjust(deviceData); + String mac = deviceData.get("mac").toString(); + //���redis������������������ + Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + mac); + if (!ObjectUtils.isEmpty(adjustFormula)) { + Map<String, Object> deviceInfo = getDeviceByMac(mac); + Map<String, Object> monitorPoint = (Map<String, Object>) deviceInfo.get("monitorPoint"); + Object areaCode = monitorPoint.get("areaCode"); + Object cityCode = monitorPoint.get("cityCode"); + + Map<String, Object> aqiMap = redisTemplate.opsForHash().entries(RedisConstants.AQI_DATA + areaCode); + if (ObjectUtils.isEmpty(aqiMap)) { + aqiMap = redisTemplate.opsForHash().entries(RedisConstants.AQI_DATA + cityCode); + } + return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap); + } + return deviceData; } @Override @@ -493,18 +489,18 @@ return state; } - private void insertOrganizationUnitAlarm(Integer orgId,Integer versionId){ + private void insertOrganizationUnitAlarm(Integer orgId, Integer versionId) { QueryWrapper<OrganizationUnitAlarm> queryOrganizationVersionWrapper = new QueryWrapper<>(); - queryOrganizationVersionWrapper.eq("organization_id",orgId); - queryOrganizationVersionWrapper.eq("version_id",versionId); - queryOrganizationVersionWrapper.eq("is_delete",Constants.NOT_DELETE); + queryOrganizationVersionWrapper.eq("organization_id", orgId); + queryOrganizationVersionWrapper.eq("version_id", versionId); + queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE); List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrganizationVersionWrapper); - if(ObjectUtils.isEmpty(organizationUnitAlarms)){ - QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper =new QueryWrapper<>(); - queryVersionSensorUnitWrapper.eq("version_id",versionId); - queryVersionSensorUnitWrapper.eq("is_delete",Constants.NOT_DELETE); + if (ObjectUtils.isEmpty(organizationUnitAlarms)) { + QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper = new QueryWrapper<>(); + queryVersionSensorUnitWrapper.eq("version_id", versionId); + queryVersionSensorUnitWrapper.eq("is_delete", Constants.NOT_DELETE); List<VersionSensorUnit> versionSensorUnits = versionSensorUnitMapper.selectList(queryVersionSensorUnitWrapper); - if(!ObjectUtils.isEmpty(versionSensorUnits)){ + if (!ObjectUtils.isEmpty(versionSensorUnits)) { for (VersionSensorUnit versionSensorUnit : versionSensorUnits) { OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm(); organizationUnitAlarm.setOrganizationId(orgId); @@ -518,19 +514,19 @@ } } - private void deleteOrganizationUnitAlarm(Integer orgId,Integer versionId){ + private void deleteOrganizationUnitAlarm(Integer orgId, Integer versionId) { QueryWrapper<Device> queryOrganizationVersionWrapper = new QueryWrapper<>(); - queryOrganizationVersionWrapper.eq("organization_id",orgId); - queryOrganizationVersionWrapper.eq("device_version_id",versionId); - queryOrganizationVersionWrapper.eq("is_delete",Constants.NOT_DELETE); + queryOrganizationVersionWrapper.eq("organization_id", orgId); + queryOrganizationVersionWrapper.eq("device_version_id", versionId); + queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE); List<Device> devices = deviceMapper.selectList(queryOrganizationVersionWrapper); - if(ObjectUtils.isEmpty(devices)){//������������������������������������������������������ + if (ObjectUtils.isEmpty(devices)) {//������������������������������������������������������ UpdateWrapper deleteWrapper = new UpdateWrapper(); - deleteWrapper.eq("organization_id",orgId); - deleteWrapper.eq("version_id",versionId); - deleteWrapper.eq("is_delete",Constants.NOT_DELETE); - deleteWrapper.set("is_delete",Constants.DELETE); - organizationUnitAlarmMapper.update(null,deleteWrapper); + deleteWrapper.eq("organization_id", orgId); + deleteWrapper.eq("version_id", versionId); + deleteWrapper.eq("is_delete", Constants.NOT_DELETE); + deleteWrapper.set("is_delete", Constants.DELETE); + organizationUnitAlarmMapper.update(null, deleteWrapper); } } } -- Gitblit v1.8.0