using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
using RedGlovePermission.DBUtility;

namespace RedGlovePermission.DAL
{
	/// <summary>
	/// 数据访问类RGP_RoleAuthorityList。
	/// </summary>
	public class RGP_RoleAuthorityList
	{
		public RGP_RoleAuthorityList()
		{}

		/// <summary>
        /// 判断记录是否存在
		/// </summary>
		public bool Exists(RedGlovePermission.Model.RGP_RoleAuthorityList model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from RGP_RoleAuthorityList where ");
            if (model.UserID != 0)//判断是角色权限还是用户权限
            {strSql.Append("UserID=@UserID");}
            else
            {strSql.Append("RoleID=@RoleID");}
            strSql.Append(" and ModuleID=@ModuleID and AuthorityTag=@AuthorityTag");
			SqlParameter[] parameters = {
                    new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@ModuleID", SqlDbType.Int,4),
					new SqlParameter("@AuthorityTag", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.UserID;
			parameters[1].Value = model.RoleID;
			parameters[2].Value = model.ModuleID;
            parameters[3].Value = model.AuthorityTag;

			return SqlServerHelper.Exists(strSql.ToString(),parameters);
		}

		/// <summary>
        /// 修改角色模块权限
		/// </summary>
        public bool UpdateRoleAuthority(ArrayList list)
		{
            ArrayList AuthorityList = new ArrayList();

            for (int i = 0; i < list.Count; i++)
            {
                string[] val = list[i].ToString().Split('|');
                if (val[3] == "0")//如果为0就删除权限
                {
                    AuthorityList.Add("delete RGP_RoleAuthorityList where RoleID=" + val[0] + " and ModuleID=" + val[1] + " and AuthorityTag='" + val[2] + "'");
                }
                else
                {
                    AuthorityList.Add("insert into RGP_RoleAuthorityList(RoleID,ModuleID,AuthorityTag) values (" + val[0] + "," + val[1] + ",'" + val[2] + "')");
                }
            }

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

        /// <summary>
        /// 修改用户模块权限
        /// </summary>
        public bool UpdateUserAuthority(ArrayList list)
        {
            ArrayList AuthorityList = new ArrayList();

            for (int i = 0; i < list.Count; i++)
            {
                string[] val = list[i].ToString().Split('|');
                if (val[3] == "0")//如果为0就删除权限
                {
                    AuthorityList.Add("delete RGP_RoleAuthorityList where UserID=" + val[0] + " ModuleID=" + val[1] + " and AuthorityTag='" + val[2] + "'");
                }
                else
                {
                    AuthorityList.Add("insert into RGP_RoleAuthorityList(UserID,ModuleID,AuthorityTag) values (" + val[0] + "," + val[1] + ",'" + val[2] + "')");
                }
            }

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

		/// <summary>
		/// 读取角色的模块权限
		/// </summary>
        public DataSet GetRoleAuthorityList(int RoleID, int ModuleID)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select * FROM RGP_RoleAuthorityList where RoleID=" + RoleID.ToString() + " and ModuleID=" + ModuleID.ToString());

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

        /// <summary>
        /// 读取用户的模块权限
        /// </summary>
        public DataSet GetUserAuthorityList(int UserID, int ModuleID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM RGP_RoleAuthorityList where UserID=" + UserID.ToString() + " and ModuleID=" + ModuleID.ToString());

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

