package model import ( "cc/db" "encoding/json" ) // Weather 天气 type Weather struct { 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"` // 天气信息 } // 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, weather_type_name, value) VALUES (?, ?, ?, ?)" _, rer = db.DB.Exec(sql, w.ProjectID, w.UpdateAt, w.WeatherTypeName, 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 }