using Newtonsoft.Json;
using System;
using System.Linq;

namespace BLL
{
    /// <summary>
    /// 天气记录
    /// </summary>
    public class WeatherService
    {
        public static Model.SGGLDB db = Funs.DB;

        /// <summary>
        /// 根据主键获取SGGLDB
        /// </summary>
        /// <param name="weatherId"></param>
        /// <returns></returns>
        public static Model.Weather GetWeatherByDateAndCity(DateTime date, string city)
        {
            return Funs.DB.Weather.FirstOrDefault(e => e.Date == date && e.City == city);
        }

        /// <summary>
        /// 添加天气
        /// </summary>
        /// <param name="weather"></param>
        public static void AddWeather(Model.Weather weather)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(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();
            }
        }

        /// <summary>
        /// 根据主键删除天气
        /// </summary>
        /// <param name="weatherId"></param>
        public static void DeleteWeatherById(string weatherId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Weather weather = db.Weather.FirstOrDefault(e => e.WeatherId == weatherId);
            if (weather != null)
            {
                db.Weather.DeleteOnSubmit(weather);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static Model.Weather GetWeather(string projectId)
        {
            try
            {
                using (Model.SGGLDB db = new Model.SGGLDB(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<dynamic>(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;
            }
        }
    }
}