/// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">帐号</param>
        /// <param name="password">密码</param>
        /// <returns>用户</returns>
        public User Login(string account, string password)
        {
            User result = null;

            UserHibernate hibernate = new UserHibernate();
            User user = hibernate.SelectByAccount(account);
            if (user != null)
            {
                if ((user.Password == password) && (user.Validity))
                {
                    result = user;
                    user.Authentication = true;
                }
            }
            else
            {
                int count = hibernate.Count();
                if (count == 0)
                {
                    result = new User();
                    result.Guid = "Prerogative";
                    result.Prerogative = true;
                    result.Authentication = true;
                }
            }
            if ((result != null) && (result.Authentication))
            {
                this.Refresh(result);
            }

            return result;
        }
        public bool Delete(User value)
        {
            bool result = false;

            string sql = string.Format("delete from m_user_module as t where [t].[user_id] = '{0}'", value.Guid);
            List<Parameter> parameters = new List<Parameter>();

            DatabaseHibernate hibernate = new DatabaseHibernate();

            result = hibernate.Write(Variable.Link, sql, parameters);

            if (result)
            {
                sql = string.Format("delete from e_user_room as t where [t].[user_id] = '{0}'", value.Guid);
                parameters.Clear();
                result = hibernate.Write(Variable.Link, sql, parameters);
            }

            if (result)
            {
                sql = string.Format("delete from m_user as t where [t].[guid] = '{0}'", value.Guid);
                parameters.Clear();
                result = hibernate.Write(Variable.Link, sql, parameters);
            }

            return result;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(User value)
        {
            bool result = false;

            UserHibernate hibernate = new UserHibernate();
            result = hibernate.Insert(value);

            return result;
        }
        public bool Delete(User value)
        {
            bool result = false;

            UserHibernate hibernate = new UserHibernate();
            result = hibernate.Delete(value);

            return result;
        }
        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            bool done = false;
            StringBuilder stringBuilder = new StringBuilder();

            UserBusiness business = new UserBusiness();

            if (this.CheckBoxListUsers.Items != null)
            {
                List<User> users = new List<User>();
                for (int i = 0; i < this.CheckBoxListUsers.Items.Count; i++)
                {
                    if (this.CheckBoxListUsers.Items[i].Selected)
                    {
                        User user = new User();
                        user.Guid = this.CheckBoxListUsers.Items[i].Value;
                        users.Add(user);
                    }
                }

                if ((users != null) && (users.Count > 0))
                {
                    int success = 0;
                    int fail = 0;
                    for (int i = 0; i < users.Count; i++)
                    {
                        done = business.Delete(users[i]);
                        if (done)
                        {
                            success++;
                        }
                        else
                        {
                            fail++;
                        }
                    }
                    stringBuilder.Append(string.Format("删除{0}个用户成功!", success));
                    stringBuilder.Append(string.Format("删除{0}个用户失败!", fail));
                }
            }

            this.LabelMessage.Text = stringBuilder.ToString();

            this.InitializeBind();
        }
 protected void Session_Start(object sender, EventArgs e)
 {
     EnvironmentalMonitor.Support.Module.Manage.User user = new EnvironmentalMonitor.Support.Module.Manage.User();
     user.Prerogative = false;
     this.Session.Add(Constant.SESSION_KEY_USER, user);
 }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Update(User value)
        {
            bool result = false;

            string sql = string.Format("update m_user as t set [t].[update_user_id] = :update_user_id, [t].[update_time] = :update_time, [t].[remark] = :remark, [t].[validity] = :validity, [t].[name] = :name, [t].[account] = :account, [t].[password] = :password, [t].[prerogative] = :prerogative where [t].[guid] = '{0}'", value.Guid);
            List<Parameter> parameters = new List<Parameter>();

            parameters.Add(new Parameter("update_user_id", DatabaseHibernate.Parameter(value.UpdateUserId)));
            parameters.Add(new Parameter("update_time", DatabaseHibernate.Parameter(value.UpdateTime)));
            parameters.Add(new Parameter("remark", DatabaseHibernate.Parameter(value.Remark)));
            parameters.Add(new Parameter("validity", DatabaseHibernate.Parameter(value.Validity)));

            parameters.Add(new Parameter("name", DatabaseHibernate.Parameter(value.Name)));
            parameters.Add(new Parameter("account", DatabaseHibernate.Parameter(value.Account.ToUpper())));
            parameters.Add(new Parameter("password", DatabaseHibernate.Parameter(value.Password)));
            parameters.Add(new Parameter("prerogative", DatabaseHibernate.Parameter(value.Prerogative)));

            DatabaseHibernate hibernate = new DatabaseHibernate();

            result = hibernate.Write(Variable.Link, sql, parameters);

            return result;
        }
        /// <summary>
        /// 通过帐户查询
        /// </summary>
        /// <param name="account">帐号</param>
        /// <returns>用户</returns>
        public User SelectByAccount(string account)
        {
            User result = null;

            string sql = string.Format("select {0} from m_user as t where ucase([t].[account]) = ucase(:account)", this.Asterisk("[t]."));
            List<Parameter> parameters = new List<Parameter>();

            parameters.Add(new Parameter("account", DatabaseHibernate.Parameter(account)));

            DatabaseHibernate hibernate = new DatabaseHibernate();

            List<object[]> values = hibernate.Read(Variable.Link, sql, parameters);

            if ((values != null) && (values.Count == 1))
            {
                result = new User();

                object[] moduleValues = values[0];

                result = this.Parse(moduleValues);
            }

            return result;
        }
        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="guid">主键</param>
        /// <returns>用户</returns>
        public User QueryByGuid(string guid)
        {
            User result = null;

            string sql = string.Format("select {0} from m_user as t where [t].[guid] = :guid", this.Asterisk("[t]."));
            List<Parameter> parameters = new List<Parameter>();

            parameters.Add(new Parameter("guid", DatabaseHibernate.Parameter(guid)));

            DatabaseHibernate hibernate = new DatabaseHibernate();

            List<object[]> values = hibernate.Read(Variable.Link, sql, parameters);

            if ((values != null) && (values.Count == 1))
            {
                result = new User();

                object[] moduleValues = values[0];

                result = this.Parse(moduleValues);
            }

            return result;
        }
        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="values">数据</param>
        /// <returns>用户</returns>
        public User Parse(object[] values)
        {
            User result = new User();

            try
            {
                result = DatabaseHibernate.ParseModule(result, values) as User;

                result.Name = DatabaseHibernate.ParseString(values[7]);
                result.Account = DatabaseHibernate.ParseString(values[8]);
                result.Password = DatabaseHibernate.ParseString(values[9]);
                result.Prerogative = DatabaseHibernate.ParseBoolean(values[10]);
            }
            catch (Exception exception)
            {
                result = null;
                EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
            }

            return result;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(User value)
        {
            bool result = false;

            string sql = string.Format("insert into m_user ({0}) values (:guid, :insert_user_id, :insert_time, :update_user_id, :update_time, :remark, :validity, :name, :account, :password, :prerogative)", this.Asterisk(""));
            List<Parameter> parameters = new List<Parameter>();

            parameters.Add(new Parameter("guid", DatabaseHibernate.Parameter(DatabaseHibernate.GUID())));
            parameters.Add(new Parameter("insert_user_id", DatabaseHibernate.Parameter(value.InsertUserId)));
            parameters.Add(new Parameter("insert_time", DatabaseHibernate.Parameter(value.InsertTime)));
            parameters.Add(new Parameter("update_user_id", DatabaseHibernate.Parameter(value.UpdateUserId)));
            parameters.Add(new Parameter("update_time", DatabaseHibernate.Parameter(value.UpdateTime)));
            parameters.Add(new Parameter("remark", DatabaseHibernate.Parameter(value.Remark)));
            parameters.Add(new Parameter("validity", DatabaseHibernate.Parameter(value.Validity)));
            parameters.Add(new Parameter("name", DatabaseHibernate.Parameter(value.Name)));
            parameters.Add(new Parameter("account", DatabaseHibernate.Parameter(value.Account.ToUpper())));
            parameters.Add(new Parameter("password", DatabaseHibernate.Parameter(value.Password)));
            parameters.Add(new Parameter("prerogative", DatabaseHibernate.Parameter(value.Prerogative)));

            DatabaseHibernate hibernate = new DatabaseHibernate();

            result = hibernate.Write(Variable.Link, sql, parameters);

            return result;
        }
        private void InitializeBind()
        {
            this.RefreshUser();

            this.TextBoxName.Text = string.Empty;
            this.TextBoxAccount.Text = string.Empty;
            this.TextBoxPassword.Attributes["value"] = string.Empty;
            this.TextBoxRepassword.Attributes["value"] = string.Empty;

            User sessionUser = this.Session[Constant.SESSION_KEY_USER] as User;

            this.CheckBoxListRooms.DataSource = sessionUser.Rooms;
            this.CheckBoxListRooms.DataTextField = "Name";
            this.CheckBoxListRooms.DataValueField = "Guid";
            this.CheckBoxListRooms.DataBind();

            this.CheckBoxListModules.DataSource = sessionUser.Catalogs;
            this.CheckBoxListModules.DataTextField = "Name";
            this.CheckBoxListModules.DataValueField = "Code";
            this.CheckBoxListModules.DataBind();

            List<User> users = new List<User>();
            User emptyUser = new User();
            emptyUser.Guid = string.Empty;
            emptyUser.Name = string.Empty;
            emptyUser.Account = string.Empty;
            users.Add(emptyUser);
            users.AddRange(sessionUser.Users);
            this.DropDownListUsers.DataSource = users;
            this.DropDownListUsers.DataTextField = "AccountName";
            this.DropDownListUsers.DataValueField = "Guid";
            this.DropDownListUsers.DataBind();
        }
        public List<User> QueryByInsertUser(User value)
        {
            List<User> results = null;

            UserHibernate hibernate = new UserHibernate();
            results = hibernate.QueryByInsertUser(value.Guid);

            return results;
        }
        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            bool done = false;
            StringBuilder stringBuilder = new StringBuilder();

            UserBusiness business = new UserBusiness();
            User module = new User();

            this.InitializeInsertModule(module);

            module.Name = this.TextBoxName.Text;
            module.Account = this.TextBoxAccount.Text;
            module.Password = this.TextBoxPassword.Text;
            module.Prerogative = false;

            User user = business.QueryByAccount(module.Account);

            if (user == null)
            {
                done = business.Insert(module);

                if (done)
                {
                    stringBuilder.Append("新增用户成功!");
                }
                else
                {
                    stringBuilder.Append("新增用户失败!");
                }
            }
            else
            {
                stringBuilder.Append("用户已经存在(账户相同)!");
            }

            if (done)
            {
                user = business.QueryByAccount(module.Account);
                if ((user != null) && (user.Guid != null))
                {
                    if (this.CheckBoxListRooms.Items != null)
                    {
                        List<UserRoom> userRooms = new List<UserRoom>();
                        for (int i = 0; i < this.CheckBoxListRooms.Items.Count; i++)
                        {
                            if (this.CheckBoxListRooms.Items[i].Selected)
                            {
                                UserRoom userRoom = new UserRoom();
                                this.InitializeInsertModule(userRoom);

                                userRoom.UserId = user.Guid;
                                userRoom.RoomId = this.CheckBoxListRooms.Items[i].Value;

                                userRooms.Add(userRoom);
                            }
                        }
                        UserRoomBusiness userRoomBusiness = new UserRoomBusiness();
                        done = userRoomBusiness.Refresh(user.Guid, userRooms);

                        if (done)
                        {
                            stringBuilder.Append("更新机房成功!");
                        }
                        else
                        {
                            stringBuilder.Append("更新机房失败!");
                        }
                    }
                }
                if ((user != null) && (user.Guid != null))
                {
                    if (this.CheckBoxListModules.Items != null)
                    {
                        List<UserModule> userModules = new List<UserModule>();
                        for (int i = 0; i < this.CheckBoxListModules.Items.Count; i++)
                        {
                            if (this.CheckBoxListModules.Items[i].Selected)
                            {
                                UserModule userModule = new UserModule();
                                this.InitializeInsertModule(userModule);

                                userModule.UserId = user.Guid;
                                userModule.ModuleCode = this.CheckBoxListModules.Items[i].Value;

                                userModules.Add(userModule);
                            }
                        }
                        UserModuleBusiness userModuleBusiness = new UserModuleBusiness();
                        done = userModuleBusiness.Refresh(user.Guid, userModules);

                        if (done)
                        {
                            stringBuilder.Append("更新权限成功!");
                        }
                        else
                        {
                            stringBuilder.Append("更新权限失败!");
                        }
                    }
                }
            }

            this.LabelMessage.Text = stringBuilder.ToString();

            this.InitializeBind();
        }
        /// <summary>
        /// 验证页面
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="pageName">页面类型</param>
        /// <returns>结果</returns>
        public bool VerifyPage(User user, Type pageType)
        {
            bool result = false;

            if (!result)
            {
                if (user.Prerogative)
                {
                    result = user.Prerogative;
                }
            }
            if (!result)
            {
                object page = pageType.Assembly.CreateInstance(pageType.FullName);
                if (page != null)
                {
                    PropertyInfo propertyInfo = pageType.GetProperty("Code");
                    if (propertyInfo != null)
                    {
                        object codeValue = propertyInfo.GetValue(page, null);
                        if (codeValue != null)
                        {
                            string code = codeValue as string;
                            if (code != null)
                            {
                                if (string.Equals(code, UserModule.DEFAULT_MODULE, StringComparison.CurrentCulture))
                                {
                                    result = true;
                                }
                                else
                                {
                                    for (int i = 0; i < user.UserModules.Count; i++)
                                    {
                                        if (string.Equals(code, user.UserModules[i].ModuleCode, StringComparison.CurrentCulture))
                                        {
                                            result = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
        public List<User> RefreshUsers(User value)
        {
            List<User> results = null;

            int total = 0;
            if (value.Prerogative)
            {
                results = this.Query(1, int.MaxValue, ref total);
            }
            else
            {
                results = this.QueryByInsertUser(value);
            }
            for (int i = results.Count - 1; i >= 0; i--)
            {
                if (string.Equals(value.Guid, results[i].Guid, StringComparison.CurrentCulture))
                {
                    results.RemoveAt(i);
                }
            }

            return results;
        }
        public List<UserModule> RefreshUserModules(User value)
        {
            List<UserModule> results = null;

            if (value.Prerogative)
            {
                results = new List<UserModule>();
                for (int i = 0; i < UserModule.Modules.Length; i++)
                {
                    UserModule userModule = new UserModule();
                    userModule.ModuleCode = UserModule.Modules[i];
                    results.Add(userModule);
                }
            }
            else
            {
                UserModuleBusiness business = new UserModuleBusiness();
                int total = 0;
                results = business.QueryByUser(1, int.MaxValue, ref total, value.Guid);
            }

            return results;
        }
        public List<Room> RefreshRooms(User value)
        {
            List<Room> results = null;

            RoomHibernate roomHibernate = new RoomHibernate();
            int total = 0;
            List<Room> rooms = roomHibernate.Query(1, int.MaxValue, ref total);
            if (value.Prerogative)
            {
                results = rooms;
            }
            else
            {
                UserRoomHiberante userRoomHiberante = new UserRoomHiberante();
                List<UserRoom> userRooms = userRoomHiberante.QueryByUser(1, int.MaxValue, ref total, value.Guid);
                if ((userRooms != null) && (userRooms.Count > 0))
                {
                    if ((rooms != null) && (rooms.Count > 0))
                    {
                        results = new List<Room>();
                        for (int i = 0; i < userRooms.Count; i++)
                        {
                            for (int j = 0; j < rooms.Count; j++)
                            {
                                if (string.Equals(userRooms[i].RoomId, rooms[j].Guid, StringComparison.CurrentCulture))
                                {
                                    results.Add(rooms[j]);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return results;
        }
        public void Refresh(User value)
        {
            value.Rooms = this.RefreshRooms(value);
            value.Users = this.RefreshUsers(value);
            value.UserModules = this.RefreshUserModules(value);

            if (value.UserModules != null)
            {
                value.Catalogs = new List<Catalog>();
                List<UserModule> userModules = value.UserModules;
                for (int i = 0; i < userModules.Count; i++)
                {
                    Catalog catalog = null;
                    switch (userModules[i].ModuleCode)
                    {
                        case UserModule.MANAGE_MODULE:
                            catalog = new Catalog(UserModule.MANAGE_MODULE, "用户管理", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.MANAGE_MODULE, "新增用户", string.Format("{0}Manages/Users/Insert.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.MANAGE_MODULE, "修改用户", string.Format("{0}Manages/Users/Update.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.MANAGE_MODULE, "删除用户", string.Format("{0}Manages/Users/Delete.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.ROOM_MODULE:
                            catalog = new Catalog(UserModule.ROOM_MODULE, "机房配置", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.ROOM_MODULE, "新增机房", string.Format("{0}Environmentals/Rooms/Insert.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.ROOM_MODULE, "修改机房", string.Format("{0}Environmentals/Rooms/Update.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.ROOM_MODULE, "删除机房", string.Format("{0}Environmentals/Rooms/Delete.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.MACHINE_MODULE:
                            catalog = new Catalog(UserModule.MACHINE_MODULE, "检测仪配置", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.MACHINE_MODULE, "新增检测仪", string.Format("{0}Environmentals/Machines/Insert.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.MACHINE_MODULE, "修改检测仪", string.Format("{0}Environmentals/Machines/Update.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.MACHINE_MODULE, "删除检测仪", string.Format("{0}Environmentals/Machines/Delete.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.MACHINE_ADVANCED_MODULE:
                            catalog = new Catalog(UserModule.MACHINE_ADVANCED_MODULE, "检测仪高级配置", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.MACHINE_ADVANCED_MODULE, "检测仪IP设置", string.Format("{0}Environmentals/Machines/SetupIp.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.MACHINE_ADVANCED_MODULE, "检测仪物理地址设置", string.Format("{0}Environmentals/Machines/SetupMac.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.DETECTOR_TYPE_MODULE:
                            catalog = new Catalog(UserModule.DETECTOR_TYPE_MODULE, "探头类型配置", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.DETECTOR_TYPE_MODULE, "新增探头类型", string.Format("{0}Environmentals/DetectorTypes/Insert.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.DETECTOR_TYPE_MODULE, "修改探头类型", string.Format("{0}Environmentals/DetectorTypes/Update.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.DETECTOR_TYPE_MODULE, "删除探头类型", string.Format("{0}Environmentals/DetectorTypes/Delete.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.DETECTOR_MODULE:
                            catalog = new Catalog(UserModule.DETECTOR_MODULE, "探头配置", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.DETECTOR_MODULE, "新增探头", string.Format("{0}Environmentals/Detectors/Insert.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.DETECTOR_MODULE, "修改探头", string.Format("{0}Environmentals/Detectors/Update.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.DETECTOR_MODULE, "删除探头", string.Format("{0}Environmentals/Detectors/Delete.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.ALARM_MODULE:
                            catalog = new Catalog(UserModule.ALARM_MODULE, "报警设置", string.Format("{0}Environmentals/Machines/SetupAlarm.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath));
                            break;
                        case UserModule.MONITOR_MODULE:
                            catalog = new Catalog(UserModule.MONITOR_MODULE, "监控模式", "#");
                            catalog.Catalogs = new List<Catalog>();
                            catalog.Catalogs.Add(new Catalog(UserModule.MONITOR_MODULE, "平面图配置", string.Format("{0}Environmentals/Monitors/FloorPlan.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            catalog.Catalogs.Add(new Catalog(UserModule.MONITOR_MODULE, "实时监控", string.Format("{0}Environmentals/Monitors/Realtime.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath)));
                            break;
                        case UserModule.QUERY_MODULE:
                            catalog = new Catalog(UserModule.QUERY_MODULE, "查询模式", string.Format("{0}Environmentals/Querys/Default.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath));
                            break;
                    }
                    if (catalog != null)
                    {
                        value.Catalogs.Add(catalog);
                    }
                }
            }
        }