using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Dao;

namespace Service.Implement
{
    public class RoomTypeManager : GenericManagerBase<RoomType>, IRoomTypeManager
    {
        public IRoomTypeModalRepository RoomTypeModalRepository { get; set; }
        public IWXMenusRepository WXMenusRepository { get; set; }
        public IRoomTypeWXMenusRepository RoomTypeWXMenusRepository { get; set; }
        public IRoomTypeSceneRepository RoomTypeSceneRepository { get; set; }
        public IRoomTypeSceneModalRepository RoomTypeSceneModalRepository { get; set; }
        public IRoomTypeChannelsRepository RoomTypeChannelsRepository { get; set; }
        public IRoomTypeProgramFilesRepository RoomTypeProgramFilesRepository { get; set; }

        public IList<RoomType> LoadAllByPage(out long total, int page, int rows, string order, string sort, int hotelID)
        {
            return ((IRoomTypeRepository)(CurrentRepository)).LoadAllByPage(out total, page, rows, order, sort, hotelID).ToList();
        }

        public RoomType GetByCode(string code, int hotelID)
        {
            return ((IRoomTypeRepository)(CurrentRepository)).LoadAll().Where(r => r.Code == code && r.HotelID == hotelID && r.IsDeleted != true).FirstOrDefault();
        }
        /// <summary>
        /// 保存微信控制菜单
        /// </summary>
        /// <param name="entity"></param>
        private void SaveRoomTypeWXMenus(RoomType entity)
        {
            var lists = WXMenusRepository.LoadAll();
            foreach (var list in lists)
            {
                RoomTypeWXMenusRepository.Save(new RoomTypeWXMenus
                {
                    RoomType = entity,
                    Code = list.Code,
                    Name = list.Name,
                    EName = list.EName,
                    TWName = list.TWName,
                    Class = list.Class,
                    Sort = list.Sort,
                    ActiveIndicator = true
                });
            }
        }
        /// <summary>
        /// 保存房型
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override object Save(RoomType entity)
        {
            entity.Last_Modified_Time = DateTime.Now;//标识有更新
            object result = base.Save(entity);
            SaveRoomTypeWXMenus(entity);
            return result;
        }
        public override void Update(RoomType entity)
        {
            entity.Last_Modified_Time = DateTime.Now;//标识有更新
            base.Update(entity);
        }

