/// <summary>
        /// 缓存预热,强制重新缓存
        /// </summary>
        /// <returns>影响行数</returns>
        public static int CachePreheatingMACAddress()
        {
            int result = 0;

            List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();

            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryCode, "MacAddress"));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldEnabled, 1));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldDeletionStateCode, 0));

            // 把所有的数据都缓存起来的代码
            BaseParameterManager manager = new BaseParameterManager();

            using (var redisClient = PooledRedisHelper.GetClient())
            {
                using (IDataReader dataReader = manager.ExecuteReader(parameters))
                {
                    while (dataReader.Read())
                    {
                        string key = "MAC:" + dataReader[BaseParameterEntity.FieldParameterId].ToString();

                        string macAddress = dataReader[BaseParameterEntity.FieldParameterContent].ToString().ToLower();
                        redisClient.AddItemToSet(key, macAddress);

                        redisClient.ExpireEntryAt(key, DateTime.Now.AddMonths(3));

                        result++;
                        System.Console.WriteLine(result.ToString() + " : " + macAddress);
                    }
                    dataReader.Close();
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// 缓存预热,强制重新缓存
        /// </summary>
        /// <returns>影响行数</returns>
        public static int CachePreheatingMacAddress()
        {
            var result = 0;

            var parameters = new List <KeyValuePair <string, object> >();

            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryCode, "MacAddress"));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldEnabled, 1));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldDeleted, 0));

            // 把所有的数据都缓存起来的代码
            var manager = new BaseParameterManager();

            var dataReader = manager.ExecuteReader(parameters);

            if (dataReader != null && !dataReader.IsClosed)
            {
                while (dataReader.Read())
                {
                    var key = "MAC:" + dataReader[BaseParameterEntity.FieldParameterId];

                    var macAddress = dataReader[BaseParameterEntity.FieldParameterContent].ToString().ToLower();
                    CacheUtil.Set(key, macAddress);
                    result++;
                    if (result % 500 == 0)
                    {
                        Console.WriteLine(result + " : " + macAddress);
                    }
                }

                dataReader.Close();
            }

            return(result);
        }
        public static int CachePreheatingMACAddressByUser(IRedisClient redisClient, string userId)
        {
            int result = 0;

            List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();

            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldParameterId, userId));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryCode, "MacAddress"));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldEnabled, 1));
            parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldDeletionStateCode, 0));

            string key = "MAC:" + userId;

            BaseParameterManager parameterManager = new BaseParameterManager();

            parameterManager.SelectFields = BaseParameterEntity.FieldParameterContent;
            using (IDataReader dataReader = parameterManager.ExecuteReader(parameters))
            {
                while (dataReader.Read())
                {
                    string macAddress = dataReader[BaseParameterEntity.FieldParameterContent].ToString().ToLower();
                    redisClient.AddItemToSet(key, macAddress);
                    result++;
                }
                dataReader.Close();

                redisClient.ExpireEntryAt(key, DateTime.Now.AddMonths(3));
            }

            return(result);
        }
        /// <summary>
        /// 根据用户编号删除预热的IP
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static int CachePreheatingIpAddressByUser(string userId)
        {
            var result = 0;

            var parameters = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>(BaseParameterEntity.FieldParameterId, userId),
                new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryCode, "IPAddress"),
                new KeyValuePair <string, object>(BaseParameterEntity.FieldEnabled, 1),
                new KeyValuePair <string, object>(BaseParameterEntity.FieldDeleted, 0)
            };

            var key = "IP:" + userId;

            var parameterManager = new BaseParameterManager
            {
                SelectFields = BaseParameterEntity.FieldParameterContent
            };
            var dataReader = parameterManager.ExecuteReader(parameters);

            if (dataReader != null && !dataReader.IsClosed)
            {
                while (dataReader.Read())
                {
                    var ipAddress = dataReader[BaseParameterEntity.FieldParameterContent].ToString().ToLower();
                    CacheUtil.Set(key, ipAddress);
                    result++;
                }

                dataReader.Close();
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// 设置参数值
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="categoryCode">分类编号</param>
        /// <param name="parameterId">参数主键</param>
        /// <param name="parameterCode">参数编号</param>
        /// <param name="parameterContent">参数内容</param>
        /// <returns>影响行数</returns>
        public int SetParameter(BaseUserInfo userInfo, string tableName, string categoryCode, string parameterId, string parameterCode, string parameterContent)
        {
            int result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo, tableName);
                result      = manager.SetParameter(tableName, categoryCode, parameterId, parameterCode, parameterContent);
            });
            return(result);
        }
        /// <summary>
        /// 按参数编号删除
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="categoryCode">分类编号</param>
        /// <param name="parameterId">参数主键</param>
        /// <param name="parameterCode">参数编号</param>
        /// <returns>影响行数</returns>
        public int DeleteByParameterCode(BaseUserInfo userInfo, string categoryCode, string parameterId, string parameterCode)
        {
            var result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo);
                result      = manager.DeleteByParameterCode(categoryCode, parameterId, parameterCode);
            });
            return(result);
        }
        /// <summary>
        /// 按编号获取参数列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="categoryCode">分类编号</param>
        /// <param name="parameterId">参数主键</param>
        /// <param name="parameterCode">参数编号</param>
        /// <returns>数据表</returns>
        public DataTable GetDataTableParameterCode(BaseUserInfo userInfo, string categoryCode, string parameterId, string parameterCode)
        {
            var dt = new DataTable(BaseParameterEntity.CurrentTableName);

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var manager  = new BaseParameterManager(dbHelper, userInfo);
                dt           = manager.GetDataTableParameterCode(categoryCode, parameterId, parameterCode);
                dt.TableName = BaseParameterEntity.CurrentTableName;
            });
            return(dt);
        }
        /// <summary>
        /// 获取参数值
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="categoryCode">分类编号</param>
        /// <param name="parameterId">参数主键</param>
        /// <param name="parameterCode">参数编号</param>
        /// <returns>参数值</returns>
        public string GetParameter(BaseUserInfo userInfo, string tableName, string categoryCode, string parameterId, string parameterCode)
        {
            var result = string.Empty;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo, tableName);
                result      = manager.GetParameter(tableName, categoryCode, parameterId, parameterCode);
            });

            return(result);
        }
        /// <summary>
        /// 用户名是否重复
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="parameters">字段名,字段值</param>
        /// <returns>已存在</returns>
        public bool Exists(BaseUserInfo userInfo, List <KeyValuePair <string, object> > parameters)
        {
            var result = false;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo);
                result      = manager.Exists(parameters);
            });

            return(result);
        }
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">实体</param>
        /// <returns>主键</returns>
        public string Add(BaseUserInfo userInfo, string tableName, BaseParameterEntity entity)
        {
            var result = string.Empty;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDbWithTransaction(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo, tableName);
                result      = manager.Add(entity);
            });

            return(result);
        }
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public BaseParameterEntity GetEntity(BaseUserInfo userInfo, string tableName, string id)
        {
            BaseParameterEntity entity = null;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo, tableName);
                entity      = manager.GetEntity(id);
            });

            return(entity);
        }
        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(BaseUserInfo userInfo, string id)
        {
            var result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo);
                result      = manager.SetDeleted(id, true, true);
            });

            return(result);
        }
        /// <summary>
        /// 获取记录
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetSystemParameter(BaseUserInfo userInfo)
        {
            var result = new DataTable(BaseParameterEntity.CurrentTableName);

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var manager      = new BaseParameterManager(dbHelper, userInfo, "SystemParameter");
                result           = manager.GetSystemParameter();
                result.TableName = BaseParameterEntity.CurrentTableName;
            });

            return(result);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">实体</param>
        /// <returns>影响行数</returns>
        public int Update(BaseUserInfo userInfo, string tableName, BaseParameterEntity entity)
        {
            var result = 0;

            var returnCode    = string.Empty;
            var returnMessage = string.Empty;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo, tableName);
                result      = manager.Update(entity);
            });
            return(result);
        }
        /// <summary>
        /// 批量删除参数
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            var result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseParameterManager(dbHelper, userInfo);
                for (var i = 0; i < ids.Length; i++)
                {
                    result += manager.SetDeleted(ids[i], true, true);
                }
            });

            return(result);
        }
