From 093a58b2b7df015c371daa1c1634ff8bfd8f003b Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Wed, 21 Mar 2018 10:12:27 +0800
Subject: [PATCH] 报表 优化

---
 src/main/java/com/moral/service/impl/DeviceServiceImpl.java |   66 +++++++++++++++++++++++---------
 1 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/DeviceServiceImpl.java b/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
index bf3ad03..74b3571 100644
--- a/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -12,6 +12,7 @@
 import com.moral.common.bean.PageResult;
 import com.moral.common.util.ExampleUtil;
 import com.moral.common.util.RedisUtils;
+import com.moral.mapper.MonitorPointMapper;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -32,7 +33,8 @@
 
 	@Resource
 	private DeviceMapper deviceMapper;
-
+    @Resource
+    private MonitorPointMapper monitorPointMapper;
 	@Resource
 	private AccountService accountService;
     private Class ENTITY_CLASS = Device.class;
@@ -165,9 +167,13 @@
 	private void loadDeviceState(List<Device> list){
 		//���redis������������
 		list.stream().map( device -> {
-			String mac = Optional.of(device.getMac()).get();
-			String state = getSateFromRedis(device.getMonitorPointId(),mac.toLowerCase());
-			device.setState(state);
+			String mac = device.getMac();
+			if(!StringUtils.isBlank(mac)){
+				String state = getSateFromRedis(device.getMonitorPointId(),mac.toLowerCase());
+				device.setState(state);
+			}else{
+				device.setState(Constants.DEVICE_STATE_OFFLINE);
+			}
 			return device;
 		}).count();
 	}
@@ -198,7 +204,24 @@
 		}
 		return device;
 	}
-
+	/*
+	  ������ redis ���������������
+	 */
+	private void refreshDeviceInRedis(Device device){
+	     if(!StringUtils.isBlank(device.getMac())){
+             Device simpleDevice = new Device();
+             simpleDevice.setId(device.getId());
+             simpleDevice.setDeviceVersion(device.getDeviceVersion());
+             simpleDevice.setMac(device.getMac());
+             simpleDevice.setMonitorPointId(device.getMonitorPointId());
+             if(device.getMonitorPointId()!=null){
+                 List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(device.getMonitorPointId());
+                 simpleDevice.setOrganizationIds(orgIds);
+             }
+             String key = "device_"+device.getMac();
+             redisUtils.set(key,simpleDevice);
+         }
+	}
 	@Override
 	public PageBean queryByPageBean(PageBean pageBean) {
 		Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
@@ -246,6 +269,8 @@
 				deviceMapper.insertSelective(device);
 			}else{
 				deviceMapper.updateByPrimaryKeySelective(device);
+				//������redis���������������
+				refreshDeviceInRedis(device);
 			}
 		}
 		catch (Exception ex){
@@ -269,21 +294,24 @@
 	@Override
 	public List<Map<String,String>> queryDevicesState(List<String> macList,Boolean withData) {
 		List<Map<String,String>> list = macList.stream().map(mac->{
-			mac = mac.toLowerCase();
-			Device device = getDeviceWithOrgIdsByMac(mac);
 			Map<String,String> resultMap = new HashMap<>();
-			Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac);
-			if(!MapUtils.isEmpty(stateMap)){
-				resultMap.putAll(stateMap);
-			}else{
-				resultMap.put("state",Constants.DEVICE_STATE_OFFLINE);
-				resultMap.put("mac",mac);
-			}
-			if(BooleanUtils.isTrue(withData)){
-				String dataKey = "data_"+mac;
-				Map<String,String> dataMap = redisUtils.get(dataKey,new TypeReference<Map<String,String>>(){});
-				if(!MapUtils.isEmpty(dataMap)){
-					resultMap.putAll(dataMap);
+			if(!StringUtils.isBlank(mac)){
+				mac = mac.toLowerCase();
+				Device device = getDeviceWithOrgIdsByMac(mac);
+				Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac);
+				if(!MapUtils.isEmpty(stateMap)){
+					resultMap.putAll(stateMap);
+				}else{
+					resultMap.put("state",Constants.DEVICE_STATE_OFFLINE);
+					resultMap.put("mac",mac);
+				}
+				//������data
+				if(BooleanUtils.isTrue(withData)){
+					String dataKey = "data_"+mac;
+					Map<String,String> dataMap = redisUtils.get(dataKey,new TypeReference<Map<String,String>>(){});
+					if(!MapUtils.isEmpty(dataMap)){
+						resultMap.putAll(dataMap);
+					}
 				}
 			}
 			return  resultMap;

--
Gitblit v1.8.0