Files
EdgeAPI/internal/rpc/services/service_region_country.go

77 lines
2.0 KiB
Go
Raw Normal View History

2020-11-06 11:02:53 +08:00
package services
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
2020-11-06 11:02:53 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
2021-07-11 18:05:57 +08:00
// RegionCountryService 国家相关服务
2020-11-06 11:02:53 +08:00
type RegionCountryService struct {
BaseService
2020-11-06 11:02:53 +08:00
}
2021-07-11 18:05:57 +08:00
// FindAllEnabledRegionCountries 查找所有的国家列表
2020-11-06 11:02:53 +08:00
func (this *RegionCountryService) FindAllEnabledRegionCountries(ctx context.Context, req *pb.FindAllEnabledRegionCountriesRequest) (*pb.FindAllEnabledRegionCountriesResponse, error) {
// 校验请求
2022-01-06 16:25:23 +08:00
_, _, err := this.ValidateNodeId(ctx)
2020-11-06 11:02:53 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
countries, err := regions.SharedRegionCountryDAO.FindAllEnabledCountriesOrderByPinyin(tx)
2020-11-06 11:02:53 +08:00
if err != nil {
return nil, err
}
result := []*pb.RegionCountry{}
for _, country := range countries {
pinyinStrings := []string{}
err = json.Unmarshal([]byte(country.Pinyin), &pinyinStrings)
if err != nil {
return nil, err
}
if len(pinyinStrings) == 0 {
continue
}
result = append(result, &pb.RegionCountry{
Id: int64(country.Id),
Name: country.Name,
Codes: country.DecodeCodes(),
2020-11-06 11:02:53 +08:00
Pinyin: pinyinStrings,
})
}
return &pb.FindAllEnabledRegionCountriesResponse{
2022-01-06 16:25:23 +08:00
RegionCountries: result,
2020-11-06 11:02:53 +08:00
}, nil
}
2021-07-11 18:05:57 +08:00
// FindEnabledRegionCountry 查找单个国家信息
func (this *RegionCountryService) FindEnabledRegionCountry(ctx context.Context, req *pb.FindEnabledRegionCountryRequest) (*pb.FindEnabledRegionCountryResponse, error) {
// 校验请求
2022-01-06 16:25:23 +08:00
_, _, err := this.ValidateNodeId(ctx)
if err != nil {
return nil, err
}
tx := this.NullTx()
2022-01-06 16:25:23 +08:00
country, err := regions.SharedRegionCountryDAO.FindEnabledRegionCountry(tx, req.RegionCountryId)
if err != nil {
return nil, err
}
if country == nil {
2022-01-06 16:25:23 +08:00
return &pb.FindEnabledRegionCountryResponse{RegionCountry: nil}, nil
}
2022-01-06 16:25:23 +08:00
return &pb.FindEnabledRegionCountryResponse{RegionCountry: &pb.RegionCountry{
Id: int64(country.Id),
Name: country.Name,
Codes: country.DecodeCodes(),
}}, nil
}