using Newtonsoft.Json; using System; using System.Linq; namespace BLL { /// /// 天气记录 /// public class WeatherService { public static Model.SUBQHSEDB db = Funs.DB; /// /// 根据主键获取SUBQHSEDB /// /// /// public static Model.Weather GetWeatherByDateAndCity(DateTime date, string city) { return Funs.DB.Weather.FirstOrDefault(e => e.Date == date && e.City == city); } /// /// 添加天气 /// /// public static void AddWeather(Model.Weather weather) { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { Model.Weather newWeather = new Model.Weather { WeatherId = weather.WeatherId, City = weather.City, Date = weather.Date, WeatherRef = weather.WeatherRef, CurrTem = weather.CurrTem, AllTem = weather.AllTem }; db.Weather.InsertOnSubmit(newWeather); db.SubmitChanges(); } } /// /// 根据主键删除天气 /// /// public static void DeleteWeatherById(string weatherId) { Model.SUBQHSEDB db = Funs.DB; Model.Weather weather = db.Weather.FirstOrDefault(e => e.WeatherId == weatherId); if (weather != null) { db.Weather.DeleteOnSubmit(weather); db.SubmitChanges(); } } /// /// /// public static Model.Weather GetWeather(string projectId) { try { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { Model.Weather getWeather = new Model.Weather(); string appkey = "7416f4dd68c9352e02be31b12f15d74f"; //配置您申请的appkey var project = ProjectService.GetProjectByProjectId(projectId); string city = "天津"; if (project != null && !string.IsNullOrEmpty(project.City)) { city = project.City; } if (!string.IsNullOrEmpty(city)) { getWeather = GetWeatherByDateAndCity(DateTime.Now.Date, city); if (getWeather == null) //未生成天气记录 { //string result = BLL.CommonService.CreateGetHttpResponse("http://apis.juhe.cn/simpleWeather/query?city=" + city + "&key=" + appkey); //var j2 = JsonConvert.DeserializeObject(result); //if (j2.reason == "查询成功!") //{ // getWeather = new Model.Weather // { // WeatherId = SQLHelper.GetNewID(), // City = city, // Date = DateTime.Now.Date, // WeatherRef = j2.result.realtime.info, // CurrTem = j2.result.realtime.temperature, // AllTem = j2.result.future[0].temperature, // Humidity = j2.result.realtime.humidity, // Wid = j2.result.realtime.wid, // Direct = j2.result.realtime.direct, // Power = j2.result.realtime.power, // Aqi = j2.result.realtime.aqi, // ProjectId = projectId, // }; // db.Weather.InsertOnSubmit(getWeather); // db.SubmitChanges(); //} } } return getWeather; } } catch (Exception ex) { ErrLogInfo.WriteLog("获取天气异常。", ex); return null; } } } }