using System;
using System.Data;
using System.Web;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using AVC.Passport.DataEntity;
using AVC.Passport.DataAccess;

namespace AVC.Passport
{
	public class UserMstBiz
	{
		public static void AddUserMst(UserMst objUserMst)
		{
			UserMstInsert objUserMstDAL = new UserMstInsert();
			objUserMstDAL.ReceiveParameter(objUserMst);
			objUserMstDAL.ExecuteNonQuery();
			objUserMst.UserID = objUserMstDAL.UserID;
		}

		public static DataTable GetAllUserMst()
		{
			UserMstSelectAll objUserMstDAL = new UserMstSelectAll();
			return objUserMstDAL.ExecuteDataTable();
		}

		public static void UpdateUserMst(UserMst objUserMst)
		{
			UserMstUpdate objUserMstDAL = new UserMstUpdate();
			objUserMstDAL.ReceiveParameter(objUserMst);
			objUserMstDAL.ExecuteNonQuery();
		}
        public static DataTable GetUserRights(string SubSystemID, int UserID)
        {
            GetUserRights objRightDAL = new GetUserRights();
            objRightDAL.UserID = UserID;
            objRightDAL.SubSystemID = SubSystemID;
            return objRightDAL.ExecuteDataTable();
        }
        public static DataTable GetUserMstSelectAllPaged(int CurrentPage, out int TotalPage, out int TotalRecord, int PageSize, string SortBy)
        {
            UserMstSelectAllPaged objUserMstDAL = new UserMstSelectAllPaged();
            objUserMstDAL.CurrentPage = CurrentPage;
            objUserMstDAL.PageSize = PageSize;
            objUserMstDAL.SortBy = SortBy;
            DataTable UserMstTable = objUserMstDAL.ExecuteDataTable();
            TotalPage = objUserMstDAL.TotalPage;
            TotalRecord = objUserMstDAL.TotalRecord;
            return UserMstTable;
        }

		public static void DeleteUserMst(System.Int32 UserID)
		{
			UserMstDelete objUserMstDAL = new UserMstDelete();
			objUserMstDAL.UserID = UserID;
			objUserMstDAL.ExecuteNonQuery();
		}

		public static UserMst GetUserMst(System.Int32 UserID)
		{
			UserMstSelectByPk objUserMstDAL = new UserMstSelectByPk();
			UserMst objUserMst = new UserMst();
			objUserMstDAL.UserID = UserID;
			objUserMst.SetMetaDataTable(objUserMstDAL.ExecuteDataTable());
			return objUserMst;
		}

		public static DataTable GetUserMstBySubSystemID(System.String SubSystemID)
		{
			UserMstSelectBySubSystemID objUserMstDAL = new UserMstSelectBySubSystemID();
			objUserMstDAL.SubSystemID = SubSystemID;
			return objUserMstDAL.ExecuteDataTable();
		}

        public static DataTable GetAllRights(string SubSystemID,int UserID)
        {
            GetUserAllRights objUserMstDAL = new GetUserAllRights();
            objUserMstDAL.UserID = UserID;
            objUserMstDAL.SubSystemID = SubSystemID;
            return objUserMstDAL.ExecuteDataTable();
        }

        public static DataTable GetUserGroup(string SubSystemID, int UserID)
        {
            GetUserGroups objUserGroupDAL = new GetUserGroups();
            objUserGroupDAL.UserID = UserID;
            objUserGroupDAL.SubSystemID = SubSystemID;
            return objUserGroupDAL.ExecuteDataTable();
        }

        public static DataTable GetGroups(int UserID, string SubSystemID)
        {
            GetUserGroups objUserMstDAL = new GetUserGroups();
            objUserMstDAL.UserID = UserID;
            objUserMstDAL.SubSystemID = SubSystemID;
            return objUserMstDAL.ExecuteDataTable();
        }

