2025-01-19 12:21:00 +08:00
|
|
|
class City < ApplicationRecord
|
|
|
|
extend FriendlyId
|
2025-01-22 14:04:58 +08:00
|
|
|
friendly_id :slug_candidates, use: :slugged
|
2025-02-12 13:08:12 +08:00
|
|
|
belongs_to :country, optional: true
|
2025-02-08 17:42:50 +08:00
|
|
|
belongs_to :state, optional: true
|
2025-01-19 12:21:00 +08:00
|
|
|
|
|
|
|
has_many :weather_arts, dependent: :destroy
|
|
|
|
|
2025-01-27 00:43:18 +08:00
|
|
|
has_many :visits, class_name: "Ahoy::Visit", foreign_key: :city_id
|
|
|
|
has_many :events, class_name: "Ahoy::Event", foreign_key: :city_id
|
|
|
|
|
2025-01-23 14:10:13 +08:00
|
|
|
delegate :region, to: :country
|
|
|
|
|
2025-01-19 12:21:00 +08:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :latitude, presence: true
|
|
|
|
validates :longitude, presence: true
|
|
|
|
|
2025-01-21 18:27:26 +08:00
|
|
|
delegate :region, to: :country
|
|
|
|
|
2025-01-22 14:04:58 +08:00
|
|
|
scope :by_region, ->(region_id) { joins(:country).where(countries: { region_id: region_id }) }
|
|
|
|
scope :by_country, ->(country_id) { where(country_id: country_id) }
|
|
|
|
scope :active, -> { where(active: true) }
|
2025-02-07 13:11:42 +08:00
|
|
|
scope :inactive, -> { where(active: false) }
|
2025-01-22 14:04:58 +08:00
|
|
|
|
2025-02-12 13:28:47 +08:00
|
|
|
# 在 City 模型中
|
|
|
|
scope :by_popularity, ->(period = :year, limit = 100) {
|
|
|
|
# 根据时间周期确定时间范围
|
2025-02-12 17:54:57 +08:00
|
|
|
start_time =
|
2025-02-12 13:28:47 +08:00
|
|
|
case period.to_sym
|
2025-02-12 17:54:57 +08:00
|
|
|
when :day
|
|
|
|
1.day.ago
|
2025-02-12 13:28:47 +08:00
|
|
|
when :week
|
2025-02-12 17:54:57 +08:00
|
|
|
1.week.ago
|
2025-02-12 13:28:47 +08:00
|
|
|
when :month
|
2025-02-12 17:54:57 +08:00
|
|
|
1.month.ago
|
2025-02-12 13:28:47 +08:00
|
|
|
when :year
|
2025-02-12 17:54:57 +08:00
|
|
|
1.year.ago
|
2025-02-12 13:28:47 +08:00
|
|
|
else
|
2025-02-12 17:54:57 +08:00
|
|
|
1.year.ago
|
2025-02-12 13:28:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# 根据数据库类型构建不同的查询
|
|
|
|
base_query = if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite"
|
|
|
|
joins(<<-SQL.squish)
|
2025-02-13 15:25:12 +08:00
|
|
|
LEFT JOIN ahoy_events ON#{' '}
|
2025-02-12 13:28:47 +08:00
|
|
|
json_extract(ahoy_events.properties, '$.city_id') = cities.id
|
|
|
|
AND json_extract(ahoy_events.properties, '$.event_type') = 'city_view'
|
2025-02-12 17:54:57 +08:00
|
|
|
AND ahoy_events.time > '#{start_time}'
|
2025-02-12 13:28:47 +08:00
|
|
|
SQL
|
2025-01-27 00:43:18 +08:00
|
|
|
else
|
2025-02-12 13:28:47 +08:00
|
|
|
joins(<<-SQL.squish)
|
2025-02-13 15:25:12 +08:00
|
|
|
LEFT JOIN ahoy_events ON#{' '}
|
2025-02-12 13:28:47 +08:00
|
|
|
(ahoy_events.properties::jsonb->>'city_id')::integer = cities.id
|
|
|
|
AND ahoy_events.properties::jsonb->>'event_type' = 'city_view'
|
2025-02-12 17:54:57 +08:00
|
|
|
AND ahoy_events.time > '#{start_time}'
|
2025-02-12 13:28:47 +08:00
|
|
|
SQL
|
2025-01-27 00:43:18 +08:00
|
|
|
end
|
2025-02-12 13:28:47 +08:00
|
|
|
|
|
|
|
base_query
|
|
|
|
.group("cities.id")
|
|
|
|
.select("cities.*, COUNT(ahoy_events.id) as visit_count")
|
|
|
|
.order("visit_count DESC")
|
|
|
|
.limit(limit)
|
2025-01-27 00:43:18 +08:00
|
|
|
}
|
|
|
|
|
2025-02-08 17:42:50 +08:00
|
|
|
scope :least_popular_active, ->(limit = 100) {
|
2025-01-27 00:50:35 +08:00
|
|
|
if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite"
|
2025-01-27 00:48:07 +08:00
|
|
|
active
|
|
|
|
.joins("LEFT JOIN ahoy_events ON json_extract(ahoy_events.properties, '$.city_id') = cities.id
|
|
|
|
AND json_extract(ahoy_events.properties, '$.event_type') = 'city_view'")
|
|
|
|
.group("cities.id")
|
|
|
|
.select("cities.*, COUNT(ahoy_events.id) as visit_count")
|
2025-02-08 17:42:50 +08:00
|
|
|
.order("visit_count ASC, cities.name ASC").limit(limit)
|
2025-01-27 00:48:07 +08:00
|
|
|
else
|
|
|
|
active
|
2025-01-27 01:05:16 +08:00
|
|
|
.joins("LEFT JOIN ahoy_events ON (ahoy_events.properties::jsonb->>'city_id')::integer = cities.id
|
|
|
|
AND ahoy_events.properties::jsonb->>'event_type' = 'city_view'")
|
2025-01-27 00:48:07 +08:00
|
|
|
.group("cities.id")
|
|
|
|
.select("cities.*, COUNT(ahoy_events.id) as visit_count")
|
2025-02-08 17:42:50 +08:00
|
|
|
.order("visit_count ASC, cities.name ASC").limit(limit)
|
2025-01-27 00:48:07 +08:00
|
|
|
end
|
|
|
|
}
|
2025-02-08 17:42:50 +08:00
|
|
|
scope :most_popular_inactive, ->(limit = 100) {
|
2025-01-28 01:44:07 +08:00
|
|
|
if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite"
|
2025-01-28 01:43:59 +08:00
|
|
|
where(active: false)
|
|
|
|
.joins("LEFT JOIN ahoy_events ON json_extract(ahoy_events.properties, '$.city_id') = cities.id
|
|
|
|
AND json_extract(ahoy_events.properties, '$.event_type') = 'city_view'")
|
|
|
|
.group("cities.id")
|
|
|
|
.select("cities.*, COUNT(ahoy_events.id) as visit_count")
|
2025-02-08 17:42:50 +08:00
|
|
|
.order("COUNT(ahoy_events.id) DESC, cities.name ASC").limit(limit)
|
2025-01-28 01:43:59 +08:00
|
|
|
else
|
|
|
|
where(active: false)
|
|
|
|
.joins("LEFT JOIN ahoy_events ON (ahoy_events.properties::jsonb->>'city_id')::integer = cities.id
|
|
|
|
AND ahoy_events.properties::jsonb->>'event_type' = 'city_view'")
|
|
|
|
.group("cities.id")
|
|
|
|
.select("cities.*, COUNT(ahoy_events.id) as visit_count")
|
2025-02-08 17:42:50 +08:00
|
|
|
.order("COUNT(ahoy_events.id) DESC, cities.name ASC").limit(limit)
|
2025-01-28 01:43:59 +08:00
|
|
|
end
|
|
|
|
}
|
2025-02-12 14:00:03 +08:00
|
|
|
scope :search_by_name, ->(query) {
|
|
|
|
return all if query.blank?
|
|
|
|
|
|
|
|
decoded_query = URI.decode_www_form_component(query).downcase
|
|
|
|
|
|
|
|
where(
|
|
|
|
"LOWER(cities.name) LIKE :query", query: "%#{decoded_query}%"
|
|
|
|
)
|
|
|
|
}
|
2025-01-28 01:43:59 +08:00
|
|
|
|
2025-02-15 00:19:04 +08:00
|
|
|
# 定义 latest_weather_art 关联
|
|
|
|
has_one :latest_weather_art, -> { order(weather_date: :desc) },
|
2025-02-15 12:20:24 +08:00
|
|
|
class_name: "WeatherArt"
|
2025-02-15 00:19:04 +08:00
|
|
|
|
|
|
|
# 包含最新天气艺术的 scope
|
|
|
|
scope :with_latest_weather_art, -> {
|
|
|
|
includes(:latest_weather_art)
|
|
|
|
}
|
|
|
|
|
|
|
|
# 只获取有最新天气艺术的城市
|
|
|
|
scope :has_weather_art, -> {
|
|
|
|
joins(:weather_arts).distinct
|
|
|
|
}
|
|
|
|
|
|
|
|
# 按最新天气更新时间排序
|
|
|
|
scope :order_by_latest_weather, -> {
|
|
|
|
joins(:weather_arts)
|
2025-02-15 12:20:24 +08:00
|
|
|
.group("cities.id")
|
|
|
|
.order("MAX(weather_arts.weather_date) DESC")
|
2025-02-15 00:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# 获取最近24小时内更新过天气的城市
|
|
|
|
scope :recently_updated, -> {
|
|
|
|
joins(:weather_arts)
|
2025-02-15 12:20:24 +08:00
|
|
|
.where("weather_arts.weather_date > ?", 24.hours.ago)
|
2025-02-15 00:19:04 +08:00
|
|
|
.distinct
|
|
|
|
}
|
|
|
|
|
2025-01-27 00:48:07 +08:00
|
|
|
|
2025-01-21 18:27:26 +08:00
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2025-01-22 14:04:58 +08:00
|
|
|
def slug_candidates
|
|
|
|
[
|
|
|
|
:name,
|
|
|
|
[ :country, :name ]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2025-01-21 18:27:26 +08:00
|
|
|
def localized_name
|
2025-01-28 01:49:13 +08:00
|
|
|
I18n.t("cities.#{name.parameterize.underscore}", default: name)
|
2025-01-21 18:27:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
"#{name}, #{country}"
|
|
|
|
end
|
|
|
|
|
2025-01-19 12:21:00 +08:00
|
|
|
def should_generate_new_friendly_id?
|
|
|
|
name_changed? || super
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ransackable_associations(auth_object = nil)
|
|
|
|
[ "weather_arts" ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
2025-01-25 00:54:23 +08:00
|
|
|
[ "active", "country_id", "created_at", "id", "id_value", "last_image_generation", "last_weather_fetch", "latitude", "longitude", "name", "priority", "region", "slug", "timezone", "updated_at" ]
|
2025-01-19 12:21:00 +08:00
|
|
|
end
|
|
|
|
|
2025-01-23 23:59:48 +08:00
|
|
|
def last_weather_fetch
|
|
|
|
# latest_weather_art&.created_at
|
|
|
|
Rails.cache.fetch("city/#{id}/last_weather_fetch", expires_in: 1.hour) do
|
|
|
|
latest_weather_art&.created_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_image_generation
|
|
|
|
# latest_weather_art&.image&.created_at
|
|
|
|
Rails.cache.fetch("city/#{id}/last_image_generation", expires_in: 1.hour) do
|
|
|
|
latest_weather_art&.image&.created_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-01-20 18:02:28 +08:00
|
|
|
def latest_weather_art
|
|
|
|
weather_arts.order(weather_date: :desc).first
|
|
|
|
end
|
2025-01-27 00:43:18 +08:00
|
|
|
|
2025-02-07 17:32:49 +08:00
|
|
|
def view_count(period = :day)
|
|
|
|
start_time = case period
|
|
|
|
when :day
|
|
|
|
1.day.ago
|
|
|
|
when :week
|
|
|
|
7.days.ago
|
|
|
|
when :month
|
|
|
|
1.month.ago
|
|
|
|
when :year
|
|
|
|
1.year.ago
|
|
|
|
else
|
|
|
|
1.year.ago
|
|
|
|
end
|
2025-01-27 00:43:36 +08:00
|
|
|
if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite"
|
2025-02-07 17:32:49 +08:00
|
|
|
# Ahoy::Event.where("json_extract(properties, '$.event_type') = 'city_view' AND json_extract(properties, '$.city_id') = ?", self.id).count
|
|
|
|
Ahoy::Event
|
|
|
|
.where("time >= ?", start_time)
|
|
|
|
.where("json_extract(properties, '$.event_type') = 'city_view' AND json_extract(properties, '$.city_id') = ?",
|
|
|
|
self.id)
|
|
|
|
.count
|
2025-01-27 00:43:18 +08:00
|
|
|
else
|
2025-02-07 17:32:49 +08:00
|
|
|
# Ahoy::Event.where("properties::jsonb->>'event_type' = 'city_view' AND (properties::jsonb->>'city_id')::integer = ?", self.id).count
|
|
|
|
Ahoy::Event
|
|
|
|
.where("time >= ?", start_time)
|
|
|
|
.where("properties::jsonb->>'event_type' = 'city_view' AND (properties::jsonb->>'city_id')::integer = ?",
|
|
|
|
self.id)
|
|
|
|
.count
|
|
|
|
end
|
2025-02-15 12:20:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_current_time(type = :date, use_local_timezone = true)
|
|
|
|
# 获取时区
|
|
|
|
timezone_info = self.country&.timezones.present? ?
|
|
|
|
eval(self.country.timezones).first :
|
|
|
|
{ "zoneName" => "UTC", "gmtOffsetName" => "UTC+00:00" }
|
|
|
|
|
|
|
|
# 设置时区对象
|
|
|
|
time_zone = ActiveSupport::TimeZone[timezone_info["zoneName"]] ||
|
|
|
|
ActiveSupport::TimeZone["UTC"]
|
|
|
|
time = Time.current
|
|
|
|
|
|
|
|
case type
|
|
|
|
when :date
|
|
|
|
# 格式化日期
|
|
|
|
time.strftime("%B %d, %Y")
|
|
|
|
when :time
|
|
|
|
use_local_timezone ?
|
|
|
|
"#{time.in_time_zone(time_zone).strftime('%H:%M')} #{timezone_info['gmtOffsetName']}" :
|
|
|
|
"#{time.utc.strftime('%H:%M')} UTC"
|
|
|
|
when :all
|
|
|
|
# 返回日期 + 时间 + UTC 信息
|
|
|
|
date = time.strftime("%B %d, %Y")
|
|
|
|
time = use_local_timezone ?
|
|
|
|
updated_at.in_time_zone(time_zone).strftime("%H:%M") + " #{timezone_info['gmtOffsetName']}" :
|
|
|
|
updated_at.utc.strftime("%H:%M") + " UTC"
|
|
|
|
"#{date} #{time}"
|
|
|
|
else
|
|
|
|
"Unknown #{type}"
|
2025-01-27 00:43:18 +08:00
|
|
|
end
|
2025-02-15 12:20:24 +08:00
|
|
|
end
|
2025-01-19 12:21:00 +08:00
|
|
|
end
|