Example #16
0
        public static int RefreshCache(string tableName)
        {
            int result = 0;

            BaseParameterManager       parameterManager = new BaseParameterManager(tableName);
            List <BaseParameterEntity> list             = parameterManager.GetList <BaseParameterEntity>(
                new KeyValuePair <string, object>(BaseParameterEntity.FieldDeletionStateCode, 0)
                , new KeyValuePair <string, object>(BaseParameterEntity.FieldEnabled, 1));

            foreach (var entity in list)
            {
                // 2016-03-11 吉日嘎拉 强制刷新缓存
                BaseParameterManager.SetParameterByCache(tableName, entity);
                result++;
            }

            return(result);
        }
Example #17
0
        /// <summary>
        /// 批量删除参数
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbHelper dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType))
            {
                try
                {
                    dbHelper.Open(UserCenterDbConnection);
                    BaseParameterManager parameterManager = new BaseParameterManager(dbHelper, userInfo);
                    for (int i = 0; i < ids.Length; i++)
                    {
                        returnValue += parameterManager.Delete(ids[i]);
                    }
                    BaseLogManager.Instance.Add(dbHelper, userInfo, this.serviceName, AppMessage.ParameterService_BatchDelete, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return(returnValue);
        }
Example #18
0
        /// <summary>
        /// 按编号获取参数列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="categoryId">分类主键</param>
        /// <param name="parameterId">参数主键</param>
        /// <param name="parameterCode">参数编号</param>
        /// <returns>数据表</returns>
        public DataTable GetDataTableParameterCode(BaseUserInfo userInfo, string categoryId, string parameterId, string parameterCode)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(BaseParameterEntity.TableName);
            using (IDbHelper dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType))
            {
                try
                {
                    dbHelper.Open(UserCenterDbConnection);
                    BaseParameterManager parameterManager = new BaseParameterManager(dbHelper, userInfo);
                    dataTable           = parameterManager.GetDataTableParameterCode(categoryId, parameterId, parameterCode);
                    dataTable.TableName = BaseParameterEntity.TableName;
                    BaseLogManager.Instance.Add(dbHelper, userInfo, this.serviceName, AppMessage.ParameterService_GetDataTableParameterCode, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return(dataTable);
        }
        /// <summary>
        /// 获取参数列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="categoryCode">分类编号</param>
        /// <param name="parameterId">参数主键</param>
        /// <returns>数据表</returns>
        public DataTable GetDataTableByParameter(BaseUserInfo userInfo, string categoryCode, string parameterId)
        {
            var result = new DataTable(BaseParameterEntity.CurrentTableName);

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var manager      = new BaseParameterManager(dbHelper, userInfo);
                result           = manager.GetDataTableByParameter(categoryCode, parameterId);
                result.TableName = BaseParameterEntity.CurrentTableName;

                // 2015-12-21 吉日嘎拉,这里重新设置mac缓存,删除掉缓存。
                if (categoryCode.Equals("MacAddress"))
                {
                    BaseUserManager.ResetMacAddressByCache(parameterId);
                }
            });

            return(result);
        }
        /// <summary>
        /// 检查用户的 macAddress 绑定是否正常
        ///
        /// 防止重复多读数据?
        /// 是否判断正确?
        /// 可以按每个用户缓存?
        /// 若没有就自动化增加?
        /// mac 限制完善?
        /// mac 限制缓存预热?
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="macAddress">硬件地址</param>
        /// <returns>正确</returns>
        public static bool CheckMACAddressByCache(string userId, string macAddress)
        {
            // 默认是不成功的,防止出错误
            bool result = false;

            // 检查参数的有效性
            if (string.IsNullOrEmpty(userId))
            {
                return(result);
            }
            if (string.IsNullOrEmpty(macAddress))
            {
                return(result);
            }

            // 提高效率,全小写转换
            macAddress = macAddress.ToLower();

            // 这里是处理,多个mac的问题
            string[] mac = macAddress.Split(';');

            using (var redisClient = PooledRedisHelper.GetClient())
            {
                string key = "MAC:" + userId;

                // 若是缓存里过期了?
                if (!redisClient.ContainsKey(key))
                {
                    // 重新缓存用户的限制数据
                    if (CachePreheatingMACAddressByUser(redisClient, userId) == 0)
                    {
                        // 若没有设置mac限制,需要把限制都自动加上来。
                        // 没有加到数据的,就是表明是新增加的用户、第一次登录的用户
                        BaseParameterManager parameterManager = new BaseParameterManager();
                        for (int i = 0; i < mac.Length; i++)
                        {
                            if (!string.IsNullOrEmpty(mac[i]))
                            {
                                // 把收集过来的mac地址需要保存起来
                                BaseParameterEntity parameterEntity = new BaseParameterEntity();
                                parameterEntity.Id            = Guid.NewGuid().ToString("N");
                                parameterEntity.CategoryCode  = "MacAddress";
                                parameterEntity.ParameterCode = "Single";
                                parameterEntity.ParameterId   = userId;
                                // 这里之际保存小写、就效率也高,省事了
                                parameterEntity.ParameterContent = mac[i].Trim();
                                parameterManager.Add(parameterEntity);
                            }
                        }
                        result = true;
                    }
                }

                // 若还是没有?表示是新增的
                if (redisClient.ContainsKey(key))
                {
                    // 若已经存在,就需要进行缓存里的判断?
                    // 这里要提高效率,不能反复打开缓存
                    for (int i = 0; i < mac.Length; i++)
                    {
                        // 这里对数据还不放心,进行优化处理
                        if (!string.IsNullOrEmpty(mac[i]))
                        {
                            mac[i] = mac[i].Trim();
                            result = redisClient.SetContainsItem(key, mac[i]);
                            if (result)
                            {
                                // 这里要提高判断的效率
                                break;
                            }
                        }
                    }
                    // 若没有验证成功、把当前的 macAddress 保存起来, 方便后台管理的人加上去。
                    if (!result)
                    {
                        List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
                        parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldMACAddress, macAddress));
                        BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager();
                        userLogOnManager.SetProperty(userId, parameters);
                    }
                }
            }

            return(result);
        }
