using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace SK.MemberDAL
{
	/// <summary>
	/// RightMember 的摘要说明。
	/// </summary>
	public class RightMember
	{
		/// <summary>
		/// 管理员权限
		/// </summary>
		public RightMember()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		/// <summary>
		/// 添加指定管理员拥有某项权限
		/// </summary>
		/// <param name="RightID">权限编号</param>
		/// <param name="MemberID">管理员编号</param>
		public void Add(object RightID,object MemberID)
		{
			SqlParameter[] oParms =new SqlParameter[2];
			oParms[0] = new SqlParameter("@RightID",RightID);
			oParms[1] = new SqlParameter("@MemberID",MemberID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"insert into RightMember(RightID,MemberID) values(@RightID,@MemberID)",oParms);
		}
		public void Add(object ProductID,object RightID,object MemberID)
		{
			SqlParameter[] oParms =new SqlParameter[3];
			oParms[0] = new SqlParameter("@ProductID",ProductID);
			oParms[1] = new SqlParameter("@RightID",RightID);
			oParms[2] = new SqlParameter("@MemberID",MemberID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"insert into RightMember(ProductID,RightID,MemberID) values(@ProductID,@RightID,@MemberID)",oParms);
		}
		/// <summary>
		/// 删除某一个权限的所有管理员
		/// </summary>
		/// <param name="RightID"></param>
		public void Delete(object RightID)
		{
			SqlParameter[] oParms =new SqlParameter[1];
			oParms[0] = new SqlParameter("@RightID",RightID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"delete RightMember where RightID=@RightID",oParms);
		}
		public void Delete_Member(object MemberID)
		{
			SqlParameter[] oParms =new SqlParameter[1];
			oParms[0] = new SqlParameter("@MemberID",MemberID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"delete RightMember where MemberID=@MemberID",oParms);
		}
		/*
		/// <summary>
		/// 指定页面的管理员列表
		/// </summary>
		/// <param name="RightID">权限编号</param>
		/// <param name="HasRight"></param>
		/// <returns></returns>
		public DataSet List(object RightID,bool HasRight)
		{
			SqlParameter[] oParms = new SqlParameter[1];
			oParms[0] = new SqlParameter("@RightID",RightID);
			if(HasRight)
				return SqlHelper.ExecuteDataset(Data.ConnString,CommandType.Text,"select * from Member where MemberID in(select MemberID from RightMember where RightID=@RightID)",oParms);
			else
				return SqlHelper.ExecuteDataset(Data.ConnString,CommandType.Text,"select * from Member where MemberID not in(select MemberID from RightMember where RightID=@RightID)",oParms);
		}*/
		public DataSet List(object ProductID,object MemberID)
		{
			SqlParameter[] oParms = new SqlParameter[2];
			oParms[0] = new SqlParameter("@ProductID",ProductID);
			oParms[1] = new SqlParameter("@MemberID",MemberID);

			return SqlHelper.ExecuteDataset(Data.ConnString,CommandType.Text,"select * from RightMember where ProductID=@ProductID and MemberID=@MemberID",oParms);
		}
		/// <summary>
		/// 指定的管理员是否有该页面的管理权限
		/// </summary>
		/// <param name="PageName"></param>
		/// <param name="MemberID"></param>
		/// <returns></returns>
		public bool Allow(object PageName,object MemberID)
		{
			SqlParameter[] oParms =new SqlParameter[2];
			oParms[0] = new SqlParameter("@PageName",PageName);
			oParms[1] = new SqlParameter("@MemberID",MemberID);

			return Convert.ToInt32(SqlHelper.ExecuteScalar(Data.ConnString,CommandType.Text,"select Count(RightMemberID) from RightMember where MemberID=@MemberID and RightID=(select RightID from [Right] where PageName=@PageName)",oParms))>0;
		}
		public bool Allow(object ProductID,object PageName,object MemberID)
		{
			SqlParameter[] oParms =new SqlParameter[3];
			oParms[0] = new SqlParameter("@ProductID",ProductID);
			oParms[1] = new SqlParameter("@PageName",PageName);
			oParms[2] = new SqlParameter("@MemberID",MemberID);

			if(ProductID.ToString()=="0")
				return Convert.ToInt32(SqlHelper.ExecuteScalar(Data.ConnString,CommandType.Text,"select Count(RightMemberID) from RightMember where ProductID=@ProductID and MemberID=@MemberID and RightID=(select RightID from [Right] where PageName=@PageName and ProductTypeID=0)",oParms))>0;
			else
				return Convert.ToInt32(SqlHelper.ExecuteScalar(Data.ConnString,CommandType.Text,"select Count(RightMemberID) from RightMember where ProductID=@ProductID and MemberID=@MemberID and RightID=(select RightID from [Right] where PageName=@PageName and ProductTypeID=(select ProductTypeID from Product where ProductID=@ProductID))",oParms))>0;
		}
	}
}
