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

namespace Dao.Implement
{
    public class SysHotelGroupRepository : RepositoryBase<SysHotelGroup>, ISysHotelGroupRepository
	{
        public IList<SysHotelGroup> GetGroupList(SysHotelGroup group)
        {
            IList<SysHotelGroup> groupList = new List<SysHotelGroup>();

            if (group == null)
            {
                return groupList;
            }

            groupList.Add(group);

            IList<SysHotelGroup> subGroups = LoadAll().Where(r => r.Parent == group && r.IsDeleted != true).OrderBy(o => o.Sort).ToList();

            foreach (SysHotelGroup subGroup in subGroups)
            {
                foreach (SysHotelGroup subGroup1 in GetGroupList(subGroup))
                {
                    groupList.Add(subGroup1);
                }
            }

            return groupList;
        }


        public GroupMenu GetGroupMenu(int id)
        {
            GroupMenu groupMenu = null;

            using (IStatelessSession session = SessionFactory.OpenStatelessSession())
            using (IDbCommand command = session.Connection.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT ID, ParentID, Name, Sort FROM tb_Sys_HotelGroups WHERE ID=@ID AND IsDeleted<>1";

                IDbDataParameter parameter = command.CreateParameter();
                parameter.DbType = DbType.Int32;
                parameter.ParameterName = "@ID";
                parameter.Value = id;
                command.Parameters.Add(parameter);

                using (IDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        groupMenu = new GroupMenu();
                        groupMenu.ID = reader.GetInt32(0);
                        groupMenu.Name = reader.GetString(2);
                        groupMenu.Sort = reader.GetInt32(3);

                        IList<GroupMenu> subGroupMenus = GetGroupMenuListByParentID(groupMenu.ID);
                        foreach (var subGroupMenu in subGroupMenus)
                        {
                            groupMenu.SubGroupMenus.Add(subGroupMenu);
                        }
                    }
                }
            }

            return groupMenu;
        }

		public IList<GroupMenu> GetGroupMenuListByParentID(int? parentId)
		{
			IList<GroupMenu> groupMenus = new List<GroupMenu>();

			using (IStatelessSession session = SessionFactory.OpenStatelessSession())
			using (IDbCommand command = session.Connection.CreateCommand())
			{
				command.CommandType = CommandType.Text;

				if (parentId.HasValue)
				{
                    command.CommandText = "SELECT ID, ParentID, Name, Sort FROM tb_Sys_HotelGroups WHERE ParentID=@ParentID AND IsDeleted<>1 ORDER BY Sort";

					IDbDataParameter parameter = command.CreateParameter();
					parameter.DbType = DbType.Int32;
					parameter.ParameterName = "@ParentID";
					parameter.Value = parentId.Value;
					command.Parameters.Add(parameter);
				}
				else 
				{
                    command.CommandText = "SELECT ID, ParentID, Name, Sort FROM tb_Sys_HotelGroups WHERE ParentID IS NULL AND IsDeleted<>1 ORDER BY Sort";
				}

				using (IDataReader reader = command.ExecuteReader())
				{
					while (reader.Read())
					{
						var groupMenu = new GroupMenu();
						groupMenu.ID = reader.GetInt32(0);
						groupMenu.Name = reader.GetString(2);
						groupMenu.Sort = reader.GetInt32(3);

						IList<GroupMenu> subGroupMenus = GetGroupMenuListByParentID(groupMenu.ID);
						foreach (var subGroupMenu in subGroupMenus)
						{
							groupMenu.SubGroupMenus.Add(subGroupMenu);
						}

						groupMenus.Add(groupMenu);
					}
				}
			}

			return groupMenus;
        }
        /// <summary>
        /// 重载删除，只做标记
        /// </summary>
        /// <param name="id"></param>
        public override void Delete(object id)
        {
            var entity = this.HibernateTemplate.Get<SysHotelGroup>(id);
            if (entity == null)
            {
                return;
            }
            entity.IsDeleted = true;
            entity.Last_Modified_Time = DateTime.Now;
            this.HibernateTemplate.Update(entity);
        }
        /// <summary>
        /// 重载删除，只做标记
        /// </summary>
        /// <param name="idList"></param>
        public override void Delete(IList<object> idList)
        {
            foreach (var id in idList)
            {
                var entity = this.HibernateTemplate.Get<SysHotelGroup>(id);
                if (entity == null)
                {
                    return;
                }
                entity.IsDeleted = true;
                entity.Last_Modified_Time = DateTime.Now;
                this.HibernateTemplate.Update(entity);
            }
        }
	}
}
