-
Notifications
You must be signed in to change notification settings - Fork 11
/
division.go
264 lines (214 loc) · 5.06 KB
/
division.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package gb2260
import (
"fmt"
"regexp"
"strings"
)
// GB2260 GB2260
type GB2260 struct {
Store map[string]string
Revision string
}
var (
_LatestRevision = "201904"
)
// NewGB2260 If revision is not specified, use the latest data.
func NewGB2260(revision string) GB2260 {
if revision == "" {
revision = _LatestRevision
}
return GB2260{
Store: Divisions[revision],
Revision: revision,
}
}
func Revisions() []string {
var revisions []string
for revision, _ := range Divisions {
revisions = append(revisions, revision)
}
return revisions
}
// Get Obtain Division
func (g GB2260) Get(code string) *Division {
name, ok := g.Store[code]
if !ok {
return nil
}
return &Division{
Code: code,
Name: name,
Revision: g.Revision,
gb: g,
}
}
// Search Division
func Search(code string, revisions []string) *Division {
for _, revision := range revisions {
gb := NewGB2260(revision)
division := gb.Get(code)
if division != nil {
return division
}
}
return nil
}
// Provinces Return a list of provinces in Division data structure.
func (g GB2260) Provinces() []*Division {
var divisions []*Division
for code, name := range g.Store {
if strings.HasSuffix(code, "0000") {
d := &Division{
Code: code,
Name: name,
Revision: g.Revision,
gb: g,
}
divisions = append(divisions, d)
}
}
return divisions
}
// Prefectures Return a list of prefecture level cities in Division data structure.
// A province_code is a 6-length province code. It can also be:
// 2-length code
// 4-length code that endswith 00
func (g GB2260) Prefectures(provinceCode string) []*Division {
if len(provinceCode) < 2 {
return nil
}
var divisions []*Division
province := provinceCode[:2]
prefecutres, err := regexp.Compile(fmt.Sprintf(`%s\d{4}`, province))
if err != nil {
return nil
}
realProvinceCode := province + "0000"
for code, name := range g.Store {
if prefecutres.MatchString(code) && code != realProvinceCode {
d := Division{
Code: code,
Name: name,
Revision: g.Revision,
gb: g,
}
divisions = append(divisions, &d)
}
}
return divisions
}
// Counties Return a list of counties in Division data structure.
// A prefecture_code is a 6-length code that endswith 00. It can also be a 4-length code.
func (g GB2260) Counties(prefectureCode string) []*Division {
if len(prefectureCode) < 4 {
return nil
}
prefecutre := prefectureCode[:4]
var divisions []*Division
country, err := regexp.Compile(fmt.Sprintf(`%s\d{2}`, prefecutre))
if err != nil {
return nil
}
realPrefectureCode := prefecutre + "00"
for code, name := range g.Store {
if country.MatchString(code) && code != realPrefectureCode {
d := Division{
Code: code,
Name: name,
Revision: g.Revision,
gb: g,
}
divisions = append(divisions, &d)
}
}
return divisions
}
// Division 地区
type Division struct {
Code string `json:"code"` // The six-digit number of the specific administrative division.
Name string `json:"name"` // The Chinese name of the specific administrative division.
Revision string `json:"revision"` // Optional. The revision year, and empty means "latest".
gb GB2260
}
func (div Division) String() string {
return fmt.Sprintf("code:%s, name:%s, revision:%s", div.Code, div.Name, div.Revision)
}
// Equal use hash to compare equal
func (div Division) Equal(other Division) bool {
return div.Code == other.Code && div.Name == other.Name && div.Revision == other.Revision
}
// Province 省份
func (div Division) Province() *Division {
code := div.Code[0:2] + "0000"
return div.gb.Get(code)
}
// Prefecture 地区
func (div Division) Prefecture() *Division {
if div.IsProvince() {
return nil
}
code := div.Code[:4] + "00"
return div.gb.Get(code)
}
// Country 县
func (div Division) Country() *Division {
if div.IsProvince() || div.IsPrefecture() {
return nil
}
return &div
}
// Description 描述
func (div Division) Description() string {
stack := div.Stack()
var names []string
names = make([]string, 0)
for _, s := range stack {
if s != nil {
names = append(names, s.Name)
}
}
return strings.Join(names, " ")
}
// IsProvince 是否省份
func (div Division) IsProvince() bool {
pro := div.Province()
if pro == nil {
return false
}
return pro.Equal(div)
}
// IsPrefecture 是否地区
func (div Division) IsPrefecture() bool {
pre := div.Prefecture()
if pre == nil {
return false
}
return pre.Equal(div)
}
// IsCountry 是否县
func (div Division) IsCountry() bool {
country := div.Country()
if country == nil {
return false
}
return country.Equal(div)
}
// Stack 省,地区,县
func (div Division) Stack() []*Division {
var stacks []*Division
stacks = make([]*Division, 0)
province := div.Province()
if province != nil {
stacks = append(stacks, province)
}
if div.IsPrefecture() || div.IsCountry() {
prefecture := div.Prefecture()
if prefecture != nil {
stacks = append(stacks, prefecture)
}
}
if div.IsCountry() {
stacks = append(stacks, &div)
}
return stacks
}