using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        /// <summary>
        /// 根据主键获取事故类型
        /// </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);
        }

        public static Model.Weather GetWeatherByDateAndProjectId(DateTime date, string projectId)
        {
            return Funs.DB.Weather.FirstOrDefault(e => e.Date == date && e.ProjectId == projectId);
        }

        /// <summary>
        /// 添加事故类型
        /// </summary>
        /// <param name="weather"></param>
        public static void AddWeather(Model.Weather weather)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Weather newWeather = new Model.Weather
            {
                WeatherId = weather.WeatherId,
                City = weather.City,
                Date = weather.Date,
                WeatherRef = weather.WeatherRef,
                CurrTem = weather.CurrTem,
                AllTem = weather.AllTem,
                Humidity=weather.Humidity,
                Wid = weather.Wid,
                Direct = weather.Direct,
                Power = weather.Power,
                Aqi = weather.Aqi,
                ProjectId = weather.ProjectId,
            };
            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;
            }
        }        
    }
}