/// <summary>
 /// 添加, 这里可以人工干预,提高程序的性能
 /// </summary>
 /// <param name="entity">实体</param>
 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>
 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>
 /// <returns>主键</returns>
 public string Add(BaseExceptionEntity entity, bool identity = true, bool returnId = true)
 {
     Identity  = identity;
     ReturnId  = returnId;
     entity.Id = AddEntity(entity).ToInt();
     return(entity.Id.ToString());
 }
        /// <summary>
        /// 记录异常情况
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="url">网址</param>
        /// <returns>主键</returns>
        public string AddEntity(Exception ex, string url)
        {
            var entity = new BaseExceptionEntity
            {
                //出错源地址,暂时放Title中
                Title = url,
                //异常消息
                Message = ex.Message,
                //异常源
                Category = ex.Source,
                //异常类型
                ThreadName = ex.GetType().FullName,
                //异常方法
                ProcessName = ex.TargetSite?.Name,
                //异常堆栈
                FormattedMessage = ex.StackTrace
            };

            if (UserInfo != null)
            {
                entity.IpAddress    = UserInfo.IpAddress;
                entity.CreateUserId = UserInfo.UserId;
                entity.CreateBy     = UserInfo.RealName;
            }
            else
            {
                entity.CreateBy = entity.CreateBy;
            }

            return(AddEntity(entity));
        }
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键</param>
        public BaseExceptionEntity GetEntity(string id)
        {
            DataTable           dataTable       = this.GetDataTableById(id);
            BaseExceptionEntity exceptionEntity = new BaseExceptionEntity(dataTable);

            return(exceptionEntity);
        }
 /// <summary>
 /// 添加或更新(主键是否为0)
 /// </summary>
 /// <param name="entity">实体</param>
 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>
 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>
 /// <returns>主键</returns>
 public string AddOrUpdate(BaseExceptionEntity entity, bool identity = true, bool returnId = true)
 {
     Identity = identity;
     ReturnId = returnId;
     if (entity.Id == 0)
     {
         entity.Id = AddEntity(entity).ToInt();
         return(entity.Id.ToString());
     }
     else
     {
         return(UpdateEntity(entity) > 0 ? entity.Id.ToString() : string.Empty);
     }
 }
 /// <summary>
 /// 设置实体
 /// </summary>
 /// <param name="sqlBuilder">Sql语句生成器</param>
 /// <param name="entity">实体</param>
 private void SetEntity(SqlBuilder sqlBuilder, BaseExceptionEntity entity)
 {
     SetEntityExtend(sqlBuilder, entity);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldSystemCode, entity.SystemCode);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldEventId, entity.EventId);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldCategory, entity.Category);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldPriority, entity.Priority);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldSeverity, entity.Severity);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldTitle, entity.Title);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldTimestamp, entity.Timestamp);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldMachineName, entity.MachineName);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldIpAddress, entity.IpAddress);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldAppDomainName, entity.AppDomainName);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldProcessId, entity.ProcessId);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldProcessName, entity.ProcessName);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldThreadName, entity.ThreadName);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldWin32ThreadId, entity.Win32ThreadId);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldMessage, entity.Message);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldFormattedMessage, entity.FormattedMessage);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldSortCode, entity.SortCode);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldDeleted, entity.Deleted);
     sqlBuilder.SetValue(BaseExceptionEntity.FieldEnabled, entity.Enabled);
 }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体</param>
        public int UpdateEntity(BaseExceptionEntity entity)
        {
            var sqlBuilder = new SqlBuilder(DbHelper);

            sqlBuilder.BeginUpdate(CurrentTableName);
            SetEntity(sqlBuilder, entity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDbNow(BaseExceptionEntity.FieldUpdateTime);
            sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateIp, Utils.GetIp());
            sqlBuilder.SetWhere(PrimaryKey, entity.Id);
            //return sqlBuilder.EndUpdate();
            var result = sqlBuilder.EndUpdate();

            if (result > 0)
            {
                RemoveCache(entity.Id);
            }
            return(result);
        }
 // 这个是声明扩展方法
 partial void SetEntityExtend(SqlBuilder sqlBuilder, BaseExceptionEntity entity);
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体</param>
        public string AddEntity(BaseExceptionEntity entity)
        {
            var key = string.Empty;

            if (entity.SortCode == 0)
            {
                var managerSequence = new BaseSequenceManager(DbHelper, Identity);
                if (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2)
                {
                    key = managerSequence.Increment($"SC_{CurrentTableName}_SEQ");
                }
                else
                {
                    key = managerSequence.Increment(CurrentTableName);
                }
                entity.SortCode = key.ToInt();
            }
            var sqlBuilder = new SqlBuilder(DbHelper, Identity, ReturnId);

            sqlBuilder.BeginInsert(CurrentTableName, PrimaryKey);
            if (!Identity)
            {
                // 这里已经是指定了主键了,所以不需要返回主键了
                sqlBuilder.ReturnId = false;
                sqlBuilder.SetValue(PrimaryKey, entity.Id);
            }
            else
            {
                if (!ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2))
                {
                    if (DbHelper.CurrentDbType == CurrentDbType.Oracle)
                    {
                        sqlBuilder.SetFormula(PrimaryKey, $"{CurrentTableName}_SEQ.NEXTVAL");
                    }
                    if (DbHelper.CurrentDbType == CurrentDbType.Db2)
                    {
                        sqlBuilder.SetFormula(PrimaryKey, $"NEXT VALUE FOR {CurrentTableName}_SEQ");
                    }
                }
                else
                {
                    if (Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2))
                    {
                        var managerSequence = new BaseSequenceManager(DbHelper);
                        entity.Id = managerSequence.Increment($"{CurrentTableName}_SEQ").ToInt();
                        sqlBuilder.SetValue(PrimaryKey, entity.Id);
                    }
                }
            }
            SetEntity(sqlBuilder, entity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseExceptionEntity.FieldCreateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldCreateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldCreateBy, UserInfo.RealName);
            }
            else
            {
                sqlBuilder.SetValue(BaseExceptionEntity.FieldCreateBy, entity.CreateBy);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldCreateUserName, entity.CreateUserName);
            }
            sqlBuilder.SetDbNow(BaseExceptionEntity.FieldCreateTime);
            sqlBuilder.SetValue(BaseExceptionEntity.FieldCreateIp, Utils.GetIp());
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDbNow(BaseExceptionEntity.FieldUpdateTime);
            sqlBuilder.SetValue(BaseExceptionEntity.FieldUpdateIp, Utils.GetIp());
            if (Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access))
            {
                key = sqlBuilder.EndInsert().ToString();
            }
            else
            {
                sqlBuilder.EndInsert();
            }
            if (Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2))
            {
                key = entity.Id.ToString();
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                RemoveCache();
            }
            return(key);
        }
 /// <summary>
 /// 更新
 /// </summary>
 /// <param name="entity">实体</param>
 public int Update(BaseExceptionEntity entity)
 {
     return(UpdateEntity(entity));
 }