| | |
| | | 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; |
| | | |
| | | |
| | |
| | | CityMapper cityMapper; |
| | | @Resource |
| | | AreaMapper areaMapper; |
| | | |
| | | @Resource |
| | | private TownMapper townMapper; |
| | | |
| | | @Resource |
| | | private VillageMapper villageMapper; |
| | | |
| | | @Override |
| | | public List<Province> getProvinces() { |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String selectFullNameByCode(Integer code){ |
| | | String codeStr = code.toString(); |
| | | String fullName = ""; |
| | | // 此时为地区code |
| | | if(!codeStr.endsWith("00")){ |
| | | String provinceCode = codeStr.substring(0,2)+"0000"; |
| | | Province province = provinceMapper.selectByPrimaryKey(Integer.valueOf(provinceCode)); |
| | | String cityCode = codeStr.substring(0,4)+"00"; |
| | | City city = cityMapper.selectByPrimaryKey(Integer.valueOf(cityCode)); |
| | | Area area = areaMapper.selectByPrimaryKey(code); |
| | | fullName = province.getProvinceName()+city.getCityName()+area.getAreaName(); |
| | | } else if(!codeStr.endsWith("0000")){ |
| | | // 此时为 地级市code |
| | | String provinceCode = codeStr.substring(0,2)+"0000"; |
| | | Province province = provinceMapper.selectByPrimaryKey(Integer.valueOf(provinceCode)); |
| | | City city = cityMapper.selectByPrimaryKey(code); |
| | | fullName = province.getProvinceName()+city.getCityName(); |
| | | } else { |
| | | // 此时为 省code |
| | | Province province = provinceMapper.selectByPrimaryKey(code); |
| | | fullName = province.getProvinceName(); |
| | | 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; |
| | | 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); |
| | | } |
| | | } |