using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using Domain;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Hql;
using NHibernate.SqlTypes;

namespace Dao.Implement
{
	public class RoomServiceRepository : RepositoryBase<RoomService>, IRoomServiceRepository
	{
		public IList<RoomService> LoadAllByPage(out long total, int page, int rows, string order, string sort, string roomNumber, string startTime, string endTime, string[] services)
		{
			ICriteria criteria = Session.CreateCriteria(typeof(RoomService));

			if (!String.IsNullOrWhiteSpace(roomNumber))
			{
				criteria.Add(Expression.Like("RoomNumber", "%" + roomNumber + "%"));
			}

			if (!String.IsNullOrWhiteSpace(startTime))
			{
				criteria.Add(Expression.Sql("CONVERT(varchar(16),{alias}.StartTime,120)=(?)", startTime, NHibernateUtil.String));
			}

			if (!String.IsNullOrWhiteSpace(endTime))
			{
				criteria.Add(Expression.Sql("CONVERT(varchar(16),{alias}.EndTime,120)=(?)", endTime, NHibernateUtil.String));
			}

			if (services != null && services.Length > 0)
			{
				criteria.Add(Expression.In("AlarmCode", services));
			}

			var list = criteria.List<RoomService>();

			total = list.Count;

			var result = list.AsQueryable().OrderBy(sort + " " + order);

			result = result.Skip((page - 1) * rows).Take(rows);

			return result.ToList();
		}
        
        #region 服务

        public IList<object[]> LoadCurrentRoomServices(int hotelID, IList<int> groupIDs)
		{
            string sql = String.Format(@"SELECT r.HostID,r.RoomTypeModalID,h.RoomNumber,ISNULL(a.Name,'') AS Name,ISNULL(a.EnglishName,'') AS EnglishName,r.UpdateTime,ISNULL(a.TWName,'') AS TWName,h.HostNumber,a.ModalAddress
				FROM tb_HostModal r 
				INNER JOIN tb_Hosts h ON h.ID=r.HostID
				INNER JOIN tb_RoomTypeModal a ON a.ID=r.RoomTypeModalID
				WHERE r.[Status]=1 AND h.HotelID={1} AND a.[Type]=4 AND h.GroupID IN({0}) AND ISNULL(a.Name,'')<>''
				ORDER BY a.Sort", String.Join(",", groupIDs), hotelID);

			return Session.CreateSQLQuery(sql).List<object[]>();
		}

		#endregion

		#region 异常

        public IList<object[]> LoadCurrentRoomAbnormities(int hotelID, IList<int> groupIDs)
		{
            string sql = String.Format(@"SELECT r.ID, a.Name, h.RoomNumber, r.StartTime,a.EName
										FROM tb_RoomService r 
										INNER JOIN tb_Hosts h ON h.ID=r.HostID
										INNER JOIN tb_AlarmSetting a ON a.Code=r.AlarmCode
										WHERE r.[Status]=1 AND a.HotelID={1} AND a.Value=1 AND a.[Type]='C' AND h.GroupID IN({0})
										ORDER BY a.Sort", String.Join(",", groupIDs), hotelID);

			return Session.CreateSQLQuery(sql).List<object[]>();
		}

		#endregion

        public DataTable LoadCurrentRoomServicesOrAbnormitiesCount(int hotelID, int groupID, DeviceType deviceType)
        {
            
            var cmd = Session.Connection.CreateCommand();

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

            var parameter = cmd.CreateParameter();
            parameter.ParameterName = "@type";
            parameter.DbType = DbType.Int16;
            parameter.Value = deviceType;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@hotelID";
            parameter.DbType = DbType.Int16;
            parameter.Value = hotelID;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@groupId";
            parameter.DbType = DbType.Int16;
            parameter.Value = groupID;
            cmd.Parameters.Add(parameter);

            var da = new SqlDataAdapter(cmd as SqlCommand);
            var ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

		public RoomService Get(int hostID, string alarmCode, bool status)
		{
			return LoadAll().FirstOrDefault(r => r.HostID == hostID && r.AlarmCode == alarmCode && r.Status == status);
		}

		public RoomService Get(int hostID, string alarmCode)
		{
			return LoadAll().FirstOrDefault(r => r.HostID == hostID && r.AlarmCode == alarmCode);
		}

		public IList<RoomService> LoadAll(int hostId)
		{
			return LoadAll().Where(r => r.HostID == hostId).ToList();
		}
	}
}
