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

173 lines
4.7 KiB
Go
Raw Normal View History

2020-11-06 11:02:53 +08:00
package services
import (
"context"
"encoding/json"
2024-07-27 14:15:25 +08:00
"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 查找所有的国家列表
// Deprecated
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
}
2022-07-22 15:05:30 +08:00
var 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{}
2022-03-22 19:30:30 +08:00
err = json.Unmarshal(country.Pinyin, &pinyinStrings)
2020-11-06 11:02:53 +08:00
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,
2020-11-06 11:02:53 +08:00
})
}
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 查找单个国家信息
// Deprecated
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
}
2022-07-22 15:05:30 +08:00
var 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.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()
}