public virtual ReturnInfo <UserInfo> Vali([DisplayName2("用户"), Required] string user, [DisplayName2("密码"), Required] string password, CommonUseData comData = null)
        {
            ReturnInfo <UserInfo> returnInfo = new ReturnInfo <UserInfo>();

            return(ExecReturnFuncAndConnectionId <UserInfo>((reInfo, connId) =>
            {
                UserInfo result = Persistence.SelectByLoginIdAndPassword(user, MD5Util.Encryption16(password), connId);
                if (result == null)
                {
                    reInfo.SetFailureMsg("登录ID或密码不对");
                    return null;
                }

                if (result.Enabled)
                {
                    returnInfo.Data = result;
                    reInfo.SetSuccessMsg("登录成功");

                    result.Memo = result.Password = result.Modifier = result.Creater = null;

                    Log.InfoAsync($"登录ID:{result.LoginId},用户编号:{result.Code},名称:{result.Name} 成功授权",
                                  null, typeof(UserService).Name, eventId: comData.GetEventId());
                }
                else
                {
                    reInfo.SetFailureMsg("Sorry.您的登录ID未启用,不能登录此系统");
                }

                return result;
            }, returnInfo, accessMode: AccessMode.SLAVE));
        }
        /// <summary>
        /// 插入模型列表
        /// </summary>
        /// <param name="models">模型列表</param>
        /// <param name="dbConnection">数据库连接</param>
        /// <param name="propertyNames">属性名称集合</param>
        /// <param name="dbTransaction">数据库事务</param>
        /// <param name="comData">通用数据</param>
        /// <returns>影响行数</returns>
        protected override int Insert(IList <ModelT> models, IDbConnection dbConnection, string[] propertyNames = null, IDbTransaction dbTransaction = null, CommonUseData comData = null)
        {
            DynamicParameters parameters;
            var sql = InsertSql(models, out parameters, propertyNames, comData: comData);

            return(ExecRecordSqlLog <int>(sql, () =>
            {
                if (IsSupportIdempotent)
                {
                    try
                    {
                        return dbConnection.Execute(sql, parameters, dbTransaction);
                    }
                    catch (Exception ex)
                    {
                        // 如果主键重复,则忽略异常,实现幂等
                        if (IsExceptionPkRepeat(ex))
                        {
                            var ids = models.Select(p => p.Id).ToArray();
                            Log.InfoAsync($"批量插入.发生主键重复异常,幂等操作忽略该异常.主键:{ids.ToMergeString(",")}", ex, this.GetType().Name, comData.GetEventId(), "Insert");
                            return models.Count;
                        }

                        throw new Exception(ex.Message, ex);
                    }
                }
                else
                {
                    return dbConnection.Execute(sql, parameters, dbTransaction);
                }
            }, comData: comData, "Insert"));
        }
Example #3
0
        /// <summary>
        /// 执行记录SQL日志
        /// </summary>
        /// <typeparam name="ReturnT">返回类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="callbackExecDb">回调执行数据库</param>
        /// <param name="comData">通用数据</param>
        /// <param name="tag">标签</param>
        /// <returns>返回值</returns>
        protected ReturnT ExecRecordSqlLog <ReturnT>(string sql, Func <ReturnT> callbackExecDb, CommonUseData comData = null, params string[] tag)
        {
            var level = SqlLogLevel;

            if (level == LogLevelEnum.NONE)
            {
                return(callbackExecDb());
            }

            var       eventId = comData.GetEventId();
            Exception errEx   = null;
            var       watch   = new Stopwatch();

            watch.Start();
            try
            {
                var result = callbackExecDb();
                watch.Stop();

                return(result);
            }
            catch (Exception ex)
            {
                watch.Stop();
                errEx = ex;

                throw new Exception(ex.Message, ex);
            }
            finally
            {
                var msg = sql + $".耗时:{watch.ElapsedMilliseconds}ms.";
                if (errEx == null)
                {
                    switch (level)
                    {
                    case LogLevelEnum.TRACE:
                        Log.TraceAsync(msg, source: this.GetType().Name, eventId: eventId, tags: tag);

                        break;

                    case LogLevelEnum.DEBUG:
                        Log.DebugAsync(msg, source: this.GetType().Name, eventId: eventId, tags: tag);

                        break;

                    case LogLevelEnum.INFO:
                        Log.InfoAsync(msg, source: this.GetType().Name, eventId: eventId, tags: tag);

                        break;

                    case LogLevelEnum.WRAN:
                        Log.WranAsync(msg, source: this.GetType().Name, eventId: eventId, tags: tag);

                        break;

                    case LogLevelEnum.ERROR:
                        Log.ErrorAsync(msg, source: this.GetType().Name, eventId: eventId, tags: tag);

                        break;

                    case LogLevelEnum.FATAL:
                        Log.FatalAsync(msg, source: this.GetType().Name, eventId: eventId, tags: tag);

                        break;
                    }
                }
                else
                {
                    Log.ErrorAsync(msg, ex: errEx, source: this.GetType().Name, eventId: eventId, tags: tag);
                }
            }
        }
        /// <summary>
        /// 插入模型
        /// </summary>
        /// <param name="model">模型</param>
        /// <param name="dbConnection">数据库连接</param>
        /// <param name="propertyNames">属性名称集合</param>
        /// <param name="dbTransaction">数据库事务</param>
        /// <param name="comData">通用数据</param>
        /// <returns>影响行数</returns>
        protected override int Insert(ModelT model, IDbConnection dbConnection, string[] propertyNames = null, IDbTransaction dbTransaction = null, CommonUseData comData = null)
        {
            var isAuto = PrimaryKeyIncr(model.Id);
            var sql    = InsertSql(model, propertyNames, isAuto, comData: comData);

            if (IsSupportIdempotent)
            {
                if (isAuto)
                {
                    throw new NotSupportedException("幂等操作必须由业务生成主键,不能使用数据库自增生成");
                }

                return(ExecRecordSqlLog <int>(sql, () =>
                {
                    try
                    {
                        return dbConnection.Execute(sql, model, dbTransaction);
                    }
                    catch (Exception ex)
                    {
                        // 如果主键重复,则忽略异常,实现幂等
                        if (IsExceptionPkRepeat(ex))
                        {
                            Log.InfoAsync($"插入.发生主键重复异常,幂等操作忽略该异常.主键:{model.Id}", ex, this.GetType().Name, comData.GetEventId(), "Insert");
                            return 1;
                        }

                        throw new Exception(ex.Message, ex);
                    }
                }, comData: comData, "Insert"));
            }
            else
            {
                if (isAuto)
                {
                    model.Id = ExecRecordSqlLog <IdT>(sql, () =>
                    {
                        return(dbConnection.ExecuteScalar <IdT>(sql, model, dbTransaction));
                    }, comData: comData, "Insert");

                    return(1);
                }
                else
                {
                    return(ExecRecordSqlLog <int>(sql, () =>
                    {
                        return dbConnection.Execute(sql, model, dbTransaction);
                    }, comData: comData, "Insert"));
                }
            }
        }