using System;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Domain;
using NHibernate;

namespace Dao.Implement
{
    public class HostModalRepository : RepositoryBase<HostModal>, IHostModalRepository
    {
        public IQueryable<HostModal> LoadByHostID(int hostID)
        {
            return LoadAll().Where(r => r.HostID == hostID);
        }

        public HostModal Get(int hostID, int roomTypeModalId)
        {
            return LoadAll().Where(r => r.HostID == hostID && r.Modal != null && r.Modal.ID == roomTypeModalId).FirstOrDefault();//该方法太慢了
        }

        public HostModal Get(int hostID, string modalAddress)
        {
            return LoadAll().Where(r => r.HostID == hostID && r.Modal != null && r.Modal.ModalAddress == modalAddress).FirstOrDefault();//该方法太慢了
        }
        /// <summary>
        /// 根据主机ID删除所有回路
        /// </summary>
        /// <param name="hostID"></param>
        public void DeteleByHostID(int hostID)
        {
            Session.CreateSQLQuery("DELETE FROM tb_HostModal WHERE HostID=?")
                .SetInt32(0, hostID)
                .ExecuteUpdate();
        }
        /// <summary>
        /// 根据房型回路ID删除所有主机的对应回路
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="roomTypeSceneID"></param>
        public void DeteleByRoomTypeModal(int hostID, int roomTypeModalID)
        {
            Session.CreateSQLQuery("DELETE FROM tb_HostModal WHERE HostID=? and RoomTypeModalID=?")
                .SetInt32(0, hostID)
                .SetInt32(1, roomTypeModalID)
                .ExecuteUpdate();
        }
    }
}
