优化代码结构,修复查询逻辑,添加构建脚本
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user