mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-06 15:35:47 +08:00
新版IP库管理阶段性提交(未完成)
This commit is contained in:
@@ -2,11 +2,14 @@ package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -38,7 +41,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
// EnableRegionProvince 启用条目
|
||||
func (this *RegionProvinceDAO) EnableRegionProvince(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -47,7 +50,7 @@ func (this *RegionProvinceDAO) EnableRegionProvince(tx *dbs.Tx, id int64) error
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
// DisableRegionProvince 禁用条目
|
||||
func (this *RegionProvinceDAO) DisableRegionProvince(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -56,7 +59,7 @@ func (this *RegionProvinceDAO) DisableRegionProvince(tx *dbs.Tx, id int64) error
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
// FindEnabledRegionProvince 查找启用中的条目
|
||||
func (this *RegionProvinceDAO) FindEnabledRegionProvince(tx *dbs.Tx, id int64) (*RegionProvince, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -68,7 +71,7 @@ func (this *RegionProvinceDAO) FindEnabledRegionProvince(tx *dbs.Tx, id int64) (
|
||||
return result.(*RegionProvince), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
// FindRegionProvinceName 根据主键查找名称
|
||||
func (this *RegionProvinceDAO) FindRegionProvinceName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -76,7 +79,7 @@ func (this *RegionProvinceDAO) FindRegionProvinceName(tx *dbs.Tx, id int64) (str
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 根据数据ID查找省份
|
||||
// FindProvinceIdWithDataId 根据数据ID查找省份
|
||||
func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
@@ -84,17 +87,18 @@ func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId strin
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 根据省份名查找省份ID
|
||||
// FindProvinceIdWithName 根据省份名查找省份ID
|
||||
func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("countryId", countryId).
|
||||
Where("JSON_CONTAINS(codes, :provinceName)").
|
||||
Param("provinceName", strconv.Quote(provinceName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
Where("(name=:provinceName OR customName=:provinceName OR JSON_CONTAINS(codes, :provinceNameJSON) OR JSON_CONTAINS(customCodes, :provinceNameJSON))").
|
||||
Param("provinceName", provinceName).
|
||||
Param("provinceNameJSON", strconv.Quote(provinceName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 根据省份名查找省份ID,并可使用缓存
|
||||
// FindProvinceIdWithNameCacheable 根据省份名查找省份ID,并可使用缓存
|
||||
func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
||||
var key = provinceName + "@" + numberutils.FormatInt64(countryId)
|
||||
|
||||
@@ -110,14 +114,16 @@ func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, count
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
SharedCacheLocker.Lock()
|
||||
regionProvinceNameAndIdCacheMap[key] = provinceId
|
||||
SharedCacheLocker.Unlock()
|
||||
if provinceId > 0 {
|
||||
SharedCacheLocker.Lock()
|
||||
regionProvinceNameAndIdCacheMap[key] = provinceId
|
||||
SharedCacheLocker.Unlock()
|
||||
}
|
||||
|
||||
return provinceId, nil
|
||||
}
|
||||
|
||||
// 创建省份
|
||||
// CreateProvince 创建省份
|
||||
func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name string, dataId string) (int64, error) {
|
||||
var op = NewRegionProvinceOperator()
|
||||
op.CountryId = countryId
|
||||
@@ -138,7 +144,7 @@ func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 查找所有省份
|
||||
// FindAllEnabledProvincesWithCountryId 查找某个国家/地区的所有省份
|
||||
func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(tx *dbs.Tx, countryId int64) (result []*RegionProvince, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(RegionProvinceStateEnabled).
|
||||
@@ -148,3 +154,76 @@ func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(tx *dbs.Tx,
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllEnabledProvinces 查找所有省份
|
||||
func (this *RegionProvinceDAO) FindAllEnabledProvinces(tx *dbs.Tx) (result []*RegionProvince, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(RegionProvinceStateEnabled).
|
||||
Asc().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateProvinceCustom 修改自定义省份信息
|
||||
func (this *RegionProvinceDAO) UpdateProvinceCustom(tx *dbs.Tx, provinceId int64, customName string, customCodes []string) error {
|
||||
if customCodes == nil {
|
||||
customCodes = []string{}
|
||||
}
|
||||
customCodesJSON, err := json.Marshal(customCodes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 清空缓存
|
||||
defer func() {
|
||||
SharedCacheLocker.Lock()
|
||||
regionProvinceNameAndIdCacheMap = map[string]int64{}
|
||||
SharedCacheLocker.Unlock()
|
||||
}()
|
||||
|
||||
return this.Query(tx).
|
||||
Pk(provinceId).
|
||||
Set("customName", customName).
|
||||
Set("customCodes", customCodesJSON).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// FindSimilarProvinces 查找类似省份名
|
||||
func (this *RegionProvinceDAO) FindSimilarProvinces(provinces []*RegionProvince, provinceName string, size int) (result []*RegionProvince) {
|
||||
if len(provinces) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var similarResult = []maps.Map{}
|
||||
|
||||
for _, province := range provinces {
|
||||
var similarityList = []float32{}
|
||||
for _, code := range province.AllCodes() {
|
||||
var similarity = utils.Similar(provinceName, code)
|
||||
if similarity > 0 {
|
||||
similarityList = append(similarityList, similarity)
|
||||
}
|
||||
}
|
||||
if len(similarityList) > 0 {
|
||||
similarResult = append(similarResult, maps.Map{
|
||||
"similarity": numberutils.Max(similarityList...),
|
||||
"province": province,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(similarResult, func(i, j int) bool {
|
||||
return similarResult[i].GetFloat32("similarity") > similarResult[j].GetFloat32("similarity")
|
||||
})
|
||||
|
||||
if len(similarResult) > size {
|
||||
similarResult = similarResult[:size]
|
||||
}
|
||||
|
||||
for _, r := range similarResult {
|
||||
result = append(result, r.Get("province").(*RegionProvince))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user