package com.moral.service.impl;
|
|
import com.moral.common.exception.BusinessException;
|
import com.moral.entity.Area;
|
import com.moral.entity.City;
|
import com.moral.entity.Province;
|
import com.moral.entity.Town;
|
import com.moral.entity.Village;
|
import com.moral.mapper.AreaMapper;
|
import com.moral.mapper.CityMapper;
|
import com.moral.mapper.ProvinceMapper;
|
import com.moral.mapper.TownMapper;
|
import com.moral.mapper.VillageMapper;
|
import com.moral.service.AreaService;
|
import org.springframework.stereotype.Service;
|
import tk.mybatis.mapper.entity.Example;
|
|
import javax.annotation.Resource;
|
import javax.validation.constraints.NotNull;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
@Service
|
public class AreaServiceImpl implements AreaService{
|
@Resource
|
ProvinceMapper provinceMapper;
|
@Resource
|
CityMapper cityMapper;
|
@Resource
|
AreaMapper areaMapper;
|
|
@Resource
|
private TownMapper townMapper;
|
|
@Resource
|
private VillageMapper villageMapper;
|
|
@Override
|
public List<Province> getProvinces() {
|
return provinceMapper.selectAll();
|
}
|
|
@Override
|
public List<City> getCities(int provinceCode) {
|
Example example = new Example(City.class);
|
example.or().andEqualTo("provinceCode",provinceCode);
|
return cityMapper.selectByExample(example);
|
}
|
|
@Override
|
public List<Area> getAreas(int cityCode) {
|
Example example = new Example(Area.class);
|
example.or().andEqualTo("cityCode",cityCode);
|
return areaMapper.selectByExample(example);
|
}
|
|
/**
|
* 通过地区编码获取 中文全程,例如 江苏省 苏州市 昆山市
|
* @param code 可以是省 市 区 到地区编码
|
* @return
|
*/
|
@Override
|
public String queryFullNameByCode(Long code){
|
StringBuilder fullName = new StringBuilder();
|
List<Number> codeList = regionCodeToList(code);
|
for (int num = 0;num<codeList.size();num++){
|
int codeLevel = num+1;
|
Number currentCode = codeList.get(num);
|
switch (codeLevel) {
|
case 1:
|
Province province = provinceMapper.selectByPrimaryKey(currentCode);
|
if (province !=null) {
|
String provinceName = province.getProvinceName();
|
provinceName = provinceName!=null ? provinceName.replace(" ","") : "";
|
fullName.append(provinceName);
|
}
|
break;
|
case 2:
|
City city = cityMapper.selectByPrimaryKey(currentCode);
|
if(city!=null) {
|
String cityName = city.getCityName();
|
cityName = cityName!=null ? cityName.replace(" ",""):"";
|
fullName.append(" ");
|
fullName.append(cityName);
|
}
|
break;
|
case 3:
|
Area area = areaMapper.selectByPrimaryKey(currentCode);
|
if(area!=null){
|
String areaName = area.getAreaName();
|
areaName = areaName!=null ? areaName.replace(" ",""):"";
|
fullName.append(" ");
|
fullName.append(areaName);
|
}
|
break;
|
case 4:
|
Town town = townMapper.selectByPrimaryKey(currentCode);
|
if(town!=null){
|
String townName = town.getTownName();
|
townName = townName!=null ? townName.replace(" ",""):"";
|
fullName.append(" ");
|
fullName.append(townName);
|
}
|
break;
|
default:
|
Village village = villageMapper.selectByPrimaryKey(currentCode);
|
if(village!=null) {
|
String villageName = village.getVillageName();
|
villageName = villageName!=null ? villageName.replace(" ",""):"";
|
fullName.append(" ");
|
fullName.append(villageName);
|
}
|
break;
|
}
|
}
|
return fullName.toString();
|
}
|
private List<Number> regionCodeToList(Long code) {
|
List<Number> codeList = new ArrayList();
|
if(code != null){
|
String codeStr = code.toString();
|
int codeLevel = 0;
|
codeLevel =getCodeLevel(codeStr);
|
int codeLevelTemp = 1;
|
do {
|
String currentCodeStr = getCodeByLevel(codeStr,codeLevelTemp);
|
Number currentCode = 0;
|
if(codeLevelTemp < 4) {
|
currentCode = Integer.parseInt(currentCodeStr);
|
}else{
|
currentCode = Long.parseLong(currentCodeStr);
|
}
|
codeList.add(currentCode);
|
codeLevelTemp++;
|
}while (codeLevelTemp <= codeLevel);
|
}
|
return codeList;
|
}
|
private int getCodeLevel(String code){
|
return isProvinceCode(code) ? 1 :
|
isCityCode(code) ? 2 :
|
isAreaCode(code) ? 3 :
|
isTownCode(code) ? 4 : 5;
|
}
|
static private String[] SpecialTwonCodes = new String[]{"653130103001"};
|
static private boolean isSpecialTwonCode(String code){
|
for (String codeStr:SpecialTwonCodes){
|
if(codeStr.equals(code)){
|
return true;
|
}
|
}
|
return false;
|
}
|
private String getCodeByLevel(@NotNull String sourceCode,int codeLevel){
|
String zeroStr = "0000";
|
int totalCount = 0;
|
int effectiveCount = 0;
|
if(codeLevel <4){
|
totalCount = 6;
|
effectiveCount = codeLevel == 1 ? 2:
|
codeLevel == 2 ? 4:6;
|
} else{
|
totalCount = 12;
|
effectiveCount = codeLevel == 4 ? 9:12;
|
}
|
//特例
|
if(codeLevel==4&&isSpecialTwonCode(sourceCode)){
|
return sourceCode;
|
}
|
if(totalCount == effectiveCount && sourceCode.length() == totalCount){
|
return sourceCode;
|
}else {
|
if (sourceCode.length()<totalCount){
|
throw new BusinessException("totalCount is more than the length of sourceCode");
|
}
|
return sourceCode.substring(0,effectiveCount)+zeroStr.substring(0,totalCount-effectiveCount);
|
}
|
}
|
private boolean isProvinceCode(String code){
|
return code.length() == 6 && code.endsWith("0000");
|
}
|
private boolean isCityCode(String code){
|
return code.length() == 6 && code.endsWith("00");
|
}
|
private boolean isAreaCode(String code){
|
return code.length() == 6 && !code.endsWith("00");
|
}
|
private boolean isTownCode(String code){
|
return code.length() == 12 && (code.endsWith("000")||isSpecialTwonCode(code));
|
}
|
private boolean isVillageCode(String code){
|
return code.length() == 12 && !code.endsWith("000");
|
}
|
@Override
|
public List<Town> getTowns(Integer areaCode) {
|
Example example = new Example(Town.class);
|
example.or().andEqualTo("areaCode",areaCode);
|
return townMapper.selectByExample(example);
|
}
|
|
@Override
|
public List<Village> getVillages(Long townCode) {
|
Example example = new Example(Village.class);
|
example.createCriteria().andEqualTo("townCode",townCode);
|
return villageMapper.selectByExample(example);
|
}
|
}
|