mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	查找省份对应ID时,自动尝试省略省、区之类的后缀
This commit is contained in:
		@@ -13,6 +13,7 @@ import (
 | 
				
			|||||||
	"github.com/iwind/TeaGo/types"
 | 
						"github.com/iwind/TeaGo/types"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -449,6 +450,14 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
 | 
				
			|||||||
	for _, province := range dbProvinces {
 | 
						for _, province := range dbProvinces {
 | 
				
			||||||
		for _, code := range province.AllCodes() {
 | 
							for _, code := range province.AllCodes() {
 | 
				
			||||||
			provinceMap[types.String(province.CountryId)+"_"+code] = int64(province.ValueId)
 | 
								provinceMap[types.String(province.CountryId)+"_"+code] = int64(province.ValueId)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								for _, suffix := range regions.RegionProvinceSuffixes {
 | 
				
			||||||
 | 
									if strings.HasSuffix(code, suffix) {
 | 
				
			||||||
 | 
										provinceMap[types.String(province.CountryId)+"_"+strings.TrimSuffix(code, suffix)] = int64(province.ValueId)
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										provinceMap[types.String(province.CountryId)+"_"+(code+suffix)] = int64(province.ValueId)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,6 +11,7 @@ import (
 | 
				
			|||||||
	"github.com/iwind/TeaGo/types"
 | 
						"github.com/iwind/TeaGo/types"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -18,6 +19,8 @@ const (
 | 
				
			|||||||
	RegionProvinceStateDisabled = 0 // 已禁用
 | 
						RegionProvinceStateDisabled = 0 // 已禁用
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var RegionProvinceSuffixes = []string{"省", "州", "区", "大区", "特区", "港", "岛", "环礁", "谷地", "山", "口岸", "郡", "县", "城", "河", "河畔", "市"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type RegionProvinceDAO dbs.DAO
 | 
					type RegionProvinceDAO dbs.DAO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewRegionProvinceDAO() *RegionProvinceDAO {
 | 
					func NewRegionProvinceDAO() *RegionProvinceDAO {
 | 
				
			||||||
@@ -95,6 +98,37 @@ func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId strin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// FindProvinceIdWithName 根据省份名查找省份ID
 | 
					// FindProvinceIdWithName 根据省份名查找省份ID
 | 
				
			||||||
func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
 | 
					func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							provinceId, err := this.findProvinceIdWithExactName(tx, countryId, provinceName)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return 0, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if provinceId > 0 {
 | 
				
			||||||
 | 
								return provinceId, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 候选词
 | 
				
			||||||
 | 
						for _, suffix := range RegionProvinceSuffixes {
 | 
				
			||||||
 | 
							var name string
 | 
				
			||||||
 | 
							if strings.HasSuffix(provinceName, suffix) {
 | 
				
			||||||
 | 
								name = strings.TrimSuffix(provinceName, suffix)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								name = provinceName + suffix
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							provinceId, err := this.findProvinceIdWithExactName(tx, countryId, name)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return 0, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if provinceId > 0 {
 | 
				
			||||||
 | 
								return provinceId, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (this *RegionProvinceDAO) findProvinceIdWithExactName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
 | 
				
			||||||
	return this.Query(tx).
 | 
						return this.Query(tx).
 | 
				
			||||||
		Attr("countryId", countryId).
 | 
							Attr("countryId", countryId).
 | 
				
			||||||
		Where("(name=:provinceName OR customName=:provinceName OR JSON_CONTAINS(codes, :provinceNameJSON) OR JSON_CONTAINS(customCodes, :provinceNameJSON))").
 | 
							Where("(name=:provinceName OR customName=:provinceName OR JSON_CONTAINS(codes, :provinceNameJSON) OR JSON_CONTAINS(customCodes, :provinceNameJSON))").
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,6 +26,25 @@ func TestRegionProvinceDAO_FindProvinceIdWithName(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestRegionProvinceDAO_FindProvinceIdWithName_Suffix(t *testing.T) {
 | 
				
			||||||
 | 
						dbs.NotifyReady()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var tx *dbs.Tx
 | 
				
			||||||
 | 
						for _, name := range []string{
 | 
				
			||||||
 | 
							"维埃纳",
 | 
				
			||||||
 | 
							"维埃纳省",
 | 
				
			||||||
 | 
							"维埃纳大区",
 | 
				
			||||||
 | 
							"维埃纳市",
 | 
				
			||||||
 | 
							"维埃纳小区", // expect 0
 | 
				
			||||||
 | 
						} {
 | 
				
			||||||
 | 
							provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithName(tx, 74, name)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							t.Log(name, "=>", provinceId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRegionProvinceDAO_FindSimilarProvinces(t *testing.T) {
 | 
					func TestRegionProvinceDAO_FindSimilarProvinces(t *testing.T) {
 | 
				
			||||||
	dbs.NotifyReady()
 | 
						dbs.NotifyReady()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user