using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RedGlovePermission.DBUtility;
//该源码下载自www.51aspx.com(５１aｓｐｘ．ｃｏｍ)

namespace RedGlovePermission.DAL
{
	/// <summary>
    /// 模块权限数据访问类
	/// </summary>
	public class RGP_ModuleAuthorityList
	{
		public RGP_ModuleAuthorityList()
		{}

		/// <summary>
		/// 增加模块权限
		/// </summary>
		/// <param name="list">权限列表</param>
		/// <returns></returns>
        public bool CreateAuthorityList(ArrayList list)
		{
            ArrayList AuthorityList = new ArrayList();

            for (int i = 0; i < list.Count; i++)
            {
                string[] val=list[i].ToString().Split('|');
                AuthorityList.Add("insert into RGP_ModuleAuthorityList(ModuleID,AuthorityTag) values (" + val[0] + ",'" + val[1] + "')");
            }

            try 
            {
                SqlServerHelper.ExecuteSqlTran(AuthorityList);
                return true;
            }
            catch
            {
                return false;
            }
		}

		/// <summary>
		/// 更新模块权限
		/// </summary>
        public bool UpdateAuthorityList(ArrayList list)
		{
            ArrayList AuthorityList = new ArrayList();

            for (int i = 0; i < list.Count; i++)
            {
                string[] val=list[i].ToString().Split('|');
                if (val[2] == "0")//如果为0就删除权限
                {
                    //删除模块权限
                    AuthorityList.Add("delete RGP_ModuleAuthorityList where ModuleID=" + val[0] + " and AuthorityTag='" + val[1] + "'");
                    //删除角色所对应该模块标识的权限
                    AuthorityList.Add("delete RGP_RoleAuthorityList where ModuleID=" + val[0] + " and AuthorityTag='" + val[1] + "'");
                }
                else
                {
                    AuthorityList.Add("insert into RGP_ModuleAuthorityList(ModuleID,AuthorityTag) values (" + val[0] + ",'" + val[1] + "')");
                }
            }

            try
            {
                SqlServerHelper.ExecuteSqlTran(AuthorityList);
                return true;
            }
            catch
            {
                return false;
            }
		}

        /// <summary>
        /// 删除指定模块的权限列表
        /// </summary>
        /// <param name="ModuleID">模块ID</param>
        /// <returns></returns>
        public bool DeleteAuthority(int ModuleID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete RGP_ModuleAuthorityList ");
            strSql.Append(" where ModuleID=@ModuleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleID", SqlDbType.Int,4)};
            parameters[0].Value = ModuleID;

            if (SqlServerHelper.ExecuteSql(strSql.ToString(), parameters) >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 获得指定模块的权限列表
		/// </summary>
		/// <param name="ModuleID">模块ID</param>
		/// <returns></returns>
        public DataSet GetAuthorityList(int ModuleID)
		{
            StringBuilder strSql=new StringBuilder();
            strSql.Append("select * from RGP_ModuleAuthorityList where ModuleID=" + ModuleID.ToString());

			return SqlServerHelper.Query(strSql.ToString());
		}

        /// <summary>
		/// 获得指定模块的权限列表
		/// </summary>
		/// <param name="ModuleID">模块ID</param>
		/// <returns></returns>
        public DataSet GetAuthorityNameList(int ModuleID)
		{
            StringBuilder strSql=new StringBuilder();
            strSql.Append("SELECT RGP_ModuleAuthorityList.*, RGP_AuthorityDir.AuthorityName FROM RGP_ModuleAuthorityList INNER JOIN RGP_AuthorityDir ON RGP_ModuleAuthorityList.AuthorityTag = RGP_AuthorityDir.AuthorityTag where ModuleID=" + ModuleID.ToString());

			return SqlServerHelper.Query(strSql.ToString());
		}
	}
}

