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"
|
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"
|
2020-11-06 11:02:53 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
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
|
|
|
|
|
|
|
2021-01-25 16:40:03 +08:00
|
|
|
|
var regionCityNameAndIdCacheMap = map[string]int64{} // city name @ province id => id
|
|
|
|
|
|
|
2020-11-05 11:51:37 +08:00
|
|
|
|
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).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
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).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
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).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
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).
|
2020-11-05 11:51:37 +08:00
|
|
|
|
Pk(id).
|
|
|
|
|
|
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).
|
|
|
|
|
|
ResultPk().
|
|
|
|
|
|
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) {
|
2020-11-06 11:02:53 +08:00
|
|
|
|
op := NewRegionCityOperator()
|
|
|
|
|
|
op.ProvinceId = provinceId
|
|
|
|
|
|
op.Name = name
|
|
|
|
|
|
op.DataId = dataId
|
|
|
|
|
|
op.State = RegionCityStateEnabled
|
|
|
|
|
|
|
|
|
|
|
|
codes := []string{name}
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
return types.Int64(op.Id), nil
|
|
|
|
|
|
}
|
2021-01-25 16:40:03 +08:00
|
|
|
|
|
2022-01-06 16:25:23 +08:00
|
|
|
|
// FindCityIdWithNameCacheable 根据城市名查找城市ID
|
2021-01-25 16:40:03 +08:00
|
|
|
|
func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
|
|
|
|
|
|
key := cityName + "@" + numberutils.FormatInt64(provinceId)
|
|
|
|
|
|
|
|
|
|
|
|
SharedCacheLocker.RLock()
|
|
|
|
|
|
cityId, ok := regionCityNameAndIdCacheMap[key]
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
SharedCacheLocker.RUnlock()
|
|
|
|
|
|
return cityId, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
SharedCacheLocker.RUnlock()
|
|
|
|
|
|
|
|
|
|
|
|
cityId, err := this.Query(tx).
|
|
|
|
|
|
Attr("provinceId", provinceId).
|
|
|
|
|
|
Where("JSON_CONTAINS(codes, :cityName)").
|
2021-11-08 20:52:15 +08:00
|
|
|
|
Param("cityName", strconv.Quote(cityName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
2021-01-25 16:40:03 +08:00
|
|
|
|
ResultPk().
|
|
|
|
|
|
FindInt64Col(0)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
SharedCacheLocker.Lock()
|
|
|
|
|
|
regionCityNameAndIdCacheMap[key] = cityId
|
|
|
|
|
|
SharedCacheLocker.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
return cityId, nil
|
|
|
|
|
|
}
|
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
|
|
|
|
|
|
}
|