using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Domain;

namespace Dao.Implement
{
	public class HostAirRecordRepository : RepositoryBase<HostAirRecord>, IHostAirRecordRepository
	{
		public DataTable LoadHostAirRecords(out long total, int page, int rows, string order, string sort, string roomNumber, int airNo, string startTime, string endTime, string tempTypes, int hotelID)
		{
			total = 0;

			IDbCommand cmd = Session.Connection.CreateCommand();

			cmd.CommandText = "QueryHostAirRecords";
			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 = "@airNo";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = airNo;
			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);

			parameter = cmd.CreateParameter();
			parameter.ParameterName = "@tempTypes";
			parameter.DbType = DbType.String;
			parameter.Direction = ParameterDirection.Input;
			parameter.Value = tempTypes;
            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;

			return ds.Tables[0];
		}

		public HostAirRecord GetLastHostAirRecord(int hostId, int airNo, TempType type)
		{
			return LoadAll().FirstOrDefault(r => r.HostID == hostId && r.AirNo == airNo && r.TempType == type && r.EndTime == null);
		}

		public HostAirRecord GetLastHostAirRecord(int hostId, int airNo, TempType type, int temp)
		{
			return LoadAll().FirstOrDefault(r => r.HostID == hostId && r.AirNo == airNo && r.TempType == type && r.Temp == temp && r.EndTime == null);
		}


	}
}
