kaiyu
2020-11-24 1d7e1b771160dd8b7f25765e402ce25cac676641
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
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);
    }
}