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 HostFaultRecordsRepository : RepositoryBase<HostFaultRecords>, IHostFaultRecordsRepository
    {
        public IList<string> LoadRoomNumbers(int hotelID)
        {
            IList<string> roomNumbers = new List<string>();
            IDbCommand cmd = Session.Connection.CreateCommand();
            cmd.CommandText = "SELECT DISTINCT hfr.RoomNumber FROM tb_HostFaultRecords hfr LEFT JOIN tb_Hosts h ON h.ID=hfr.HostID WHERE h.HotelID=" + hotelID + " ORDER BY RoomNumber";
            cmd.CommandType = CommandType.Text;
            using (IDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    roomNumbers.Add(reader["RoomNumber"].ToString());
                }
            }
            return roomNumbers;
        }

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

        public DataTable LoadHostFaultRecords(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 = "QueryHostFaultRecords";
            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];
        }
	}
}
