namespace Admin.NET.Core;
public static class RepositoryExtension
{
///
/// 实体假删除 _rep.Context.Updateable(entity).FakeDelete().ExecuteCommandAsync();
///
///
///
///
public static IUpdateable FakeDelete(this IUpdateable updateable) where T : EntityBase, new()
{
return updateable.ReSetValue(x => { x.IsDelete = true; })
.IgnoreColumns(ignoreAllNullColumns: true)
.EnableDiffLogEvent() // 记录差异日志
.UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }); // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
}
///
/// 排序方式(默认降序)
///
///
///
/// 默认排序字段
/// 是否降序
///
public static ISugarQueryable OrderBuilder(this ISugarQueryable queryable, BasePageInput pageInput, string defualtSortField = "Id", bool descSort = true)
{
var orderStr = "";
// 约定默认每张表都有Id排序
if (!string.IsNullOrWhiteSpace(defualtSortField))
{
orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc";
}
TypeAdapterConfig config = new();
config.ForType().IgnoreNullValues(true);
Mapper mapper = new(config); // 务必将mapper设为单实例
var nowPagerInput = mapper.Map(pageInput);
// 排序是否可用-排序字段和排序顺序都为非空才启用排序
if (!string.IsNullOrEmpty(nowPagerInput.Field) && !string.IsNullOrEmpty(nowPagerInput.Order))
{
orderStr = $"{nowPagerInput.Field} {(nowPagerInput.Order == nowPagerInput.DescStr ? "Desc" : "Asc")}";
}
return queryable.OrderByIF(!string.IsNullOrWhiteSpace(orderStr), orderStr);
}
}