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"
|
2021-01-25 16:40:03 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
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"
|
2022-08-13 23:55:48 +08:00
|
|
|
|
"sort"
|
2021-11-08 20:52:15 +08:00
|
|
|
|
"strconv"
|
2020-11-05 11:51:37 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
RegionCityStateEnabled = 1 // 已启用
|
|
|
|
|
|
RegionCityStateDisabled = 0 // 已禁用
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type RegionCityDAO dbs.DAO
|
|
|
|
|
|
|
|
|
|
|
|
func NewRegionCityDAO() *RegionCityDAO {
|
|
|
|
|
|
return dbs.NewDAO(&RegionCityDAO{
|
|
|
|
|
|
DAOObject: dbs.DAOObject{
|
|
|
|
|
|
DB: Tea.Env,
|
|
|
|
|
|
Table: "edgeRegionCities",
|
|
|
|
|
|
Model: new(RegionCity),
|
|
|
|
|
|
PkName: "id",
|
|
|
|
|
|
},
|
|
|
|
|
|
}).(*RegionCityDAO)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var SharedRegionCityDAO *RegionCityDAO
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
dbs.OnReady(func() {
|
|
|
|
|
|
SharedRegionCityDAO = NewRegionCityDAO()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// EnableRegionCity 启用条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCityDAO) EnableRegionCity(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", RegionCityStateEnabled).
|
|
|
|
|
|
Update()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// DisableRegionCity 禁用条目
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCityDAO) DisableRegionCity(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", RegionCityStateDisabled).
|
|
|
|
|
|
Update()
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// FindEnabledRegionCity 查找启用中的条目
|
2021-01-25 16:40:03 +08:00
|
|
|
|
func (this *RegionCityDAO) FindEnabledRegionCity(tx *dbs.Tx, id int64) (*RegionCity, error) {
|
2021-01-01 23:31:30 +08:00
|
|
|
|
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", RegionCityStateEnabled).
|
|
|
|
|
|
Find()
|
|
|
|
|
|
if result == nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return result.(*RegionCity), err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// FindRegionCityName 根据主键查找名称
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCityDAO) FindRegionCityName(tx *dbs.Tx, id uint32) (string, error) {
|
|
|
|
|
|
return 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("")
|
|
|
|
|
|
}
|
2020-11-06 11:02:53 +08:00
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// FindCityWithDataId 根据数据ID查找城市
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCityDAO) FindCityWithDataId(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(RegionCityField_ValueId).
|
2020-11-06 11:02:53 +08:00
|
|
|
|
FindInt64Col(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// CreateCity 创建城市
|
2021-01-01 23:31:30 +08:00
|
|
|
|
func (this *RegionCityDAO) CreateCity(tx *dbs.Tx, provinceId int64, name string, dataId string) (int64, error) {
|
2022-07-24 09:56:27 +08:00
|
|
|
|
var op = NewRegionCityOperator()
|
2020-11-06 11:02:53 +08:00
|
|
|
|
op.ProvinceId = provinceId
|
|
|
|
|
|
op.Name = name
|
|
|
|
|
|
op.DataId = dataId
|
|
|
|
|
|
op.State = RegionCityStateEnabled
|
|
|
|
|
|
|
2023-07-07 09:52:46 +08:00
|
|
|
|
var codes = []string{name}
|
2020-11-06 11:02:53 +08:00
|
|
|
|
codesJSON, err := json.Marshal(codes)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
op.Codes = codesJSON
|
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 cityId = types.Int64(op.Id)
|
|
|
|
|
|
|
|
|
|
|
|
// value id
|
|
|
|
|
|
err = this.Query(tx).
|
|
|
|
|
|
Pk(cityId).
|
|
|
|
|
|
Set(RegionCityField_ValueId, cityId).
|
|
|
|
|
|
UpdateQuickly()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cityId, nil
|
2020-11-06 11:02:53 +08:00
|
|
|
|
}
|
2021-01-25 16:40:03 +08:00
|
|
|
|
|
2022-08-13 23:55:48 +08:00
|
|
|
|
// FindCityIdWithName 根据城市名查找城市ID
|
|
|
|
|
|
func (this *RegionCityDAO) FindCityIdWithName(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
|
|
|
|
|
|
return this.Query(tx).
|
|
|
|
|
|
Attr("provinceId", provinceId).
|
|
|
|
|
|
Where("(name=:cityName OR customName=:cityName OR JSON_CONTAINS(codes, :cityNameJSON) OR JSON_CONTAINS(customCodes, :cityNameJSON))").
|
|
|
|
|
|
Param("cityName", cityName).
|
|
|
|
|
|
Param("cityNameJSON", strconv.Quote(cityName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Result(RegionCityField_ValueId).
|
2022-08-13 23:55:48 +08:00
|
|
|
|
FindInt64Col(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// FindAllEnabledCities 获取所有城市信息
|
|
|
|
|
|
func (this *RegionCityDAO) FindAllEnabledCities(tx *dbs.Tx) (result []*RegionCity, err error) {
|
|
|
|
|
|
_, err = this.Query(tx).
|
|
|
|
|
|
State(RegionCityStateEnabled).
|
|
|
|
|
|
Slice(&result).
|
|
|
|
|
|
FindAll()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-08-13 23:55:48 +08:00
|
|
|
|
|
|
|
|
|
|
// FindAllEnabledCitiesWithProvinceId 获取某个省份下的所有城市
|
|
|
|
|
|
func (this *RegionCityDAO) FindAllEnabledCitiesWithProvinceId(tx *dbs.Tx, provinceId int64) (result []*RegionCity, err error) {
|
|
|
|
|
|
_, err = this.Query(tx).
|
|
|
|
|
|
Attr("provinceId", provinceId).
|
|
|
|
|
|
State(RegionCityStateEnabled).
|
|
|
|
|
|
Slice(&result).
|
|
|
|
|
|
FindAll()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateCityCustom 自定义城市信息
|
|
|
|
|
|
func (this *RegionCityDAO) UpdateCityCustom(tx *dbs.Tx, cityId int64, customName string, customCodes []string) error {
|
|
|
|
|
|
if customCodes == nil {
|
|
|
|
|
|
customCodes = []string{}
|
|
|
|
|
|
}
|
|
|
|
|
|
customCodesJSON, err := json.Marshal(customCodes)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this.Query(tx).
|
2023-07-07 09:52:46 +08:00
|
|
|
|
Attr(RegionCityField_ValueId, cityId).
|
2022-08-13 23:55:48 +08:00
|
|
|
|
Set("customName", customName).
|
|
|
|
|
|
Set("customCodes", customCodesJSON).
|
|
|
|
|
|
UpdateQuickly()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FindSimilarCities 查找类似城市名
|
|
|
|
|
|
func (this *RegionCityDAO) FindSimilarCities(cities []*RegionCity, cityName string, size int) (result []*RegionCity) {
|
|
|
|
|
|
if len(cities) == 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var similarResult = []maps.Map{}
|
|
|
|
|
|
|
|
|
|
|
|
for _, city := range cities {
|
|
|
|
|
|
var similarityList = []float32{}
|
|
|
|
|
|
for _, code := range city.AllCodes() {
|
|
|
|
|
|
var similarity = utils.Similar(cityName, code)
|
|
|
|
|
|
if similarity > 0 {
|
|
|
|
|
|
similarityList = append(similarityList, similarity)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(similarityList) > 0 {
|
|
|
|
|
|
similarResult = append(similarResult, maps.Map{
|
|
|
|
|
|
"similarity": numberutils.Max(similarityList...),
|
|
|
|
|
|
"city": city,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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("city").(*RegionCity))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|