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

namespace Dao.Implement
{
	public class OverviewRepository : HibernateDaoSupport, IOverviewRepository
	{
		public object LoadRoomServiceState()
		{
			var result = Session.GetNamedQuery("QueryRoomServiceState").UniqueResult<object[]>();

			var state = new
			{
				DND = result[0],
				Clean = result[1],
				Call = result[2],
				Checkout = result[3],
				SOS = result[4],
				Laundry = result[5],
				Luggage = result[6],
				MealDelivery = result[7],
				DrawCloth = result[8]
			};

			return state;
		}

        public object LoadRoomRentState(int hotelID)
		{
            var result = Session.GetNamedQuery("QueryRoomRentState").SetInt32("hotelID", hotelID).UniqueResult<object[]>();

			var state = new
			{
				RentedRoom = result[0],
				RoomForRent = result[1],
				CheckoutRoom = result[2],
				EmptyRoom = result[3],
				RoomTotal = result[4],
				RentPercent = result[5],
				SomeoneRentRoom = result[6],
				NoOneRentRoom = result[7],
				SomeoneNotRentRoom = result[8],
				NoOneNotRentRoom = result[9]
			};

			return state;
		}

		public object LoadRoomState()
		{
			var result = Session.GetNamedQuery("QueryRoomState").UniqueResult<object[]>();

			var state = new
			{
				ConnectingRoom = result[0],
				NotEnergySavingRoom = result[1],
				FaultRoom = result[2],
				SunRoom = result[3],
				GroupOrientedLeasingRoom = result[4],
				SuiteRoom = result[5],
				ReligiousRoom = result[6],
				AmbulatoryRoom = result[7],
				VIPRoom = result[8]
			};

			return state;
		}

        public object LoadDoorState(int hotelID)
        {
            var result = Session.GetNamedQuery("QueryDoorState").SetInt32("hotelID", hotelID).UniqueResult<object[]>();

            var state = new
            {
                RoonOpen = result[0],
                RoomClose = result[1]
            };

            return state;
        }

        public object LoadSafeBoxState()
        {
            var result = Session.GetNamedQuery("QuerySafeBoxState").UniqueResult<object[]>();

            var state = new
            {
                SafeNormalSwitch = result[0],
                GuestsAreNotSafeOpen = result[1],
                CheckOutTheSafeShutdown = result[2]
            };

            return state;
        }

        public object LoadAirConditionState(int hotelID)
		{
            var result = Session.GetNamedQuery("QueryAirConditionState").SetInt32("hotelID", hotelID).UniqueResult<object[]>();

			var state = new
			{
				Running = result[0],
				NoRunning = result[1],
                SpeedLow = 0,
                SpeedMedium = 0,
                SpeedHigh = 0,
                ColdValveOn = 0,
                ThermalValveOn =0,
                LockModeRoom = 0,
                TimingRoom = 0,
                ManualModeRoom = 0,
                DisableSpeedHighRoom = 0,
                EnableRoomStatusRoom = 0,
                EnableSleepModeRoom = 0
			};

			return state;
		}

		public DataTable LoadHostModalRecords(out long total, int page, int rows, string order, string sort, string roomNumber, string startTime, string endTime, DeviceType? deviceType, string modalIds, int hotelID)
		{
			total = 0;

			IDbCommand cmd = Session.Connection.CreateCommand();

			cmd.CommandText = "QueryHostModalRecords";
			cmd.CommandType = CommandType.StoredProcedure;

			IDbDataParameter parameter = cmd.CreateParameter();
			parameter.ParameterName = "@total";
			parameter.DbType = DbType.Int32;
			parameter.Direction = ParameterDirection.Output;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@page";
			parameter.DbType = DbType.Int32;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = page;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@rows";
			parameter.DbType = DbType.Int32;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = rows;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@order";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = order;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@sort";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = sort;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@roomNumber";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = roomNumber;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@startTime";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = startTime;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@endTime";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = endTime;
			cmd.Parameters.Add(parameter);

			if (deviceType.HasValue)
			{
				parameter = cmd.CreateParameter();
				parameter.ParameterName = "@deviceType";
				parameter.DbType = DbType.String;
				parameter.Direction = ParameterDirection.Input;
				parameter.Value = (int)deviceType.GetValueOrDefault();
				cmd.Parameters.Add(parameter);
			}

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@modalAddresses";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = modalIds;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@hotelID";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = hotelID;
            cmd.Parameters.Add(parameter);
			
			IDbDataAdapter da = new SqlDataAdapter(cmd as SqlCommand);			
			DataSet ds = new DataSet();			
			da.Fill(ds);
			//parameter = cmd.Parameters["@total"] as IDbDataParameter;
			//total = (parameter != null && parameter.Value != null && parameter.Value != DBNull.Value) ? Convert.ToInt64(parameter.Value) : 0;
            total = ds.Tables[0].Rows.Count;
			return ds.Tables[0];
		}
        /// <summary>
        /// 获取回路异常记录
        /// </summary>
        /// <param name="type"></param>
        /// <param name="hotelID"></param>
        /// <returns></returns>
        public DataTable LoadHostAbnormalRecords(int type, int hotelID)
        {
            IDbCommand cmd = Session.Connection.CreateCommand();

            cmd.CommandText = "QueryHostAbnormalRecords";
            cmd.CommandType = CommandType.StoredProcedure;

            IDbDataParameter parameter = cmd.CreateParameter();
            parameter.ParameterName = "@type";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = type;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@hotelID";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = hotelID;
            cmd.Parameters.Add(parameter);

            IDbDataAdapter da = new SqlDataAdapter(cmd as SqlCommand);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataSet LoadHostAbnormalRecordsAll(int type)
        {
            IDbCommand cmd = Session.Connection.CreateCommand();

            cmd.CommandText = "QueryHostAbnormalRecordsAll";
            cmd.CommandType = CommandType.StoredProcedure;

            IDbDataParameter parameter = cmd.CreateParameter();
            parameter.ParameterName = "@type";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = type;
            cmd.Parameters.Add(parameter);

            IDbDataAdapter da = new SqlDataAdapter(cmd as SqlCommand);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

		public DataTable LoadEnergyStatitics(string roomNumber, DateTimeUnit dateUnit, string startDate, string endDate, DeviceType? deviceType, string modalIds)
		{
			IDbCommand cmd = Session.Connection.CreateCommand();

			cmd.CommandText = "QueryEnergyStatistics";
			cmd.CommandType = CommandType.StoredProcedure;

			var parameter = cmd.CreateParameter();
			parameter.ParameterName = "@roomNumber";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = roomNumber;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@dateUnit";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = dateUnit.ToString();
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@startDate";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = startDate;
			cmd.Parameters.Add(parameter);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@endDate";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = endDate;
			cmd.Parameters.Add(parameter);

			if (deviceType.HasValue)
			{
				parameter = cmd.CreateParameter();
				parameter.ParameterName = "@deviceType";
				parameter.DbType = DbType.String;
				parameter.Direction = ParameterDirection.Input;
				parameter.Value = (int)deviceType.GetValueOrDefault();
				cmd.Parameters.Add(parameter);
			}

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@modalIds";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = modalIds;
			cmd.Parameters.Add(parameter);

			IDbDataAdapter adapter = new SqlDataAdapter(cmd as SqlCommand);

			DataSet set = new DataSet();

			adapter.Fill(set);

			return set.Tables[0];
		}
	}
}
