2022-08-13 23:55:59 +08:00
|
|
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
|
|
|
|
|
|
package iplibrary
|
|
|
|
|
|
2022-08-21 20:36:56 +08:00
|
|
|
import (
|
|
|
|
|
"github.com/iwind/TeaGo/lists"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2022-08-13 23:55:59 +08:00
|
|
|
type QueryResult struct {
|
|
|
|
|
item *ipItem
|
2022-08-14 19:42:05 +08:00
|
|
|
meta *Meta
|
2022-08-13 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *QueryResult) IsOk() bool {
|
|
|
|
|
return this.item != nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *QueryResult) CountryId() int64 {
|
|
|
|
|
if this.item != nil {
|
2022-08-22 08:31:53 +08:00
|
|
|
return int64(this.item.Region.CountryId)
|
2022-08-13 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 19:42:05 +08:00
|
|
|
func (this *QueryResult) CountryName() string {
|
2022-08-21 20:36:56 +08:00
|
|
|
if this.item == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.CountryId > 0 {
|
|
|
|
|
var country = this.meta.CountryWithId(this.item.Region.CountryId)
|
2022-08-14 19:42:05 +08:00
|
|
|
if country != nil {
|
|
|
|
|
return country.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 20:36:56 +08:00
|
|
|
func (this *QueryResult) CountryCodes() []string {
|
|
|
|
|
if this.item == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.CountryId > 0 {
|
|
|
|
|
var country = this.meta.CountryWithId(this.item.Region.CountryId)
|
2022-08-21 20:36:56 +08:00
|
|
|
if country != nil {
|
|
|
|
|
return country.Codes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 23:55:59 +08:00
|
|
|
func (this *QueryResult) ProvinceId() int64 {
|
|
|
|
|
if this.item != nil {
|
2022-08-22 08:31:53 +08:00
|
|
|
return int64(this.item.Region.ProvinceId)
|
2022-08-13 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 19:42:05 +08:00
|
|
|
func (this *QueryResult) ProvinceName() string {
|
2022-08-21 20:36:56 +08:00
|
|
|
if this.item == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.ProvinceId > 0 {
|
|
|
|
|
var province = this.meta.ProvinceWithId(this.item.Region.ProvinceId)
|
2022-08-14 19:42:05 +08:00
|
|
|
if province != nil {
|
|
|
|
|
return province.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 20:36:56 +08:00
|
|
|
func (this *QueryResult) ProvinceCodes() []string {
|
|
|
|
|
if this.item == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.ProvinceId > 0 {
|
|
|
|
|
var province = this.meta.ProvinceWithId(this.item.Region.ProvinceId)
|
2022-08-21 20:36:56 +08:00
|
|
|
if province != nil {
|
|
|
|
|
return province.Codes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 23:55:59 +08:00
|
|
|
func (this *QueryResult) CityId() int64 {
|
|
|
|
|
if this.item != nil {
|
2022-08-22 08:31:53 +08:00
|
|
|
return int64(this.item.Region.CityId)
|
2022-08-13 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 19:42:05 +08:00
|
|
|
func (this *QueryResult) CityName() string {
|
2022-08-21 20:36:56 +08:00
|
|
|
if this.item == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.CityId > 0 {
|
|
|
|
|
var city = this.meta.CityWithId(this.item.Region.CityId)
|
2022-08-14 19:42:05 +08:00
|
|
|
if city != nil {
|
|
|
|
|
return city.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 23:55:59 +08:00
|
|
|
func (this *QueryResult) TownId() int64 {
|
|
|
|
|
if this.item != nil {
|
2022-08-22 08:31:53 +08:00
|
|
|
return int64(this.item.Region.TownId)
|
2022-08-13 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 19:42:05 +08:00
|
|
|
func (this *QueryResult) TownName() string {
|
2022-08-21 20:36:56 +08:00
|
|
|
if this.item == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.TownId > 0 {
|
|
|
|
|
var town = this.meta.TownWithId(this.item.Region.TownId)
|
2022-08-14 19:42:05 +08:00
|
|
|
if town != nil {
|
|
|
|
|
return town.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 23:55:59 +08:00
|
|
|
func (this *QueryResult) ProviderId() int64 {
|
|
|
|
|
if this.item != nil {
|
2022-08-22 08:31:53 +08:00
|
|
|
return int64(this.item.Region.ProviderId)
|
2022-08-13 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2022-08-14 19:42:05 +08:00
|
|
|
|
|
|
|
|
func (this *QueryResult) ProviderName() string {
|
2022-08-21 20:36:56 +08:00
|
|
|
if this.item == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.ProviderId > 0 {
|
|
|
|
|
var provider = this.meta.ProviderWithId(this.item.Region.ProviderId)
|
2022-08-14 19:42:05 +08:00
|
|
|
if provider != nil {
|
|
|
|
|
return provider.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-21 20:36:56 +08:00
|
|
|
|
|
|
|
|
func (this *QueryResult) ProviderCodes() []string {
|
|
|
|
|
if this.item == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-08-22 08:31:53 +08:00
|
|
|
if this.item.Region.ProviderId > 0 {
|
|
|
|
|
var provider = this.meta.ProviderWithId(this.item.Region.ProviderId)
|
2022-08-21 20:36:56 +08:00
|
|
|
if provider != nil {
|
|
|
|
|
return provider.Codes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *QueryResult) Summary() string {
|
|
|
|
|
if this.item == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pieces = []string{}
|
|
|
|
|
var countryName = this.CountryName()
|
|
|
|
|
var provinceName = this.ProvinceName()
|
|
|
|
|
var cityName = this.CityName()
|
|
|
|
|
var townName = this.TownName()
|
|
|
|
|
var providerName = this.ProviderName()
|
|
|
|
|
|
|
|
|
|
if len(countryName) > 0 {
|
|
|
|
|
pieces = append(pieces, countryName)
|
|
|
|
|
}
|
|
|
|
|
if len(provinceName) > 0 && !lists.ContainsString(pieces, provinceName) {
|
|
|
|
|
pieces = append(pieces, provinceName)
|
|
|
|
|
}
|
|
|
|
|
if len(cityName) > 0 && !lists.ContainsString(pieces, cityName) && !lists.ContainsString(pieces, strings.TrimSuffix(cityName, "市")) {
|
|
|
|
|
pieces = append(pieces, cityName)
|
|
|
|
|
}
|
|
|
|
|
if len(townName) > 0 && !lists.ContainsString(pieces, townName) && !lists.ContainsString(pieces, strings.TrimSuffix(townName, "县")) {
|
2023-03-29 20:06:15 +08:00
|
|
|
pieces = append(pieces, townName)
|
2022-08-21 20:36:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(providerName) > 0 && !lists.ContainsString(pieces, providerName) {
|
|
|
|
|
if len(pieces) > 0 {
|
|
|
|
|
pieces = append(pieces, "|")
|
|
|
|
|
}
|
|
|
|
|
pieces = append(pieces, providerName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.Join(pieces, " ")
|
|
|
|
|
}
|