优化代码结构,修复查询逻辑,添加构建脚本

This commit is contained in:
2025-08-09 16:17:41 +08:00
parent 4bbc05fe1f
commit 3782feaf85
8 changed files with 38 additions and 15 deletions

1
.gitignore vendored
View File

@@ -8,6 +8,7 @@
*.dll
*.so
*.dylib
dist/
# Test binary, built with `go test -c`
*.test

View File

@@ -1,5 +1,4 @@
## TODO
此实现目前并不是完全并发安全的:
目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题
- 目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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
}

1
build.ps1 Normal file
View File

@@ -0,0 +1 @@
$env:GOOS="linux"; $env:GOARCH="amd64"; $env:CGO_ENABLED=0; go build -o dist/zz main.go

15
main.go
View File

@@ -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)

View File

@@ -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"`
}