2020-11-05 11:51:37 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2020-11-06 11:02:53 +08:00
|
|
|
"encoding/json"
|
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"
|
|
|
|
|
"github.com/mozillazg/go-pinyin"
|
|
|
|
|
"strings"
|
2020-11-05 11:51:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
RegionCountryStateEnabled = 1 // 已启用
|
|
|
|
|
RegionCountryStateDisabled = 0 // 已禁用
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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(id uint32) error {
|
|
|
|
|
_, err := this.Query().
|
|
|
|
|
Pk(id).
|
|
|
|
|
Set("state", RegionCountryStateEnabled).
|
|
|
|
|
Update()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 禁用条目
|
2020-11-06 11:02:53 +08:00
|
|
|
func (this *RegionCountryDAO) DisableRegionCountry(id int64) error {
|
2020-11-05 11:51:37 +08:00
|
|
|
_, err := this.Query().
|
|
|
|
|
Pk(id).
|
|
|
|
|
Set("state", RegionCountryStateDisabled).
|
|
|
|
|
Update()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找启用中的条目
|
2020-11-06 11:02:53 +08:00
|
|
|
func (this *RegionCountryDAO) FindEnabledRegionCountry(id int64) (*RegionCountry, error) {
|
2020-11-05 11:51:37 +08:00
|
|
|
result, err := this.Query().
|
|
|
|
|
Pk(id).
|
|
|
|
|
Attr("state", RegionCountryStateEnabled).
|
|
|
|
|
Find()
|
|
|
|
|
if result == nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return result.(*RegionCountry), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据主键查找名称
|
2020-11-06 11:02:53 +08:00
|
|
|
func (this *RegionCountryDAO) FindRegionCountryName(id int64) (string, error) {
|
2020-11-05 11:51:37 +08:00
|
|
|
return this.Query().
|
|
|
|
|
Pk(id).
|
|
|
|
|
Result("name").
|
|
|
|
|
FindStringCol("")
|
|
|
|
|
}
|
2020-11-06 11:02:53 +08:00
|
|
|
|
|
|
|
|
// 根据数据ID查找国家
|
|
|
|
|
func (this *RegionCountryDAO) FindCountryIdWithDataId(dataId string) (int64, error) {
|
|
|
|
|
return this.Query().
|
|
|
|
|
Attr("dataId", dataId).
|
|
|
|
|
ResultPk().
|
|
|
|
|
FindInt64Col(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据数据ID创建国家
|
|
|
|
|
func (this *RegionCountryDAO) CreateCountry(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(op)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return types.Int64(op.Id), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找所有可用的国家
|
|
|
|
|
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin() (result []*RegionCountry, err error) {
|
|
|
|
|
_, err = this.Query().
|
|
|
|
|
State(RegionCountryStateEnabled).
|
|
|
|
|
Slice(&result).
|
|
|
|
|
Asc("pinyin").
|
|
|
|
|
FindAll()
|
|
|
|
|
return
|
|
|
|
|
}
|