From 8bb0e02e8fd166f35782870983fd2140142df409 Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Mon, 30 Nov 2020 15:52:12 +0800
Subject: [PATCH] 获取账户地图信息添加权限校验

---
 src/main/java/com/moral/service/impl/MapPathServiceImpl.java |  215 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 193 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/MapPathServiceImpl.java b/src/main/java/com/moral/service/impl/MapPathServiceImpl.java
index 2e70098..ccd8b93 100644
--- a/src/main/java/com/moral/service/impl/MapPathServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/MapPathServiceImpl.java
@@ -1,20 +1,21 @@
 package com.moral.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.moral.common.util.WebTokenUtils;
 import com.moral.controller.ScreenController;
-import com.moral.entity.Organization;
-import com.moral.entity.WebProvince;
-import com.moral.mapper.MapPathMapper;
+import com.moral.entity.*;
+import com.moral.mapper.*;
+import com.moral.service.DictionaryDataService;
 import com.moral.service.MapPathService;
+import com.moral.service.MonitorPointService;
 import com.moral.service.OrganizationService;
 import org.apache.log4j.Logger;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
 import javax.annotation.Resource;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @Service
 public class MapPathServiceImpl implements MapPathService {
@@ -23,6 +24,42 @@
     MapPathMapper mapPathMapper;
     @Resource
     OrganizationService organizationService;
+    @Resource
+    DictionaryDataService dictionaryDataService;
+    @Resource
+    MonitorPointService monitorPointService;
+    @Resource
+    ProvinceMapper provinceMapper;
+    @Resource
+    CityMapper cityMapper;
+    @Resource
+    AreaMapper areaMapper;
+    @Resource
+    MonitorPointMapper monitorPointMapper;
+    @Resource
+    RedisTemplate redisTemplate;
+
+
+
+    /*@Override
+    public List<WebProvince> getMapPath(String token) {
+        List<WebProvince> results;
+        try {
+            Map<String, Object> params = new HashMap<>();
+            Integer id = Integer.parseInt(WebTokenUtils.getIdBytoken(token));
+            Organization organization = organizationService.getOrganizationByAccountId(id);
+            Map<String, Object> regionCodeAndType = getRegionCodeAndTypeByOrg(organization);
+            String regionType = (String) regionCodeAndType.get("regionType");
+            String regionCode = (String) regionCodeAndType.get("regionCode");
+            params.put(regionType, regionCode);
+            results = mapPathMapper.getMapPathByRegionCode(params);
+            filterMapPath(organization, results);
+            return results;
+        } catch (Exception e) {
+            log.error(e.getMessage());
+        }
+        return null;
+    }*/
 
     /**
      * @Description: ������������token������������������������
@@ -33,25 +70,152 @@
      */
     @Override
     public List<WebProvince> getMapPath(String token) {
-        List<WebProvince> results;
-        try {
-            Map<String, Object> params = new HashMap<>();
-            Integer id = Integer.parseInt(WebTokenUtils.getIdBytoken(token));
-            Organization organization = organizationService.getOrganizationByAccountId(id);
-
-            Map<String, Object> regionCodeAndType = getRegionCodeAndTypeByOrg(organization);
-            String regionType = (String) regionCodeAndType.get("regionType");
-            String regionCode = (String) regionCodeAndType.get("regionCode");
-            params.put(regionType, regionCode);
-            results = mapPathMapper.getMapPathByRegionCode(params);
-            return results;
-        } catch (Exception e) {
-            log.error(e.getMessage());
+        Integer id = Integer.parseInt(WebTokenUtils.getIdBytoken(token));
+        Organization organization = organizationService.getOrganizationByAccountId(id);
+        List<MonitorPoint> monitorPoints;
+        //���������������������������������������������������������������������������������������������������������������org���������������������������
+        if (dictionaryDataService.querySupperOrgId().equals(organization.getId())) {
+            monitorPoints = monitorPointMapper.selectAll();
+        } else {
+            Integer parentOrgId = organization.getId();
+            Set<Integer> childOrgIds = organizationService.getChildOrganizationIds(parentOrgId);
+            childOrgIds.add(parentOrgId);
+            monitorPoints = monitorPointService.getMonitorPointsByOrganizationIds(childOrgIds);
         }
-        return null;
+
+        Set<Integer> provinceCodesSet = new HashSet<>();
+        Set<Integer> cityCodesSet = new HashSet<>();
+        Set<Integer> areaCodesSet = new HashSet<>();
+        for (MonitorPoint monitorPoint : monitorPoints) {
+            provinceCodesSet.add(monitorPoint.getProvinceCode());
+            cityCodesSet.add(monitorPoint.getCityCode());
+            areaCodesSet.add(monitorPoint.getAreaCode());
+        }
+
+        //Set������List������������
+        List<Integer> provinceCodes = new ArrayList<>(provinceCodesSet);
+        List<Integer> cityCodes = new ArrayList<>(cityCodesSet);
+        List<Integer> areaCodes = new ArrayList<>(areaCodesSet);
+        provinceCodes.removeAll(Collections.singleton(null));
+        cityCodes.removeAll(Collections.singleton(null));
+        areaCodes.removeAll(Collections.singleton(null));
+        Comparator<Integer> comparator = new ComparatorUtil();
+        Collections.sort(provinceCodes, comparator);
+        Collections.sort(cityCodes, comparator);
+        Collections.sort(areaCodes, comparator);
+
+        List<WebProvince> mapPath = new ArrayList<>();
+        for (Integer provinceCode : provinceCodes) {
+            String provinceCodeStr = String.valueOf(provinceCode);
+            String provinceJSON = (String) redisTemplate.opsForHash().get("province_Map", String.valueOf(provinceCode));
+            Province province = JSON.parseObject(provinceJSON).toJavaObject(Province.class);
+            WebProvince webProvince = new WebProvince(province.getProvinceCode(), province.getProvinceName(), new ArrayList<>());
+            for (Integer cityCode : cityCodes) {
+                String cityCodeStr = String.valueOf(cityCode);
+                //������������������������������
+                if (cityCodeStr.substring(0, 2).equals(provinceCodeStr.substring(0, 2))) {
+                    String cityJSON = (String) redisTemplate.opsForHash().get("city_Map", String.valueOf(cityCode));
+                    City city = JSON.parseObject(cityJSON).toJavaObject(City.class);
+                    WebCity WebCity = new WebCity(city.getCityCode(), city.getCityName(), provinceCode, new ArrayList<>());
+                    for (Integer areaCode : areaCodes) {
+                        String areaCodeStr = String.valueOf(areaCode);
+                        if (cityCodeStr.substring(0, 4).equals(areaCodeStr.substring(0, 4))) {
+                            String areaJSON = (String) redisTemplate.opsForHash().get("area_Map", String.valueOf(areaCode));
+                            Area area = JSON.parseObject(areaJSON).toJavaObject(Area.class);
+                            WebCity.getAreas().add(area);
+                        }
+                    }
+                    webProvince.getCities().add(WebCity);
+                }
+            }
+            mapPath.add(webProvince);
+        }
+        return mapPath;
     }
 
-    public static Map<String, Object> getRegionCodeAndTypeByOrg(Organization organization) {
+    @Override
+    public List<WebProvince> getMapPathTest(String token) {
+        long monitorStartTime = System.currentTimeMillis();
+        Integer id = Integer.parseInt(WebTokenUtils.getIdBytoken(token));
+        Organization organization = organizationService.getOrganizationByAccountId(id);
+        List<MonitorPoint> monitorPoints;
+        //���������������������������������������������������������������������������������������������������������������org���������������������������
+        if (dictionaryDataService.querySupperOrgId().equals(organization.getId())) {
+            monitorPoints = monitorPointMapper.selectAll();
+        } else {
+            Integer parentOrgId = organization.getId();
+            Set<Integer> childOrgIds = organizationService.getChildOrganizationIds(parentOrgId);
+            childOrgIds.add(parentOrgId);
+            monitorPoints = monitorPointService.getMonitorPointsByOrganizationIds(childOrgIds);
+        }
+        System.out.println("������������������������:" + String.valueOf(System.currentTimeMillis() - monitorStartTime));
+
+        long listStartTime = System.currentTimeMillis();
+        Set<Integer> provinceCodesSet = new HashSet<>();
+        Set<Integer> cityCodesSet = new HashSet<>();
+        Set<Integer> areaCodesSet = new HashSet<>();
+        for (MonitorPoint monitorPoint : monitorPoints) {
+            provinceCodesSet.add(monitorPoint.getProvinceCode());
+            cityCodesSet.add(monitorPoint.getCityCode());
+            areaCodesSet.add(monitorPoint.getAreaCode());
+        }
+
+        //Set������List������������
+        List<Integer> provinceCodes = new ArrayList<>(provinceCodesSet);
+        List<Integer> cityCodes = new ArrayList<>(cityCodesSet);
+        List<Integer> areaCodes = new ArrayList<>(areaCodesSet);
+        provinceCodes.removeAll(Collections.singleton(null));
+        cityCodes.removeAll(Collections.singleton(null));
+        areaCodes.removeAll(Collections.singleton(null));
+        Comparator<Integer> comparator = new ComparatorUtil();
+        Collections.sort(provinceCodes, comparator);
+        Collections.sort(cityCodes, comparator);
+        Collections.sort(areaCodes, comparator);
+        System.out.println("������������������������������������" + String.valueOf(System.currentTimeMillis() - listStartTime));
+
+        List<WebProvince> mapPath = new ArrayList<>();
+        long redisStartTime = System.currentTimeMillis();
+        for (Integer provinceCode : provinceCodes) {
+            String provinceCodeStr = String.valueOf(provinceCode);
+            Province province = new Province();
+            province.setProvinceCode(provinceCode);
+            province = provinceMapper.selectOne(province);
+            WebProvince webProvince = new WebProvince(province.getProvinceCode(), province.getProvinceName(), new ArrayList<>());
+            for (Integer cityCode : cityCodes) {
+                String cityCodeStr = String.valueOf(cityCode);
+                //������������������������������
+                if (cityCodeStr.substring(0, 2).equals(provinceCodeStr.substring(0, 2))) {
+                    City city = new City();
+                    city.setCityCode(cityCode);
+                    city = cityMapper.selectOne(city);
+                    WebCity WebCity = new WebCity(city.getCityCode(), city.getCityName(), provinceCode, new ArrayList<>());
+                    for (Integer areaCode : areaCodes) {
+                        String areaCodeStr = String.valueOf(areaCode);
+                        if (cityCodeStr.substring(0, 4).equals(areaCodeStr.substring(0, 4))) {
+                            Area area = new Area();
+                            area.setAreaCode(areaCode);
+                            area = areaMapper.selectOne(area);
+                            WebCity.getAreas().add(area);
+                        }
+                    }
+                    webProvince.getCities().add(WebCity);
+                }
+            }
+            mapPath.add(webProvince);
+        }
+        System.out.println("redis������������:" + String.valueOf(System.currentTimeMillis() - redisStartTime));
+        return mapPath;
+    }
+
+
+    /**
+     * @Description: ������������id���������������������������������������������
+     * @Param: [organization]
+     * @return: java.util.Map<java.lang.String                               ,                               java.lang.Object>
+     * @Author: ������������
+     * @Date: 2020/11/30
+     */
+    private Map<String, Object> getRegionCodeAndTypeByOrg(Organization organization) {
         Map<String, Object> result = new HashMap<>();
         String regionCode = "";
         String regionType = "";
@@ -77,4 +241,11 @@
         result.put("regionType", regionType);
         return result;
     }
+
+    class ComparatorUtil implements Comparator<Integer> {
+        @Override
+        public int compare(Integer o1, Integer o2) {
+            return o1 - o2;
+        }
+    }
 }

--
Gitblit v1.8.0