mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-02-09 10:25:38 +08:00
IP库管理阶段性提交(未完成)
This commit is contained in:
@@ -128,17 +128,19 @@ func (this *IPLibraryFileService) FindIPLibraryFile(ctx context.Context, req *pb
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindIPLibraryFileResponse{IpLibraryFile: &pb.IPLibraryFile{
|
||||
Id: int64(libraryFile.Id),
|
||||
FileId: int64(libraryFile.FileId),
|
||||
IsFinished: libraryFile.IsFinished,
|
||||
CreatedAt: int64(libraryFile.CreatedAt),
|
||||
CountryNames: pbCountryNames,
|
||||
Provinces: pbProvinces,
|
||||
Cities: pbCities,
|
||||
Towns: pbTowns,
|
||||
ProviderNames: pbProviderNames,
|
||||
}}, nil
|
||||
return &pb.FindIPLibraryFileResponse{
|
||||
IpLibraryFile: &pb.IPLibraryFile{
|
||||
Id: int64(libraryFile.Id),
|
||||
FileId: int64(libraryFile.FileId),
|
||||
IsFinished: libraryFile.IsFinished,
|
||||
CreatedAt: int64(libraryFile.CreatedAt),
|
||||
CountryNames: pbCountryNames,
|
||||
Provinces: pbProvinces,
|
||||
Cities: pbCities,
|
||||
Towns: pbTowns,
|
||||
ProviderNames: pbProviderNames,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateIPLibraryFile 创建IP库文件
|
||||
@@ -341,9 +343,9 @@ func (this *IPLibraryFileService) CheckCitiesWithIPLibraryFileId(ctx context.Con
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var countryMap = map[string]int64{} // countryName => countryId
|
||||
var provinceMap = map[string]int64{} // countryId_provinceName => provinceId
|
||||
var provinceNamesMap = map[int64][][3]string{} // provinceId => [][3]{countryName, provinceName, cityName}
|
||||
var countryMap = map[string]int64{} // countryName => countryId
|
||||
var provinceMap = map[string]int64{} // countryId_provinceName => provinceId
|
||||
var cityNamesMap = map[int64][][3]string{} // provinceId => [][3]{countryName, provinceName, cityName}
|
||||
var provinceIds = []int64{}
|
||||
for _, city := range cities {
|
||||
var countryName = city[0]
|
||||
@@ -363,14 +365,14 @@ func (this *IPLibraryFileService) CheckCitiesWithIPLibraryFileId(ctx context.Con
|
||||
var key = types.String(countryId) + "_" + provinceName
|
||||
provinceId, ok := provinceMap[key]
|
||||
if ok {
|
||||
provinceNamesMap[provinceId] = append(provinceNamesMap[provinceId], [3]string{countryName, provinceName, cityName})
|
||||
cityNamesMap[provinceId] = append(cityNamesMap[provinceId], [3]string{countryName, provinceName, cityName})
|
||||
} else {
|
||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithName(tx, countryId, provinceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
provinceMap[key] = provinceId
|
||||
provinceNamesMap[provinceId] = append(provinceNamesMap[provinceId], [3]string{countryName, provinceName, cityName})
|
||||
cityNamesMap[provinceId] = append(cityNamesMap[provinceId], [3]string{countryName, provinceName, cityName})
|
||||
if provinceId > 0 {
|
||||
provinceIds = append(provinceIds, provinceId)
|
||||
}
|
||||
@@ -384,7 +386,7 @@ func (this *IPLibraryFileService) CheckCitiesWithIPLibraryFileId(ctx context.Con
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, city := range provinceNamesMap[provinceId] {
|
||||
for _, city := range cityNamesMap[provinceId] {
|
||||
var countryName = city[0]
|
||||
var provinceName = city[1]
|
||||
var cityName = city[2]
|
||||
@@ -419,6 +421,118 @@ func (this *IPLibraryFileService) CheckCitiesWithIPLibraryFileId(ctx context.Con
|
||||
return &pb.CheckCitiesWithIPLibraryFileIdResponse{MissingCities: pbMissingCities}, nil
|
||||
}
|
||||
|
||||
// CheckTownsWithIPLibraryFileId 检查区县
|
||||
func (this *IPLibraryFileService) CheckTownsWithIPLibraryFileId(ctx context.Context, req *pb.CheckTownsWithIPLibraryFileIdRequest) (*pb.CheckTownsWithIPLibraryFileIdResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
towns, err := models.SharedIPLibraryFileDAO.FindLibraryFileTowns(tx, req.IpLibraryFileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var countryMap = map[string]int64{} // countryName => countryId
|
||||
var provinceMap = map[string]int64{} // countryId_provinceName => provinceId
|
||||
var cityMap = map[string]int64{} // province_cityName => cityId
|
||||
|
||||
var townNamesMap = map[int64][][4]string{} // cityId => [][4]{countryName, provinceName, cityName, townName}
|
||||
var cityIds = []int64{}
|
||||
for _, town := range towns {
|
||||
var countryName = town[0]
|
||||
var provinceName = town[1]
|
||||
var cityName = town[2]
|
||||
var townName = town[3]
|
||||
|
||||
// country
|
||||
countryId, ok := countryMap[countryName]
|
||||
if !ok {
|
||||
countryId, err = regions.SharedRegionCountryDAO.FindCountryIdWithName(tx, countryName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
countryMap[countryName] = countryId
|
||||
|
||||
// province
|
||||
var provinceKey = types.String(countryId) + "_" + provinceName
|
||||
provinceId, ok := provinceMap[provinceKey]
|
||||
if !ok {
|
||||
if countryId > 0 {
|
||||
provinceId, err = regions.SharedRegionProvinceDAO.FindProvinceIdWithName(tx, countryId, provinceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
provinceMap[provinceKey] = provinceId
|
||||
}
|
||||
|
||||
// city
|
||||
var cityKey = types.String(provinceId) + "_" + cityName
|
||||
cityId, ok := cityMap[cityKey]
|
||||
if !ok {
|
||||
if provinceId > 0 {
|
||||
cityId, err = regions.SharedRegionCityDAO.FindCityIdWithName(tx, provinceId, cityName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
cityMap[cityKey] = cityId
|
||||
}
|
||||
|
||||
// town
|
||||
townNamesMap[cityId] = append(townNamesMap[cityId], [4]string{countryName, provinceName, cityName, townName})
|
||||
}
|
||||
|
||||
var pbMissingTowns = []*pb.CheckTownsWithIPLibraryFileIdResponse_MissingTown{}
|
||||
for _, cityId := range cityIds {
|
||||
allTowns, err := regions.SharedRegionTownDAO.FindAllRegionTownsWithCityId(tx, cityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, town := range townNamesMap[cityId] {
|
||||
var countryName = town[0]
|
||||
var provinceName = town[1]
|
||||
var cityName = town[2]
|
||||
var townName = town[3]
|
||||
|
||||
townId, err := regions.SharedRegionTownDAO.FindTownIdWithName(tx, cityId, townName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if townId > 0 {
|
||||
// 已存在,则跳过
|
||||
continue
|
||||
}
|
||||
|
||||
var similarTowns = regions.SharedRegionTownDAO.FindSimilarTowns(allTowns, townName, 5)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbMissingTown = &pb.CheckTownsWithIPLibraryFileIdResponse_MissingTown{}
|
||||
pbMissingTown.CountryName = countryName
|
||||
pbMissingTown.ProvinceName = provinceName
|
||||
pbMissingTown.CityName = cityName
|
||||
pbMissingTown.TownName = townName
|
||||
|
||||
for _, similarTown := range similarTowns {
|
||||
pbMissingTown.SimilarTowns = append(pbMissingTown.SimilarTowns, &pb.RegionTown{
|
||||
Id: int64(similarTown.Id),
|
||||
Name: similarTown.Name,
|
||||
DisplayName: similarTown.DisplayName(),
|
||||
})
|
||||
}
|
||||
pbMissingTowns = append(pbMissingTowns, pbMissingTown)
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.CheckTownsWithIPLibraryFileIdResponse{MissingTowns: pbMissingTowns}, nil
|
||||
}
|
||||
|
||||
// CheckProvidersWithIPLibraryFileId 检查ISP运营商
|
||||
func (this *IPLibraryFileService) CheckProvidersWithIPLibraryFileId(ctx context.Context, req *pb.CheckProvidersWithIPLibraryFileIdRequest) (*pb.CheckProvidersWithIPLibraryFileIdResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
|
||||
158
internal/rpc/services/service_region_town.go
Normal file
158
internal/rpc/services/service_region_town.go
Normal file
@@ -0,0 +1,158 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// RegionTownService 区县相关服务
|
||||
type RegionTownService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// FindAllRegionTowns 查找所有区县
|
||||
func (this *RegionTownService) FindAllRegionTowns(ctx context.Context, req *pb.FindAllRegionTownsRequest) (*pb.FindAllRegionTownsResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
towns, err := regions.SharedRegionTownDAO.FindAllRegionTowns(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbTowns = []*pb.RegionTown{}
|
||||
|
||||
var citiesMap = map[int64]*regions.RegionCity{} // provinceId => *RegionCity
|
||||
|
||||
for _, town := range towns {
|
||||
var cityId = int64(town.CityId)
|
||||
|
||||
var pbCity = &pb.RegionCity{Id: cityId}
|
||||
if req.IncludeRegionCity {
|
||||
city, ok := citiesMap[cityId]
|
||||
if !ok {
|
||||
city, err = regions.SharedRegionCityDAO.FindEnabledRegionCity(tx, cityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if city == nil {
|
||||
continue
|
||||
}
|
||||
citiesMap[cityId] = city
|
||||
}
|
||||
|
||||
pbCity = &pb.RegionCity{
|
||||
Id: int64(city.Id),
|
||||
Name: city.Name,
|
||||
Codes: city.DecodeCodes(),
|
||||
CustomName: city.CustomName,
|
||||
CustomCodes: city.DecodeCustomCodes(),
|
||||
DisplayName: city.DisplayName(),
|
||||
}
|
||||
}
|
||||
|
||||
pbTowns = append(pbTowns, &pb.RegionTown{
|
||||
Id: int64(town.Id),
|
||||
Name: town.Name,
|
||||
Codes: town.DecodeCodes(),
|
||||
RegionCityId: int64(town.CityId),
|
||||
RegionCity: pbCity,
|
||||
CustomName: town.CustomName,
|
||||
CustomCodes: town.DecodeCustomCodes(),
|
||||
DisplayName: town.DisplayName(),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllRegionTownsResponse{
|
||||
RegionTowns: pbTowns,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FindAllRegionTownsWithRegionCityId 查找某个城市的所有区县
|
||||
func (this *RegionTownService) FindAllRegionTownsWithRegionCityId(ctx context.Context, req *pb.FindAllRegionTownsWithRegionCityIdRequest) (*pb.FindAllRegionTownsWithRegionCityIdResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
towns, err := regions.SharedRegionTownDAO.FindAllRegionTownsWithCityId(tx, req.RegionCityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbTowns = []*pb.RegionTown{}
|
||||
|
||||
for _, town := range towns {
|
||||
var cityId = int64(town.CityId)
|
||||
|
||||
var pbCity = &pb.RegionCity{Id: cityId}
|
||||
|
||||
pbTowns = append(pbTowns, &pb.RegionTown{
|
||||
Id: int64(town.Id),
|
||||
Name: town.Name,
|
||||
Codes: town.DecodeCodes(),
|
||||
RegionCityId: int64(town.CityId),
|
||||
RegionCity: pbCity,
|
||||
CustomName: town.CustomName,
|
||||
CustomCodes: town.DecodeCustomCodes(),
|
||||
DisplayName: town.DisplayName(),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllRegionTownsWithRegionCityIdResponse{
|
||||
RegionTowns: pbTowns,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FindRegionTown 查找单个区县信息
|
||||
func (this *RegionTownService) FindRegionTown(ctx context.Context, req *pb.FindRegionTownRequest) (*pb.FindRegionTownResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
town, err := regions.SharedRegionTownDAO.FindEnabledRegionTown(tx, req.RegionTownId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if town == nil {
|
||||
return &pb.FindRegionTownResponse{
|
||||
RegionTown: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &pb.FindRegionTownResponse{
|
||||
RegionTown: &pb.RegionTown{
|
||||
Id: int64(town.Id),
|
||||
Name: town.Name,
|
||||
Codes: town.DecodeCodes(),
|
||||
RegionCityId: int64(town.CityId),
|
||||
CustomName: town.CustomName,
|
||||
CustomCodes: town.DecodeCustomCodes(),
|
||||
DisplayName: town.DisplayName(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateRegionTownCustom 修改区县定制信息
|
||||
func (this *RegionTownService) UpdateRegionTownCustom(ctx context.Context, req *pb.UpdateRegionTownCustomRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = regions.SharedRegionTownDAO.UpdateTownCustom(tx, req.RegionTownId, req.CustomName, req.CustomCodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user