From 3782feaf856088225cf72c2ac8ff387bcc9ef43b Mon Sep 17 00:00:00 2001 From: luorijun Date: Sat, 9 Aug 2025 16:17:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=9F=A5=E8=AF=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=9E=84=E5=BB=BA=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 3 +-- actions/cities.go | 22 ++++++++++++++++------ actions/configs.go | 5 +++-- actions/gateways.go | 5 +++-- build.ps1 | 1 + main.go | 15 ++++++++++++--- model/gateway.go | 1 + 8 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 build.ps1 diff --git a/.gitignore b/.gitignore index 8c1ca2d..7e77fca 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.dll *.so *.dylib +dist/ # Test binary, built with `go test -c` *.test diff --git a/README.md b/README.md index a09d439..962091e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ ## TODO 此实现目前并不是完全并发安全的: - -目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题 \ No newline at end of file +- 目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题 \ No newline at end of file diff --git a/actions/cities.go b/actions/cities.go index ec742f6..4b416af 100644 --- a/actions/cities.go +++ b/actions/cities.go @@ -2,8 +2,9 @@ package actions import ( "fmt" - "gorm.io/gorm" "zzman/model" + + "gorm.io/gorm" ) func FindCities(tx *gorm.DB) ([]model.City, error) { @@ -15,15 +16,24 @@ func FindCities(tx *gorm.DB) ([]model.City, error) { } func FindCitiesWithEdgesCount(tx *gorm.DB) ([]model.City, error) { - var cities []model.City + var datas []struct { + model.City + EdgesCount int `gorm:"column:edges_count"` + } err := tx.Debug(). - Select("cities.*, COUNT(edges.id) AS edges_count"). - Joins("LEFT JOIN edges ON edges.city_id = cities.id"). - Group("cities.id"). - Find(&cities).Error + Select("cityhash.*, COUNT(edge.id) AS edges_count"). + Joins("LEFT JOIN edge ON edge.city_id = cityhash.id"). + Where("cityhash.label IS NOT NULL"). + Group("cityhash.id"). + Find(&datas).Error if err != nil { return nil, fmt.Errorf("failed to find cities with edges count: %w", err) } + var cities = make([]model.City, len(datas)) + for i, data := range datas { + cities[i] = data.City + cities[i].EdgesCount = data.EdgesCount + } return cities, nil } diff --git a/actions/configs.go b/actions/configs.go index cf31454..30630de 100644 --- a/actions/configs.go +++ b/actions/configs.go @@ -1,8 +1,9 @@ package actions import ( - "gorm.io/gorm" "zzman/model" + + "gorm.io/gorm" ) func FindConfigsByGateway(tx *gorm.DB, macaddr string) ([]model.Config, error) { @@ -20,7 +21,7 @@ func CreateConfigs(tx *gorm.DB, configs []model.Config) error { } // 使用事务批量插入配置 - return tx.Create(&configs).Error + return tx.Omit("createtime", "updatetime").Create(&configs).Error } func UpdateConfigs(tx *gorm.DB, configs []model.ConfigUpdate) error { diff --git a/actions/gateways.go b/actions/gateways.go index 8001ab6..e964560 100644 --- a/actions/gateways.go +++ b/actions/gateways.go @@ -2,14 +2,15 @@ package actions import ( "fmt" - "gorm.io/gorm" "math" "zzman/model" + + "gorm.io/gorm" ) func FindGateways(tx *gorm.DB) ([]model.Gateway, error) { var gateways []model.Gateway - err := tx.Find(gateways).Error + err := tx.Where("enable = 1").Find(&gateways).Error if err != nil { return nil, err } diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..35c3833 --- /dev/null +++ b/build.ps1 @@ -0,0 +1 @@ +$env:GOOS="linux"; $env:GOARCH="amd64"; $env:CGO_ENABLED=0; go build -o dist/zz main.go \ No newline at end of file diff --git a/main.go b/main.go index d88a963..ceea2f4 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log/slog" "os" + "path/filepath" "slices" "zzman/actions" "zzman/clients" @@ -16,7 +17,14 @@ func main() { // 初始化环境 slog.Debug("初始化环境变量") - err := godotenv.Load() + ex, err := os.Executable() + if err != nil { + panic(err) + } + exPath := filepath.Dir(ex) + file := filepath.Join(exPath, ".env") + println("加载环境变量文件:", file) + err = godotenv.Load(file) if err != nil { slog.Error(fmt.Errorf("初始化变量失败:%w", err).Error()) } @@ -43,8 +51,9 @@ func main() { var args actions.UpdateArgs if len(os.Args) >= 3 { - slices.Contains(os.Args, "--mock") - args.Mock = true + if slices.Contains(os.Args, "--mock") { + args.Mock = true + } } err := actions.Update(model.DB, args) diff --git a/model/gateway.go b/model/gateway.go index 5916a95..3a0755e 100644 --- a/model/gateway.go +++ b/model/gateway.go @@ -10,6 +10,7 @@ type Gateway struct { TokenTime string `gorm:"column:token_time"` PrivateIp string `gorm:"column:inner_ip"` ProxyIp string `gorm:"column:l2ip"` + Enable bool `gorm:"column:enable"` CreateTime string `gorm:"column:createtime"` UpdateTime string `gorm:"column:updatetime"` }