mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-30 18:16:36 +08:00
对服务增加基础的数据统计/部分代码分Package
This commit is contained in:
133
internal/db/models/regions/region_city_dao.go
Normal file
133
internal/db/models/regions/region_city_dao.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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/types"
|
||||
)
|
||||
|
||||
const (
|
||||
RegionCityStateEnabled = 1 // 已启用
|
||||
RegionCityStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type RegionCityDAO dbs.DAO
|
||||
|
||||
var regionCityNameAndIdCacheMap = map[string]int64{} // city name @ province id => id
|
||||
|
||||
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()
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *RegionCityDAO) EnableRegionCity(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionCityStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *RegionCityDAO) DisableRegionCity(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionCityStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *RegionCityDAO) FindEnabledRegionCity(tx *dbs.Tx, id int64) (*RegionCity, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", RegionCityStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*RegionCity), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *RegionCityDAO) FindRegionCityName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 根据数据ID查找城市
|
||||
func (this *RegionCityDAO) FindCityWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 创建城市
|
||||
func (this *RegionCityDAO) CreateCity(tx *dbs.Tx, provinceId int64, name string, dataId string) (int64, error) {
|
||||
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
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 根据城市名查找城市ID
|
||||
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)").
|
||||
Param("cityName", "\""+cityName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
SharedCacheLocker.Lock()
|
||||
regionCityNameAndIdCacheMap[key] = cityId
|
||||
SharedCacheLocker.Unlock()
|
||||
|
||||
return cityId, nil
|
||||
}
|
||||
22
internal/db/models/regions/region_city_dao_test.go
Normal file
22
internal/db/models/regions/region_city_dao_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRegionCityDAO_FindCityIdWithCityNameCacheable(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
now := time.Now()
|
||||
cityId, err := SharedRegionCityDAO.FindCityIdWithNameCacheable(nil, 1, "北京市")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("cityId", cityId, time.Since(now).Seconds()*1000, "ms")
|
||||
}
|
||||
}
|
||||
24
internal/db/models/regions/region_city_model.go
Normal file
24
internal/db/models/regions/region_city_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package regions
|
||||
|
||||
//
|
||||
type RegionCity struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
ProvinceId uint32 `field:"provinceId"` // 省份ID
|
||||
Name string `field:"name"` // 名称
|
||||
Codes string `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
DataId string `field:"dataId"` // 原始数据ID
|
||||
}
|
||||
|
||||
type RegionCityOperator struct {
|
||||
Id interface{} // ID
|
||||
ProvinceId interface{} // 省份ID
|
||||
Name interface{} // 名称
|
||||
Codes interface{} // 代号
|
||||
State interface{} // 状态
|
||||
DataId interface{} // 原始数据ID
|
||||
}
|
||||
|
||||
func NewRegionCityOperator() *RegionCityOperator {
|
||||
return &RegionCityOperator{}
|
||||
}
|
||||
1
internal/db/models/regions/region_city_model_ext.go
Normal file
1
internal/db/models/regions/region_city_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package regions
|
||||
155
internal/db/models/regions/region_country_dao.go
Normal file
155
internal/db/models/regions/region_country_dao.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"github.com/mozillazg/go-pinyin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
RegionCountryStateEnabled = 1 // 已启用
|
||||
RegionCountryStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => int
|
||||
|
||||
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()
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionCountryStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionCountryStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*RegionCountry, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", RegionCountryStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*RegionCountry), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 根据数据ID查找国家
|
||||
func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 根据国家名查找国家ID
|
||||
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Where("JSON_CONTAINS(codes, :countryName)").
|
||||
Param("countryName", "\""+countryName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 根据国家名查找国家ID,并可使用缓存
|
||||
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
|
||||
SharedCacheLocker.RLock()
|
||||
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
|
||||
if ok {
|
||||
SharedCacheLocker.RUnlock()
|
||||
return provinceId, nil
|
||||
}
|
||||
SharedCacheLocker.RUnlock()
|
||||
|
||||
countryId, err := this.FindCountryIdWithName(tx, countryName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
SharedCacheLocker.Lock()
|
||||
regionCountryNameAndIdCacheMap[countryName] = countryId
|
||||
SharedCacheLocker.Unlock()
|
||||
|
||||
return countryId, nil
|
||||
}
|
||||
|
||||
// 根据数据ID创建国家
|
||||
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
|
||||
op := NewRegionCountryOperator()
|
||||
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, " ")})
|
||||
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
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 查找所有可用的国家
|
||||
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(RegionCountryStateEnabled).
|
||||
Slice(&result).
|
||||
Asc("pinyin").
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
22
internal/db/models/regions/region_country_dao_test.go
Normal file
22
internal/db/models/regions/region_country_dao_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRegionCountryDAO_FindCountryIdWithCountryNameCacheable(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
now := time.Now()
|
||||
countryId, err := SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, "中国")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("countryId", countryId, time.Since(now).Seconds()*1000, "ms")
|
||||
}
|
||||
}
|
||||
24
internal/db/models/regions/region_country_model.go
Normal file
24
internal/db/models/regions/region_country_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package regions
|
||||
|
||||
//
|
||||
type RegionCountry struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 名称
|
||||
Codes string `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
DataId string `field:"dataId"` // 原始数据ID
|
||||
Pinyin string `field:"pinyin"` // 拼音
|
||||
}
|
||||
|
||||
type RegionCountryOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 名称
|
||||
Codes interface{} // 代号
|
||||
State interface{} // 状态
|
||||
DataId interface{} // 原始数据ID
|
||||
Pinyin interface{} // 拼音
|
||||
}
|
||||
|
||||
func NewRegionCountryOperator() *RegionCountryOperator {
|
||||
return &RegionCountryOperator{}
|
||||
}
|
||||
18
internal/db/models/regions/region_country_model_ext.go
Normal file
18
internal/db/models/regions/region_country_model_ext.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
)
|
||||
|
||||
func (this *RegionCountry) DecodeCodes() []string {
|
||||
if len(this.Codes) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
result := []string{}
|
||||
err := json.Unmarshal([]byte(this.Codes), &result)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
113
internal/db/models/regions/region_provider_dao.go
Normal file
113
internal/db/models/regions/region_provider_dao.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
RegionProviderStateEnabled = 1 // 已启用
|
||||
RegionProviderStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var regionProviderNameAndIdCacheMap = map[string]int64{} // provider name => id
|
||||
|
||||
type RegionProviderDAO dbs.DAO
|
||||
|
||||
func NewRegionProviderDAO() *RegionProviderDAO {
|
||||
return dbs.NewDAO(&RegionProviderDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeRegionProviders",
|
||||
Model: new(RegionProvider),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*RegionProviderDAO)
|
||||
}
|
||||
|
||||
var SharedRegionProviderDAO *RegionProviderDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedRegionProviderDAO = NewRegionProviderDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *RegionProviderDAO) EnableRegionProvider(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionProviderStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *RegionProviderDAO) DisableRegionProvider(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionProviderStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *RegionProviderDAO) FindEnabledRegionProvider(tx *dbs.Tx, id uint32) (*RegionProvider, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", RegionProviderStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*RegionProvider), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *RegionProviderDAO) FindRegionProviderName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 根据服务商名称查找服务商ID
|
||||
func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, providerName string) (int64, error) {
|
||||
SharedCacheLocker.RLock()
|
||||
providerId, ok := regionProviderNameAndIdCacheMap[providerName]
|
||||
if ok {
|
||||
SharedCacheLocker.RUnlock()
|
||||
return providerId, nil
|
||||
}
|
||||
SharedCacheLocker.RUnlock()
|
||||
|
||||
providerId, err := this.Query(tx).
|
||||
Where("JSON_CONTAINS(codes, :providerName)").
|
||||
Param("providerName", "\""+providerName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
SharedCacheLocker.Lock()
|
||||
regionProviderNameAndIdCacheMap[providerName] = providerId
|
||||
SharedCacheLocker.Unlock()
|
||||
|
||||
return providerId, nil
|
||||
}
|
||||
|
||||
// 创建Provider
|
||||
func (this *RegionProviderDAO) CreateProvider(tx *dbs.Tx, name string) (int64, error) {
|
||||
op := NewRegionProviderOperator()
|
||||
op.Name = name
|
||||
|
||||
codesJSON, err := json.Marshal([]string{name})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
33
internal/db/models/regions/region_provider_dao_test.go
Normal file
33
internal/db/models/regions/region_provider_dao_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRegionProviderDAO_FindProviderIdWithProviderNameCacheable(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
now := time.Now()
|
||||
providerId, err := SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, "电信")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("providerId", providerId, time.Since(now).Seconds()*1000, "ms")
|
||||
}
|
||||
|
||||
t.Log("=====")
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
now := time.Now()
|
||||
providerId, err := SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, "胡乱填的")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("providerId", providerId, time.Since(now).Seconds()*1000, "ms")
|
||||
}
|
||||
}
|
||||
20
internal/db/models/regions/region_provider_model.go
Normal file
20
internal/db/models/regions/region_provider_model.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package regions
|
||||
|
||||
//
|
||||
type RegionProvider struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 名称
|
||||
Codes string `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type RegionProviderOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 名称
|
||||
Codes interface{} // 代号
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewRegionProviderOperator() *RegionProviderOperator {
|
||||
return &RegionProviderOperator{}
|
||||
}
|
||||
1
internal/db/models/regions/region_provider_model_ext.go
Normal file
1
internal/db/models/regions/region_provider_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package regions
|
||||
149
internal/db/models/regions/region_province_dao.go
Normal file
149
internal/db/models/regions/region_province_dao.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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/types"
|
||||
)
|
||||
|
||||
const (
|
||||
RegionProvinceStateEnabled = 1 // 已启用
|
||||
RegionProvinceStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var regionProvinceNameAndIdCacheMap = map[string]int64{} // province name @ country id => province id
|
||||
|
||||
type RegionProvinceDAO dbs.DAO
|
||||
|
||||
func NewRegionProvinceDAO() *RegionProvinceDAO {
|
||||
return dbs.NewDAO(&RegionProvinceDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeRegionProvinces",
|
||||
Model: new(RegionProvince),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*RegionProvinceDAO)
|
||||
}
|
||||
|
||||
var SharedRegionProvinceDAO *RegionProvinceDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedRegionProvinceDAO = NewRegionProvinceDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *RegionProvinceDAO) EnableRegionProvince(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionProvinceStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *RegionProvinceDAO) DisableRegionProvince(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", RegionProvinceStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *RegionProvinceDAO) FindEnabledRegionProvince(tx *dbs.Tx, id int64) (*RegionProvince, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", RegionProvinceStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*RegionProvince), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *RegionProvinceDAO) FindRegionProvinceName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 根据数据ID查找省份
|
||||
func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 根据省份名查找省份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", "\""+provinceName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 根据省份名查找省份ID,并可使用缓存
|
||||
func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
||||
var key = provinceName + "@" + numberutils.FormatInt64(countryId)
|
||||
|
||||
SharedCacheLocker.RLock()
|
||||
provinceId, ok := regionProvinceNameAndIdCacheMap[key]
|
||||
if ok {
|
||||
SharedCacheLocker.RUnlock()
|
||||
return provinceId, nil
|
||||
}
|
||||
SharedCacheLocker.RUnlock()
|
||||
|
||||
provinceId, err := this.FindProvinceIdWithName(tx, countryId, provinceName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
SharedCacheLocker.Lock()
|
||||
regionProvinceNameAndIdCacheMap[key] = provinceId
|
||||
SharedCacheLocker.Unlock()
|
||||
|
||||
return provinceId, nil
|
||||
}
|
||||
|
||||
// 创建省份
|
||||
func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name string, dataId string) (int64, error) {
|
||||
op := NewRegionProvinceOperator()
|
||||
op.CountryId = countryId
|
||||
op.Name = name
|
||||
op.DataId = dataId
|
||||
op.State = RegionProvinceStateEnabled
|
||||
|
||||
codes := []string{name}
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 查找所有省份
|
||||
func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(tx *dbs.Tx, countryId int64) (result []*RegionProvince, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(RegionProvinceStateEnabled).
|
||||
Attr("countryId", countryId).
|
||||
Asc().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
31
internal/db/models/regions/region_province_dao_test.go
Normal file
31
internal/db/models/regions/region_province_dao_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRegionProvinceDAO_FindProvinceIdWithProvinceName(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
now := time.Now()
|
||||
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, 1, "安徽省")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
|
||||
}
|
||||
|
||||
t.Log("====")
|
||||
for i := 0; i < 5; i++ {
|
||||
now := time.Now()
|
||||
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, 2, "安徽省")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
|
||||
}
|
||||
}
|
||||
24
internal/db/models/regions/region_province_model.go
Normal file
24
internal/db/models/regions/region_province_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package regions
|
||||
|
||||
//
|
||||
type RegionProvince struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
CountryId uint32 `field:"countryId"` // 国家ID
|
||||
Name string `field:"name"` // 名称
|
||||
Codes string `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
DataId string `field:"dataId"` // 原始数据ID
|
||||
}
|
||||
|
||||
type RegionProvinceOperator struct {
|
||||
Id interface{} // ID
|
||||
CountryId interface{} // 国家ID
|
||||
Name interface{} // 名称
|
||||
Codes interface{} // 代号
|
||||
State interface{} // 状态
|
||||
DataId interface{} // 原始数据ID
|
||||
}
|
||||
|
||||
func NewRegionProvinceOperator() *RegionProvinceOperator {
|
||||
return &RegionProvinceOperator{}
|
||||
}
|
||||
18
internal/db/models/regions/region_province_model_ext.go
Normal file
18
internal/db/models/regions/region_province_model_ext.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package regions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
)
|
||||
|
||||
func (this *RegionProvince) DecodeCodes() []string {
|
||||
if len(this.Codes) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
result := []string{}
|
||||
err := json.Unmarshal([]byte(this.Codes), &result)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
5
internal/db/models/regions/utils.go
Normal file
5
internal/db/models/regions/utils.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package regions
|
||||
|
||||
import "sync"
|
||||
|
||||
var SharedCacheLocker = sync.RWMutex{}
|
||||
Reference in New Issue
Block a user