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

namespace Dao.Implement
{
	public class HostRoomCardRepository : RepositoryBase<HostRoomCard>, IHostRoomCardRepository
	{
		public IQueryable<HostRoomCard> LoadAllByPage(out long total, int page, int rows, string order, string sort)
		{
			var list = this.LoadAll();

			total = list.LongCount();

			list = list.OrderBy(sort + " " + order);
			list = list.Skip((page - 1) * rows).Take(rows);

			return list;
		}

		public HostRoomCard GetLastHostRoomCard(int hostID, string cardNumber)
		{
			return LoadAll()
					.Where(r => r.HostID == hostID && r.CardNumber == cardNumber && r.OutCardTime == null)
					.OrderByDescending(r => r.InCardTime)
					.FirstOrDefault();
		}

		public HostRoomCard GetLastHostRoomCard(int hostID, RoomCardType cardType)
		{
			return LoadAll()
					.Where(r => r.HostID == hostID && r.CardType != null && r.CardType == cardType && r.OutCardTime == null)
					.OrderByDescending(r => r.InCardTime)
					.FirstOrDefault();
		}

		public HostRoomCard GetLastHostRoomCard(int hostID)
		{
            return LoadAll().Where(r => r.OutCardTime == null).FirstOrDefault();
		}

        public void DeleteAll(int hotelID)
        {
            Session.CreateSQLQuery("DELETE FROM tb_HostRoomCard WHERE HostID IN (SELECT ID FROM tb_Hosts WHERE HotelID = ?)").SetInt32(0, hotelID).ExecuteUpdate();
        }

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

        public DataTable LoadHostRoomCard(out long total, int page, int rows, string order, string sort, int hotelID, string cardType, string roomNumber, string userName, string cardNumber, string startDate,string endDate)
        {
            total = 0;

            IDbCommand cmd = Session.Connection.CreateCommand();

            cmd.CommandText = "QueryHostRoomCard";
            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 = "@hotelID";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = hotelID;
            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 = "@cardType";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = cardType;
            cmd.Parameters.Add(parameter);

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

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

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

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