Files
IUQT/acquaintances/biz/model/area.go
2025-07-14 17:49:33 +08:00

68 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/json"
"gorm.io/gorm"
"os"
)
// Country 国家和地区信息结构体基于ISO 3166-1标准
type Country struct {
gorm.Model
ChineseName string `gorm:"column:chinese_name;type:varchar(50);not null;comment:'中文简称'"`
EnglishName string `gorm:"column:english_name;type:varchar(100);not null;comment:'英文简称'"`
ISO2 string `gorm:"column:iso2;type:char(2);not null;uniqueIndex:idx_iso2;comment:'ISO 3166-1两位字母码'"`
ISO3 string `gorm:"column:iso3;type:char(3);not null;uniqueIndex:idx_iso3;comment:'ISO 3166-1三位字母码'"`
NumCode string `gorm:"column:num_code;type:char(3);not null;uniqueIndex:idx_num_code;comment:'ISO 3166-1三位数字码'"`
PhoneCode string `gorm:"column:phone_code;type:varchar(20);comment:'国际电话区号(如+86)'"`
DomainSuffix string `gorm:"column:domain_suffix;type:varchar(10);comment:'国家/地区顶级域名(如.cn)'"`
}
// TableName 设置表名
func (Country) TableName() string {
return "country"
}
type CountryList struct {
CountryList []Country `json:"CountryList"`
}
func ReadData() ([]Country, error) {
// 读取JSON文件
filePath := "config/area.json" // JSON文件路径
fileData, err := os.ReadFile(filePath)
if err != nil {
hlog.Error("读取文件失败: %v\n", err)
return nil, err
}
// 解析JSON数据
var Country CountryList
if err := json.Unmarshal(fileData, &Country); err != nil {
hlog.Error("解析JSON失败: %v\n", err)
return nil, err
}
return Country.CountryList, err
}
// 表初始化示例
func InitCountriesTable(db *gorm.DB) error {
// 检查是否已有数据
var count int64
db.Model(&Country{}).Count(&count)
if count > 0 {
return nil // 已有数据,无需导入
}
// 导入ISO 3166-1标准数据
countriesData, err := ReadData()
if err != nil {
return err
}
// 批量插入数据
return db.Create(&countriesData).Error
}