|
|
@ -1,4 +1,6 @@
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
using DS.Module.Core.Data;
|
|
|
|
using DS.Module.Core.Data;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using SqlSugar;
|
|
|
|
using SqlSugar;
|
|
|
@ -7,6 +9,36 @@ namespace DS.Module.Core.Extensions;
|
|
|
|
|
|
|
|
|
|
|
|
public static partial class Extensions
|
|
|
|
public static partial class Extensions
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
static readonly ConcurrentDictionary<Type, string[]> OrderFieldCache = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static List<OrderByModel> GetOrderFields<T>(OrderByType orderByType = OrderByType.Desc)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Type type = typeof(T);
|
|
|
|
|
|
|
|
if (!OrderFieldCache.TryGetValue(type, out string[]? fields))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<string> list = new List<string>(2);
|
|
|
|
|
|
|
|
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//查找ID或创建时间,暂时只设置一个默认排序字段
|
|
|
|
|
|
|
|
var propId = Array.Find(properties, x => x.Name == "Id");
|
|
|
|
|
|
|
|
if (propId != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
list.Add(propId.Name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var propCT = Array.Find(properties, x => x.Name == "CreateTime");
|
|
|
|
|
|
|
|
if (propCT != null)
|
|
|
|
|
|
|
|
list.Add(propCT.Name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fields = [.. list];
|
|
|
|
|
|
|
|
OrderFieldCache.AddOrUpdate(type, fields, (k, v) => v = fields);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fields.Select(x => new OrderByModel { FieldName = x, OrderByType = orderByType }).ToList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 多排序方法
|
|
|
|
/// 多排序方法
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -41,7 +73,7 @@ public static partial class Extensions
|
|
|
|
PageCondition page)
|
|
|
|
PageCondition page)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
page.NotNull(nameof(page));
|
|
|
|
page.NotNull(nameof(page));
|
|
|
|
var result = page.IsExport? source.WhereNoPage(page.SortConditions) : source.Where(page.PageIndex, page.PageSize, page.SortConditions);
|
|
|
|
var result = page.IsExport ? source.WhereNoPage(page.SortConditions) : source.Where(page.PageIndex, page.PageSize, page.SortConditions);
|
|
|
|
var list = result.data;
|
|
|
|
var list = result.data;
|
|
|
|
var total = result.totalNumber;
|
|
|
|
var total = result.totalNumber;
|
|
|
|
return DataResult<List<TEntity>>.PageList(total, list, MultiLanguageConst.DataQuerySuccess);
|
|
|
|
return DataResult<List<TEntity>>.PageList(total, list, MultiLanguageConst.DataQuerySuccess);
|
|
|
@ -78,7 +110,7 @@ public static partial class Extensions
|
|
|
|
if (source.Count() == 0)
|
|
|
|
if (source.Count() == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//return new Exception("没有数据操作权限!");
|
|
|
|
//return new Exception("没有数据操作权限!");
|
|
|
|
Check.ExceptionEasy("NO Operation", "没有数据操作权限!");
|
|
|
|
Check.ExceptionEasy("NO Operation", "没有数据操作权限!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -101,9 +133,11 @@ public static partial class Extensions
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// orderSource = source.OrderBy("Id ascending");
|
|
|
|
orderSource = source;
|
|
|
|
orderSource = source.OrderBy("CreateTime desc");
|
|
|
|
|
|
|
|
// orderSource = source.OrderBy("GID");
|
|
|
|
var orderFields = GetOrderFields<TEntity>();
|
|
|
|
|
|
|
|
if (orderFields?.Count > 0)
|
|
|
|
|
|
|
|
orderSource = source.OrderBy(orderFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -117,15 +151,18 @@ public static partial class Extensions
|
|
|
|
? source.ToPageList(pageIndex, pageSize, ref total)
|
|
|
|
? source.ToPageList(pageIndex, pageSize, ref total)
|
|
|
|
: Enumerable.Empty<TEntity>().ToList(), total);
|
|
|
|
: Enumerable.Empty<TEntity>().ToList(), total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private static (List<TEntity> data, int totalNumber) WhereNoPage<TEntity>(this ISugarQueryable<TEntity> source,SortCondition[] orderConditions)
|
|
|
|
private static (List<TEntity> data, int totalNumber) WhereNoPage<TEntity>(this ISugarQueryable<TEntity> source, SortCondition[] orderConditions)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var total = source.Count();
|
|
|
|
var total = source.Count();
|
|
|
|
|
|
|
|
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//orderSource = source.OrderBy("Id");
|
|
|
|
orderSource = source;
|
|
|
|
orderSource = source.OrderBy("CreateTime desc");
|
|
|
|
|
|
|
|
|
|
|
|
var orderFields = GetOrderFields<TEntity>();
|
|
|
|
|
|
|
|
if (orderFields?.Count > 0)
|
|
|
|
|
|
|
|
orderSource = source.OrderBy(orderFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -145,8 +182,11 @@ public static partial class Extensions
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//orderSource = source.OrderBy("Id");
|
|
|
|
orderSource = source;
|
|
|
|
orderSource = source.OrderBy("CreateTime desc");
|
|
|
|
|
|
|
|
|
|
|
|
var orderFields = GetOrderFields<TEntity>();
|
|
|
|
|
|
|
|
if (orderFields?.Count > 0)
|
|
|
|
|
|
|
|
orderSource = source.OrderBy(orderFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -165,7 +205,11 @@ public static partial class Extensions
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
ISugarQueryable<TEntity> orderSource;
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
if (orderConditions == null || orderConditions.Length == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
orderSource = source.OrderBy("CreateTime desc");
|
|
|
|
orderSource = source;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var orderFields = GetOrderFields<TEntity>();
|
|
|
|
|
|
|
|
if (orderFields?.Count > 0)
|
|
|
|
|
|
|
|
orderSource = source.OrderBy(orderFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|