You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

169 lines
5.1 KiB

using SqlSugar;
using System.Linq.Expressions;
using System.Reflection;
using Wood.Entity;
using Wood.Util;
namespace Wood.Data.Repository
{
public static class RepositoryExtension
{
#region 过滤器清除
/// <summary>
/// 清除delete 过滤器
/// </summary>
/// <returns>ISugarQueryable<T></returns>
public static ISugarQueryable<T> ClearDeleteFilter<T>(this SimpleClient<T> repository) where T : EntityBase, new()
{
return repository.AsQueryable().ClearFilter<IDeletedFilter>();
}
/// <summary>
/// 清除 数据范围 过滤器
/// </summary>
/// <returns>ISugarQueryable<T></returns>
public static ISugarQueryable<T> ClearDataScopeFilter<T>(this SimpleClient<T> repository) where T : EntityBase, new()
{
return repository.AsQueryable().ClearFilter<IOrgIdFilter, ICreateUserIdFilter>();
}
/// <summary>
/// 设置自定义 status 过滤器
/// </summary>
/// <returns>ISugarQueryable<T></returns>
public static ISugarQueryable<T> AddStatusFilter<T>(this SimpleClient<T> repository, int status = 1) where T : EntityBaseExtra, new()
{
var query = repository.AsQueryable();
return query.Where(it => status == it.Status);
}
/// <summary>
/// 设置自定义 status 过滤器
/// </summary>
/// <returns>ISugarQueryable<T></returns>
public static ISugarQueryable<T> AddStatusFilter<T>(this SimpleClient<T> repository, params int[] status) where T : EntityBaseExtra, new()
{
var query = repository.AsQueryable();
return query.Where(it => status.Contains(it.Status));
}
#endregion
/// <summary>
/// 实体假删除异步 _rep.FakeDeleteAsync(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity">要删除的实体</param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this SimpleClient<T> repository, T entity) where T : EntityBase, new()
{
entity.IsDelete = true;
return repository.Context.Updateable(entity).UpdateColumns(it => new { it.IsDelete, it.UpdateTime, it.UpdateUserId }).ExecuteCommandAsync();
}
/// <summary>
/// 实体假删除 db.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="whereExpression"></param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this SimpleClient<T> repository, Expression<Func<T, bool>> whereExpression) where T : EntityBase, new()
{
long? userId = GlobalContext.UserInfo?.UserId;
return repository.Context.Updateable<T>()
.AS()
.SetColumns("IsDelete", true)
.SetColumns("UpdateUserId", userId)
.SetColumns("UpdateTime", DateTime.Now)
.Where(whereExpression)
.ExecuteCommandAsync();
}
/// <summary>
/// 实体假删除 db.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this SimpleClient<T> repository, List<T> entity) where T : EntityBase, new()
{
foreach (var item in entity)
item.IsDelete = true;
return repository.Context.Updateable(entity).UpdateColumns(it => new { it.IsDelete, it.UpdateTime, it.UpdateUserId }).ExecuteCommandAsync();
}
/// <summary>
/// 根据表名查询
/// </summary>
/// <param name="queryable"></param>
/// <returns> </returns>
public static ISugarQueryable<T> AS<T>(this ISugarQueryable<T> queryable)
{
var info = GetTableInfo<T>();
return queryable.AS<T>($"{info}");
}
/// <summary>
/// 根据表名查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="queryable"></param>
/// <returns></returns>
public static ISugarQueryable<T, T2> AS<T, T2>(this ISugarQueryable<T, T2> queryable)
{
var info = GetTableInfo<T2>();
return queryable.AS<T2>($"{info}");
}
/// <summary>
/// 根据表名更新
/// </summary>
/// <param name="updateable"></param>
/// <returns> </returns>
public static IUpdateable<T> AS<T>(this IUpdateable<T> updateable) where T : EntityBase, new()
{
var info = GetTableInfo<T>();
return updateable.AS($"{info}");
}
/// <summary>
/// 根据表名新增
/// </summary>
/// <param name="insertable"></param>
/// <returns> </returns>
public static IInsertable<T> AS<T>(this IInsertable<T> insertable) where T : EntityBase, new()
{
var info = GetTableInfo<T>();
return insertable.AS($"{info}");
}
/// <summary>
/// 根据表名删除
/// </summary>
/// <param name="deleteable"></param>
/// <returns> </returns>
public static IDeleteable<T> AS<T>(this IDeleteable<T> deleteable) where T : EntityBase, new()
{
var info = GetTableInfo<T>();
return deleteable.AS($"{info}");
}
/// <summary>
/// 根据实体类型获取表信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static string? GetTableInfo<T>()
{
var entityType = typeof(T);
var tableName = entityType.GetCustomAttribute<SugarTable>()?.TableName;
return tableName;
}
}
}