kaiyu
2020-11-30 8bb0e02e8fd166f35782870983fd2140142df409
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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.*;
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.*;
 
@Service
public class MapPathServiceImpl implements MapPathService {
    public static Logger log = Logger.getLogger(MapPathServiceImpl.class);
    @Resource
    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获取用户地图权限
     * @Param: [token]
     * @return: java.util.List<com.moral.entity.WebProvince>
     * @Author: 下雨听风
     * @Date: 2020/11/26
     */
    @Override
    public List<WebProvince> getMapPath(String token) {
        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);
        }
 
        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;
    }
 
    @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 = "";
        Long villageCode = organization.getVillageCode();
        Long townCode = organization.getTownCode();
        Integer areaCode = organization.getAreaCode();
        Integer cityCode = organization.getCityCode();
        Integer provinceCode = organization.getProvinceCode();
 
        if (!ObjectUtils.isEmpty(areaCode)) {
            regionCode = String.valueOf(areaCode);
            regionType = "areaCode";
        } else if (!ObjectUtils.isEmpty(cityCode)) {
            regionCode = String.valueOf(cityCode);
            regionType = "cityCode";
        } else if (!ObjectUtils.isEmpty(provinceCode)) {
            regionCode = String.valueOf(provinceCode);
            regionType = "provinceCode";
        } else {
            return null;
        }
        result.put("regionCode", regionCode);
        result.put("regionType", regionType);
        return result;
    }
 
    class ComparatorUtil implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    }
}