using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Domain;
using Service;
using CommonEntity;

namespace WebSite.Controllers
{
    public class AirConditionControlController : BaseController
    {
        private const int AUTHORITY_AirConditionControl = 12;
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AirConditionControlController));

        #region Services

        public IRoomTypeManager RoomTypeManager { get; set; }
        public IHostManager HostManager { get; set; }
        public IGroupManager GroupManager { get; set; }
        public IRoomStatusManager RoomStatusManager { get; set; }
        public IHostAirManager HostAirManager { get; set; }
        public ISysUserManager SysUserManager { get; set; }
        public IRoomTypeAirManager RoomTypeAirManager { get; set; }
        public IHotelSeasonManager HotelSeasonManager { get; set; }
        public IHotelAirControlManager HotelAirControlManager { get; set; }

        /// <summary>
        /// 阿宝添加的
        /// </summary>
        public IRoomNoBodyMananger RoomNoBodyMananger { get; set; }
        public ISysHotelManager SysHotelManager { get; set; }
        public IECO_SettingMananger ECO_SettingManager { get; set; }
        public ILieECOMananger LieECOManager { get; set; }

        #endregion

        /// <summary>
        /// 风速设置权限ID
        /// </summary>
        private const int AUTHORITY_SPEEDSETTING = 1002;

        #region Actions

        [Authorize]
        public ActionResult Index()
        {
            ViewData["EnableSpeed"] = SysUserManager.HasAuthority(User.Identity.Name, AUTHORITY_SPEEDSETTING);
            return View("SimonIndex");
        }
        /// <summary>
        /// 获取季节设置信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult LoadSeason()
        {
            HotelSeason hotelSeason = HotelSeasonManager.LoadByHotelID(CurrentHotelID);
            List<HotelAirControl> hotelAirControls = HotelAirControlManager.LoadByHotelID(CurrentHotelID);//控制设置信息
            SysHotel hotelData = SysHotelManager.Get(CurrentHotelID);//获取上下午时间
            RoomNobodyHowTo nn = RoomNoBodyMananger.LoadAll().FirstOrDefault(A => A.CurrentHotelID == CurrentHotelID);

            ECO_Setting ECO = ECO_SettingManager.LoadAll().FirstOrDefault(A => A.HotelID == CurrentHotelID);
            var LLL = LieECOManager.LoadAll().Where(A => A.HotelID == CurrentHotelID).OrderBy(A => A.ID).ToList();

            string GGG = CacheKey.KT_Timer_Controller + "_" + CurrentHotelCode;
            var LLL1= CSRedisCacheHelper.Get_Partition<List<LingChenECO>>(GGG, 5);

            string LieKey = CacheKey.LieECOKey + "_" + CurrentHotelID;
            if (LLL != null && LLL.Count > 0)
            {
                var LLLData = LieECOManager.LoadAll().Where(A => A.HotelID == CurrentHotelID && A.IsEnable).ToList();
                CSRedisCacheHelper.Set_Partition<List<LieECO>>(LieKey,LLLData,1);
            }
            if (nn != null)
            {
                return Json(new
                {
                    IsSuccess = true,
                    Data = hotelSeason,
                    AirControlData = hotelAirControls,
                    HotelData = new { StartDayTime = hotelData.StartDayTime, EndDayTime = hotelData.EndDayTime },
                    RoomNoBodyHowTo = nn,
                    ECO_Setting = ECO,
                    LieECOList = LLL,
                    TimerECO=LLL1
                }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new
                {
                    IsSuccess = true,
                    Data = hotelSeason,
                    AirControlData = hotelAirControls,
                    HotelData = new { StartDayTime = hotelData.StartDayTime, EndDayTime = hotelData.EndDayTime },
                    ECO_Setting = ECO,
                    LieECOList = LLL,
                    TimerECO=LLL1
                }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 季节下发
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <param name="airNo"></param>
        /// <param name="property"></param>
        /// <param name="status"></param>
        /// <param name="month1"></param>
        /// <param name="month2"></param>
        /// <param name="month3"></param>
        /// <param name="month4"></param>
        /// <param name="month5"></param>
        /// <param name="month6"></param>
        /// <param name="month7"></param>
        /// <param name="month8"></param>
        /// <param name="month9"></param>
        /// <param name="month10"></param>
        /// <param name="month11"></param>
        /// <param name="month12"></param>
        /// <returns></returns>
        public ActionResult SetAirProperty(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID, int airNo, AirProperty property, int status,
            int? month1, int? month2, int? month3, int? month4, int? month5, int? month6, int? month7, int? month8, int? month9, int? month10, int? month11, int? month12)
        {
            try
            {
                IList<string> roomNumberList = new List<string>();
                IList<Host> hosts = FindHosts(hostID, groupID, roomStatusID, roomTypeID);
                IList<string> offlineRoomNumberList = new List<string>();
                foreach (var host in hosts)
                {
                    if (host != null)
                    {
                        if (host.Status)
                        {
                            byte[] seasonData = new byte[12];
                            if (property == AirProperty.Season)
                            {
                                seasonData[0] = (byte)month1;
                                seasonData[1] = (byte)month2;
                                seasonData[2] = (byte)month3;
                                seasonData[3] = (byte)month4;
                                seasonData[4] = (byte)month5;
                                seasonData[5] = (byte)month6;
                                seasonData[6] = (byte)month7;
                                seasonData[7] = (byte)month8;
                                seasonData[8] = (byte)month9;
                                seasonData[9] = (byte)month10;
                                seasonData[10] = (byte)month11;
                                seasonData[11] = (byte)month12;
                            }
                            HostAirManager.SetAirProperty(host, airNo, property, status, seasonData);
                            roomNumberList.Add(host.RoomNumber);
                        }
                        else
                        {
                            offlineRoomNumberList.Add(host.RoomNumber);
                        }
                    }
                }
                string logDetail = "【" + String.Join(",", roomNumberList.ToArray()) + "】【" + HttpContext.InnerLanguage("AirProperty" + property.ToString()) + "】";
                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), logDetail);
                if (offlineRoomNumberList.Count > 0)
                {
                    string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), String.Join(",", offlineRoomNumberList.ToArray()));
                    return Json(new { IsSuccess = true, Message = message });
                }
                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("IssuedSuccess") });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("SingleAirconditioningSettingsIssuedFail"), ex);
                }
                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), ex.Message, false);
                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("IssuedFailedFecause") + ex.Message });
            }
        }
        /// <summary>
        /// 保存空调设置
        /// </summary>
        /// <param name="conditionType"></param>
        /// <param name="delayTime"></param>
        /// <param name="id1"></param>
        /// <param name="season1"></param>
        /// <param name="activeIndicator1"></param>
        /// <param name="status1"></param>
        /// <param name="settingTemp1"></param>
        /// <param name="mode1"></param>
        /// <param name="fanSpeed1"></param>
        /// <param name="id2"></param>
        /// <param name="season2"></param>
        /// <param name="activeIndicator2"></param>
        /// <param name="status2"></param>
        /// <param name="settingTemp2"></param>
        /// <param name="mode2"></param>
        /// <param name="fanSpeed2"></param>
        /// <param name="id3"></param>
        /// <param name="season3"></param>
        /// <param name="activeIndicator3"></param>
        /// <param name="status3"></param>
        /// <param name="settingTemp3"></param>
        /// <param name="mode3"></param>
        /// <param name="fanSpeed3"></param>
        /// <returns></returns>
        public ActionResult SaveAirControl(int yanchitime, string how, string temp, int id, int conditionType, int delayTime,
            int id1, int season1, bool activeIndicator1, int status1, int settingTemp1, int mode1, int fanSpeed1,
            int id2, int season2, bool activeIndicator2, int status2, int settingTemp2, int mode2, int fanSpeed2,
            int id3, int season3, bool activeIndicator3, int status3, int settingTemp3, int mode3, int fanSpeed3)
        {

            //阿宝修改了
            try
            {
                SysHotel currentHotel = SysHotelManager.Get(CurrentHotelID);
                //春季
                HotelAirControl entity = new HotelAirControl() { ID = id1, SysHotel = currentHotel, ConditionType = conditionType, DelayTime = delayTime, Season = season1, ActiveIndicator = activeIndicator1, Status = status1, SettingTemp = settingTemp1, Mode = mode1, FanSpeed = fanSpeed1, ModifiedDate = DateTime.Now };
                HotelAirControlManager.SaveOrUpdate(entity);
                id1 = entity.ID;
                //夏季
                entity = new HotelAirControl() { ID = id2, SysHotel = currentHotel, ConditionType = conditionType, DelayTime = delayTime, Season = season2, ActiveIndicator = activeIndicator2, Status = status2, SettingTemp = settingTemp2, Mode = mode2, FanSpeed = fanSpeed2, ModifiedDate = DateTime.Now };
                HotelAirControlManager.SaveOrUpdate(entity);
                id2 = entity.ID;
                //冬季
                entity = new HotelAirControl() { ID = id3, SysHotel = currentHotel, ConditionType = conditionType, DelayTime = delayTime, Season = season3, ActiveIndicator = activeIndicator3, Status = status3, SettingTemp = settingTemp3, Mode = mode3, FanSpeed = fanSpeed3, ModifiedDate = DateTime.Now };
                HotelAirControlManager.SaveOrUpdate(entity);
                id3 = entity.ID;

                string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                RoomNobodyHowTo no = new RoomNobodyHowTo();
                no.ID = id;
                no.CurrentHotelID = CurrentHotelID;
                no.DelayTime = yanchitime;
                no.How = how;
                no.Temperature = int.Parse(temp);
                no.CreateTime = ti;
                int id4 = 0;
                if (id == 0)
                {
                    id4 = RoomNoBodyMananger.Add(no);
                }
                else
                {
                    RoomNoBodyMananger.Update(no);
                    id4 = id;
                }
                //string logDetail = "【" + String.Join(",", roomNumberList.ToArray()) + "】【" + HttpContext.InnerLanguage("AirProperty" + property.ToString()) + "】";
                //SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), logDetail);
                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SaveSuccess"), ID1 = id1, ID2 = id2, ID3 = id3, ID4 = id4 });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("SingleAirconditioningSettingsIssuedFail"), ex);
                }
                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), ex.Message, false);
                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("SaveFailedBecause") + ex.Message });
            }
        }

        [HttpPost()]
        public ActionResult SaveAirControlRoomNoBody(TempClass obj)
        {
            return null;
        }
        /// <summary>
        /// 空调设置整体下发
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <param name="jsonAirConditionSetting"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Send(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID, string jsonAirConditionSetting)
        {
            try
            {
                IList<string> roomNumberList = new List<string>();
                IList<Host> hosts = FindHosts(hostID, groupID, roomStatusID, roomTypeID);
                IDictionary<string, string> data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonAirConditionSetting);
                IList<string> offlineRoomNumberList = new List<string>();
                foreach (var host in hosts)
                {
                    if (host == null)
                    {
                        continue;
                    }
                    if (!host.Status)
                    {
                        offlineRoomNumberList.Add(host.RoomNumber);
                        continue;
                    }
                    var hostAir = HostAirManager.GetByModalTypeID(host.ID, Convert.ToInt32(data["ModalTypeID"]));
                    if (hostAir == null)
                    {
                        throw new ApplicationException(HttpContext.InnerLanguage("AirConditioner") + "【" + data["ModalTypeID"] + "】" + HttpContext.InnerLanguage("NotExist") + "。");
                    }
                    hostAir.SettingTemp = Convert.ToInt32(data["SettingTemp"]);
                    hostAir.CompensatoryTemp = Convert.ToSingle(data["CompensatoryTemp"]);
                    hostAir.Speed = Convert.ToInt32(data["Speed"]);
                    hostAir.Mode = Convert.ToInt32(data["Mode"]);
                    hostAir.IsLockTemp = Convert.ToBoolean(data["IsLockTemp"]);
                    hostAir.LockTemp = Convert.ToInt32(data["LockTemp"]);

                    hostAir.KeepTemp = Convert.ToInt32(data["KeepTemp"]);
                    hostAir.InitTemp = Convert.ToInt32(data["InitTemp"]);
                    hostAir.HightTemp = Convert.ToInt32(data["HightTemp"]);
                    hostAir.LowerTemp = Convert.ToInt32(data["LowerTemp"]);
                    hostAir.ColdHotSwitchDelayTime = Convert.ToInt32(data["ColdHotSwitchDelayTime"]);
                    hostAir.ColdHotMode = Convert.ToInt32(data["ColdHotMode"]);
                    hostAir.DeadTemp = Convert.ToInt32(data["DeadTemp"]);
                    hostAir.HotDevition = Convert.ToInt32(data["HotDevition"]);
                    hostAir.ColdDevition = Convert.ToInt32(data["ColdDevition"]);
                    hostAir.WelcomeTime = Convert.ToInt32(data["WelcomeTime"]);
                    hostAir.RelateRoomStatus = Convert.ToBoolean(data["RelateRoomStatus"]);
                    hostAir.RelateDoorContact = Convert.ToBoolean(data["RelateDoorContact"]);
                    hostAir.FanStop = Convert.ToBoolean(data["FanStop"]);
                    hostAir.DisableFanHighSpeed = Convert.ToBoolean(data["DisableFanHighSpeed"]);

                    hostAir.SleepFlag = Convert.ToBoolean(data["SleepFlag"]);
                    hostAir.SleepStartTime = data["SleepStartTime"];
                    hostAir.SleepEndTime = data["SleepEndTime"];
                    hostAir.SleepDevition = Convert.ToInt32(data["SleepDevition"]);

                    hostAir.TimeFlag = Convert.ToBoolean(data["TimeFlag"]);
                    hostAir.TimeStartTime1 = data["TimeStartTime1"];
                    hostAir.TimeEndTime1 = data["TimeEndTime1"];
                    hostAir.TimeStartTime2 = data["TimeStartTime2"];
                    hostAir.TimeEndTime2 = data["TimeEndTime2"];
                    hostAir.TimeStartTime3 = data["TimeStartTime3"];
                    hostAir.TimeEndTime3 = data["TimeEndTime3"];

                    HostAirManager.ApplyAirConditionSetting(hostAir, host);

                    roomNumberList.Add(host.RoomNumber);
                }
                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirAllProperties"), "【" + String.Join(",", roomNumberList.ToArray()) + "】");
                if (offlineRoomNumberList.Count > 0)
                {
                    string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), String.Join(",", offlineRoomNumberList.ToArray()));
                    return Json(new { IsSuccess = true, Message = message });
                }
                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("IssuedSuccess") });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("AirConditioningSettingOverallIssuedFailed"), ex);
                }
                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirAllProperties"), ex.Message, false);
                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("IssuedFailedFecause") + ex.Message });
            }
        }

        /// <summary>
        /// 设置空调锁定温度
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <param name="airNo"></param>
        /// <param name="isLock"></param>
        /// <param name="lockTemp"></param>
        /// <returns></returns>
        public ActionResult SetAirLockTemp(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID, int airNo, bool isLock, int lockTemp)
        {
            try
            {
                IList<string> roomNumberList = new List<string>();

                IList<Host> hosts = FindHosts(hostID, groupID, roomStatusID, roomTypeID);

                IList<string> offlineRoomNumberList = new List<string>();

                foreach (var host in hosts)
                {
                    if (host != null)
                    {
                        if (host.Status)
                        {
                            HostAirManager.SetLockTemp(host, airNo, isLock, lockTemp);

                            roomNumberList.Add(host.RoomNumber);
                        }
                        else
                        {
                            offlineRoomNumberList.Add(host.RoomNumber);
                        }
                    }
                }

                string logDetail = "【" + String.Join(",", roomNumberList.ToArray()) + "】【" + HttpContext.InnerLanguage("AirPropertyLockTemp") + "】";

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), logDetail);

                if (offlineRoomNumberList.Count > 0)
                {
                    string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), String.Join(",", offlineRoomNumberList.ToArray()));

                    return Json(new { IsSuccess = true, Message = message });
                }

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("IssuedSuccess") });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("SettingTemperatureAirConditionerLockFailure"), ex);
                }

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), ex.Message, false);

                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("IssuedFailedFecause") + ex.Message });
            }
        }

        /// <summary>
        /// 设置空调睡眠模式
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <param name="airNo"></param>
        /// <param name="sleepFlag"></param>
        /// <param name="SleepDevition"></param>
        /// <param name="sleepStartTime"></param>
        /// <param name="sleepEndTime"></param>
        /// <returns></returns>
        public ActionResult SetAirSleepMode(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID, int airNo, bool sleepFlag, int sleepDevition, string sleepStartTime, string sleepEndTime)
        {
            try
            {
                IList<string> roomNumberList = new List<string>();

                IList<Host> hosts = FindHosts(hostID, groupID, roomStatusID, roomTypeID);

                IList<string> offlineRoomNumberList = new List<string>();

                foreach (var host in hosts)
                {
                    if (host != null)
                    {
                        if (host.Status)
                        {
                            HostAirManager.SetSleepMode(host, airNo, sleepFlag, sleepDevition, sleepStartTime, sleepEndTime);

                            roomNumberList.Add(host.RoomNumber);
                        }
                        else
                        {
                            offlineRoomNumberList.Add(host.RoomNumber);
                        }
                    }
                }

                string logDetail = "【" + String.Join(",", roomNumberList.ToArray()) + "】【" + HttpContext.InnerLanguage("AirPropertySleepMode") + "】";

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), logDetail);

                if (offlineRoomNumberList.Count > 0)
                {
                    string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), String.Join(",", offlineRoomNumberList.ToArray()));

                    return Json(new { IsSuccess = true, Message = message });
                }

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("IssuedSuccess") });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("SettingAirConditionerSleepModeFailure"), ex);
                }

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), ex.Message, false);

                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("IssuedFailedFecause") + ex.Message });
            }
        }

        /// <summary>
        /// 设置空调定时设置
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <param name="airNo"></param>
        /// <param name="sleepFlag"></param>
        /// <param name="SleepDevition"></param>
        /// <param name="sleepStartTime"></param>
        /// <param name="sleepEndTime"></param>
        /// <returns></returns>
        public ActionResult SetAirTimeSetting(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID,
                                            int airNo, bool timeFlag, string startTime1, string endTime1,
                                            string startTime2, string endTime2, string startTime3, string endTime3)
        {
            try
            {
                IList<string> roomNumberList = new List<string>();

                IList<Host> hosts = FindHosts(hostID, groupID, roomStatusID, roomTypeID);

                IList<string> offlineRoomNumberList = new List<string>();

                foreach (var host in hosts)
                {
                    if (host != null)
                    {
                        if (host.Status)
                        {
                            HostAirManager.SetTimeSetting(host, airNo, timeFlag, startTime1, endTime1, startTime2, endTime2, startTime3, endTime3);

                            roomNumberList.Add(host.RoomNumber);
                        }
                        else
                        {
                            offlineRoomNumberList.Add(host.RoomNumber);
                        }
                    }
                }

                string logDetail = "【" + String.Join(",", roomNumberList.ToArray()) + "】【" + HttpContext.InnerLanguage("AirPropertyTime") + "】";

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), logDetail);

                if (offlineRoomNumberList.Count > 0)
                {
                    string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), String.Join(",", offlineRoomNumberList.ToArray()));

                    return Json(new { IsSuccess = true, Message = message });
                }

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("IssuedSuccess") });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("SettingTemperatureAirConditionerLockFailure"), ex);
                }

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), ex.Message, false);

                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("IssuedFailedFecause") + ex.Message });
            }
        }


        /// <summary>
        /// 设置空调补偿温度
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <param name="airNo"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public ActionResult SetAirCompensatoryTemp(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID, int airNo, float status)
        {
            try
            {
                IList<string> roomNumberList = new List<string>();

                IList<Host> hosts = FindHosts(hostID, groupID, roomStatusID, roomTypeID);

                IList<string> offlineRoomNumberList = new List<string>();

                foreach (var host in hosts)
                {
                    if (host != null)
                    {
                        if (host.Status)
                        {
                            HostAirManager.SetCompensatoryTemp(host, airNo, status);

                            roomNumberList.Add(host.RoomNumber);
                        }
                        else
                        {
                            offlineRoomNumberList.Add(host.RoomNumber);
                        }
                    }
                }

                string logDetail = "【" + String.Join(",", roomNumberList.ToArray()) + "】【" + HttpContext.InnerLanguage("AirPropertyCompensatoryTemp") + ":" + status + "】";

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), logDetail);

                if (offlineRoomNumberList.Count > 0)
                {
                    string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), String.Join(",", offlineRoomNumberList.ToArray()));

                    return Json(new { IsSuccess = true, Message = message });
                }

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("IssuedSuccess") });
            }
            catch (Exception ex)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(HttpContext.InnerLanguage("SettingAirConditionerCompensationTemperatureFailure"), ex);
                }

                SaveSystemLog(AUTHORITY_AirConditionControl, HttpContext.InnerLanguage("SetRoomAirProperty"), ex.Message, false);

                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("IssuedFailedFecause") + ex.Message });
            }
        }

        #region 加载控件数据

        [Authorize]
        public ActionResult LoadAirConditionsForCombobox(int roomTypeID)
        {
            var roomType = RoomTypeManager.Get(roomTypeID);
            List<object> result = new List<object>();
            if (roomType != null)
            {
                var airs = RoomTypeAirManager.LoadAll(roomType);
                result = airs.Select(r => new
                {
                    r.ID,
                    CustomerName = ((Boolean)Session["isCN"]) ? r.CustomerName : r.EnglishName,
                    ModalTypeID = r.ModalType.ID,
                    roomType.ControlType,
                    //ControlTypeName = EnumDescription.GetDescription(roomType.ControlType)
                    ControlTypeName = roomType.ControlType == ControlType.Two ? HttpContext.InnerLanguage("TwoControl") : HttpContext.InnerLanguage("FourControl")
                }).ToList<object>();
            }

            return Json(result);
        }

        #endregion

        #endregion

        #region Private Methods
        /// <summary>
        /// 获取主机
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="groupID"></param>
        /// <param name="roomStatusID"></param>
        /// <param name="roomTypeID"></param>
        /// <returns></returns>
        private IList<Host> FindHosts(int? hostID, int? groupID, int? roomStatusID, int? roomTypeID)
        {
            IList<Host> hosts = new List<Host>();
            if (hostID.HasValue)
            {
                var host = HostManager.Get(hostID.GetValueOrDefault());
                if (host != null)
                {
                    hosts.Add(host);
                }
            }
            else
            {
                IList<Group> groups = new List<Group>();
                if (groupID.HasValue)
                {
                    groups = GroupManager.GetGroupList(GroupManager.Get(groupID));
                }
                RoomStatus roomStatus = null;
                if (roomStatusID.HasValue)
                {
                    roomStatus = RoomStatusManager.Get(roomStatusID);
                }
                RoomType roomType = null;
                if (roomTypeID.HasValue)
                {
                    roomType = RoomTypeManager.Get(roomTypeID);
                }
                hosts = HostManager.LoadAll(groups, roomStatus, roomType);
            }
            return hosts;
        }

        #endregion
    }

    public class TempClass
    {
        public string How { get; set; }
        public int Temperature { get; set; }
    }
}
