2020-07-08 16:12:49 +08:00
|
|
|
|
using System;
|
2021-01-12 00:39:40 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2020-09-01 17:23:31 +08:00
|
|
|
|
using System.Reflection;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign.TableModels
|
|
|
|
|
{
|
2021-01-12 00:39:40 +08:00
|
|
|
|
public class SortModel<TField> : ITableSortModel, IComparer<TField>
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-09-01 17:23:31 +08:00
|
|
|
|
private PropertyInfo _propertyInfo;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2021-01-12 00:39:40 +08:00
|
|
|
|
private readonly Func<TField, TField, int> _comparer;
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
public int Priority { get; }
|
|
|
|
|
|
|
|
|
|
public string FieldName { get; }
|
|
|
|
|
|
2021-02-14 20:15:53 +08:00
|
|
|
|
public string Sort => _sortDirection?.Name;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
|
|
|
|
SortDirection ITableSortModel.SortDirection => _sortDirection;
|
|
|
|
|
|
|
|
|
|
private SortDirection _sortDirection;
|
|
|
|
|
|
|
|
|
|
public SortModel(PropertyInfo propertyInfo, int priority, SortDirection defaultSortOrder, Func<TField, TField, int> comparer)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-09-01 17:23:31 +08:00
|
|
|
|
this._propertyInfo = propertyInfo;
|
2021-01-12 00:39:40 +08:00
|
|
|
|
_comparer = comparer;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
this.Priority = priority;
|
2020-09-01 17:23:31 +08:00
|
|
|
|
this.FieldName = propertyInfo?.Name;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
this._sortDirection = defaultSortOrder ?? SortDirection.None;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
void ITableSortModel.SetSortDirection(SortDirection sortDirection)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
_sortDirection = sortDirection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IOrderedQueryable<TItem> ITableSortModel.SortList<TItem>(IQueryable<TItem> source)
|
|
|
|
|
{
|
|
|
|
|
if (_sortDirection == SortDirection.None)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
return source as IOrderedQueryable<TItem>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sourceExpression = Expression.Parameter(typeof(TItem));
|
|
|
|
|
|
2020-09-01 17:23:31 +08:00
|
|
|
|
var propertyExpression = Expression.Property(sourceExpression, _propertyInfo);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2020-09-01 17:23:31 +08:00
|
|
|
|
var lambda = Expression.Lambda<Func<TItem, TField>>(propertyExpression, sourceExpression);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
if (_sortDirection == SortDirection.Ascending)
|
2020-09-01 17:23:31 +08:00
|
|
|
|
{
|
2021-01-12 00:39:40 +08:00
|
|
|
|
return _comparer == null ? source.OrderBy(lambda) : source.OrderBy(lambda, this);
|
2020-09-01 17:23:31 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-12 00:39:40 +08:00
|
|
|
|
return _comparer == null ? source.OrderByDescending(lambda) : source.OrderByDescending(lambda, this);
|
2020-09-01 17:23:31 +08:00
|
|
|
|
}
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 00:39:40 +08:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public int Compare(TField x, TField y)
|
|
|
|
|
{
|
|
|
|
|
return _comparer?.Invoke(x, y) ?? 0;
|
|
|
|
|
}
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|