-
Notifications
You must be signed in to change notification settings - Fork 0
/
divisionjson.go
145 lines (130 loc) · 3.54 KB
/
divisionjson.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
// Package divisionjson ...
package divisionjson
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"github.com/nhaancs/bhms/business/core/division"
"github.com/nhaancs/bhms/foundation/logger"
)
var (
//go:embed division.json
divJSON string
)
// =============================================================================
type Store struct {
log *logger.Logger
level1 []divisionJSON // divisions level 1
level2 map[int][]divisionJSON // lv1ID => division lv2
level3 map[int][]divisionJSON // lv2ID => division lv3
allMap map[int]divisionJSON // ID => division
}
// NewStore constructs the api for data access.
func NewStore(log *logger.Logger) (*Store, error) {
var (
id int // autoincrement id
provinces []province
s = Store{
log: log,
level2: make(map[int][]divisionJSON),
level3: make(map[int][]divisionJSON),
allMap: make(map[int]divisionJSON),
}
)
// get from file
if err := json.Unmarshal([]byte(divJSON), &provinces); err != nil {
return nil, fmt.Errorf("provinces:unmarshal:%w", err)
}
if len(provinces) == 0 {
return nil, fmt.Errorf("empty provinces")
}
// convert json data to store
s.level1 = make([]divisionJSON, len(provinces))
for i := range provinces {
// level 1
if err := provinces[i].validate(); err != nil {
return nil, err
}
id++
lv1ID := id
d := divisionJSON{
ID: lv1ID,
Name: provinces[i].Name,
Level: 1,
Code: provinces[i].Code,
ParentID: 0,
}
s.level1[i] = d
s.allMap[lv1ID] = d
// level 2
s.level2[lv1ID] = make([]divisionJSON, len(provinces[i].Districts))
for j := range provinces[i].Districts {
if err := provinces[i].Districts[j].validate(); err != nil {
return nil, err
}
id++
lv2ID := id
di := divisionJSON{
ID: lv2ID,
Name: provinces[i].Districts[j].Name,
Level: 2,
Code: provinces[i].Districts[j].Code,
ParentID: lv1ID,
}
s.level2[lv1ID][j] = di
s.allMap[lv2ID] = di
// level 3
s.level3[lv2ID] = make([]divisionJSON, len(provinces[i].Districts[j].Wards))
for k := range provinces[i].Districts[j].Wards {
if err := provinces[i].Districts[j].Wards[k].validate(); err != nil {
return nil, err
}
id++
lv3ID := id
war := divisionJSON{
ID: lv3ID,
Name: provinces[i].Districts[j].Wards[k].Name,
Level: 3,
Code: provinces[i].Districts[j].Wards[k].Code,
ParentID: lv2ID,
}
s.level3[lv2ID][k] = war
s.allMap[lv3ID] = war
}
}
}
return &s, nil
}
func (s *Store) QueryByID(ctx context.Context, id int) (division.Division, error) {
div, exist := s.allMap[id]
if !exist {
return division.Division{}, fmt.Errorf("querybyid: %w", division.ErrNotFound)
}
return toCoreDivision(div)
}
func (s *Store) QueryByParentID(ctx context.Context, id int) ([]division.Division, error) {
parent, err := s.QueryByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("querybyparentid parent: %w", division.ErrNotFound)
}
switch parent.Level {
case 2:
divisions, exist := s.level2[id]
if !exist {
return nil, fmt.Errorf("querybyparentid level 2: %w", division.ErrNotFound)
}
return toCoreDivisions(divisions)
case 3:
divisions, exist := s.level3[id]
if !exist {
return nil, fmt.Errorf("querybyparentid level 3: %w", division.ErrNotFound)
}
return toCoreDivisions(divisions)
default:
return nil, fmt.Errorf("invalid division level: %d", parent.Level)
}
}
func (s *Store) QueryLevel1s(ctx context.Context) ([]division.Division, error) {
return toCoreDivisions(s.level1)
}