Example #21
0
        /// <summary>
        /// 进行登录操作
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="createNewOpenId"></param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="macAddress">MAC地址</param>
        /// <param name="checkUserPassword">是否要检查用户密码</param>
        /// <returns>用户信息</returns>
        public BaseUserInfo LogOn(string userName, string password, bool createNewOpenId = false, string ipAddress = null, string macAddress = null, bool checkUserPassword = true)
        {
            BaseUserInfo userInfo = null;

            string realName = string.Empty;

            if (UserInfo != null)
            {
                realName = UserInfo.RealName;
            }

            if (ipAddress == null)
            {
                if (UserInfo != null)
                {
                    ipAddress = UserInfo.IPAddress;
                }
            }

            // 01: 系统是否采用了在线用户的限制
            if (BaseSystemInfo.OnLineLimit > 0)
            {
                if (this.CheckOnLineLimit())
                {
                    this.ReturnStatusCode = StatusCode.ErrorOnLineLimit.ToString();
                    BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0089 + BaseSystemInfo.OnLineLimit.ToString());
                    return(userInfo);
                }
            }

            // 04. 默认为用户没有找到状态,查找用户
            // 这是为了达到安全要求,不能提示用户未找到,那容易让别人猜测到帐户
            if (BaseSystemInfo.CheckPasswordStrength)
            {
                this.ReturnStatusCode = StatusCode.ErrorLogOn.ToString();
            }
            else
            {
                this.ReturnStatusCode = StatusCode.UserNotFound.ToString();
            }

            // 02. 查询数据库中的用户数据?只查询未被删除的
            // 先按用户名登录
            DataTable dataTable = this.GetDataTable(new KeyValuePair <string, object>(BaseUserEntity.FieldUserName, userName)
                                                    , new KeyValuePair <string, object>(BaseUserEntity.FieldDeletionStateCode, 0));

            // 若不是严格检查,可以采用多种方式登录
            if (!BaseSystemInfo.CheckPasswordStrength)
            {
                if (dataTable.Rows.Count == 0)
                {
                    // 若没数据再按工号登录
                    dataTable = this.GetDataTable(new KeyValuePair <string, object>(BaseUserEntity.FieldCode, userName)
                                                  , new KeyValuePair <string, object>(BaseUserEntity.FieldDeletionStateCode, 0));
                }
                if (dataTable.Rows.Count == 0)
                {
                    // 若没数据再按邮件登录
                    dataTable = this.GetDataTable(new KeyValuePair <string, object>(BaseUserEntity.FieldEmail, userName)
                                                  , new KeyValuePair <string, object>(BaseUserEntity.FieldDeletionStateCode, 0));
                }
                if (dataTable.Rows.Count == 0)
                {
                    // 若没数据再按手机号码登录
                    dataTable = this.GetDataTable(new KeyValuePair <string, object>(BaseUserEntity.FieldMobile, userName)
                                                  , new KeyValuePair <string, object>(BaseUserEntity.FieldDeletionStateCode, 0));
                }
                if (dataTable.Rows.Count == 0)
                {
                    // 若没数据再按手机号码登录
                    dataTable = this.GetDataTable(new KeyValuePair <string, object>(BaseUserEntity.FieldTelephone, userName)
                                                  , new KeyValuePair <string, object>(BaseUserEntity.FieldDeletionStateCode, 0));
                }
            }
            BaseUserEntity userEntity = null;

            if (dataTable.Rows.Count > 1)
            {
                this.ReturnStatusCode = StatusCode.UserDuplicate.ToString();
            }
            else if (dataTable.Rows.Count == 1)
            {
                // 03. 系统是否采用了密码加密策略?
                string encryptPassword = string.Empty;
                if (checkUserPassword)
                {
                    if (BaseSystemInfo.ServerEncryptPassword)
                    {
                        password = this.EncryptUserPassword(password);
                    }
                }

                // 05. 判断密码,是否允许登录,是否离职是否正确
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    userEntity = new BaseUserEntity(dataRow);
                    if (!string.IsNullOrEmpty(userEntity.AuditStatus) && userEntity.AuditStatus.EndsWith(AuditStatus.WaitForAudit.ToString()))
                    {
                        this.ReturnStatusCode = AuditStatus.WaitForAudit.ToString();
                        BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0078);
                        return(userInfo);
                    }
                    // 用户是否有效的
                    if (userEntity.Enabled == 0)
                    {
                        this.ReturnStatusCode = StatusCode.LogOnDeny.ToString();
                        BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0079);
                        return(userInfo);
                    }
                    // 用户是否有效的
                    if (userEntity.Enabled == -1)
                    {
                        this.ReturnStatusCode = StatusCode.UserNotActive.ToString();
                        BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0080);
                        return(userInfo);
                    }

                    // 06. 允许登录时间是否有限制
                    if (userEntity.AllowEndTime != null)
                    {
                        userEntity.AllowEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, userEntity.AllowEndTime.Value.Hour, userEntity.AllowEndTime.Value.Minute, userEntity.AllowEndTime.Value.Second);
                    }
                    if (userEntity.AllowStartTime != null)
                    {
                        userEntity.AllowStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, userEntity.AllowStartTime.Value.Hour, userEntity.AllowStartTime.Value.Minute, userEntity.AllowStartTime.Value.Second);
                        if (DateTime.Now < userEntity.AllowStartTime)
                        {
                            this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                            BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0081 + userEntity.AllowStartTime.Value.ToString("HH:mm"));
                            return(userInfo);
                        }
                    }
                    if (userEntity.AllowEndTime != null)
                    {
                        if (DateTime.Now > userEntity.AllowEndTime)
                        {
                            this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                            BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0082 + userEntity.AllowEndTime.Value.ToString("HH:mm"));
                            return(userInfo);
                        }
                    }

                    // 07. 锁定日期是否有限制
                    if (userEntity.LockStartDate != null)
                    {
                        if (DateTime.Now > userEntity.LockStartDate)
                        {
                            if (userEntity.LockEndDate == null || DateTime.Now < userEntity.LockEndDate)
                            {
                                this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                                BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0083 + userEntity.LockStartDate.Value.ToString("yyyy-MM-dd"));
                                return(userInfo);
                            }
                        }
                    }
                    if (userEntity.LockEndDate != null)
                    {
                        if (DateTime.Now < userEntity.LockEndDate)
                        {
                            this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                            BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0084 + userEntity.LockEndDate.Value.ToString("yyyy-MM-dd"));
                            return(userInfo);
                        }
                    }

                    // 08. 是否检查用户IP地址,是否进行访问限制?管理员不检查IP.
                    if (BaseSystemInfo.CheckIPAddress && !this.IsAdministrator(userEntity.Id.ToString()))
                    {
                        List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
                        parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldParameterId, userEntity.Id.ToString()));
                        parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryId, "IPAddress"));
                        // 没有设置IP地址时不检查
                        BaseParameterManager baseParameterManager = new BaseParameterManager(this.DbHelper);
                        if (baseParameterManager.Exists(parameters))
                        {
                            if (!string.IsNullOrEmpty(ipAddress) && !this.CheckIPAddress(ipAddress, userEntity.Id.ToString()))
                            {
                                this.ReturnStatusCode = StatusCode.ErrorIPAddress.ToString();
                                BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, ipAddress, ipAddress, AppMessage.MSG0085);
                                return(userInfo);
                            }
                        }

                        // 没有设置MAC地址时不检查
                        parameters = new List <KeyValuePair <string, object> >();
                        parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldParameterId, userEntity.Id.ToString()));
                        parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryId, "MacAddress"));
                        if (baseParameterManager.Exists(parameters))
                        {
                            if (!string.IsNullOrEmpty(macAddress) && !this.CheckMacAddress(macAddress, userEntity.Id.ToString()))
                            {
                                this.ReturnStatusCode = StatusCode.ErrorMacAddress.ToString();
                                BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, macAddress, ipAddress, AppMessage.MSG0086);
                                return(userInfo);
                            }
                        }
                    }

                    // 10. 只允许登录一次,需要检查是否自己重新登录了,或者自己扮演自己了
                    if ((UserInfo != null) && (!UserInfo.Id.Equals(userEntity.Id.ToString())))
                    {
                        if (BaseSystemInfo.CheckOnLine)
                        {
                            if (userEntity.UserOnLine > 0)
                            {
                                this.ReturnStatusCode = StatusCode.ErrorOnLine.ToString();
                                BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0087);
                                return(userInfo);
                            }
                        }
                    }

                    // 11. 密码是否正确(null 与空看成是相等的)
                    if (!(string.IsNullOrEmpty(userEntity.UserPassword) && string.IsNullOrEmpty(password)))
                    {
                        bool userPasswordOK = true;
                        // 用户密码是空的
                        if (string.IsNullOrEmpty(userEntity.UserPassword))
                        {
                            // 但是输入了不为空的密码
                            if (!string.IsNullOrEmpty(password))
                            {
                                userPasswordOK = false;
                            }
                        }
                        else
                        {
                            // 用户的密码不为空,但是用户是输入了密码
                            if (string.IsNullOrEmpty(password))
                            {
                                userPasswordOK = false;
                            }
                            else
                            {
                                // 再判断用户的密码与输入的是否相同
                                userPasswordOK = userEntity.UserPassword.Equals(password);
                            }
                        }
                        // 用户的密码不相等
                        if (!userPasswordOK)
                        {
                            // 密码错误后 1:应该记录日志
                            BaseLogManager.Instance.Add(DbHelper, userEntity.Id.ToString(), userEntity.RealName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userEntity.RealName, ipAddress, AppMessage.MSG0088);
                            // TODO: 密码错误后 2:看最近1个小时输入了几次错误了?24小时里。
                            // TODO: 密码错误后 3:若错误密码数量已经超过了指定的限制,那用户就需要被锁定1个小时。
                            // TODO: 密码错误后 4:同时需要处理返回值,是由于密码次数过多导致的被锁定,登录时也应该能读取这个状态比较,时间过期了,也应该进行处理一下状态。
                            // 密码强度检查,若是要有安全要求比较高的,返回的提醒消息要进行特殊处理,不能返回非常明确的提示信息。
                            if (BaseSystemInfo.CheckPasswordStrength)
                            {
                                this.ReturnStatusCode = StatusCode.ErrorLogOn.ToString();
                            }
                            else
                            {
                                this.ReturnStatusCode = StatusCode.PasswordError.ToString();
                            }
                            return(userInfo);
                        }
                    }

                    // 09. 更新IP地址,更新MAC地址
                    if (!string.IsNullOrEmpty(ipAddress))
                    {
                        this.SetProperty(userEntity.Id, new KeyValuePair <string, object>(BaseUserEntity.FieldIPAddress, ipAddress));
                    }
                    if (!string.IsNullOrEmpty(macAddress))
                    {
                        this.SetProperty(userEntity.Id, new KeyValuePair <string, object>(BaseUserEntity.FieldMACAddress, macAddress));
                    }

                    // 可以正常登录了
                    this.ReturnStatusCode = StatusCode.OK.ToString();

                    // 13. 登录、重新登录、扮演时的在线状态进行更新
                    this.ChangeOnLine(userEntity.Id.ToString());

                    userInfo = this.ConvertToUserInfo(userEntity);
                    // 获得员工的信息,这里员工的一些信息还是有错误,部门的主键啥的
                    if (userEntity.IsStaff == 1)
                    {
                        // BaseStaffManager staffManager = new BaseStaffManager(DbHelper, UserInfo);
                        // 这里需要按 员工的用户ID来进行查找对应的员工-用户关系
                        // BaseStaffEntity staffEntity = new BaseStaffEntity(staffManager.GetDataTable(BaseStaffEntity.FieldUserId, userEntity.Id));
                        // if (staffEntity.Id > 0)
                        // {
                        // userInfo = staffManager.ConvertToUserInfo(staffEntity, userInfo);
                        // }
                    }
                    userInfo.IPAddress  = ipAddress;
                    userInfo.MACAddress = macAddress;
                    userInfo.Password   = password;
                    // 这里是判断用户是否为系统管理员的
                    userInfo.IsAdministrator = IsAdministrator(userInfo.Id);
                    userInfo.StaffId         = new BaseStaffManager(DbHelper).GetIdByUserId(userInfo.Id);
                    // 数据找到了,就可以退出循环了)
                    break;
                }
            }

            // 14. 记录系统访问日志
            if (this.ReturnStatusCode == StatusCode.OK.ToString())
            {
                BaseLogManager.Instance.Add(DbHelper, userEntity.Id.ToString(), userEntity.RealName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userEntity.RealName, ipAddress, AppMessage.BaseUserManager_LogOnSuccess);
                if (string.IsNullOrEmpty(userInfo.OpenId))
                {
                    createNewOpenId = true;
                }
                if (createNewOpenId)
                {
                    userInfo.OpenId = this.UpdateVisitDate(userEntity.Id.ToString(), createNewOpenId);
                }
                else
                {
                    this.UpdateVisitDate(userEntity.Id.ToString());
                }
            }
            else
            {
                BaseLogManager.Instance.Add(DbHelper, userName, realName, "LogOn", AppMessage.BaseUserManager, "LogOn", AppMessage.BaseUserManager_LogOn, userName, ipAddress, AppMessage.MSG0090);
            }
            return(userInfo);
        }
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="oldPassword">原密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <returns>影响行数</returns>
        public virtual int ChangePassword(string oldPassword, string newPassword, out string statusCode)
        {
            #if (DEBUG)
            int milliStart = Environment.TickCount;
            #endif

            int returnValue = 0;
            // 密码强度检查
            if (BaseSystemInfo.CheckPasswordStrength)
            {
                if (String.IsNullOrEmpty(newPassword))
                {
                    statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                    return(returnValue);
                }
            }
            // 加密密码
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                oldPassword = this.EncryptUserPassword(oldPassword);
                newPassword = this.EncryptUserPassword(newPassword);
            }
            // 判断输入原始密码是否正确
            BaseUserEntity userEntity = new BaseUserEntity();
            userEntity.GetSingle(this.GetDataTableById(UserInfo.Id));
            if (userEntity.UserPassword == null)
            {
                userEntity.UserPassword = string.Empty;
            }
            // 密码错误
            if (!userEntity.UserPassword.Equals(oldPassword))
            {
                statusCode = StatusCode.OldPasswordError.ToString();
                return(returnValue);
            }
            // 对比是否最近2次用过这个密码
            if (BaseSystemInfo.CheckPasswordStrength)
            {
                int i = 0;
                BaseParameterManager parameterManager = new BaseParameterManager(this.DbHelper, this.UserInfo);
                DataTable            dataTable        = parameterManager.GetDataTableParameterCode("User", this.UserInfo.Id, "Password");
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    string parameter = dataRow[BaseParameterEntity.FieldParameterContent].ToString();
                    if (parameter.Equals(newPassword))
                    {
                        statusCode = StatusCode.PasswordCanNotBeRepeat.ToString();
                        return(returnValue);
                    }
                    i++;
                    {
                        // 判断连续2个密码就是可以了
                        if (i > 2)
                        {
                            break;
                        }
                    }
                }
            }
            // 更改密码,同时修改密码的修改日期
            List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
            parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldUserPassword, newPassword));
            // 注意日期格式,ACCESS中要用字符
            parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldChangePasswordDate, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            returnValue = this.SetProperty(new KeyValuePair <string, object>(BaseUserEntity.FieldId, UserInfo.Id), parameters);
            if (returnValue == 1)
            {
                statusCode = StatusCode.ChangePasswordOK.ToString();
                // 若是强类型密码检查,那就保存密码修改历史,防止最近2-3次的密码相同的功能实现。
                if (BaseSystemInfo.CheckPasswordStrength)
                {
                    BaseParameterManager parameterManager = new BaseParameterManager(this.DbHelper, this.UserInfo);
                    BaseParameterEntity  parameterEntity  = new BaseParameterEntity();
                    parameterEntity.CategoryId        = "User";
                    parameterEntity.ParameterId       = this.UserInfo.Id;
                    parameterEntity.ParameterCode     = "Password";
                    parameterEntity.ParameterContent  = newPassword;
                    parameterEntity.DeletionStateCode = 0;
                    parameterEntity.Enabled           = true;
                    parameterEntity.Worked            = true;
                    parameterManager.AddEntity(parameterEntity);
                }
            }
            else
            {
                // 数据可能被删除
                statusCode = StatusCode.ErrorDeleted.ToString();
            }

            // 写入调试信息
            #if (DEBUG)
            int milliEnd = Environment.TickCount;
            Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " " + " BaseUserManager.ChangePassword(" + userEntity.Id + ")");
            #endif

            return(returnValue);
        }