using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using Spring.Data.Core;
using Spring.Data.NHibernate.Generic.Support;
using Domain;

namespace Dao.Implement
{
    public class HostFaultsRepository : RepositoryBase<HostFaults>, IHostFaultsRepository
    {
        public IQueryable<HostFaults> LoadAllByPage(out long total, int page, int rows, string order, string sort, int hotelID)
        {
            var list = this.LoadAll().Where(r => r.HostID == hotelID);
            total = list.LongCount();
            list = list.OrderBy(sort + " " + order);
            list = list.Skip((page - 1) * rows).Take(rows);
            return list;
        }

        public HostFaults Get(int hostID, string address)
        {
            return LoadAll().Where(r => r.HostID == hostID && r.Address == address).FirstOrDefault();
        }

        public void DeleteByAddress(string address, int roomTypeID)
        {
            string sql = String.Format(@"DELETE FROM tb_HostFaults WHERE [Address]={0} AND 
                HostID IN (SELECT h.ID FROM tb_Hosts h WHERE H.RoomTypeID={1})", String.Join(",", address), roomTypeID);

            Session.CreateSQLQuery(sql).ExecuteUpdate();
        }

        public void DeteleByHostID(int hostID)
        {
            Session.CreateSQLQuery("DELETE FROM tb_HostFaults WHERE HostID=?").SetInt32(0, hostID).ExecuteUpdate();
        }

        public IList<object[]> LoadCurrentHostFaultStatistics(int hotelID, IList<int> groupIDs, int type)
        {
            string sql = "";

            switch (type)
            {
                case 1://状态：1离线,0在线
                    sql = String.Format(@"SELECT DISTINCT h.RoomNumber,hf.[Address] FROM tb_HostFaults hf INNER JOIN tb_Hosts h ON h.ID=hf.HostID
                    WHERE h.GroupID IN({0}) AND h.HotelID={1} AND hf.AbnormalStatus=1 
                    ORDER BY h.RoomNumber", String.Join(",", groupIDs), hotelID);
                    break;
                case 2://电量
                    sql = String.Format(@"SELECT DISTINCT h.RoomNumber,hf.[Address] FROM tb_HostFaults hf INNER JOIN tb_Hosts h ON h.ID=hf.HostID
                    WHERE h.GroupID IN({0}) AND h.HotelID={1} AND hf.AbnormalElectricQty<26 
                    ORDER BY h.RoomNumber", String.Join(",", groupIDs), hotelID);
                    break;
                case 3://电流（10MA）
                    break;
                case 4://1901故障检测次数
                    break;
            }

            return Session.CreateSQLQuery(sql).List<object[]>();
        }

        //        public IList<object[]> LoadCurrentHostFault(int hostID)
        //        {
        //            string sql = String.Format(@"SELECT hf.[Address],hf.AbnormalStatus,hf.StatusDate,hf.AbnormalElectricQty,hf.ElectricQtyDate,
        //ISNULL((SELECT TOP 1 rtm.Name FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS AddressName,
        //ISNULL((SELECT TOP 1 rtm.EnglishName FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS AddressEnglishName,
        //ISNULL((SELECT TOP 1 rtm.TWName FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS AddressTWName
        //FROM tb_HostFaults hf INNER JOIN tb_Hosts h ON h.ID=hf.HostID WHERE h.ID={0} ORDER BY hf.[Address]", hostID);

        //            return Session.CreateSQLQuery(sql).List<object[]>();
        //        }

        //异常信息显示
        public IList<object[]> LoadHostFault(int hostID)
        {
//            string sql = String.Format(@"SELECT ISNULL((SELECT TOP 1 rtm.Name FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS AddressName,
//                            ISNULL((SELECT TOP 1 rtm.EnglishName FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS AddressEnglishName,
//                            ISNULL((SELECT TOP 1 rtm.TWName FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS AddressTWName,
//		                    ISNULL((SELECT TOP 1 rtm.OfflineDisplay FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS OfflineDisplay,
//		                    ISNULL((SELECT TOP 1 rtm.LowPowerDisplay FROM tb_RoomTypeModal rtm WHERE rtm.RoomTypeID=h.RoomTypeID AND hf.[Address]=rtm.ModalAddress),'') AS LowPowerDisplay,hf.*
//                            FROM tb_HostFaults hf INNER JOIN tb_Hosts h ON h.ID=hf.HostID WHERE h.ID={0} ORDER BY hf.[Address]", hostID);

            string sql = string.Format(@" 
            SELECT
              ISNULL(rtm.NAME, '') AS AddressName,
              ISNULL(rtm.EnglishName, '') AS AddressEnglishName,
              ISNULL(rtm.TWName, '') AS AddressTWName,
              ISNULL(rtm.OfflineDisplay, '') AS OfflineDisplay,
              ISNULL(rtm.LowPowerDisplay, '') AS LowPowerDisplay,
              hf.*
            FROM
              tb_HostFaults hf
              INNER JOIN tb_Hosts h ON h.ID = hf.HostID
              INNER JOIN tb_RoomTypeModal rtm ON rtm.RoomTypeID = h.RoomTypeID
                AND hf.[Address] = rtm.ModalAddress
            WHERE
              h.ID = {0}
            ORDER BY
              hf.[Address];",hostID);
            return Session.CreateSQLQuery(sql).List<object[]>();
        }
    }
}
