From ea697c781f1e4b69ed5cf418f38d831101a12967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=B4=AA=E9=BE=99?= <599179587@qq.com> Date: Wed, 2 Aug 2017 13:17:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4redis=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ifish/helper/DeviceHelper.java | 24 +++++++++++ .../java/com/ifish/helper/DeviceHelperI.java | 21 +++++++++- .../java/com/ifish/helper/RedisKeyHelper.java | 40 +++++++++++++++---- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/ifish/helper/DeviceHelper.java b/src/main/java/com/ifish/helper/DeviceHelper.java index a75d3ad..51ad6fa 100644 --- a/src/main/java/com/ifish/helper/DeviceHelper.java +++ b/src/main/java/com/ifish/helper/DeviceHelper.java @@ -270,10 +270,12 @@ public class DeviceHelper implements DeviceHelperI { /** * 根据设备ID获取设备统计信息 * + * @param macAddress * @param deviceId * @return * @throws Exception */ + @Override public Tbl_Device_Statistics getDeviceStatisticsByMacAddress(String macAddress) throws Exception { Tbl_Device_Statistics device = null; String key = redisKeyHelperI.getTbl_Device_StatisticsRedisKeyByMacAddress(macAddress); @@ -312,4 +314,26 @@ public class DeviceHelper implements DeviceHelperI { return device; } + /** + * 根据摄像头ID从缓存中取得设备信息 + * + * @param cameraId + * @return + * @throws Exception + */ + @Override + public Tbl_Device getDeviceByCameraId(String cameraId) throws Exception { + Tbl_Device device = null; + String key = redisKeyHelperI.getTbl_CameraRedisKeyByCameraId(cameraId); + String redisString = redisHelperI.getRedis(key); + if (StringUtils.isNotBlank(redisString)) { + device = (Tbl_Device) IfishUtil.JsonToBean(redisString, Tbl_Device.class); + } else { + device = tbl_Device_Mapper.getDeviceByCameraId(cameraId); + if (device != null && device.getDeviceId() > 0) { + redisHelperI.setRedis(key, IfishUtil.ObjectToJson(device)); + } + } + return device; + } } diff --git a/src/main/java/com/ifish/helper/DeviceHelperI.java b/src/main/java/com/ifish/helper/DeviceHelperI.java index 57d8b8e..4ddaf53 100644 --- a/src/main/java/com/ifish/helper/DeviceHelperI.java +++ b/src/main/java/com/ifish/helper/DeviceHelperI.java @@ -44,6 +44,15 @@ public interface DeviceHelperI { */ public void updateWarnOnoff(String macAddress, String onOff); + /** + * 根据设备Id获取设备详情 + * + * @param deviceId + * @return + * @throws Exception + */ + Tbl_Device getDeviceById(Integer deviceId) throws Exception; + /** * 根据mac地址查找设备 * @@ -53,6 +62,14 @@ public interface DeviceHelperI { */ public Tbl_Device getDeviceByMacAddress(String macAddress); + /** + * 根据摄像头ID获取设备信息 + * + * @param cameraId + * @return + */ + Tbl_Device getDeviceByCameraId(String cameraId) throws Exception; + /** * 修改设备统计信息 * @@ -70,13 +87,13 @@ public interface DeviceHelperI { public List getTbl_Device_UsersListByDeviceId(Integer deviceId); /** - * 根据设备Id获取设备详情 + * 根据设备ID获取设备统计信息 * * @param deviceId * @return * @throws Exception */ - Tbl_Device getDeviceById(Integer deviceId) throws Exception; + public Tbl_Device_Statistics getDeviceStatisticsByMacAddress(String macAddress) throws Exception; /** * 根据设备ID获取设备统计信息 diff --git a/src/main/java/com/ifish/helper/RedisKeyHelper.java b/src/main/java/com/ifish/helper/RedisKeyHelper.java index 82fbebf..268e36f 100644 --- a/src/main/java/com/ifish/helper/RedisKeyHelper.java +++ b/src/main/java/com/ifish/helper/RedisKeyHelper.java @@ -26,9 +26,10 @@ public class RedisKeyHelper implements RedisKeyHelperI { @Autowired private RedisHelperI redisHelperI; - @Autowired private DeviceHelperI deviceHelper; + @Autowired + private UserHelperI userHelperI; /** * 根据phoneNumber获取Tbl_User用户信息的Redis缓存key键值 @@ -310,7 +311,6 @@ public class RedisKeyHelper implements RedisKeyHelperI { * @return */ @Override - public String getTbl_Live_Room_CountRedisKey() { return RedisKey.LIVEROOM_COUNT; } @@ -377,8 +377,18 @@ public class RedisKeyHelper implements RedisKeyHelperI { */ @Override public void deleteRedisByTbl_Device(Tbl_Device device) { - deleteTbl_DeviceInfo(device); + try { + if (device.getDeviceId() != null && device.getDeviceId() != 0 && StringUtils.isBlank(device.getMacAddress())) { + device = deviceHelper.getDeviceById(device.getDeviceId()); + } + if (StringUtils.isNotBlank(device.getMacAddress()) && (device.getDeviceId() == null || device.getDeviceId() == 0)) { + device = deviceHelper.getDeviceByMacAddress(device.getMacAddress()); + } + if (StringUtils.isNotBlank(device.getCameraId()) && (device.getDeviceId() == null || device.getDeviceId() == 0)) { + device = deviceHelper.getDeviceByCameraId(device.getCameraId()); + } + deleteTbl_DeviceInfo(device); Tbl_Device_Statistics device_Statistics = deviceHelper.getDeviceStatisticsByDeviceId(device.getDeviceId()); deleteTbl_Device_StatisticsInfo(device_Statistics); } catch (Exception e) { @@ -405,8 +415,15 @@ public class RedisKeyHelper implements RedisKeyHelperI { */ @Override public void deleteRedisByTbl_Device_Statistics(Tbl_Device_Statistics device) { - deleteTbl_Device_StatisticsInfo(device); + try { + if (device.getDeviceId() != null && device.getDeviceId() != 0 && StringUtils.isBlank(device.getMacAddress())) { + device = deviceHelper.getDeviceStatisticsByDeviceId(device.getDeviceId()); + } + if (StringUtils.isNotBlank(device.getMacAddress()) && (device.getDeviceId() == null || device.getDeviceId() == 0)) { + device = deviceHelper.getDeviceStatisticsByMacAddress(device.getMacAddress()); + } + deleteTbl_Device_StatisticsInfo(device); Tbl_Device device1 = deviceHelper.getDeviceById(device.getDeviceId()); deleteTbl_DeviceInfo(device1); } catch (Exception e) { @@ -415,10 +432,10 @@ public class RedisKeyHelper implements RedisKeyHelperI { public void deleteTbl_Device_StatisticsInfo(Tbl_Device_Statistics device) { if (device.getDeviceId() != null && device.getDeviceId() > 0) { - redisHelperI.deleteRedis(getTbl_DeviceRedisKeyByDeviceId(device.getDeviceId())); + redisHelperI.deleteRedis(getTbl_Device_StatisticsRedisKeyByDeviceId(device.getDeviceId())); } if (StringUtils.isNotBlank(device.getMacAddress())) { - redisHelperI.deleteRedis(getTbl_DeviceRedisKeyByMacAddress(device.getMacAddress())); + redisHelperI.deleteRedis(getTbl_Device_StatisticsRedisKeyByMacAddress(device.getMacAddress())); } } @@ -438,6 +455,7 @@ public class RedisKeyHelper implements RedisKeyHelperI { deleteTbl_Device_StatisticsInfo(device_Statistics); } catch (Exception e) { } + } public void deleteTbl_Device_UserInfo(Tbl_Device_User tbl_Device_User) { @@ -459,6 +477,12 @@ public class RedisKeyHelper implements RedisKeyHelperI { */ @Override public void deleteRedisByTbl_User(Tbl_User tbl_User) { + try { + if (tbl_User.getUserId() != null && tbl_User.getUserId() > 0) { + tbl_User = userHelperI.findById(tbl_User.getUserId()); + } + } catch (Exception e) { + } if (tbl_User.getUserId() != null && tbl_User.getUserId() > 0) { redisHelperI.deleteRedis(getTbl_UserRedisKeyByUserId(tbl_User.getUserId())); } @@ -483,11 +507,12 @@ public class RedisKeyHelper implements RedisKeyHelperI { */ @Override public void deleteRedisByTbl_Live_Room(Tbl_Live_Room live_Room) { + if (live_Room.getRoomId() != null && live_Room.getRoomId() > 0) { redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByRoomId(live_Room.getRoomId())); } if (StringUtils.isNotBlank(live_Room.getCameraId())) { - redisHelperI.deleteRedis(live_Room.getCameraId()); + redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByCameraId(live_Room.getCameraId())); } if (live_Room.getUserId() != null && live_Room.getUserId() > 0) { redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByUserId(live_Room.getUserId())); @@ -590,4 +615,5 @@ public class RedisKeyHelper implements RedisKeyHelperI { public void deleteRedisByTbl_Push_ListToAllRedisList() { redisHelperI.delRedisByTagKey(RedisKey.PUSHLIST_SELECT); } + }