CityServiceImpl.java 7.56 KB
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;
	}

}