-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceWithoutRedisAdapter.go
139 lines (124 loc) · 3.46 KB
/
serviceWithoutRedisAdapter.go
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
package division
import "errors"
// ListProvince liat all provinces
func (s WithoutRedisAdapter) ListProvince() []Division {
return provinces
}
// GetProvince get province information
func (s WithoutRedisAdapter) GetProvince(code int) (Division, error) {
for _, division := range s.ListProvince() {
if code == division.ProvinceCode {
return division, nil
}
}
return Division{}, errors.New("province code error")
}
// GetCity get city information
func (s WithoutRedisAdapter) GetCity(code int) (Division, error) {
divisions := cities[code/100]
for _, v := range divisions {
if v.CityCode == code {
return Division{
ProvinceCode: code / 100,
CityCode: code,
Name: v.Name,
}, nil
}
}
return Division{}, errors.New("city code error")
}
// GetCounty get county information
func (s WithoutRedisAdapter) GetCounty(code int) (Division, error) {
_ = counties
divisions := counties[code/100]
for _, v := range divisions {
if v.CountyCode == code {
return Division{
ProvinceCode: code / 10000,
CityCode: code / 100,
CountyCode: code,
Name: v.Name,
}, nil
}
}
return Division{}, errors.New("county code error")
}
// GetTown get town information
func (s WithoutRedisAdapter) GetTown(code int) (Division, error) {
divisions := towns[code/1000]
for _, v := range divisions {
if v.TownCode == code {
return Division{
ProvinceCode: code / 10000000,
CityCode: code / 100000,
CountyCode: code / 1000,
TownCode: code,
Name: v.Name,
}, nil
}
}
return Division{}, errors.New("town code error")
}
// GetDivisionDetail province-city-county,if the length of slice returned is 1
// it means code is of province
func (s WithoutRedisAdapter) GetDivisionDetail(code int) ([]Division, error) {
switch numType(code) {
case 2:
division, err := s.GetProvince(code)
if err != nil {
return nil, err
}
return []Division{division}, nil
case 4:
division, err := s.GetCity(code)
if err != nil {
return nil, err
}
province, _ := s.GetProvince(division.ProvinceCode)
return []Division{province, division}, nil
case 6:
division, err := s.GetCounty(code)
if err != nil {
return nil, err
}
province, _ := s.GetProvince(division.ProvinceCode)
city, _ := s.GetCity(division.CityCode)
return []Division{province, city, division}, nil
case 9:
division, err := s.GetTown(code)
if err != nil {
return nil, err
}
province, _ := s.GetProvince(division.ProvinceCode)
city, _ := s.GetCity(division.CityCode)
county, _ := s.GetCounty(division.CountyCode)
return []Division{province, city, county, division}, nil
default:
return nil, errors.New("[error] division code error")
}
}
// ListNextDivision list the next level divisions
func (s WithoutRedisAdapter) ListNextDivision(code int) ([]Division, error) {
switch numType(code) {
case 2:
return s.listNextByProvince(code), nil
case 4:
return s.listNextByCity(code), nil
case 6:
return s.listNextByCounty(code), nil
default:
return nil, errors.New("code error")
}
}
// ListNextByProvince list next level divisions by province code
func (s WithoutRedisAdapter) listNextByProvince(code int) []Division {
return cities[code]
}
// ListNextByCity list next level divisions by city code
func (s WithoutRedisAdapter) listNextByCity(code int) []Division {
return counties[code]
}
// list next level divisions by county code
func (s WithoutRedisAdapter) listNextByCounty(code int) []Division {
return towns[code]
}