package com.mzl.flower.service.system;
|
|
import com.alibaba.fastjson.JSON;
|
import com.mzl.flower.base.cache.StringCacheClient;
|
import com.mzl.flower.dto.response.system.AreaDTO;
|
import com.mzl.flower.service.BaseService;
|
import com.mzl.flower.utils.LocalUtil;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Service
|
@Transactional
|
public class ProvinceService extends BaseService {
|
|
@Autowired
|
private StringCacheClient stringCacheClient;
|
|
public String getChineseArea() {
|
String result = stringCacheClient.get("CHINA_AREA_DATA");
|
if (StringUtils.isNotBlank(result)) {
|
return result;
|
} else {
|
return refreshChineseData();
|
}
|
}
|
|
public String refreshChineseData(){
|
LocalUtil lu = LocalUtil.getNewInstance();
|
String country = "中国";
|
List<String> provinces = lu.getProvinces(country);
|
List<AreaDTO> pLs = new ArrayList<>();
|
for (String province : provinces) {
|
AreaDTO p = new AreaDTO();
|
pLs.add(p);
|
p.setCode(province);
|
p.setName(province);
|
List<String> cities = lu.getCities(country, province);
|
List<AreaDTO> cLs = new ArrayList<>();
|
p.setChildren(cLs);
|
for (String city : cities) {
|
AreaDTO c = new AreaDTO();
|
cLs.add(c);
|
c.setCode(city);
|
c.setName(city);
|
List<String> regions = lu.getDistrict(country, province, city);
|
List<AreaDTO> rLs = new ArrayList<>();
|
c.setChildren(rLs);
|
for (String region : regions) {
|
AreaDTO r = new AreaDTO();
|
rLs.add(r);
|
r.setCode(region);
|
r.setName(region);
|
}
|
}
|
}
|
String jj = JSON.toJSONString(pLs);
|
stringCacheClient.set("CHINA_AREA_DATA", jj);
|
return jj;
|
}
|
}
|