2025-01-02 22:57:51 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cc/db"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Weather 天气
|
|
|
|
type Weather struct {
|
2025-01-05 23:12:05 +08:00
|
|
|
ProjectID int `db:"project_id" json:"-"` // 项目ID
|
|
|
|
UpdateAt string `db:"update_at" json:"update_at"` // 更新时间
|
|
|
|
WeatherTypeName string `db:"weather_type_name" json:"weather_type_name"` // 天气类型名称
|
|
|
|
Value json.RawMessage `db:"value" json:"value"` // 天气信息
|
2025-01-02 22:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWeather 获取天气
|
|
|
|
func (w *Weather) GetWeather() (rer error) {
|
|
|
|
sql := "SELECT * FROM weathers WHERE project_id =?"
|
|
|
|
return db.DB.Get(w, sql, w.ProjectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWeather 更新天气
|
|
|
|
func (w *Weather) UpdateWeather() (rer error) {
|
|
|
|
// 如果不存在则创建
|
|
|
|
var count int
|
|
|
|
err := db.DB.Get(&count, "SELECT COUNT(*) FROM weathers WHERE project_id =?", w.ProjectID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if count == 0 {
|
2025-01-05 23:12:05 +08:00
|
|
|
sql := "INSERT INTO weathers (project_id, update_at, weather_type_name, value) VALUES (?, ?, ?, ?)"
|
|
|
|
_, rer = db.DB.Exec(sql, w.ProjectID, w.UpdateAt, w.WeatherTypeName, w.Value)
|
2025-01-02 22:57:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// 更新天气
|
|
|
|
sql := "UPDATE weathers SET update_at =?, value =? WHERE project_id =?"
|
|
|
|
_, rer = db.DB.Exec(sql, w.UpdateAt, w.Value, w.ProjectID)
|
|
|
|
return
|
|
|
|
}
|