weather/db/model/weather.go

39 lines
1.0 KiB
Go
Raw Normal View History

2025-01-02 22:57:51 +08:00
package model
import (
"cc/db"
"encoding/json"
)
// Weather 天气
type Weather struct {
2025-01-03 00:45:30 +08:00
ProjectID int `db:"project_id" json:"-"` // 项目ID
UpdateAt string `db:"update_at" json:"update_at"` // 更新时间
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 {
sql := "INSERT INTO weathers (project_id, update_at, value) VALUES (?, ?, ?)"
_, rer = db.DB.Exec(sql, w.ProjectID, w.UpdateAt, w.Value)
return
}
// 更新天气
sql := "UPDATE weathers SET update_at =?, value =? WHERE project_id =?"
_, rer = db.DB.Exec(sql, w.UpdateAt, w.Value, w.ProjectID)
return
}