2025-08-05 10:51:35 +08:00
|
|
|
package actions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-08-19 11:09:58 +08:00
|
|
|
"zzman/model"
|
|
|
|
|
|
2025-08-05 10:51:35 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const batchSize = 1000
|
|
|
|
|
|
|
|
|
|
func FindEdgesByCity(tx *gorm.DB, cityId int) ([]model.Edge, error) {
|
|
|
|
|
var edges []model.Edge
|
|
|
|
|
err := tx.Find(&edges, "city_id = ?", cityId).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to find edges: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return edges, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 11:09:58 +08:00
|
|
|
func SliceActiveEdges(tx *gorm.DB, cityId int, afterEdgeId int, count int) ([]model.Edge, error) {
|
2025-08-05 10:51:35 +08:00
|
|
|
var edges []model.Edge
|
2025-08-19 11:09:58 +08:00
|
|
|
err := tx.Limit(count).Find(&edges, "id > ? and city_id = ? and active = 1", afterEdgeId, cityId).Error
|
2025-08-05 10:51:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to find edges with offset: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return edges, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SaveEdges(tx *gorm.DB, edges []model.Edge) error {
|
|
|
|
|
if len(edges) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 分批处理边缘设备数据
|
|
|
|
|
for i := 0; i < len(edges); i += batchSize {
|
2025-08-19 11:09:58 +08:00
|
|
|
end := min(i+batchSize, len(edges))
|
2025-08-05 10:51:35 +08:00
|
|
|
|
|
|
|
|
batch := edges[i:end]
|
|
|
|
|
err := tx.Clauses(clause.OnConflict{
|
|
|
|
|
Columns: []clause.Column{{Name: "macaddr"}},
|
|
|
|
|
UpdateAll: true,
|
|
|
|
|
}).Create(&batch).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to save edges batch %d-%d: %w", i, end-1, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DisableEdgesByMacs(tx *gorm.DB, macs []string) error {
|
|
|
|
|
if len(macs) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 分批处理MAC地址列表
|
|
|
|
|
for i := 0; i < len(macs); i += batchSize {
|
2025-08-19 17:25:01 +08:00
|
|
|
end := min(i+batchSize, len(macs))
|
2025-08-05 10:51:35 +08:00
|
|
|
|
|
|
|
|
batch := macs[i:end]
|
|
|
|
|
err := tx.Model(&model.Edge{}).Where("macaddr IN ?", batch).Update("active", false).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to disable edges batch %d-%d: %w", i, end-1, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|