        public static DataTable GetInheritRights(string SubSystemID,int UserID)
        {
            GetUserInheritRights objUserMstDAL = new GetUserInheritRights();
            objUserMstDAL.UserID = UserID;
            objUserMstDAL.SubSystemID = SubSystemID;
            return objUserMstDAL.ExecuteDataTable();
        }

        public static DataTable GetRights(int UserID, string SubSystemID)
        {
            GetUserRights objUserMstDAL = new GetUserRights();
            objUserMstDAL.UserID = UserID;
            objUserMstDAL.SubSystemID = SubSystemID;
            return objUserMstDAL.ExecuteDataTable();
        }


        public static void AddUserGroup(int UserID, int GroupID, int CreateUser)
        {
            User_GroupInsertA objUserGroupDAL = new User_GroupInsertA();
            objUserGroupDAL.UserID = UserID;
            objUserGroupDAL.GroupID = GroupID;
            objUserGroupDAL.CreateUser = CreateUser;
            objUserGroupDAL.ExecuteNonQuery();
        }



        public static void AddDefaultUserGroup(int UserID, int CreateUser)
        {
            User_GroupAddDefault objUserGroupDAL = new User_GroupAddDefault();
            objUserGroupDAL.UserID = UserID;
            objUserGroupDAL.CreateUser = CreateUser;

            objUserGroupDAL.ExecuteNonQuery();
        }

        public static void RightInsertA(int UserID, int CreateUser, int RightID)
        {
            User_RightInsertA objUserGroupDAL = new User_RightInsertA();
            objUserGroupDAL.UserID  = UserID;
            objUserGroupDAL.CreateUser = CreateUser;
            objUserGroupDAL.RightID = RightID;
            objUserGroupDAL.ExecuteNonQuery();
        }

        public static DataTable GetUserLogInfoSelectByConditions(DateTime BeginTime, DateTime EndTime, int UserID)
        {
            UserLogInfoSelectByConditions objUserGroupDAL = new UserLogInfoSelectByConditions();
            objUserGroupDAL.UserID = UserID;
            objUserGroupDAL.BeginTime = BeginTime ;
            objUserGroupDAL.EndTime  = EndTime ;
           return  objUserGroupDAL.ExecuteDataTable();
        }

        public static DataTable GetUserMstSelectByUserName(string UserName, string SubSystemID)
        {
            UserMstSelectByUserName objUserGroupDAL = new UserMstSelectByUserName();
            objUserGroupDAL.UserName = UserName;
            objUserGroupDAL.SubSystemID =SubSystemID;
            return objUserGroupDAL.ExecuteDataTable();
        }

        public static UserMst GetUserMstByUserName(System.String SubSystemID, string UserName)
        {
            UserMstSelectByUserName objUserMstDAL = new UserMstSelectByUserName();
            UserMst objUserMst = new UserMst();
            objUserMstDAL.SubSystemID = SubSystemID;
            objUserMstDAL.UserName = UserName;
            objUserMst.SetMetaDataTable(objUserMstDAL.ExecuteDataTable());

            return objUserMst;
        }
        public static void AddUserRights(int UserID, int RightID, int CreateUser)
        {
            User_RightInsertA objUserRightDAL = new User_RightInsertA();
            objUserRightDAL.UserID = UserID;
            objUserRightDAL.RightID = RightID;
            objUserRightDAL.CreateUser = CreateUser;

            objUserRightDAL.ExecuteNonQuery();
        }