        public override void SaveOrUpdate(RoomType entity)
        {
            entity.Last_Modified_Time = DateTime.Now;//标识有更新
            base.SaveOrUpdate(entity);
        }
        /// <summary>
        /// 复制房型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Copy(int id)
        {
            try
            {
                var roomType = ((IRoomTypeRepository)(CurrentRepository)).Get(id);
                if (roomType != null)
                {
                    List<RoomType> list = ((IRoomTypeRepository)(CurrentRepository)).LoadAll().Where(r => r.HotelID == roomType.HotelID).ToList();
                    string newCode = (Convert.ToInt32(list[list.Count - 1].Code) + 1).ToString();
                    //复制房型
                    RoomType newRoomType = new RoomType();
                    newRoomType.ID = 0;
                    newRoomType.HotelID = roomType.HotelID;
                    newRoomType.Code = newCode;
                    newRoomType.Name = roomType.Name + "-复制";
                    newRoomType.HostName = newRoomType.Name;
                    newRoomType.Default = false;
                    newRoomType.Last_Modified_Time = DateTime.Now;
                    base.Save(newRoomType);
                    //复制微信菜单
                    var roomTypeWXMenus = RoomTypeWXMenusRepository.LoadAll(id);
                    foreach (RoomTypeWXMenus rtm in roomTypeWXMenus)
                    {
                        RoomTypeWXMenus newRoomTypeWXMenus = new RoomTypeWXMenus();
                        newRoomTypeWXMenus.ID = 0;
                        newRoomTypeWXMenus.RoomType = newRoomType;
                        newRoomTypeWXMenus.Code = rtm.Code;
                        newRoomTypeWXMenus.Name = rtm.Name;
                        newRoomTypeWXMenus.EName = rtm.EName;
                        newRoomTypeWXMenus.TWName = rtm.TWName;
                        newRoomTypeWXMenus.Class = rtm.Class;
                        newRoomTypeWXMenus.Sort = rtm.Sort;
                        newRoomTypeWXMenus.ActiveIndicator = rtm.ActiveIndicator;
                        newRoomTypeWXMenus.TakePower = rtm.TakePower;
                        RoomTypeWXMenusRepository.Save(newRoomTypeWXMenus);
                    }
                    //复制房型回路
                    var roomTypeModels = RoomTypeModalRepository.LoadAll().Where(r => r.RoomType == roomType).ToList();
                    foreach (RoomTypeModal rtm in roomTypeModels)
                    {
                        RoomTypeModal newRoomTypeModal = new RoomTypeModal();
                        newRoomTypeModal.ID = 0;
                        newRoomTypeModal.RoomType = newRoomType;
                        newRoomTypeModal.ModalAddress = rtm.ModalAddress;
                        newRoomTypeModal.Outlet = rtm.Outlet;
                        newRoomTypeModal.Name = rtm.Name;
                        newRoomTypeModal.EnglishName = rtm.EnglishName;
                        newRoomTypeModal.Power = rtm.Power;
                        newRoomTypeModal.Type = rtm.Type;
                        newRoomTypeModal.Subtype = rtm.Subtype;
                        newRoomTypeModal.Default = rtm.Default;
                        newRoomTypeModal.Sort = rtm.Sort;
                        newRoomTypeModal.AppApply = rtm.AppApply;
                        newRoomTypeModal.Color = rtm.Color;
                        newRoomTypeModal.ActiveIndicator = rtm.ActiveIndicator;
                        newRoomTypeModal.WXActiveIndicator = rtm.WXActiveIndicator;
                        newRoomTypeModal.LowPower = rtm.LowPower;
                        newRoomTypeModal.TWName = rtm.TWName;
                        newRoomTypeModal.OfflineDisplay = rtm.OfflineDisplay;
                        newRoomTypeModal.LowPowerDisplay = rtm.LowPowerDisplay;
                        newRoomTypeModal.Beep = rtm.Beep;
                        newRoomTypeModal.Abnormal = rtm.Abnormal;
                        RoomTypeModalRepository.Save(newRoomTypeModal);
                    }
                    //复制房型场景
                    var roomTypeScenes = RoomTypeSceneRepository.LoadAll(roomType);
                    foreach (RoomTypeScene rts in roomTypeScenes)
                    {
                        RoomTypeScene newRoomTypeScene = new RoomTypeScene();
                        newRoomTypeScene.ID = 0;
                        newRoomTypeScene.RoomType = newRoomType;
                        newRoomTypeScene.Type = rts.Type;
                        newRoomTypeScene.Name = rts.Name;
                        newRoomTypeScene.AppApply = rts.AppApply;
                        newRoomTypeScene.EnglishName = rts.EnglishName;
                        newRoomTypeScene.TWName = rts.TWName;
                        newRoomTypeScene.GroupAddress = rts.GroupAddress;
                        newRoomTypeScene.Icon = rts.Icon;
                        newRoomTypeScene.Sort = rts.Sort;
                        newRoomTypeScene.HotelID = rts.HotelID;
                        newRoomTypeScene.ActiveIndicator = rts.ActiveIndicator;
                        newRoomTypeScene.TakeInverse = rts.TakeInverse;
                        newRoomTypeScene.AliasName = rts.AliasName;
                        RoomTypeSceneRepository.Save(newRoomTypeScene);
                        //复制房型场景回路
                        var roomTypeSceneModals = RoomTypeSceneModalRepository.LoadAll(rts);
                        foreach (RoomTypeSceneModal rtsm in roomTypeSceneModals)
                        {
                            RoomTypeSceneModal newRoomTypeSceneModal = new RoomTypeSceneModal();
                            newRoomTypeSceneModal.ID = 0;
                            newRoomTypeSceneModal.Scene = newRoomTypeScene;
                            newRoomTypeSceneModal.Modal = RoomTypeModalRepository.LoadAll().Where(r => r.RoomType == newRoomType && r.ModalAddress == rtsm.Modal.ModalAddress).FirstOrDefault();
                            newRoomTypeSceneModal.Status = rtsm.Status;
                            newRoomTypeSceneModal.Brightness = rtsm.Brightness;
                            newRoomTypeSceneModal.CurrentTemp = rtsm.CurrentTemp;
                            newRoomTypeSceneModal.SettingTemp = rtsm.SettingTemp;
                            newRoomTypeSceneModal.FanSpeed = rtsm.FanSpeed;
                            newRoomTypeSceneModal.Mode = rtsm.Mode;
                            newRoomTypeSceneModal.Valve = rtsm.Valve;
                            RoomTypeSceneModalRepository.Save(newRoomTypeSceneModal);
                        }
                    }
                    //复制房型电视频道
                    var roomTypeChannels = RoomTypeChannelsRepository.LoadAll().Where(r => r.RoomType == roomType).ToList();
                    foreach (RoomTypeChannels rtc in roomTypeChannels)
                    {
                        RoomTypeChannels newRoomTypeChannels = new RoomTypeChannels();
                        newRoomTypeChannels.ID = 0;
                        newRoomTypeChannels.RoomType = newRoomType;
                        newRoomTypeChannels.Code = rtc.Code;
                        newRoomTypeChannels.Name = rtc.Name;
                        newRoomTypeChannels.Sort = rtc.Sort;
                        RoomTypeChannelsRepository.Save(newRoomTypeChannels);
                    }
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除房型
        /// </summary>
        /// <param name="id"></param>
        public override void Delete(object id)
        {
            //删除场景
            var roomTypeScenes = RoomTypeSceneRepository.LoadAll().Where(r => r.RoomType.ID == Convert.ToInt16(id)).ToList();
            foreach (RoomTypeScene rts in roomTypeScenes)
            {
                var roomTypeSceneModals = RoomTypeSceneModalRepository.LoadAll(rts);
                foreach (RoomTypeSceneModal rtsm in roomTypeSceneModals)
                {
                    RoomTypeSceneModalRepository.Delete(rtsm.ID);
                }
                RoomTypeSceneRepository.Delete(rts.ID);
            }
            //删除微信菜单
            RoomTypeWXMenusRepository.DeleteByRoomTypeID(Convert.ToInt16(id));
            //删除电视频道
            RoomTypeChannelsRepository.DeleteByRoomTypeID(Convert.ToInt16(id));
            //删除程序文件
            //RoomTypeProgramFilesRepository.DeleteByRoomTypeID(Convert.ToInt16(id));
            //删除回路
            RoomTypeModalRepository.DeleteByRoomTypeID(Convert.ToInt16(id), -1);
            ((IRoomTypeRepository)(this.CurrentRepository)).Delete(id);
        }
        /// <summary>
        /// 删除房型（批量删除）
        /// </summary>
        /// <param name="idList"></param>
        public override void Delete(IList<object> idList)
        {
            foreach (int id in idList)
            {
                //删除场景
                IList<RoomTypeScene> roomTypeScenes = RoomTypeSceneRepository.LoadAll().Where(r => r.RoomType.ID == id).ToList();
                foreach (RoomTypeScene rts in roomTypeScenes)
                {
                    IList<RoomTypeSceneModal> roomTypeSceneModals = RoomTypeSceneModalRepository.LoadAll(rts);
                    foreach (RoomTypeSceneModal rtsm in roomTypeSceneModals)
                    {
                        RoomTypeSceneModalRepository.Delete(rtsm.ID);
                    }
                    RoomTypeSceneRepository.Delete(rts.ID);
                }
                //删除微信菜单
                RoomTypeWXMenusRepository.DeleteByRoomTypeID(id);
                //删除电视频道
                RoomTypeChannelsRepository.DeleteByRoomTypeID(id);
                //删除程序文件
                //RoomTypeProgramFilesRepository.DeleteByRoomTypeID(id);
                //删除回路
                RoomTypeModalRepository.DeleteByRoomTypeID(id, -1);
            }
            ((IRoomTypeRepository)(this.CurrentRepository)).Delete(idList);
        }
    }
}
