/// <summary>
 /// 添加一个用户帐户
 /// </summary>
 /// <param name="newUser"></param>
 /// <param name="session"></param>
 /// <param name="addSuccess"></param>
 /// <param name="addFailure"></param>
 public void AddUser(User newUser, ISession session, Action<User> addSuccess, Action<User, string> addFailure)
 {
     session.UsingSafe(s =>
     {
         IsUserAccountExist(newUser.UserAccount, s,
             msg =>
             {
                 s.UsingSafeTransaction((ds, f, su) =>
                 {
                     try
                     {
                         ds.Save(newUser);
                         ds.Save(newUser.SecurityInfo);
                         su();
                         addSuccess(newUser);
                         newUser.UserPassword = "";
                     }
                     catch (Exception eX)
                     {
                         f();
                         addFailure(newUser, eX.Message);
                     }
                 });
             },
             u =>
             {
                 addFailure(newUser, "用户帐户已经存在");
             });
     });
 }
        /// <summary>
        /// 更新用户活动
        /// </summary>
        /// <param name="useraccount"></param>
        /// <param name="userActivity"></param>
        /// <param name="failure"></param>
        /// <param name="success"></param>
        public void UpdateUserTracks(string useraccount, ISession insession, UserActivities userActivity, Action<string> failure, Action success)
        {
            insession.UsingSafe(session =>
            {
                session.UsingSafeTransaction((s, f, su) =>
                {
                    try
                    {
                        UserTracking ut = new UserTracking()
                        {
                            UserAccount = useraccount,
                            UserActivity = (int)userActivity,
                            LastActivity = DateTime.Now
                        };

                        s.Save(ut);

                        su();

                        success();
                    }
                    catch (Exception eX)
                    {
                        f();
                        failure(eX.Message);
                    }
                });
            });
        }
 /// <summary>
 /// 添加角色
 /// </summary>
 /// <param name="newRole"></param>
 /// <param name="addSuccess"></param>
 /// <param name="addFailure"></param>
 public void AddRole(Role newRole, ISession session, Action<Role> addSuccess, Action<string> addFailure)
 {
     session.UsingSafe(s =>
     {
         IsRoleExist(newRole.RoleCode,
             s,
             r => { addFailure("角色代码已经存在"); },
             () =>
             {
                 s.UsingSafeTransaction((ts, failure, success) =>
                 {
                     try
                     {
                         ts.Save(newRole);
                         success();
                         addSuccess(newRole);
                     }
                     catch (Exception eX)
                     {
                         failure();
                         addFailure(eX.Message);
                     }
                 });
             });
     });
 }
        /// <summary>
        /// 判断角色是否存在
        /// </summary>
        /// <param name="roleCode"></param>
        /// <param name="roleExist"></param>
        /// <param name="roleNotExist"></param>
        public void IsRoleExist(string roleCode, ISession session, Action<Role> roleExist, Action roleNotExist)
        {
            session.UsingSafe(s=>{
                Role r = s.Get<Role>(roleCode);

                if (null == r)
                {
                    roleNotExist();
                }
                else
                {
                    roleExist(r);
                }
            });
        }
        public void IsUserAccountExist(string account, ISession session, Action<string> userNotExistAction, Action<User> userExistAction)
        {
            session.UsingSafe(s =>
            {
                User u = s.Get<User>(account);

                if (null == u)
                {
                    userNotExistAction("用户帐户不存在");
                }
                else
                {
                    userExistAction(u);
                }
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="account"></param>
        /// <param name="insession"></param>
        /// <param name="doFailure"></param>
        /// <param name="doSuccess"></param>
        public void CheckPasswordErrorTimes(string account, ISession insession, Action<string> doFailure, Action<int> doSuccess)
        {
            insession.UsingSafe(session =>
            {
                try
                {
                    IList<UserTracking> tracks = session.CreateCriteria<UserTracking>()
                        .Add(Expression.Eq("UserActivity", 1)).List<UserTracking>();

                    doSuccess(tracks.Count);
                }
                catch (Exception eX)
                {
                    doFailure(eX.Message);
                }
            });
        }
 /// <summary>
 /// 保存或更新SessionUser
 /// </summary>
 /// <param name="target"></param>
 /// <param name="failure"></param>
 /// <param name="success"></param>
 public void SaveOrUpdateSessionUser(SessionUser target, ISession session, Action<string> failure, Action<SessionUser> success)
 {
     session.UsingSafe(session1 =>
     {
         session1.UsingSafeTransaction((s, f, su) =>
         {
             try
             {
                 s.SaveOrUpdate(target);
                 su();
                 success(target);
             }
             catch (Exception eX)
             {
                 f();
                 failure(eX.Message);
             }
         });
     });
 }
        /// <summary>
        /// 清除密码输入错误记录
        /// </summary>
        /// <param name="useraccount"></param>
        /// <param name="failure"></param>
        /// <param name="success"></param>
        /// <param name="insession"></param>
        public void ClearUserPasswordInputError(string useraccount, ISession insession, Action<string> failure, Action success)
        {
            insession.UsingSafe(session =>
            {
                session.UsingSafeTransaction((s, f, su) =>
                {
                    try
                    {
                        s.Delete("from UserTracking where UserActivity = " +
                            ((int)UserActivities.SignInFailse).ToString() +
                            " and UserAccount = '" +
                            useraccount + "'");

                        su();
                        success();
                    }
                    catch (Exception eX)
                    {
                        f();
                        failure(eX.Message);
                    }
                });
            });
        }
        /// <summary>
        /// 检查用户帐户状态
        /// </summary>
        /// <param name="account"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public AccountStatus CheckUserAccount(string account, ISession session)
        {
            AccountStatus retValue = AccountStatus.None;

            session.UsingSafe(s =>
            {
                LockedUser u = s.Get<LockedUser>(account);

                if (null == u)
                    retValue = AccountStatus.Normal;
                else
                    retValue = AccountStatus.Locked;
            });

            return retValue;
        }
 /// <summary>
 /// 解锁用户账户
 /// </summary>
 /// <param name="useraccount"></param>
 /// <param name="failure"></param>
 /// <param name="success"></param>
 public void UnlockUser(string useraccount, ISession insession, Action<string> failure, Action success)
 {
     insession.UsingSafe(session =>
     {
         session.UsingSafeTransaction((s, f, su) =>
         {
             try
             {
                 _UTOper.ClearUserPasswordInputError(useraccount, s, msg =>
                 {
                     f();
                     failure(msg);
                 },
                 () =>
                 {
                     s.Delete("from LockedUser where UserAccount = '" + useraccount + "'");
                     su();
                     success();
                 });
             }
             catch (Exception eX)
             {
                 f();
                 failure(eX.Message);
             }
         });
     });
 }
        /// <summary>
        /// 锁定用户
        /// </summary>
        /// <param name="useraccount"></param>
        /// <param name="lockedType"></param>
        /// <param name="failure"></param>
        /// <param name="success"></param>
        public void LockUser(string useraccount, ISession insession, UserActivities lockedType, Action<string> failure, Action success)
        {
            insession.UsingSafe(session =>
            {
                session.UsingSafeTransaction((s, f, su) =>
                {
                    try
                    {
                        LockedUser lu = new LockedUser()
                        {
                            UserAccount = useraccount,
                            LockedDateTime = DateTime.Now,
                            LockedTypeValue = (int)lockedType
                        };

                        s.Save(lu);
                        su();
                        success();
                    }
                    catch (Exception eX)
                    {
                        f();
                        failure(eX.Message);
                    }
                });
            });
        }
        /// <summary>
        /// 获取账户的安全信息
        /// </summary>
        /// <param name="useraccount"></param>
        /// <param name="success"></param>
        /// <param name="failure"></param>
        public void GetUserSecurityInfo(string useraccount, ISession session, Action<UserSecurityInfo> success, Action<string> failure)
        {
            UserSecurityInfo usi = null;

            session.UsingSafe(s =>
            {
                usi = s.Get<UserSecurityInfo>(useraccount);

                if (null == usi)
                {
                    failure("找不到指定账户的安全信息");
                }
                else
                {
                    success(usi);
                }
            });
        }