        public static void RemoveUserRights(int UserID, int RightID)
        {
            User_RightsDelete objUserRightDAL = new User_RightsDelete();
            objUserRightDAL.UserID = UserID;
            objUserRightDAL.RightID = RightID;

            objUserRightDAL.ExecuteNonQuery();
        }
        public static void WriteCertifyPool(string SubSystemID, int UserID)
        {
            HttpContext context = HttpContext.Current;
            string strAppPath = context.Server.MapPath(@"~\CertifyPool\" + SubSystemID) + @"\";
            strAppPath = strAppPath.Replace("/", @"\").Replace(@"\\", @"\");

            if (!System.IO.Directory.Exists(strAppPath))
            {
                System.IO.Directory.CreateDirectory(strAppPath);
            }
            string strPoolName = strAppPath + UserID.ToString() + ".cer";
            DataTable dtAllRights = GetAllRights(SubSystemID, UserID);
            dtAllRights.TableName = SubSystemID;
            DataSet dsAllRights = new DataSet("Certify");
            dsAllRights.Tables.Add(dtAllRights);

            dsAllRights.WriteXml(strPoolName);

        }

        public static bool Login(string SubSystemID, string UserName, string Password, out UserMst objUserMst, out int ResultCode)
        {
      
            bool blnReturn = false;
            UserName = UserName.Trim();
            objUserMst = GetUserMstByUserName(SubSystemID, UserName);

            ResultCode = 10003;
            string strLog = "腎翹囮啖";

            if (objUserMst.IsEmpty)
            {
                blnReturn = false;
            }
            else
            {
                if (GetMD5String(Password) != objUserMst.Password)
                    blnReturn = false;
                else
                {
                    ResultCode = 0;
                    strLog = "腎翹傖髡";
                    blnReturn = true;
                    objUserMst.LastLoginTime = DateTime.Now;
                    objUserMst.Visitation += 1;
                    UpdateUserMst(objUserMst);
                }
            }

            SubSystem objSubSystem = SubSystemBiz.GetSubSystem(SubSystemID);
            if (objSubSystem.IfUserLog == 1 && HttpContext.Current != null)
            {
                //  获取LogIP
                HttpRequest request = HttpContext.Current.Request;
                UserLogInfo objUseLogInfo = new UserLogInfo();

                string UserIP = request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                if (UserIP == null || UserIP == "")
                    UserIP = request.ServerVariables["REMOTE_ADDR"];

                objUseLogInfo.LogIP = UserIP;
                objUseLogInfo.LogTime = DateTime.Now;
                objUseLogInfo.UserID = objUserMst.UserID;
                objUseLogInfo.Description = strLog;
                objUseLogInfo.SubSystemID = SubSystemID;

                AddUserLogInfo(objUseLogInfo);
            }

            if (blnReturn && HttpContext.Current != null)
            {
                DataTable dtAllRights = GetAllRights(SubSystemID, objUserMst.UserID);
                WriteCertifyPool(SubSystemID, objUserMst.UserID);
            }
            return blnReturn;
        }

        public static bool ChangePassword(int UserID, string NewPassword, out int ResultCode)
        {
         
            UserMst objUserMst = GetUserMst(UserID);
            if (objUserMst.IsEmpty)
            {
               
                ResultCode = 10000;
                return false;
            }
            if (GetMD5String(NewPassword) == objUserMst.Password)
            {
              
                ResultCode = 10001;
                return false;
            }
            objUserMst.Password = GetMD5String(NewPassword);
            UpdateUserMst(objUserMst);
            ResultCode = 0;

            return true;
        }


        private static void AddUserLogInfo(UserLogInfo objUserLogInfo)
        {
            UserLogInfoInsert objUserLogInfoDAL = new UserLogInfoInsert();
            objUserLogInfoDAL.ReceiveParameter(objUserLogInfo);
            objUserLogInfoDAL.ExecuteNonQuery();
        }

        private static string GetMD5String(string SourceString)
        {
            byte[] b = System.Text.UTF8Encoding.Default.GetBytes(SourceString);
            b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
            string strReturn = "";
            for (int i = 0; i < b.Length; i++)
                strReturn += b[i].ToString("x").PadLeft(2, '0');
            return strReturn;
        }

 

        public static void RemoveUserGroup(int UserID, int GroupID)
        {
            User_GroupDelete objUserGroupDAL = new User_GroupDelete();
            objUserGroupDAL.UserID = UserID;
            objUserGroupDAL.GroupID = GroupID;
            objUserGroupDAL.ExecuteNonQuery();
        }

	}
}