/
service_region_country.go
171 lines (149 loc) · 4.73 KB
/
service_region_country.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
package services
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// RegionCountryService 国家相关服务
type RegionCountryService struct {
BaseService
}
// FindAllEnabledRegionCountries 查找所有的国家列表
// Deprecated
func (this *RegionCountryService) FindAllEnabledRegionCountries(ctx context.Context, req *pb.FindAllEnabledRegionCountriesRequest) (*pb.FindAllEnabledRegionCountriesResponse, error) {
// 校验请求
_, _, err := this.ValidateNodeId(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
countries, err := regions.SharedRegionCountryDAO.FindAllEnabledCountriesOrderByPinyin(tx)
if err != nil {
return nil, err
}
result := []*pb.RegionCountry{}
for _, country := range countries {
pinyinStrings := []string{}
err = json.Unmarshal(country.Pinyin, &pinyinStrings)
if err != nil {
return nil, err
}
if len(pinyinStrings) == 0 {
continue
}
result = append(result, &pb.RegionCountry{
Id: int64(country.ValueId),
Name: country.Name,
Codes: country.DecodeCodes(),
Pinyin: pinyinStrings,
CustomName: country.CustomName,
CustomCodes: country.DecodeCustomCodes(),
DisplayName: country.DisplayName(),
IsCommon: country.IsCommon,
})
}
return &pb.FindAllEnabledRegionCountriesResponse{
RegionCountries: result,
}, nil
}
// FindEnabledRegionCountry 查找单个国家信息
// Deprecated
func (this *RegionCountryService) FindEnabledRegionCountry(ctx context.Context, req *pb.FindEnabledRegionCountryRequest) (*pb.FindEnabledRegionCountryResponse, error) {
// 校验请求
_, _, err := this.ValidateNodeId(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
country, err := regions.SharedRegionCountryDAO.FindEnabledRegionCountry(tx, req.RegionCountryId)
if err != nil {
return nil, err
}
if country == nil {
return &pb.FindEnabledRegionCountryResponse{RegionCountry: nil}, nil
}
return &pb.FindEnabledRegionCountryResponse{RegionCountry: &pb.RegionCountry{
Id: int64(country.ValueId),
Name: country.Name,
Codes: country.DecodeCodes(),
CustomName: country.CustomName,
CustomCodes: country.DecodeCustomCodes(),
DisplayName: country.DisplayName(),
}}, nil
}
// FindAllRegionCountries 查找所有的国家列表
func (this *RegionCountryService) FindAllRegionCountries(ctx context.Context, req *pb.FindAllRegionCountriesRequest) (*pb.FindAllRegionCountriesResponse, error) {
// 校验请求
_, _, err := this.ValidateNodeId(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
countries, err := regions.SharedRegionCountryDAO.FindAllEnabledCountriesOrderByPinyin(tx)
if err != nil {
return nil, err
}
var result = []*pb.RegionCountry{}
for _, country := range countries {
pinyinStrings := []string{}
err = json.Unmarshal(country.Pinyin, &pinyinStrings)
if err != nil {
return nil, err
}
if len(pinyinStrings) == 0 {
continue
}
result = append(result, &pb.RegionCountry{
Id: int64(country.ValueId),
Name: country.Name,
Codes: country.DecodeCodes(),
Pinyin: pinyinStrings,
CustomName: country.CustomName,
CustomCodes: country.DecodeCustomCodes(),
DisplayName: country.DisplayName(),
IsCommon: country.IsCommon,
})
}
return &pb.FindAllRegionCountriesResponse{
RegionCountries: result,
}, nil
}
// FindRegionCountry 查找单个国家信息
func (this *RegionCountryService) FindRegionCountry(ctx context.Context, req *pb.FindRegionCountryRequest) (*pb.FindRegionCountryResponse, error) {
// 校验请求
_, _, err := this.ValidateNodeId(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
country, err := regions.SharedRegionCountryDAO.FindEnabledRegionCountry(tx, req.RegionCountryId)
if err != nil {
return nil, err
}
if country == nil {
return &pb.FindRegionCountryResponse{RegionCountry: nil}, nil
}
return &pb.FindRegionCountryResponse{RegionCountry: &pb.RegionCountry{
Id: int64(country.ValueId),
Name: country.Name,
Codes: country.DecodeCodes(),
CustomName: country.CustomName,
CustomCodes: country.DecodeCustomCodes(),
DisplayName: country.DisplayName(),
IsCommon: country.IsCommon,
}}, nil
}
// UpdateRegionCountryCustom 修改城市定制信息
func (this *RegionCountryService) UpdateRegionCountryCustom(ctx context.Context, req *pb.UpdateRegionCountryCustomRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = regions.SharedRegionCountryDAO.UpdateCountryCustom(tx, req.RegionCountryId, req.CustomName, req.CustomCodes)
if err != nil {
return nil, err
}
return this.Success()
}