From 8ea24dd5fb6e1b24d3a7ef62a0060ef516f8f759 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Thu, 24 May 2018 08:58:16 +0800
Subject: [PATCH] 更新 监测点 数据

---
 src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java |   97 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 93 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 509b9d3..200db4e 100644
--- a/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -1,5 +1,6 @@
 package com.moral.service.impl;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
@@ -8,15 +9,22 @@
 
 import javax.annotation.Resource;
 
+import org.apache.commons.collections.CollectionUtils;
+import org.springframework.stereotype.Service;
+
 import com.github.pagehelper.PageHelper;
 import com.moral.common.bean.Constants;
 import com.moral.common.bean.PageBean;
-import com.moral.common.util.*;
-import org.springframework.stereotype.Service;
-
+import com.moral.common.util.ExampleUtil;
+import com.moral.common.util.RedisUtils;
+import com.moral.common.util.StringUtils;
+import com.moral.common.util.ValidateUtil;
+import com.moral.entity.Device;
 import com.moral.entity.MonitorPoint;
+import com.moral.mapper.DeviceMapper;
 import com.moral.mapper.MonitorPointMapper;
 import com.moral.service.MonitorPointService;
+
 import tk.mybatis.mapper.entity.Example;
 import tk.mybatis.mapper.entity.Example.Criteria;
 
@@ -24,6 +32,8 @@
 public class MonitorPointServiceImpl implements MonitorPointService {
     @Resource
     private MonitorPointMapper monitorPointMapper;
+    @Resource
+    private DeviceMapper deviceMapper;
     @Resource
     RedisUtils redisUtils;
     private static Class ENTITY_CLASS = MonitorPoint.class;
@@ -34,6 +44,7 @@
     }
     @Override
     public List<MonitorPoint> queryWithStateByMap(Map<String, Object> params){
+        params.put("isDelete",Constants.IS_DELETE_FALSE);
         List<MonitorPoint> monitorPointList = monitorPointMapper.selectByMap(params);
         for(MonitorPoint monitorPoint:monitorPointList){
             Integer state = getStateFromRedis(monitorPoint.getId());
@@ -84,14 +95,51 @@
                 monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE);
                 monitorPointMapper.insertSelective(monitorPoint);
             }else{
+                MonitorPoint queryMonitorPoint = new MonitorPoint();
+                queryMonitorPoint.setId(monitorPoint.getId());
+                queryMonitorPoint.setOrganizationId(monitorPoint.getOrganizationId());
+                // num = 1,���������������,������������������������
+                Integer num =  monitorPointMapper.selectCount(queryMonitorPoint);
+                boolean needRefreshCach = (num!=1);
                 monitorPointMapper.updateByPrimaryKeySelective(monitorPoint);
+                if(needRefreshCach){
+                    // ������������������������������ ���redis���������������
+                    refreshDevicesInRedis(monitorPoint.getId());
+                }
             }
         }
         catch (Exception ex){
             throw  ex;
         }
     }
+    /*
+      ������������������������������ ���redis���������������
+     */
+    private void  refreshDevicesInRedis(int monitorPointId){
+        Device queryDevice = new Device();
+        queryDevice.setMonitorPointId(monitorPointId);
+        List<Device> deviceList = deviceMapper.select(queryDevice);
+        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());
+                        // ������������������������������������������������������������������
+                        dev.setOrganizationIds(orgIds);
+                        redisUtils.set(key,dev);
+                    }
+                });
+            }
+        }
 
+    }
     @Override
     public void deleteByIds(Integer... ids) {
         MonitorPoint monitorPoint = new MonitorPoint();
@@ -115,10 +163,25 @@
 		Criteria criteria = example.createCriteria();
 
 		criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andLike("name", "%" + name + "%");
-		example.or().andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andCondition("getPY(name) like ", "%" + name + "%");
+		example.or().andEqualTo("isDelete", Constants.IS_DELETE_FALSE)
+				.andCondition("getPY(" + getReplaceStr("name") + ") like ", "%" + name + "%");
 
 		List<MonitorPoint> monitorPoints = monitorPointMapper.selectByExample(example);
 		return monitorPoints;
+	}
+
+	private String getReplaceStr(String name){
+        List<String[]> list = new ArrayList<String[]>();
+        list.add(new String[]{"���",""});
+        list.add(new String[]{"���",""});
+        for (String[] string : list) {
+        	name = replace(name,string[0],string[1]);
+		}
+		return name;
+	}
+	
+	private  String replace(String name,String fromStr,String toStr){
+		return "REPLACE (" + name + ",'" + fromStr + "','" + toStr + "')";
 	}
 
     /**
@@ -137,4 +200,30 @@
         }).collect(Collectors.toList());
         return list;
     }
+
+    @Override
+	public List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId) {
+		Example example = new Example(MonitorPoint.class);
+		Criteria criteria = example.createCriteria();
+		
+		criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
+		if (Constants.isNotSpecialOrgId(orgId)) {
+			criteria.andEqualTo("organizationId", orgId);
+		}
+		example.orderBy("name").asc();
+		return monitorPointMapper.selectByExample(example);
+	}
+	@Override
+	public List<MonitorPoint> getMonitorPointsByRegion(Map<String, Object> parameters) {
+		Example example = new Example(MonitorPoint.class);
+		Criteria criteria = example.createCriteria();
+		
+		criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
+		criteria.andEqualTo(parameters.get("name").toString(), parameters.get("value"));
+		return monitorPointMapper.selectByExample(example);
+	}
+	@Override
+    public List<Integer> queryVersionsById(Integer id){
+        return  monitorPointMapper.selectVersionsById(id);
+    }
 }

--
Gitblit v1.8.0