From 2bfc4ed9012c5cae2a01bc67bdce68eeb59dc0b8 Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Tue, 03 Sep 2019 13:38:19 +0800
Subject: [PATCH] 完善排名

---
 src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java |  212 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 186 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java b/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
index 1e9dd28..0f8c004 100644
--- a/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -2,20 +2,28 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 import javax.annotation.Resource;
+import javax.validation.constraints.NotNull;
 
+import com.moral.mapper.DictionaryDataMapper;
+import com.moral.mapper.OrganizationMapper;
 import org.apache.commons.collections.CollectionUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.util.ObjectUtils;
 
 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.ParameterUtils;
 import com.moral.common.util.RedisUtils;
 import com.moral.common.util.StringUtils;
 import com.moral.common.util.ValidateUtil;
@@ -23,7 +31,9 @@
 import com.moral.entity.MonitorPoint;
 import com.moral.mapper.DeviceMapper;
 import com.moral.mapper.MonitorPointMapper;
+import com.moral.service.DeviceService;
 import com.moral.service.MonitorPointService;
+import com.moral.service.OrganizationService;
 
 import tk.mybatis.mapper.entity.Example;
 import tk.mybatis.mapper.entity.Example.Criteria;
@@ -35,7 +45,18 @@
     @Resource
     private DeviceMapper deviceMapper;
     @Resource
+    private OrganizationMapper orgMapper;
+    @Resource
     RedisUtils redisUtils;
+    @Resource
+    DictionaryDataMapper dictionaryDataMapper;
+
+    @Resource
+    private DeviceService deviceService;
+    
+    @Resource
+    private OrganizationService organizationService;
+    
     private static Class ENTITY_CLASS = MonitorPoint.class;
     @Override
     public List<MonitorPoint> getMonitorPointsByAreaName(Map<String, Object> parameters) {
@@ -44,12 +65,20 @@
     }
     @Override
     public List<MonitorPoint> queryWithStateByMap(Map<String, Object> params){
-        List<MonitorPoint> monitorPointList = monitorPointMapper.selectByMap(params);
-        for(MonitorPoint monitorPoint:monitorPointList){
-            Integer state = getStateFromRedis(monitorPoint.getId());
-            monitorPoint.setState(state);
+        params.put("isDelete",Constants.IS_DELETE_FALSE);
+        Object orgIdObj = params.get("orgId");
+        List<MonitorPoint> monitorPointList = null;
+        if(orgIdObj != null) {
+            Integer orgId = Integer.parseInt(orgIdObj.toString());
+            List<Integer> orgIds = orgMapper.selectLowerOrgIds(orgId);
+            params.put("orgIds",orgIds);
+            monitorPointList = monitorPointMapper.selectByMap(params);
+//            for(MonitorPoint monitorPoint:monitorPointList){
+//                Integer state = getStateFromRedis(monitorPoint.getId());
+//                monitorPoint.setState(state);
+//            }
         }
-        return monitorPointList;
+        return monitorPointList == null ? new ArrayList<>() : monitorPointList;
     }
     private Integer getStateFromRedis(Integer monitorPointId){
         StringBuilder key = new StringBuilder();
@@ -69,13 +98,6 @@
     public PageBean queryByPageBean(PageBean pageBean) {
         Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
         List<Example.Criteria> criteriaList = example.getOredCriteria();
-        if(criteriaList!=null&&criteriaList.size()>0){
-            for(Example.Criteria cri : criteriaList){
-                cri.andNotEqualTo("isDelete", Constants.IS_DELETE_TRUE);
-            }
-        }else {
-            example.or().andNotEqualTo("isDelete",Constants.IS_DELETE_TRUE);
-        }
         PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize());
         List<MonitorPoint> monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example);
         return new PageBean(monitorPointList);
@@ -94,9 +116,17 @@
                 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);
-                // ������������������������������ ���redis���������������
-                refreshDevicesInRedis(monitorPoint.getId());
+                if(needRefreshCach){
+                    // ������������������������������ ���redis���������������
+                    refreshDevicesInRedis(monitorPoint.getId());
+                }
             }
         }
         catch (Exception ex){
@@ -107,24 +137,24 @@
       ������������������������������ ���redis���������������
      */
     private void  refreshDevicesInRedis(int monitorPointId){
-        Device device = new Device();
-        device.setMonitorPointId(monitorPointId);
-        List<Device> deviceList = deviceMapper.select(device);
+        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());
+//                        // ��������������������� ������������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);
+                        dev.setOrganizationIds(orgIds);
+                        redisUtils.set(key,dev);
                     }
                 });
             }
@@ -192,6 +222,21 @@
         return list;
     }
 
