feat:furion rbac搭建

This commit is contained in:
橙子
2023-04-13 21:12:06 +08:00
parent 18696ec542
commit b9dad93c9d
194 changed files with 9557 additions and 75 deletions

View File

@@ -0,0 +1,147 @@
using System.Linq.Expressions;
using Furion.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Data.Entities;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Enums;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Framework.Infrastructure.Sqlsugar.Repositories
{
public class SqlsugarRepository<T> : SimpleClient<T>, IRepository<T> ,ITransient where T : class, new()
{
public SqlsugarRepository(ISqlSugarClient context) : base(context)
{
}
/// <summary>
/// 注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
/// </summary>
public ISugarQueryable<T> _DbQueryable => AsQueryable();
protected ISqlSugarClient _Db { get { return Context; } set { } }
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = page.PageNum, PageSize = page.PageSize });
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = page.PageNum, PageSize = page.PageSize }, orderByExpression, orderByType.EnumToEnum<OrderByType>());
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageNum, page.PageSize);
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = pageNum, PageSize = pageSize });
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = pageNum, PageSize = pageSize }, orderByExpression, orderByType.EnumToEnum<OrderByType>());
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(pageNum, pageSize);
}
public async Task<bool> UpdateIgnoreNullAsync(T updateObj)
{
return await _Db.Updateable(updateObj).IgnoreColumns(true).ExecuteCommandAsync() > 0;
}
public override async Task<bool> DeleteAsync(T deleteObj)
{
//逻辑删除
if (deleteObj is ISoftDelete)
{
//反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, deleteObj);
return await UpdateAsync(deleteObj);
}
else
{
return await base.DeleteAsync(deleteObj);
}
}
public override async Task<bool> DeleteAsync(List<T> deleteObjs)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
//反射赋值
deleteObjs.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(deleteObjs);
}
else
{
return await base.DeleteAsync(deleteObjs);
}
}
public override async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
var entities = await GetListAsync(whereExpression);
//反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities);
}
else
{
return await base.DeleteAsync(whereExpression);
}
}
public override async Task<bool> DeleteByIdAsync(dynamic id)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
var entity = await GetByIdAsync(id);
//反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity);
return await UpdateAsync(entity);
}
else
{
return await _Db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
}
}
public override async Task<bool> DeleteByIdsAsync(dynamic[] ids)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
var entities = await _DbQueryable.In(ids).ToListAsync();
if (entities.Count == 0)
{
return false;
}
//反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities);
}
else
{
return await base.DeleteByIdsAsync(ids);
}
}
}
}

View File

@@ -0,0 +1,51 @@
using Furion;
using Furion.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Uow;
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
{
public class SqlsugarUnitOfWork : IUnitOfWork, ISingleton
{
public ISqlSugarClient Db { get; set; }
public ITenant Tenant { get; set; }
public bool IsTran { get; set; }
public bool IsCommit { get; set; }
public bool IsClose { get; set; }
public void Dispose()
{
if (IsTran && IsCommit == false)
{
Tenant.RollbackTran();
}
if (Db.Ado.Transaction == null && IsClose == false)
{
Db.Close();
}
}
public bool Commit()
{
if (IsTran && IsCommit == false)
{
Tenant.CommitTran();
IsCommit = true;
}
if (Db.Ado.Transaction == null && IsClose == false)
{
Db.Close();
IsClose = true;
}
return IsCommit;
}
public IRepository<T> GetRepository<T>()
{
return App.GetRequiredService<IRepository<T>>();
}
}
}

View File

@@ -0,0 +1,38 @@
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Uow;
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
{
/// <summary>
/// 此部分为sqlsugr的魔改版本
/// </summary>
internal class SqlsugarUnitOfWorkManager : IUnitOfWorkManager,ISingleton
{
public SqlsugarUnitOfWorkManager(ISqlSugarClient db)
{
Db = db;
}
public ISqlSugarClient Db { get; set; }
public IUnitOfWork CreateContext(bool isTran = true)
{
SqlsugarUnitOfWork uow = new SqlsugarUnitOfWork();
return CreateContext(isTran, uow);
}
private IUnitOfWork CreateContext(bool isTran, SqlsugarUnitOfWork sugarUnitOf)
{
sugarUnitOf.Db = Db;
sugarUnitOf.Tenant = Db.AsTenant();
sugarUnitOf.IsTran = isTran;
Db.Open();
if (isTran)
Db.AsTenant().BeginTran();
return sugarUnitOf;
}
}
}