CityServiceImpl.java
7.56 KB
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
package com.diligrp.website.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.diligrp.website.domain.City;
import com.diligrp.website.manager.CityManager;
import com.diligrp.website.service.CityService;
import com.diligrp.website.util.domain.Result;
import com.diligrp.website.util.redis.RedisUtil;
@Component("cityService")
public class CityServiceImpl implements CityService {
private static final Logger logger = LoggerFactory.getLogger(CityServiceImpl.class);
private final static String cityRedisKey = "dili_redis_city";
private final static String cityRedisKeyOne = "dili_redis_cityone";
private final static String countryRedisKeyOne = "dili_redis_country";
private final static Integer cityRedisKeyTime = 86400;
private final static String cityRedisKeyOther = "dili_redis_city_other";
private static final String countryCityRedisKey = "dili_redis_country_city_";
@Resource
private CityManager cityManager;
@Resource(name = "redisUtils")
private RedisUtil redisUtil;
@SuppressWarnings("unchecked")
public Result getCityListByParentId(Integer pid) {
List<City> list = redisUtil.getObject(cityRedisKey + pid, List.class);
List<City> city = null;
if (CollectionUtils.isEmpty(list)) {
city = cityManager.getCityListByParentId(pid);
if (!CollectionUtils.isEmpty(city)) {
List<City> cityList = new ArrayList<City>();
for (int i = 0; i < city.size(); i++) {
if (city.get(i).getRegionName().indexOf("其他") != -1) {
continue;
}
cityList.add(city.get(i));
}
redisUtil.setObjectByExpire(cityRedisKey + pid, cityList, cityRedisKeyTime);
return Result.create().addResult("city", cityList);
}
} else {
city = new ArrayList<City>();
for (int i = 0; i < list.size(); i++) {
City cty = new City();
Map<String, Object> map = (Map<String, Object>) list.get(i);
cty.setRegionId((Integer) map.get("regionId"));
cty.setRegionName((String) map.get("regionName"));
cty.setLevel((Integer) map.get("level"));
cty.setParentId((Integer) map.get("parentId"));
cty.setSort((Integer) map.get("sort"));
city.add(cty);
}
}
return Result.create().addResult("city", city);
}
public void getParentCityListById(int cityId, List<City> cityList) {
City city = redisUtil.getObject(cityRedisKeyOne + cityId, City.class);
if (city == null) {
List<City> list = cityManager.getParentCityListById(cityId);
city = list.get(0);
redisUtil.setObjectByExpire(cityRedisKeyOne + cityId, city, cityRedisKeyTime);
}
cityList.add(city);
if (city.getParentId() != 0) {
getParentCityListById(city.getParentId(), cityList);
}
}
/*
* (non-Javadoc)
*
* @see com.diligrp.website.service.CityService#getCityList(int)
*/
public Result getCityList(int cityId) {
try {
List<City> list = null;
logger.debug("方法:getCityList,参数:" + cityId + ",递归查询所有的城市信息");
City city = redisUtil.getObject(cityRedisKeyOne + cityId, City.class);
if (city == null) {
list = cityManager.getParentCityListById(cityId);
if (list.isEmpty()) {
return Result.create().addResult("city", null);
}
city = list.get(0);
redisUtil.setObjectByExpire(cityRedisKeyOne + cityId, city, cityRedisKeyTime);
}
List<City> cityList = new ArrayList<City>();
if (city.getParentId() == 0) {
cityList.add(city);
} else {
getParentCityListById(cityId, cityList);
}
return Result.create().addResult("city", cityList);
} catch (Exception e) {
logger.error("方法:getCityList,错误信息:", e);
e.printStackTrace();
return null;
}
}
@Override
public String getCountryAddress(Integer cityId) {
try {
String address = redisUtil.getString(countryRedisKeyOne + cityId);
if (!StringUtils.isBlank(address)) {
return address;
}
StringBuffer sb = new StringBuffer();
List<String> country = new ArrayList<String>();
List<City> list = null;
logger.debug("方法:getCityList,参数:" + cityId + ",递归查询所有的城市信息");
City city = redisUtil.getObject(cityRedisKeyOne + cityId, City.class);
if (city == null) {
list = cityManager.getParentCityListById(cityId);
if (list.isEmpty()) {
return null;
}
city = list.get(0);
redisUtil.setObjectByExpire(cityRedisKeyOne + cityId, city, cityRedisKeyTime);
}
if (city.getParentId() == 0) {
country.add(city.getRegionName());
} else {
getParentCountryAddress(cityId, country);
}
country.add("中国");
for (int i = country.size() - 1; i >= 0; i--) {
sb.append(country.get(i));
}
redisUtil.setStringByExpire(countryRedisKeyOne + cityId, sb.toString(), cityRedisKeyTime);
return sb.toString();
} catch (Exception e) {
logger.error("方法:getCityList,错误信息:", e);
e.printStackTrace();
return null;
}
}
public void getParentCountryAddress(int cityId, List<String> country) {
City city = redisUtil.getObject(cityRedisKeyOne + cityId, City.class);
if (city == null) {
List<City> list = cityManager.getParentCityListById(cityId);
city = list.get(0);
redisUtil.setObjectByExpire(cityRedisKeyOne + cityId, city, cityRedisKeyTime);
}
country.add(city.getRegionName());
if (city.getParentId() != 0) {
getParentCountryAddress(city.getParentId(), country);
}
}
@Override
public Result getCityByText(String cityName, int cityLevel) {
List<City> cityList = cityManager.getCityByText(cityName, cityLevel);
return Result.create().addResult("city", cityList);
}
public Result getOtherCityListByParentId(Integer pid) {
List<City> list = redisUtil.getObject(cityRedisKeyOther + pid, List.class);
List<City> city = null;
if (CollectionUtils.isEmpty(list)) {
city = cityManager.getCityListByParentId(pid);
if (!CollectionUtils.isEmpty(city)) {
redisUtil.setObjectByExpire(cityRedisKeyOther + pid, city, cityRedisKeyTime);
}
} else {
city = new ArrayList<City>();
for (int i = 0; i < list.size(); i++) {
City cty = new City();
Map<String, Object> map = (Map<String, Object>) list.get(i);
cty.setRegionId((Integer) map.get("regionId"));
cty.setRegionName((String) map.get("regionName"));
cty.setLevel((Integer) map.get("level"));
cty.setParentId((Integer) map.get("parentId"));
cty.setSort((Integer) map.get("sort"));
city.add(cty);
}
}
return Result.create().addResult("city", city);
}
public Result getCityListByCityIds(String cityIds) {
List<City> cityList = null;
if (StringUtils.isEmpty(cityIds)) {
return null;
} else {
String[] array = cityIds.split(",");
List idsList = new ArrayList();
for (int i = 0; i < array.length; i++) {
idsList.add(array[i]);
}
cityList = cityManager.getCityListByCityIds(idsList);
}
if (CollectionUtils.isEmpty(cityList)) {
return null;
} else {
return Result.create().addResult("city", cityList);
}
}
@SuppressWarnings("unchecked")
@Override
public List<City> getCityListByCountryId(Long countryId) {
String key = countryCityRedisKey + countryId;
String json = this.redisUtil.getString(key);
List<City> cities = null;
if (TextUtils.isEmpty(json)) {
cities = this.cityManager.getCityListByCountryId(countryId);
this.redisUtil.setObject(key, cities);
return cities;
}
cities = JSON.parseArray(json, City.class);
return cities;
}
}