+    /**
+     * ������������������������������������
+     * @param orgId
+     * @return
+     */
+    @Override
+    public Integer countOfSubOrgs(@NotNull  Integer orgId){
+        Example example = new Example(ENTITY_CLASS);
+        //���������������������������
+        if(!dictionaryDataMapper.isSupperOrgId(orgId)){
+            List<Integer> orgIds = orgMapper.selectLowerOrgIds(orgId);
+            example.or().andIn("organizationId",orgIds);
+        }
+        return monitorPointMapper.selectCountByExample(example);
+    }
     @Override
 	public List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId) {
 		Example example = new Example(MonitorPoint.class);
@@ -199,9 +244,124 @@
 		
 		criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
 		if (Constants.isNotSpecialOrgId(orgId)) {
-			criteria.andEqualTo("organizationId", orgId);
+			//criteria.andEqualTo("organizationId", orgId);
+			Set<Integer> organizationIds = organizationService.getChildOrganizationIds(orgId);
+			criteria.andIn("organizationId", organizationIds);
+
 		}
 		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);
+    }
+    @Override
+    public MonitorPoint queryMonitorPointById(Integer mpointId) {
+        return  this.monitorPointMapper.selectByPrimaryKey(mpointId);
+    }
+
+    @Override
+	public List<MonitorPoint> getMonitorPointsAndDevicesByRegion(Map<String, Object> parameters) {
+    	//������������
+		Object organizationId = parameters.remove("organizationId");
+		ValidateUtil.notNull(organizationId, "param.is.null");
+		ValidateUtil.notNull(parameters.get("regionCode"), "param.is.null");
+
+		//������������������
+		ParameterUtils.getRegionType4RegionCode(parameters);
+		
+		Example example = new Example(MonitorPoint.class);
+		Criteria criteria = example.createCriteria();
+		criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
+		if (Constants.isNotSpecialOrgId(Integer.valueOf(organizationId.toString()))) {
+			//criteria.andEqualTo("organizationId", organizationId);
+			Set<Integer> organizationIds = organizationService.getChildOrganizationIds(Integer.valueOf(organizationId.toString()));
+			criteria.andIn("organizationId", organizationIds);
+		}
+		criteria.andEqualTo(parameters.get("regionType") + "Code", parameters.remove("regionCode"));
+		
+		//���������������������
+		List<MonitorPoint> monitorPoints = monitorPointMapper.selectByExample(example);
+		//���������������������������������
+		Iterator<MonitorPoint> iterator = monitorPoints.iterator();
+		while (iterator.hasNext()) {
+			MonitorPoint monitorPoint = iterator.next();
+			List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPoint.getId());
+			if (ObjectUtils.isEmpty(devices)) {
+				iterator.remove();
+			} else {
+				monitorPoint.setDevices(devices);
+			}
+			
+		}
+		return monitorPoints;
+	}
+
+    @SuppressWarnings("unchecked")
+	@Override
+	public Collection<Object> getDevicesStateByRegion(Map<String, Object> parameters) {
+    	//������������
+		Object organizationId = parameters.remove("organizationId");
+		ValidateUtil.notNull(organizationId, "param.is.null");
+		ValidateUtil.notNull(parameters.get("regionCode"), "param.is.null");
+
+		//������������������
+		ParameterUtils.getRegionType4RegionCode(parameters);
+		
+		if (Constants.isNotSpecialOrgId(Integer.valueOf(organizationId.toString()))) {
+			Set<Integer> organizationIds = organizationService.getChildOrganizationIds(Integer.valueOf(organizationId.toString()));
+			parameters.put("orgIds", organizationIds);
+		}
+		List<Map<String, Object>> monitorPoints  = deviceMapper.getDevicesStateByRegion(parameters);
+		Map<String, Object> result = new HashMap<String, Object>();
+		Map<String,Object> device;
+		List<Map<String, Object>> devices;
+		for (Map<String, Object> map : monitorPoints) {
+			String id = map.get("id").toString();
+
+			device = new HashMap<String,Object>();
+			device.put("id", map.remove("deviceId"));
+			device.put("name", map.remove("deviceName"));
+			device.put("state", map.remove("state"));
+			device.put("mac", map.remove("mac"));
+
+			if (result.containsKey(id)) {
+				Map<String, Object> monitorPoint = (Map<String, Object>) result.get(id);
+				devices = (List<Map<String, Object>>) monitorPoint.get("devices");
+			} else {
+				devices = new ArrayList<Map<String, Object>>();
+				result.put(id, map);
+			}
+			devices.add(device);
+			map.put("devices", devices);
+			result.put(id, map);
+		}
+		
+		return result.values();
+	}
+    
+    @Override
+    public void isCompensateCalculation(Map<String, Object> parameters) {
+		MonitorPoint monitorPoint = monitorPointMapper.selectByPrimaryKey(Integer.valueOf(parameters.get("monitorPointId").toString()));
+		if (Integer.valueOf(320581).equals(monitorPoint.getAreaCode())) {
+			parameters.put("compensate", true);
+		}
+    	
+	}
+	@Override
+	public Map<String, Object> selectAllById(String id) {
+		int id2 = Integer.parseInt(id);
+		Map<String, Object> map = monitorPointMapper.selectAllById(id2);
+		return map;
+	}
 }

--
Gitblit v1.8.0