Files
jh-zz/actions/cities.go
2025-08-09 14:42:19 +08:00

44 lines
1011 B
Go

package actions
import (
"fmt"
"gorm.io/gorm"
"zzman/model"
)
func FindCities(tx *gorm.DB) ([]model.City, error) {
var cities []model.City
if err := tx.Find(&cities, "label is not null").Error; err != nil {
return nil, fmt.Errorf("failed to find cities: %w", err)
}
return cities, nil
}
func FindCitiesWithEdgesCount(tx *gorm.DB) ([]model.City, error) {
var cities []model.City
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
if err != nil {
return nil, fmt.Errorf("failed to find cities with edges count: %w", err)
}
return cities, nil
}
func AppendCityOffset(tx *gorm.DB, city int, offset int) error {
if offset < 0 {
return fmt.Errorf("offset must be non-negative")
}
err := tx.Model(&model.City{}).
Where("id = ?", city).
Update("offset", offset).Error
if err != nil {
return fmt.Errorf("failed to update cities offset: %w", err)
}
return nil
}