fengxiang
2018-01-26 60163c2fb5098fc522f8e80b131128d2c9a33e42
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
package com.moral.service.impl;
 
import com.moral.entity.Area;
import com.moral.entity.City;
import com.moral.entity.Province;
import com.moral.mapper.AreaMapper;
import com.moral.mapper.CityMapper;
import com.moral.mapper.ProvinceMapper;
import com.moral.service.AreaService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
 
import javax.annotation.Resource;
import java.util.List;
 
 
@Service
public class AreaServiceImpl implements AreaService{
    @Resource
    ProvinceMapper provinceMapper;
    @Resource
    CityMapper cityMapper;
    @Resource
    AreaMapper areaMapper;
 
    @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 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();
        }
        return fullName;
    }
}