2021-01-25 16:40:03 +08:00
|
|
|
|
package regions
|
2020-11-05 11:51:37 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2020-11-06 11:02:53 +08:00
|
|
|
|
"encoding/json"
|
2022-08-13 23:55:48 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
2023-07-07 09:52:46 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/regionconfigs"
|
2020-11-05 11:51:37 +08:00
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
|
|
"github.com/iwind/TeaGo/Tea"
|
|
|
|
|
|
"github.com/iwind/TeaGo/dbs"
|
2022-08-13 23:55:48 +08:00
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2020-11-06 11:02:53 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
|
|
|
|
|
"github.com/mozillazg/go-pinyin"
|
2022-08-13 23:55:48 +08:00
|
|
|
|
"sort"
|
2021-11-08 20:52:15 +08:00
|
|
|
|
"strconv"
|
2020-11-06 11:02:53 +08:00
|
|
|
|
"strings"
|
2020-11-05 11:51:37 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
RegionCountryStateEnabled = 1 // 已启用
|
|
|
|
|
|
RegionCountryStateDisabled = 0 // 已禁用
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
var regionCountryIdAndNameCacheMap = map[int64]string{} // country id => name
|
2021-01-25 16:40:03 +08:00
|
|
|
|
|
2020-11-05 11:51:37 +08:00
|
|
|
|
type RegionCountryDAO dbs.DAO
|
|
|
|
|
|
|
|
|
|
|
|
func NewRegionCountryDAO() *RegionCountryDAO {
|
|
|
|
|
|
return dbs.NewDAO(&RegionCountryDAO{
|
|
|
|
|
|
DAOObject: dbs.DAOObject{
|
|
|
|
|
|
DB: Tea.Env,
|
|
|
|
|
|
Table: "edgeRegionCountries",
|
|
|
|
|
|
Model: new(RegionCountry),
|
|
|
|
|
|
PkName: "id",
|
|
|
|
|
|
},
|
|
|
|
|
|
}).(*RegionCountryDAO)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var SharedRegionCountryDAO *RegionCountryDAO
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
dbs.OnReady(func() {
|
|
|
|
|
|
SharedRegionCountryDAO = NewRegionCountryDAO()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// EnableRegionCountry 启用条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
|
|
|
|
|
_, err := this.Query(tx).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Attr("valueId", id).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Set("state", RegionCountryStateEnabled).
|
|
|
|
|
|
Update()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// DisableRegionCountry 禁用条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
|
|
|
|
|
_, err := this.Query(tx).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Attr("valueId", id).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Set("state", RegionCountryStateDisabled).
|
|
|
|
|
|
Update()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// FindEnabledRegionCountry 查找启用中的条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*RegionCountry, error) {
|
|
|
|
|
|
result, err := this.Query(tx).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Attr("valueId", id).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Attr("state", RegionCountryStateEnabled).
|
|
|
|
|
|
Find()
|
|
|
|
|
|
if result == nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result.(*RegionCountry), err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// FindRegionCountryName 根据主键查找名称
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (string, error) {
|
2021-12-05 18:58:14 +08:00
|
|
|
|
SharedCacheLocker.Lock()
|
|
|
|
|
|
defer SharedCacheLocker.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
name, ok := regionCountryIdAndNameCacheMap[id]
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
return name, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
name, err := this.Query(tx).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Attr("valueId", id).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Result("name").
|
|
|
|
|
|
FindStringCol("")
|
2021-12-05 18:58:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-13 23:55:48 +08:00
|
|
|
|
if len(name) > 0 {
|
|
|
|
|
|
regionCountryIdAndNameCacheMap[id] = name
|
|
|
|
|
|
}
|
2021-12-05 18:58:14 +08:00
|
|
|
|
return name, nil
|
2020-11-05 11:51:37 +08:00
|
|
|
|
}
|
2020-11-06 11:02:53 +08:00
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// FindCountryIdWithDataId 根据数据ID查找国家
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
|
|
|
|
|
return this.Query(tx).
|
2020-11-06 11:02:53 +08:00
|
|
|
|
Attr("dataId", dataId).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Result(RegionCountryField_ValueId).
|
2020-11-09 10:44:00 +08:00
|
|
|
|
FindInt64Col(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// FindCountryIdWithName 根据国家名查找国家ID
|
2021-01-25 16:40:03 +08:00
|
|
|
|
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
2021-01-01 23:31:30 +08:00
|
|
|
|
return this.Query(tx).
|
2022-08-13 23:55:48 +08:00
|
|
|
|
Where("(name=:countryName OR JSON_CONTAINS(codes, :countryNameJSON) OR customName=:countryName OR JSON_CONTAINS(customCodes, :countryNameJSON))").
|
|
|
|
|
|
Param("countryName", countryName).
|
|
|
|
|
|
Param("countryNameJSON", strconv.Quote(countryName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Result(RegionCountryField_ValueId).
|
2020-11-06 11:02:53 +08:00
|
|
|
|
FindInt64Col(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-05 18:58:14 +08:00
|
|
|
|
// CreateCountry 根据数据ID创建国家
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
|
2022-07-24 09:56:27 +08:00
|
|
|
|
var op = NewRegionCountryOperator()
|
2020-11-06 11:02:53 +08:00
|
|
|
|
op.Name = name
|
|
|
|
|
|
|
|
|
|
|
|
pinyinPieces := pinyin.Pinyin(name, pinyin.NewArgs())
|
|
|
|
|
|
pinyinResult := []string{}
|
|
|
|
|
|
for _, piece := range pinyinPieces {
|
|
|
|
|
|
pinyinResult = append(pinyinResult, strings.Join(piece, " "))
|
|
|
|
|
|
}
|
|
|
|
|
|
pinyinJSON, err := json.Marshal([]string{strings.Join(pinyinResult, " ")})
|
2023-08-08 16:46:17 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
2020-11-06 11:02:53 +08:00
|
|
|
|
op.Pinyin = pinyinJSON
|
|
|
|
|
|
|
|
|
|
|
|
codes := []string{name}
|
|
|
|
|
|
codesJSON, err := json.Marshal(codes)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
op.Codes = codesJSON
|
|
|
|
|
|
|
|
|
|
|
|
op.DataId = dataId
|
|
|
|
|
|
op.State = RegionCountryStateEnabled
|
2021-01-01 23:31:30 +08:00
|
|
|
|
err = this.Save(tx, op)
|
2020-11-06 11:02:53 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
2023-07-07 09:52:46 +08:00
|
|
|
|
var countryId = types.Int64(op.Id)
|
|
|
|
|
|
|
|
|
|
|
|
err = this.Query(tx).
|
|
|
|
|
|
Pk(countryId).
|
|
|
|
|
|
Set(RegionCountryField_ValueId, countryId).
|
|
|
|
|
|
UpdateQuickly()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return countryId, nil
|
2020-11-06 11:02:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-13 23:55:48 +08:00
|
|
|
|
// FindAllEnabledCountriesOrderByPinyin 查找所有可用的国家并按拼音排序
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
2023-07-07 09:52:46 +08:00
|
|
|
|
ones, err := this.Query(tx).
|
2020-11-06 11:02:53 +08:00
|
|
|
|
State(RegionCountryStateEnabled).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Asc("JSON_EXTRACT(pinyin, '$[0]')").
|
2020-11-06 11:02:53 +08:00
|
|
|
|
FindAll()
|
2023-07-07 09:52:46 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// resort China special regions
|
|
|
|
|
|
var chinaRegionMap = map[int64]*RegionCountry{} // countryId => *RegionCountry
|
|
|
|
|
|
for _, one := range ones {
|
|
|
|
|
|
var country = one.(*RegionCountry)
|
|
|
|
|
|
var valueId = int64(country.ValueId)
|
|
|
|
|
|
if regionconfigs.CheckRegionIsInGreaterChina(valueId) {
|
|
|
|
|
|
chinaRegionMap[valueId] = country
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, one := range ones {
|
|
|
|
|
|
var country = one.(*RegionCountry)
|
|
|
|
|
|
var valueId = int64(country.ValueId)
|
|
|
|
|
|
if valueId == regionconfigs.RegionChinaId {
|
|
|
|
|
|
result = append(result, country)
|
|
|
|
|
|
|
|
|
|
|
|
// add hk, tw, mo, mainland ...
|
|
|
|
|
|
for _, subRegionId := range regionconfigs.FindAllGreaterChinaSubRegionIds() {
|
|
|
|
|
|
subRegion, ok := chinaRegionMap[subRegionId]
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
result = append(result, subRegion)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if regionconfigs.CheckRegionIsInGreaterChina(valueId) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
result = append(result, country)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-06 11:02:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-08-13 23:55:48 +08:00
|
|
|
|
|
|
|
|
|
|
// FindAllCountries 查找所有可用的国家
|
|
|
|
|
|
func (this *RegionCountryDAO) FindAllCountries(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
|
|
|
|
|
_, err = this.Query(tx).
|
|
|
|
|
|
State(RegionCountryStateEnabled).
|
|
|
|
|
|
Slice(&result).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Asc(RegionCountryField_ValueId).
|
2022-08-13 23:55:48 +08:00
|
|
|
|
FindAll()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateCountryCustom 修改国家/地区自定义
|
|
|
|
|
|
func (this *RegionCountryDAO) UpdateCountryCustom(tx *dbs.Tx, countryId 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()
|
|
|
|
|
|
regionCountryIdAndNameCacheMap = map[int64]string{}
|
|
|
|
|
|
SharedCacheLocker.Unlock()
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
return this.Query(tx).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Attr("valueId", countryId).
|
2022-08-13 23:55:48 +08:00
|
|
|
|
Set("customName", customName).
|
|
|
|
|
|
Set("customCodes", customCodesJSON).
|
|
|
|
|
|
UpdateQuickly()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FindSimilarCountries 查找类似国家/地区名
|
|
|
|
|
|
func (this *RegionCountryDAO) FindSimilarCountries(countries []*RegionCountry, countryName string, size int) (result []*RegionCountry) {
|
|
|
|
|
|
if len(countries) == 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var similarResult = []maps.Map{}
|
|
|
|
|
|
|
|
|
|
|
|
for _, country := range countries {
|
|
|
|
|
|
var similarityList = []float32{}
|
|
|
|
|
|
for _, code := range country.AllCodes() {
|
|
|
|
|
|
var similarity = utils.Similar(countryName, code)
|
|
|
|
|
|
if similarity > 0 {
|
|
|
|
|
|
similarityList = append(similarityList, similarity)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(similarityList) > 0 {
|
|
|
|
|
|
similarResult = append(similarResult, maps.Map{
|
|
|
|
|
|
"similarity": numberutils.Max(similarityList...),
|
|
|
|
|
|
"country": country,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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("country").(*RegionCountry))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|