From 140235d069ee23e2482dd9c7e5c46498037e2f97 Mon Sep 17 00:00:00 2001 From: luorijun Date: Thu, 11 Sep 2025 00:08:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8A=82=E7=82=B9=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/update.go | 27 +++++++++++++++------------ model/config.go | 4 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/actions/update.go b/actions/update.go index 25ccb70..276ccaa 100644 --- a/actions/update.go +++ b/actions/update.go @@ -73,7 +73,8 @@ func update(tx *gorm.DB, arg UpdateArgs) error { // 更新网关配置 var newConfigs = make(map[model.Gateway][]ConfigInfo) - var changes []model.Change + var citiesUpdate []model.City + var changesCreate []model.Change for _, city := range cities { @@ -85,7 +86,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error { newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{ Change: false, Remote: jd.EdgeInfo{ - Mac: oldConfig.Macaddr, + Mac: oldConfig.EdgeMac, City: oldConfig.Cityhash, }, }) @@ -94,18 +95,17 @@ func update(tx *gorm.DB, arg UpdateArgs) error { } } count := len(configsUpdate) - if len(configsUpdate) == 0 { + if count == 0 { continue } // 如果有需要变更的节点,获取足量新节点 - offset := city.Offset if count > city.EdgesCount { slog.Warn(fmt.Sprintf("城市节点数量不足,跳过本次更新,城市:%s,节点数:%d,网关数:%d", city.Name, city.EdgesCount, count)) continue } - edges, err := SliceActiveEdges(tx, city.Id, offset, count) + edges, err := SliceActiveEdges(tx, city.Id, city.Offset, count) if err != nil { return fmt.Errorf("查询城市 %s 可用节点失败:%w", city.Name, err) } @@ -117,7 +117,10 @@ func update(tx *gorm.DB, arg UpdateArgs) error { } } - city.Offset = edges[len(edges)-1].Id + citiesUpdate = append(citiesUpdate, model.City{ + Id: city.Id, + Offset: edges[len(edges)-1].Id, + }) // 分配新节点 for i, oldConfig := range configsUpdate { @@ -125,7 +128,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error { gateway := findGateway[oldConfig.GatewayMac] edge := edges[i] - slog.Debug(fmt.Sprintf("网关配置变更,网关:%s,旧节点:%s,新节点:%s", gateway.Macaddr, oldConfig.Macaddr, edge.Macaddr)) + slog.Debug(fmt.Sprintf("网关配置变更,网关:%s,旧节点:%s,新节点:%s", gateway.Macaddr, oldConfig.EdgeMac, edge.Macaddr)) newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{ Change: true, @@ -135,16 +138,16 @@ func update(tx *gorm.DB, arg UpdateArgs) error { }, Config: model.ConfigUpdate{ Id: oldConfig.Id, - Macaddr: u.P(edge.Macaddr), + EdgeMac: u.P(edge.Macaddr), IsChange: u.P(0), IsOnline: u.P(1), }, }) - changes = append(changes, model.Change{ + changesCreate = append(changesCreate, model.Change{ Time: now, CityId: city.Id, Gateway: gateway.Macaddr, - OldEdge: oldConfig.Macaddr, + OldEdge: oldConfig.EdgeMac, NewEdge: edge.Macaddr, }) } @@ -154,7 +157,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error { step = time.Now() // 更新城市偏移量 - err = UpdateCitiesOffset(tx, cities) + err = UpdateCitiesOffset(tx, citiesUpdate) if err != nil { return fmt.Errorf("更新城市偏移量失败:%w", err) } @@ -163,7 +166,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error { step = time.Now() // 记录节点变更 - err = RecordChanges(tx, changes) + err = RecordChanges(tx, changesCreate) if err != nil { return fmt.Errorf("记录节点变更失败:%w", err) } diff --git a/model/config.go b/model/config.go index 315af58..10d652a 100644 --- a/model/config.go +++ b/model/config.go @@ -4,7 +4,7 @@ type Config struct { Id int `gorm:"column:id;primaryKey"` GatewayMac string `gorm:"column:macaddr"` Table string `gorm:"column:table"` - Macaddr string `gorm:"column:edge"` + EdgeMac string `gorm:"column:edge"` Network string `gorm:"column:network"` Cityhash string `gorm:"column:cityhash"` CityLabel string `gorm:"column:label"` @@ -25,7 +25,7 @@ type ConfigUpdate struct { Id int `gorm:"column:id;primaryKey"` GatewayMac *string `gorm:"column:macaddr"` Table *string `gorm:"column:table"` - Macaddr *string `gorm:"column:edge"` + EdgeMac *string `gorm:"column:edge"` Network *string `gorm:"column:network"` Cityhash *string `gorm:"column:cityhash"` CityLabel *string `gorm:"column:label"`