Exemplo n.º 1
0
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="name"></param>
        /// <param name="openId"></param>
        /// <param name="unionId"></param>
        /// <returns></returns>
        public BaseUserOAuthEntity GetEntity(string name, string openId, string unionId)
        {
            BaseUserOAuthEntity result = null;

            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(openId))
            {
                name = dbHelper.SqlSafe(name);
                //需要显示未被删除的记录
                var sql = "SELECT TOP 1 * FROM " + CurrentTableName + " WHERE " + BaseUserOAuthEntity.FieldName + " = N'" + name + "'";
                if (!string.IsNullOrEmpty(openId))
                {
                    sql += " AND " + BaseUserOAuthEntity.FieldOpenId + " = N'" + openId + "'";
                }

                if (!string.IsNullOrEmpty(unionId))
                {
                    sql += " AND " + BaseUserOAuthEntity.FieldUnionId + " = N'" + unionId + "'";
                }

                //未删除
                sql += " AND " + BaseUserOAuthEntity.FieldDeleted + " = 0 AND " + BaseUserOAuthEntity.FieldEnabled + " = 1 ";
                //排序
                sql += " ORDER BY " + BaseUserOAuthEntity.FieldId + " DESC";
                var dt = DbHelper.Fill(sql);
                if (dt != null && dt.Rows.Count != 0)
                {
                    //result = BaseEntity.Create<AppContentEntity>(dt);
                    var cacheKey  = CurrentTableName + ".Entity." + openId;
                    var cacheTime = TimeSpan.FromMilliseconds(86400000);
                    result = CacheUtil.Cache <BaseUserOAuthEntity>(cacheKey, () => BaseEntity.Create <BaseUserOAuthEntity>(dt), true, false, cacheTime);
                }
            }
            return(result);
        }
 /// <summary>
 /// 添加, 这里可以人工干预,提高程序的性能
 /// </summary>
 /// <param name="entity">实体</param>
 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>
 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>
 /// <returns>主键</returns>
 public string Add(BaseUserOAuthEntity entity, bool identity = true, bool returnId = true)
 {
     Identity  = identity;
     ReturnId  = returnId;
     entity.Id = AddEntity(entity).ToInt();
     return(entity.Id.ToString());
 }
 /// <summary>
 /// 添加或更新(主键是否为0)
 /// </summary>
 /// <param name="entity">实体</param>
 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>
 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>
 /// <returns>主键</returns>
 public string AddOrUpdate(BaseUserOAuthEntity 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, BaseUserOAuthEntity entity)
 {
     SetEntityExtend(sqlBuilder, entity);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldSystemCode, entity.SystemCode);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUserId, entity.UserId);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldName, entity.Name);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldAccessToken, entity.AccessToken);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldRefreshToken, entity.RefreshToken);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldOpenId, entity.OpenId);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUnionId, entity.UnionId);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldDescription, entity.Description);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldSortCode, entity.SortCode);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldDeleted, entity.Deleted);
     sqlBuilder.SetValue(BaseUserOAuthEntity.FieldEnabled, entity.Enabled);
 }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体</param>
        public int UpdateEntity(BaseUserOAuthEntity entity)
        {
            var sqlBuilder = new SqlBuilder(DbHelper);

            sqlBuilder.BeginUpdate(CurrentTableName);
            SetEntity(sqlBuilder, entity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUpdateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDbNow(BaseUserOAuthEntity.FieldUpdateTime);
            sqlBuilder.SetValue(BaseUserOAuthEntity.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, BaseUserOAuthEntity entity);
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体</param>
        public string AddEntity(BaseUserOAuthEntity 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(BaseUserOAuthEntity.FieldCreateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldCreateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldCreateBy, UserInfo.RealName);
            }
            else
            {
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldCreateBy, entity.CreateBy);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldCreateUserName, entity.CreateUserName);
            }
            sqlBuilder.SetDbNow(BaseUserOAuthEntity.FieldCreateTime);
            sqlBuilder.SetValue(BaseUserOAuthEntity.FieldCreateIp, Utils.GetIp());
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUpdateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseUserOAuthEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDbNow(BaseUserOAuthEntity.FieldUpdateTime);
            sqlBuilder.SetValue(BaseUserOAuthEntity.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(BaseUserOAuthEntity entity)
 {
     return(UpdateEntity(entity));
 }