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

namespace Service.Implement
{
	public class HostRoomCardManager : GenericManagerBase<HostRoomCard>, IHostRoomCardManager
	{
        public IList<HostRoomCard> LoadAllByPage(out long total, int page, int rows, string order, string sort, int hotelID,
            string roomNumber, int roomCardTypeID, string cardNumber, string userName, DateTime startTime, DateTime endTime)
        {
            var list = CurrentRepository.LoadAll();

            list = list.Where(r => r.RoomCard.HotelID == hotelID);
            if (!String.IsNullOrEmpty(roomNumber))
            {
                list = list.Where(r => r.RoomNumber == roomNumber);
            }
            if (roomCardTypeID > -1)
            {
                list = list.Where(r => r.RoomCard.ID == roomCardTypeID);
            }
            if (!String.IsNullOrEmpty(cardNumber))
            {
                list = list.Where(r => r.CardNumber == cardNumber);
            }
            if (!String.IsNullOrEmpty(userName))
            {
                list = list.Where(r => r.UserName == userName);
            }
            list = list.Where(r => r.InCardTime >= startTime && r.InCardTime <= endTime.AddDays(1));

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

            return list.ToList();
        }

        public IList<HostRoomCard> LoadAll(int hotelID, string roomNumber, string userName)
		{
            var list = CurrentRepository.LoadAll();

            list = list.Where(r => r.HostID == hotelID);

			if (!String.IsNullOrEmpty(roomNumber))
			{
				list = list.Where(r => r.RoomNumber == roomNumber);
			}

			if (!String.IsNullOrEmpty(userName))
			{
				list = list.Where(r => r.UserName == userName);
			}

			return list.ToList();
		}

        public void DeleteAll(int hotelID)
        {
            ((IHostRoomCardRepository)CurrentRepository).DeleteAll(hotelID);
        }

        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)
        {
            return ((IHostRoomCardRepository)CurrentRepository).LoadHostRoomCard(out total, page, rows, order, sort, hotelID, cardType, roomNumber, userName, cardNumber, startDate,endDate);
        }
	}
}
