From 52303c8868a7d63238e5ac579c85721306e51a40 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Wed, 07 Mar 2018 08:52:27 +0800
Subject: [PATCH] 报表 优化

---
 src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java |   90 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 86 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java b/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
index fc0db0d..22c16d9 100644
--- a/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -1,19 +1,22 @@
 package com.moral.service.impl;
 
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import javax.annotation.Resource;
 
 import com.github.pagehelper.PageHelper;
 import com.moral.common.bean.Constants;
 import com.moral.common.bean.PageBean;
-import com.moral.common.util.ExampleUtil;
-import com.moral.common.util.MyBatisBaseMapUtil;
+import com.moral.common.util.*;
+import com.moral.entity.Device;
+import com.moral.mapper.DeviceMapper;
+import org.apache.commons.collections.CollectionUtils;
 import org.springframework.stereotype.Service;
 
-import com.moral.common.util.ValidateUtil;
 import com.moral.entity.MonitorPoint;
 import com.moral.mapper.MonitorPointMapper;
 import com.moral.service.MonitorPointService;
@@ -24,6 +27,10 @@
 public class MonitorPointServiceImpl implements MonitorPointService {
     @Resource
     private MonitorPointMapper monitorPointMapper;
+    @Resource
+    private DeviceMapper deviceMapper;
+    @Resource
+    RedisUtils redisUtils;
     private static Class ENTITY_CLASS = MonitorPoint.class;
     @Override
     public List<MonitorPoint> getMonitorPointsByAreaName(Map<String, Object> parameters) {
@@ -32,7 +39,26 @@
     }
     @Override
     public List<MonitorPoint> queryWithStateByMap(Map<String, Object> params){
-        return monitorPointMapper.selectWithStateByMap(params);
+        List<MonitorPoint> monitorPointList = monitorPointMapper.selectByMap(params);
+        for(MonitorPoint monitorPoint:monitorPointList){
+            Integer state = getStateFromRedis(monitorPoint.getId());
+            monitorPoint.setState(state);
+        }
+        return monitorPointList;
+    }
+    private Integer getStateFromRedis(Integer monitorPointId){
+        StringBuilder key = new StringBuilder();
+        key.append("state_").append(monitorPointId).append("_*");
+        List<Map> stateList = redisUtils.getList(key.toString(),Map.class);
+        int state = -1;
+        if(stateList!=null){
+            for (Map deviceState:stateList){
+                int s =  Integer.parseInt(deviceState.get("state").toString());
+                state = s>state&&s<4?s:state;
+            }
+        }
+        state = state==-1?4:state;
+        return state;
     }
     @Override
     public PageBean queryByPageBean(PageBean pageBean) {
@@ -64,13 +90,42 @@
                 monitorPointMapper.insertSelective(monitorPoint);
             }else{
                 monitorPointMapper.updateByPrimaryKeySelective(monitorPoint);
+                // ������������������������������ ���redis���������������
+                refreshDevicesInRedis(monitorPoint.getId());
             }
         }
         catch (Exception ex){
             throw  ex;
         }
     }
+    /*
+      ������������������������������ ���redis���������������
+     */
+    private void  refreshDevicesInRedis(int monitorPointId){
+        Device device = new Device();
+        device.setMonitorPointId(monitorPointId);
+        List<Device> deviceList = deviceMapper.select(device);
+        if (!CollectionUtils.isEmpty(deviceList)){
+            List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(monitorPointId);
+            if (!CollectionUtils.isEmpty(orgIds)){
+                deviceList.stream().forEach(dev ->{
+                    if(!StringUtils.isNullOrEmpty(dev.getMac())){
+                        String key = "device_"+dev.getMac();
+                        // ��������������������� ������������redis
+                        Device simpleDevice = new Device();
+                        simpleDevice.setId(dev.getId());
+                        simpleDevice.setDeviceVersion(dev.getDeviceVersion());
+                        simpleDevice.setMac(dev.getMac());
+                        simpleDevice.setMonitorPointId(dev.getMonitorPointId());
+                        // ������������������������������������������������������������������
+                        simpleDevice.setOrganizationIds(orgIds);
+                        redisUtils.set(key,simpleDevice);
+                    }
+                });
+            }
+        }
 
+    }
     @Override
     public void deleteByIds(Integer... ids) {
         MonitorPoint monitorPoint = new MonitorPoint();
@@ -99,4 +154,31 @@
 		List<MonitorPoint> monitorPoints = monitorPointMapper.selectByExample(example);
 		return monitorPoints;
 	}
+
+    /**
+     *
+     * @param idList
+     * @return  {id:,state:}
+     */
+    @Override
+    public List<Map<String, String>> queryMonitroPointsState(List<Integer> idList) {
+        List<Map<String, String>> list = idList.stream().map( id -> {
+           Integer state = getStateFromRedis(id);
+           Map<String,String> stateMap = new HashMap<>();
+           stateMap.put("id",id.toString());
+           stateMap.put("state",state.toString());
+           return stateMap;
+        }).collect(Collectors.toList());
+        return list;
+    }
+
+    @Override
+	public List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId) {
+		MonitorPoint monitorPoint = new MonitorPoint();
+		monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE);
+		if (Constants.isNotSpecialOrgId(orgId)) {
+			monitorPoint.setOrganizationId(orgId);
+		}
+		return monitorPointMapper.select(monitorPoint);
+	}
 }

--
Gitblit v1.8.0